Skip to content

Instantly share code, notes, and snippets.

@rafaelbernard
Created June 21, 2012 14:24
Show Gist options
  • Save rafaelbernard/2966026 to your computer and use it in GitHub Desktop.
Save rafaelbernard/2966026 to your computer and use it in GitHub Desktop.
Backup de todos os bancos de dados do seu servidor PostgreSQL
#!/bin/bash
# Baseado em http://mig5.net/content/mysql-postgresql-all-databases-backup-script
# Rafael Bernard Rodrigues Araujo - 14/06/2012
today=$(date +%y%m%d)
# local dir where the backups go
myDir='/disk2/backup/pg'
# this is for PostgreSQL. If you don't need it, you
# could leave it here, but remove the 'backup_pgsql'
# function call at the end of the script
CMD_PSQL=/usr/local/pgsql/bin/psql
CMD_DUMP=/usr/local/pgsql/bin/pg_dump
arq_tar=''
function backup_pgsql {
#Seleciona os bancos a serem copiados, eliminando os bancos de sistema
for db in `psql -U postgres -tq -d template1 -c "select datname from pg_database where datname not in ('template1','template0','postgres')"`; do
echo 'Iniciando o backup de '${db};
arq_tar="${myDir}/${db}-${today}.tar";
pg_dump -U postgres -F tar -f $arq_tar $db;
gzip $arq_tar;
echo 'Backup concluido';
done;
}
backup_pgsql
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment