Skip to content

Instantly share code, notes, and snippets.

@tpitale
Forked from bkenny/db-backup.sh
Last active March 7, 2022 02:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tpitale/5900882 to your computer and use it in GitHub Desktop.
Save tpitale/5900882 to your computer and use it in GitHub Desktop.
Simple shell script to backup each postgresql database to s3.
#!/bin/bash
BACKUP="/tmp/s3/db"
DATE=`date +"%Y-%m-%d"`
DIR="${BACKUP}/${DATE}/"
if [ ! -d $DIR ];
then
mkdir -p $DIR
fi
for DATABASE in `echo 'SELECT datname FROM pg_database;' | psql -t -WSECRET -U da_admin template1`
do
echo -n Backing up $DATABASE .....
pg_dump -ROx -WSECRET -U da_admin $DATABASE | gzip -c > ${DIR}/${DATABASE}.sql.gz 2>/dev/null
echo Done
done
echo -n Uploading to Amazon S3
s3cmd put ${DIR} s3://S3-BUCKET-NAME/db/${DATE}/ --recursive
echo -n Removing local directory ...
rm -rf ${DIR}
echo Done
@tpitale
Copy link
Author

tpitale commented Jul 1, 2013

This does include postgres, template0, and template1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment