how to make a PG_DUMP from a script, that allows me to choose which database to ignore and which ones do not

0

I am developing a Script in the Bash , for the automation of the creation of the Backups for the Postgresql engine. strong> in Centos , but I need to be able to ignore the databases that you define in a variable. and save the Dump in the root folder.

Here I show you an example of my Script, which generates me the Dump but I do not know how to ignore some databases and how to tell him to do all the ones I want.

Thank you very much, your help would be great

#!/bin/bash
usuario="postgres"
password="123"
db="test1"
puerto=5432 
host="localhost"
DATE='date +%Y-%m-%d'
/usr/bin/pg_dump --host $host --port $puerto --username $usuario --no-password  --format custom --blobs --verbose --file "/home/josealonso/Escritorio/backup2/B$db[$DATE].sql" "$db"
psql-l

I will join the code that I have managed to improve, during the course of the day:

#!/bin/bash
usuario='postgres'
password='123456a'
db=("test1" "test2")
puerto=5432 
host='localhost'
ignore=("test3")
for item in ${db[*]}
do
if [[ $item !=  $ignore ]];
then
DATE='date +%Y-%m-%d
basedato=$item
/usr/bin/pg_dump --host $host --port $puerto --username $usuario --no-password  --format custom --blobs --verbose --file "/home/josealonso/Escritorio/backup2/B$item[$DATE].sql" "$basedato"
echo 'El Backup De La Base de Datos' $basedato 'Fue Realizado con exito!!!'
sh script.sh
fi
done
    
asked by JOSE LUIS ALONSO MERCADO 10.05.2017 в 17:21
source

1 answer

0

I'm going to propose an alternative to what you want. In my opinion the most economical solution is to make a backup file (or backup ) for each database, are you interested? If you want to have a single file, at the end of the process it could be packaged. Using a for in bash is relatively trivial and could solve your problem.

#!/bin/bash
usuario="postgres"
password="123"
bases=(base1 base2)   # Esto es un arreglo con tus bases
puerto=5432 
host="localhost"
DATE='date +%Y-%m-%d'
# Aqui inicia el ciclo que genera los respaldos
for db in "${bases[@]}"
do 
  echo "Creando el respaldo de ${db}"
  /usr/bin/pg_dump --host $host --port $puerto --username $usuario --no-password  --format custom --blobs --verbose --file "/home/josealonso/Escritorio/backup2/B${db}[$DATE].sql" "${db}"
done
# Esto crea un archivo compactado con tus respaldos
tar cjf respaldo[$DATE].tar.bz2 /home/josealonso/Escritorio/backup2/
psql-l

Using an array with the names of the bases you want to back up, we do a '' for '' cycle that generates a file for each backup.

When finished, we create a compacted file with the contents of the directory. I put BZ2 by habit, but you can use zip if you prefer.

  

Note

     

I have not tried the script, but I hope you get the idea.

    
answered by 10.05.2017 / 23:44
source