Db2fog

Several years ago Xavier Shay published his db2s3 gem for backing up the database from your rails project to Amazon S3.

Xavier’s original code depended on the aws-s3 gem. aws-s3 works well enough, however it appears to be unmaintained and has a few rough edges. In particular, it doesn’t cleanly handle S3 regions outside US-east. It also uses class variables for configuration, making it difficult to authenticate with alternate accounts at other points in my application.

The solution to these issues was to switch the dependency from aws-s3 to fog, a newer gem that is actively maintained. As a bonus, it also brings us support for additional storage providers like Rackspace cloudfiles and Ninefold.

After checking with Xavier I’ve forked his code and released a new gem - db2fog.

To use it, start by adding it to the Gemfile in your rails app:

    gem 'db2fog'

Then configure it by putting this in config/initializers/db2fog.rb:

    DB2Fog.config = {
      :aws_access_key_id     => 'yourkey',
      :aws_secret_access_key => 'yoursecretkey',
      :directory             => 'bucket-name',
      :provider              => 'AWS'
    }

Now use the rake tasks to perform backups and restores. Run them manually first, then add them to cron for automatic backups. I recommend running a few backup every few hours and the clean task daily.

    rake db2fog:backup:full  RAILS_ENV=production
    rake db2fog:backup:clean RAILS_ENV=production

That’s it! A dead simple way to get offsite backups that will probably cost you only cents per month. For details and examples for configuring sotrage providers like Rackspace check the project README on github.