A means of automating Heroku's pgbackups and archiving them to Amazon S3 via the fog gem.
Ruby Shell
Switch branches/tags
Pull request Compare This branch is 4 commits ahead, 70 commits behind kjohnston:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
lib
test
.gitignore
.ruby-gemset
.ruby-version
.travis.yml
Gemfile
Gemfile.lock
Guardfile
README.md
Rakefile
pgbackups-archive.gemspec

README.md

Forked

Forked to use retriable gem.

pgbackups-archive

A means of automating Heroku's pgbackups and archiving them to Amazon S3 via the fog gem.

Overview

The pgbackups:archive rake task that this gem provides will capture a pgbackup, wait for it to complete, then store it within the Amazon S3 bucket you specify. This rake task can be scheduled via the Heroku Scheduler, thus producing automated, offsite, backups.

The rake task will use pgbackups' --expire flag to remove the oldest pgbackup Heroku is storing when there are no free slots remaining.

You can configure retention settings at the Amazon S3 bucket level from within the AWS Console if you like.

Use

Backups can be set up by either bundling with your existing heroku app or creating a standalone heroku app just for backups.

Option 1 - Add to your existing application

Add the gem to your Gemfile and bundle:

gem "pgbackups-archive"
bundle install

Option 2 - Creating a new standalone application (Recommended)

Create a new heroku application just for backing up your database. Then clone the pgackups-archive-app project, and push to your new heroku app. You must add a PGBACKUPS_DATABASE_URL config var pointing at your main application's database url (See below).

Aside from decoupling db backups from your main application, creating a standalone pgbackups heroku applicaton has the added benefit of being cheaper as heroku gives you a free dyno for your performing backups. Thanks Heroku!

Install Heroku addons:

heroku addons:add pgbackups
heroku addons:add scheduler:standard

Apply environment variables:

heroku config:add PGBACKUPS_AWS_ACCESS_KEY_ID="XXX"
heroku config:add PGBACKUPS_AWS_SECRET_ACCESS_KEY="YYY"
heroku config:add PGBACKUPS_BUCKET="myapp-backups"
heroku config:add PGBACKUPS_REGION="us-west-2"

By default backups work of your primary database or the value of ENV['DATABASE_URL'], but database backups from your primary can impact the performance of your application. Optionally set an alternate database to perform backups on with:

heroku config:add PGBACKUPS_DATABASE_URL="your_follower_database_url_here"

As mentioned above, the PGBACKUPS_DATABASE_URL is manditory if you are the using pgbackups-archive-app from a separate heroku environment.

Note: A good security measure would be to use a dedicated set of AWS credentials with a security policy only allowing access to the bucket you're specifying. See this Pro Tip on Assigning an AWS IAM user access to a single S3 bucket.

Add the rake task to scheduler:

heroku addons:open scheduler

Then specify rake pgbackups:archive as a task you would like to run at any of the available intervals.

Loading the Rake task

If you're using this gem in a Rails 3 app the rake task will be automatically loaded via a Railtie.

If you're using this gem with a Rails 2 app, or non-Rails app, add the following to your Rakefile:

require "pgbackups-archive"

Testing

This gem uses thincloud-test to manage its test suite configuration. This provides MiniTest, Guard and friends. To run the test suite, use the guard command and save a file or hit enter to run the full suite.

Use the pgbackups-archive-dummy test harness to setup a dummy database on Heroku to test against.

Disclaimer

I shouldn't have to say this, but I will. Your backups are your responsibility. Take charge of ensuring that they run, archive and can be restored periodically as expected. Don't rely on Heroku, this gem, or anything else out there to substitute for a regimented database backup and restore testing strategy.

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Added some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a Pull Request

Contributors

Many thanks go to the following who have contributed to making this gem even better:

License

  • Freely distributable and licensed under the MIT license.
  • Copyright (c) 2012-2013 Kenny Johnston endorse