Mar 2014

Making DB backups with Ruby

The past week I mounted a DB backup system, for a couple of Ruby web apps (Rails and Sinatra), using the Backup gem. I found the process pretty straightforward, so I decided to share my experience.

The backup gem provides a very nice set of features:

  • Databases support: MySQL, MongoDB, PostgreSQL, Redis …
  • Compression
  • Encryption
  • Storages: Amazon S3, Local, RSync, Dropbox …
  • Notifiers: Email, Twitter, Hipchat …
  • Friendly DSL
  • The plugin is totally independent to Rails, so you can use it for other applications.

Backup model example (MySQL, Amazon, Gzip and email notifications):, 'My backup description') do
  database MySQL do |db|     = "database_name"
    db.username = "username"
    db.password = "pass"     = "localhost"
    db.port     = 3306

  store_with S3 do |s3|
    s3.access_key_id     = "access_key_id"
    s3.secret_access_key = "secret_access_key"
    s3.bucket            = "bucket_name"
    s3.path              = "path/to/your/backups"

  compress_with Gzip

  notify_by Mail do |mail|
    mail.on_success     = true
    mail.on_warning     = true
    mail.on_failure     = true

    mail.from           = ""             = ""
    mail.address        = ""
    mail.port           = 587
    mail.domain         = ""
    mail.user_name      = ""
    mail.password       = "pass"
    mail.authentication = "plain"

Perform the backup with the following command:

> backup perform --trigger my_backup

You can also schedule your backups with a cron job (for example using the whenever gem) and you’ll achieve a simple and effective solution:

every, :at => '1:00 am' do
  command "backup perform --trigger my_backup"

Hope this can help you!