Get Heroku pgbackup captures every ten minutes for free! (almost)
You might have heard about the recent AWS outage. Needless to say this outage affected many websites and also many services that piggybacks on AWS cheap cloudy goodness. Heroku is one such service. Heroku also happens to be the service of choice for the startup I work for. While I applaud the herculean effort both by Amazon and by Heroku to manage and mitigate the disaster, the whole thing caught us a bit with our pants not properly around our proverbial waists. It turns out Heroku’s database backup feature, pgbackups, is not a one click automatic affair.
Before I say anything else I do want to mention that Heroku is AWESOME. If you are a small company that does not have the resources to manage your own racks of computers somewhere then I would say Heroku is the best of breed cloud based Rails hosting.
One of the few things I found a bit confusing though is their pgbackups system. You one click add the free “daily backup” feature, take a look at the docs and everything looks straightforward and simple. The only thing is that adding the feature to your app does not in any way schedule it to actually backup anything. To the best of my knowledge you basically need to use the Heroku cron service and put a call to capture a backup in there. Maybe this is obvious to some, but given how effortlessly and automatically everything else works on Heroku, I think it is easy to see how it could be over looked.
Either way, backups were not gotten, aws goes down, Heroku goes down, our site goes down.
Over 48 hours later the majority of service had been restored, except some sites that were using the shared database system like ours. One of the Heroku devs asked if we had a backup we could restore from as this would allow us to be up immediately rather than wait for amazon to restore the EBS volumes that our data happened to be sitting on. As already mentioned, we did not.
Fortunately everything did eventually come back to life and no data was lost, but what a gut wrenching scare it was. I vowed to implement very aggressive backing up of said data.
The solution I came to was to use the staging server we still had around from before our move over to Heroku (its a slicehost VPS machine) as a backup fetcher/storer. I wrote a pretty straight forward rake task that would:
- Initiate the capturing of a backup
- Download the backup
- Load the backup into a the local database of our app on another machine outside of Heroku (and AWS/EC2)
- Keep around only a handful of the backups at a time (so we don't accidently run out of HD space)
- Let me know immediately if the backup did not get fetched for any reason
Here is the code for the rake task. Its not sexy, it could probably be considerably better, but it gets the job done and was easy to write:
Then I run the thing with the following cron command:
For PATH just type echo $PATH from the command line and copy and paste the result in there. Alternatively you could just put in the absolute path for rake (use ‘which rake’ to see where the binary lives). Notice the /dev/null bit at the end. This is very important, if you don’t do this then whatever user is running the cron will get its mailbox JAMMED with all of the output from the thing. If not super familiar with cron I would highly recommend reading one of the many primers out there - its a somewhat fussy feature in unix flavored land and is very easy to screw up (I know I did, repeatedly).
With this working we now get a full dump of our production database and then made redundant by copying to a machine outside of the Heroku ecosystem - every ten minutes - for free (minus the cost of the super small slicehost box). Happy day.
Before running the task via cron you will need to switch over to that unix user and authenticate it via $ heroku auth:login. Once authenticated it should run fine as long as you don’t change your password or something of that nature.blog comments powered by Disqus