Created
January 7, 2013 10:01
-
-
Save geoffgarside/4473805 to your computer and use it in GitHub Desktop.
PostgreSQL Database Backup/Restore migration help script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# | |
backupdir="./pg_backups" | |
pgdump_args="-bF c" | |
pgdumpall_globals_args="" | |
pg_backup() { | |
if [ ! -d ${backupdir} ]; then | |
echo Creating $backupdir | |
mkdir -m 700 ${backupdir} | |
fi | |
echo | |
echo "PostgreSQL backups" | |
# Protect the data | |
umask 077 | |
rc=$? | |
now=`date "+%Y-%m-%dT%H:%M:%S"` | |
file=${backupdir}/pgglobals_${now} | |
pg_dumpall -g ${pgdumpall_globals_args} | gzip -9 > ${file}.gz | |
db=$1 | |
while shift; do | |
echo -n " $db" | |
file=${backupdir}/pgdump_${db}_${now} | |
pg_dump ${pgdump_args} -f ${file} ${db} | |
[ $? -gt 0 ] && rc=3 | |
db=$1 | |
done | |
if [ $rc -gt 0 ]; then | |
echo | |
echo "Errors reported during backup" | |
fi | |
} | |
dbnames=`psql -q -t -A -d template1 -c "SELECT datname FROM pg_database WHERE datname != 'template0'"` | |
pg_backup $dbnames | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment