Skip to content

Instantly share code, notes, and snippets.

@eduherraiz
Created January 24, 2014 09:48
Show Gist options
  • Save eduherraiz/8594695 to your computer and use it in GitHub Desktop.
Save eduherraiz/8594695 to your computer and use it in GitHub Desktop.
#!/bin/sh
BKPDIR="/var/backups/pgsql/"
TODAY=$(date +%F)
DAY=$(date +%A |tr 'A-Z' 'a-z')
MONTH=$(date +%B |tr 'A-Z' 'a-z')
TMPDIR=$(mktemp -d -p $BKPDIR) || exit 1
export PGOPTIONS='-c statement_timeout=0'
pg_dumpall -U postgres --globals-only |bzip2 > $TMPDIR/ACCOUNT-OBJECTS.$TODAY.dump.bz
for i in `psql -U postgres -c "select datname from pg_database where datname <> 'template0'" -t template1`
do
pg_dump -U postgres -C $i |bzip2 > $TMPDIR/$i.$TODAY.dump.bz
done
test -f $BKPDIR/pgsql_$DAY.tar.gz && rm $BKPDIR/pgsql_$DAY.tar.gz
tar -C $TMPDIR -c -f $BKPDIR/pgsql_$DAY.tar `ls $TMPDIR`
rm -fr $TMPDIR
if [ $(date +%d) = "01" ]; then
test -f $BKPDIR/pgsql_$MONTH.tar.gz && rm $BKPDIR/pgsql_$MONTH.tar.gz
cp $BKPDIR/pgsql_$DAY.tar $BKPDIR/pgsql_$MONTH.tar
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment