Skip to content

Instantly share code, notes, and snippets.

@samuelloza
Last active February 28, 2018 22:14
Show Gist options
  • Select an option

  • Save samuelloza/32c9dc991066499406cc8128b136e33d to your computer and use it in GitHub Desktop.

Select an option

Save samuelloza/32c9dc991066499406cc8128b136e33d to your computer and use it in GitHub Desktop.
Backup Postgres.sh
#!/bin/bash
#
# Postgres backup script
# This script must be run with root mysql account
#
# POSTGRES connection info
MYUSER=postgres
MYPASS=Zorro1
MYHOST=localhost
# Location to place backups.
backup_dir="/home/backup"
# Exclude databases, separate by spaces
excludedb="template0 template1"
##### No changes after this line #####
LANG=US
day=$(date '+%A')
databases="bd_sigre"
for db in $databases; do
skipdb=-1
if [ "$excludedb" != "" ]; then
for i in $excludedb; do
if [ "$db" == "$i" ]; then
skipdb=1
logger -p info "Skiping backup of database $db"
fi
done
fi
if [ "$skipdb" == "-1" ]; then
logger -p info "Starting backup of database $db"
PGPASSWORD="$MYPASS" pg_dump -U $MYUSER -h $MYHOST $db | ssh [email protected] "cat | gzip -9 > $backup_dir/$db-$day.sql.gz"
logger -p info "Finished backup of database $db"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment