Skip to content

Instantly share code, notes, and snippets.

@pcaro
Created December 4, 2012 11:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pcaro/4202860 to your computer and use it in GitHub Desktop.
Save pcaro/4202860 to your computer and use it in GitHub Desktop.
Postgres simple backp
#!/bin/bash
# Puedes ver la fecha de la fecha en la cabecera de los .sqlc:
# pg_restore -l backupu.sqlc | head -n 15
# Echo: "Ejecutar este script como usuario con permisos (postgres)"
BKPDIR=postgres-backup-${DATE}
RSTFILE=restore.README
USER=postgres
GLOBALS=globals.sql
DATE=`date +%Y%m%d-%Hh%M`
mkdir postgres-backup-${DATE}
cd postgres-backup-${DATE}
pg_dumpall -g -U $USER --file=$GLOBALS;
echo "#--- Date: $DATE" >> $RSTFILE
echo "#---restore globals: " >> $RSTFILE
echo "psql -U postgres < $GLOBALS : " >> $RSTFILE
psql -AtU $USER -c "SELECT datname FROM pg_database \
WHERE NOT datistemplate"| \
while read f;
do
output=$f.sqlc
echo "Backup of $f to $output"
pg_dump -U$USER --format=c --file=${output} $f;
echo "#---restore $f" >> $RSTFILE
echo "pg_restore -U postgres --dbname=$f $output" >> $RSTFILE
echo "" >> $RSTFILE
done;
cd ..
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment