How to Automatically Back Up your Heroku site to Dropbox

This post will explain how you can configure your machine to automatically back up your Heroku site each night and save the bundle to your Dropbox folder.

At a high level, here’s how it works:

1) Each night at 11pm, a cron job executes a Ruby script that tells Heroku to create a bundle of your site

2) At midnight, the same script downloads the bundle and moves it to your backup folder, which can either be a local directory or your Dropbox folder

Creating and Downloading a Heroku Bundle

The following Ruby script does one of things depending on whether a bundle exists or not.

If a bundle does not exist, the script tells Heroku to create one. If one does exist, the script will download it and move it to your backup location.

Creating a bundle can take some time depending on how large your site is, so you should wait a while before running the script again to download it, which is why we’re going to tell the cron job to wait an hour before downloading it.

The script takes two arguments:

  1. The full path to your local Heroku application
  2. The full path of your backup folder

For example, here’s how I would have the script back up jMockups:

ruby heroku_backup.rb /Users/Matt/jmockups /Users/Matt/Dropbox/Backups/

This tells the script that my app is located in /Users/Matt/jmockups and I want to save the bundle to /Users/Matt/Dropbox/Backups/.

The code:



# run a command from your app’s root directory
def cmd(str)
return `cd #{APP_DIR}; #{str}`.sub(10.chr, ”)

puts(‘* Determining bundle status…’)
status = cmd(‘heroku bundles’)

unless status.index("has no bundles.").nil?
# No bundle currently exists, so have Heroku create one
puts(‘*** Capturing bundle because none exist…’)
capture = cmd(‘heroku bundles:capture’)

bundle_name = status.split.first

unless status.index(‘complete’).nil?
puts(‘*** Bundle was captured successfully’)
puts(‘* Downloading bundle…’)

download = cmd(‘heroku bundles:download’)

puts("*** Moving #{bundle_name} to backup location…")
filename = download.split.last
newname = "#{bundle_name} (#{‘%Y-%m-%d %H%M%S’)}).tar.gz"

move = `mv #{APP_DIR}/#{filename} "#{BACKUP_DIR}/#{newname}"`

puts(‘*** Destroying remote bundle…’)
destroy = cmd("heroku bundles:destroy #{bundle_name}")

puts(‘* Done’)


unless status.index(‘capturing’).nil?
puts(‘*** Still capturing. Try again in a bit…’)


Automating the Backup Process with Cron

Once you’re convinced this works as advertised, you can automate this process with cron:

  1. Delete any existing bundles using the heroku bundles:destroy command. If you don’t the script will download the bundle at 11pm and capture it at midnight, which will work, but is probably opposite of what you intend
  2. At the command line, type crontab -e
  3. Add the following line to the crontab file, replacing the paths with your own:

0 0,23 * * * /usr/bin/ruby /Users/Matt/Documents/Backups/heroku_backup.rb /Users/Matt/jmockups /Users/Matt/Dropbox/Backups

This basically says “When the minute value of the current time is 0 and the hour is 0 or 23 (midnight or 11pm), use Ruby to run heroku_backup.rb with these parameters.

With automatic daily backups, you can rest easier by knowing that if your site gets obliterated things will suck a little bit less. And if you have additional sites, you can simply add multiple lines to the crontab file with the information for your other apps.

Plus, it beats paying $20/month for unlimited bundles with the Heroku addon. :)

One thought on “How to Automatically Back Up your Heroku site to Dropbox

  1. Running PHP scripts with Cron Job « Php Bugs

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s