Skip to content

Instantly share code, notes, and snippets.

@kane-c
Last active July 21, 2017 04:54
Show Gist options
  • Save kane-c/c497c90130412cea1c971f22382215b8 to your computer and use it in GitHub Desktop.
Save kane-c/c497c90130412cea1c971f22382215b8 to your computer and use it in GitHub Desktop.
Postgres backup script to be used with Docker/Dokku
#!/usr/bin/env sh
# Usage: backup.sh database username container-name drive-folder-id gpg-recipient
# Requires gdrive: https://github.com/prasmussen/gdrive
# wget 'https://docs.google.com/uc?id=0B3X9GlR6EmbnQ0FtZmJJUXEyRTA&export=download' -O /usr/local/bin/gdrive
# chmod +x /usr/local/bin/gdrive
# Store Postgres credentials in `.pgpass` and gdrive configuration in `.gdrive.conf`
# Ensure GPG is configured too
timeslot=`date '+%Y%m%d%H%M'`
user=$2
database=$1
container=$3
parent_id=$4
gpg_recipient=$5
backup_dir="~/backups/$database"
filename="$backup_dir/$database-$timeslot.sql"
mkdir -p $backup_dir
docker exec "$container" vacuumdb -z -U "$user" "$database" > /dev/null 2>&1
docker exec "$container" pg_dump -U "$user" -w "$database" > "$filename"
gpg -r $gpg_recipient -e $filename
cd $backup_dir
# Upload the file to Google Drive
gdrive upload --no-progress --parent $parent_id $backup_dir/$filename.gpg > /dev/null
# Delete older backups from local storage (they are still kept in Drive)
find . -type f -mtime +6 -exec rm {} \;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment