Skip to content

Instantly share code, notes, and snippets.

@mrrooijen

mrrooijen/heroku_pg_backup

Last active Apr 18, 2016
Embed
What would you like to do?
Fetches, encrypts, transfers and rotates Heroku Postgres backups to/on Amazon S3.
#! /bin/bash
# Fetches, encrypts, transfers and rotates Heroku Postgres backups to/on Amazon S3.
# Requirements:
#
# 1. Heroku Toolbelt
#
# $ wget -O- https://toolbelt.heroku.com/install-ubuntu.sh | sh
# $ heroku login
#
# 2. s3cmd
#
# $ apt-get install python-pip
# $ pip install s3cmd
# $ s3cmd --configure
#
# 3. gpg public key
# Installation:
#
# 1. Place this script in
#
# /usr/local/bin/backup
#
# 2. Make this script executable
#
# $ chmod a+x /usr/local/bin/backup
#
# 3. Optional: Add to crontab (`0 0 * * * /usr/local/bin/backup`)
#
# $ crontab -e
# Usage:
#
# $ /usr/local/bin/backup (or just `backup` if in $PATH)
### BEGIN CONFIGURATION
app=my-heroku-app
bucket=my-s3-bucket
bucket_path=s3-bucket-path
gpg_public_key=me@example.com
keep=7
### END CONFIGURATION
export PATH=/usr/local/bin:$PATH
backup_file=`date +%s`-$app.dump.gpg
echo "Acquiring URL to latest backup"
url=`heroku pg:backups public-url -a $app`
echo "Fetching, encrypting and transfering latest backup from Heroku to S3"
curl "$url" \
| gpg -e -r $gpg_public_key \
| s3cmd put - s3://$bucket/$bucket_path/$backup_file
echo "Rotating backups on S3 (keep $keep latest backups)"
s3cmd ls s3://$bucket/$bucket_path/ \
| grep -e gpg$ \
| awk '{print $4}' \
| head -n -$keep \
| xargs s3cmd del
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment