Skip to content

Instantly share code, notes, and snippets.

@iambibhas
Last active December 30, 2015 11:09
Show Gist options
  • Save iambibhas/7820947 to your computer and use it in GitHub Desktop.
Save iambibhas/7820947 to your computer and use it in GitHub Desktop.
Postgresql database backup script
# IMPORTANT: Run this from the machine that's running the DB
# e.g. Vagrant box. Or change the HOST variable
# Destination directory where backups should be put
DEST_DIR="."
HOST="localhost"
DBUSER="user"
# List of directories to backup
# each directory in a new line
DBS="this_db
that_db"
for d in $DBS
do
echo "Backing up $d"
# SLUG="$(echo -n "${d}" | sed -e 's/[^[:alnum:]]/-/g' | tr -s '-' | tr A-Z a-z)"
# binary backup file
pg_dump -U $DBUSER -h $HOST -W -F c --file=$DEST_DIR/$d-`date +"%d-%b-%y"`.backup $d
# plain SQL without owner/role
pg_dump -Ox -h $HOST -U $DBUSER $d > $DEST_DIR/$d-`date +"%d-%b-%y"`.pgsql
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment