Skip to content

Instantly share code, notes, and snippets.

@masbog
Created February 11, 2015 04:05
Show Gist options
  • Save masbog/3d3773eabff2bf893605 to your computer and use it in GitHub Desktop.
Save masbog/3d3773eabff2bf893605 to your computer and use it in GitHub Desktop.
postgre daily backup to file with shell script and can use in cronjobs
#!/bin/bash
PGHOST="8.0.6.13"
PGUSER="masbog"
PGDB="masdb"
PGVERSION="9.4"
FILENAME="/tmp/dbdump/$(date '+%A-%M-%m-%Y-%T.dump')"
STARTTIME="START AT $(date '+%A-%M-%m-%Y-%T')"
echo "removing old data rm -f /tmp/dbdump/*"
rm -f /tmp/dbdump/*
echo "Done... [ok 1/5]"
echo "psql -c 'DROP DATABASE $PGDB' -U postgres -h localhost"
psql -c 'DROP DATABASE $PGDB' -U postgres -h localhost
echo "Done... [ok 2/5]"
echo "psql -c 'CREATE DATABASE $PGDB' -U postgres -h localhost"
psql -c 'CREATE DATABASE $PGDB' -U postgres -h localhost
echo "Done... [ok 3/5]"
echo "/usr/pgsql-$PGVERSION/bin/pg_dump -h $PGHOST -Fc -o -U $PGUSER $PGDB > $FILENAME"
/usr/pgsql-$PGVERSION/bin/pg_dump -h $PGHOST -Fc -o -U $PGUSER $PGDB > $FILENAME
echo "Done... [ok 4/5]"
echo "psql -U $PGUSER $PGDB -C $FILENAME"
/usr/pgsql-$PGVERSION/bin/pg_restore -U $PGUSER -h localhost --dbname=$PGDB --verbose $FILENAME
echo "Done... [ok 5/5]"
echo "START AT $STARTTIME FINISH AT $(date '+%A-%M-%m-%Y-%T')"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment