Skip to content

Instantly share code, notes, and snippets.

@fabiopiovam
Created October 5, 2014 03:39
Show Gist options
  • Save fabiopiovam/eba930889f15064248a3 to your computer and use it in GitHub Desktop.
Save fabiopiovam/eba930889f15064248a3 to your computer and use it in GitHub Desktop.
Postgres 9.1 backup script to Cron
#!/bin/bash
# script by http://www.emidioleite.com.br/um-simples-script-para-backup-de-bases-postgres-sql-usando-linux/
# thanks!
# Location to place backups.
backup_dir="$1"
#String to append to the name of the backup files
backup_date=`date +%Y-%m-%d`
#Numbers of days you want to keep copie of your databases
number_of_days=30
databases=`/usr/lib/postgresql/9.1/bin/psql -l -t | cut -d'|' -f1 | sed -e 's/ //g' -e '/^$/d'`
for i in $databases; do
if [ "$i" != "template0" ] && [ "$i" != "template1" ]; then
echo Dumping $i to $backup_dir$i\_$backup_date
/usr/bin/pg_dump $i > $backup_dir$i\_$backup_date
fi
done
find $backup_dir -type f -prune -mtime +$number_of_days -exec rm -f {} \;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment