Switch branches/tags
Find file
Fetching contributors…
Cannot retrieve contributors at this time
103 lines (69 sloc) 2.68 KB


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.


Add the following to your project Gemfile

gem "db2fog"


Add the following to config/initializers/db2fog.rb

In general, you can use any configuration options supported by, 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

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.


# 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


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.


Specs are really weak. This code is bit hackish but is being used by quite a few people.


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

Xavier quotes the following example as inspiration: