Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save virtualstaticvoid/1640598 to your computer and use it in GitHub Desktop.
Save virtualstaticvoid/1640598 to your computer and use it in GitHub Desktop.
Automate Heroku database backups
#!/bin/bash
#
# Script to create and download a database backup on Heroku
# Written by Chris Stefano (https://github.com/virtualstaticvoid)
#
# See http://devcenter.heroku.com/articles/pgbackups for details
#
appname=$1
filename=${appname}_db_$(date +%Y%m%d)_$(date +%H%M).pg_backup
# list backup
# heroku pgbackups --app $appname
# may need to delete old backups:
# heroku pgbackups:destroy XXXX --app $appname
# create a backup
heroku pgbackups:capture --app $appname
# get the temp URL for it (AWS S3)
backup_url=`heroku pgbackups:url --app $appname`
# download it locally
curl -o $filename $backup_url
#!/bin/bash
# Script to "clean up" old database backups on Heroku
# Written by Chris Stefano (https://github.com/virtualstaticvoid)
#
# See http://devcenter.heroku.com/articles/pgbackups for details
#
# NOTE: Heroku seems to only allow 2 backups at any given time, presumable on the free tier?,
#
# !!! USE AT YOUR OWN RISK!!!
#
appname=$1
# get the backup line-items from the `pgbackups` command
backup=`heroku pgbackups --app $appname | grep ^b[0-9]*`
# extract the name of it
backup_name=${backup:0:4}
# BE GONE!!!
heroku pgbackups:destroy $backup_name --app $appname
@tiredpixel
Copy link

Hi! Thanks for this. :)

I took the liberty of forking and making a few tweaks of my own; you might be interested:

  • used --expire option, removing need for second script (I think)
  • changed naming of downloaded file (personal preference), plus used .dump extension
  • symlinked to latest dump (I always like having a quick way of accessing the latest dump)

https://gist.github.com/tiredpixel/4774216

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment