Skip to content

Instantly share code, notes, and snippets.

@mrrooijen
Last active April 18, 2016 13:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrrooijen/fe2a676ad4666761dfea96dffb47d823 to your computer and use it in GitHub Desktop.
Save mrrooijen/fe2a676ad4666761dfea96dffb47d823 to your computer and use it in GitHub Desktop.
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