DB2Fog

A rails plugin to backup Mysql or PostgreSQL to a cloud storage provider. You’re looking at a monthly spend of four cents. So pony up you cheap bastard, and store your backups offsite.

A grandfather style system is used to decide what backups to keep copies of:

  • all backups from the past 24 hours

  • one backup per day for the past week

  • one backup per week forever

Depending on your tolerance for data loss you should be running a backup at least once a day, probably more.

Installation

Add the following to your project Gemfile

gem "db2fog"

Configuration

Add the following to config/initializers/db2fog.rb

In general, you can use any configuration options supported by Fog::Storage.new, plus the :directory option. If fog adds support for extra providers they should work with just a config change to Db2Fog.

Amazon S3

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

Rackspace Cloudfiles

DB2Fog.config = {
  :rackspace_username => 'username',
  :rackspace_api_key  => 'api key',
  :directory          => 'bucket-name',
  :provider           => 'Rackspace'
}

Local Storage

DB2Fog.config = {
  :directory  => 'bucket-name',
  :local_root => Rails.root.to_s + '/db/backups',
  :provider   => 'Local'
}

Passing options to the database driver

Database adaptors may support further configuration via the :database_options hash.

DB2Fog.config = {
  :directory  => 'bucket-name',
  :local_root => Rails.root.to_s + '/db/backups',
  :provider   => 'Local',
  :database_options => {
    :pg_version => 8
  }
}

Supported database options:

pg_version 8 or 9

The major version of PostgreSQL installed on the server. Defaults to 9.

Usage

# Add to your crontab or whatever
rake db2fog:full

# Handy tasks
rake db2fog:restore  # You should be testing this regularly
rake db2fog:clean    # Clean up old backups - cron this

Alternative

If you want to trigger backups from ruby (say, from a delayed job) you can do this:

DB2Fog.new.backup
DB2Fog.new.clean

Compatibility

This is pure so ruby should run on most ruby VMs. I develop on MRI 1.9.2.

This will only work with rails 3. Supporting earlier versions is more complication than I feel like handling at the moment.

Development

Specs are a little weak and mysql based. This code is bit hackish but is being used by quite a few people.

Kudos

This is a fork of Xavier Shay’s db2s3 gem. It worked perfectly, but only supported Amazon S3 within US-east. By switching the dependency to using fog this now supports all S3 regions and multiple storage providers

Xavier’s original gem is available at github.com/xaviershay/db2s3

Xavier quotes the following example as inspiration:

github.com/pauldowman/blog_code_examples/tree/master/mysql_s3_backup