Skip to content

Instantly share code, notes, and snippets.

@spikegrobstein
Forked from robertsosinski/daily.sh
Created April 30, 2012 02:35
Show Gist options
  • Save spikegrobstein/2555023 to your computer and use it in GitHub Desktop.
Save spikegrobstein/2555023 to your computer and use it in GitHub Desktop.
PostgreSQL - Backup Util
#! /bin/bash -
####
## postgres_backup
## back up postgres on a regular basis
## USAGE:
## ./postgres_backup [ <prefix> ] [ <number_to_keep> ]
## prefix will default to 'pg'
## number_to_keep defaults to 2
##
## Creates a backup in the form of <prefix>_<number>
## Number to keep is the number of backups to keep (still not implemented.)
##
## convention says you should use 'd' for daily backups and 'h' for hourly backups.
## example (for hourly):
## ./postgres_backup h
## example (for daily):
## ./postgres_backup d
####
# initialize some variables
BACKUP_DIR="/var/postgres/backup/"
DATABASE="exchange_prod"
ENCODING="SQL_ASCII"
PG_DUMP="/usr/bin/pg_dump"
PREFIX="${1:-pg}" # default to 'pg' for prefix
cd "$BACKUP_DIR"
# clean up
rm -f "${PREFIX}_2" || true
mv "${PREFIX}_1" "${PREFIX}_2"
# do backup!
$PG_DUMP -E${ENCODING} -Upostgres -Fc -f"${PREFIX}_1" "$DATABASE" \
|| { echo "Errors occurred when backing up. OH NO." 2>&1; exit 1; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment