DB2Fog

A rails plugin to backup Mysql 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 decides 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'
}

Usage

# Add to your crontab or whatever
rake db2fog:backup:full

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

Compatibility

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

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

Development

Specs are really weak. 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