Heroku recently announced that they are changing the way you backup your app’s database. They are deprecating their bundles add-on and have added a new PostgreSQL only backup feature. This is great news as the old bundles method included a copy of your source code as well. In one instance, our backups went from 90MB compressed to just 17MB uncompressed.
Installing the Heroku PGBackups Add-On
First, you’ll need to install the latest version of the heroku gem.
sudo gem install heroku
Next, go into your project directory and install the pgbackups add-on.
cd ~/projects/MyProject/
heroku addons:add pgbackups
Once installed you can easily make a backup to the cloud.
heroku pg:backups capture --expire
There are two things to note here:
- This backup now happens synchronously instead of in the background. This makes automation much easier as you don’t have to sleep for a few minutes and hope it’s done.
- The ––expire parameter automatically deletes the oldest backup, making historical cleanup a cinch.
Once the backup completes you can fire the download command off. This will get a public url (heroku pgbackups:url) and download it using curl to a directory on your computer. The file is also renamed with a timestamp like 20101213 for easy sorting.
curl -o /MyComputer/Drobo/backups/MyProject/MyProject_pgbackup_`date '+%Y%m%d'`.sql `heroku pgbackups:url`
That’s it. They’ve really done a great job making it easier to backup your database and it works every time.
What else can you do?
For more information, checkout the Heroku documentation on pgbackups. They have a great example at the bottom for transferring your database between production and staging sites, something we do all the time.