Auto Backup Database in Rails Application

We use backup gem which easy full stack backup operations on UNIX-like systems.

1. Installation

gem install backup

*Notes: Do not add gem backup to another application's Gemfile. This means you should not add Backup to a Rails application's Gemfile. 

 

2. Configuration 

We'll generate a backup model file with options:

  + PostgreSQL database

  + Store in S3

  + Compressor: Gzip format

  + Send email to notify after backup process finished.

$ backup generate:model --trigger your_backup --databases="postgresql" --storages="s3" --compressor="gzip" --notifiers="mail"

 

It will generate a model backup file in ~/Backup/models/your_backup.rb that looks like this:

Model.new(:your_backup , 'Description for your_backup') do

  ##
  # PostgreSQL [Database]
  #
  database PostgreSQL do |db|
    # To dump all databases, set `db.name = :all` (or leave blank)
    db.name               = "database_name"
    db.username           = "username"
    db.password           = "password"
    db.host               = "localhost"
    db.port               = 5432
    db.socket             = "/tmp/pg.sock"
    # When dumping all databases, `skip_tables` and `only_tables` are ignored.
    db.skip_tables        = ["skip", "these", "tables"]
    db.only_tables        = ["only", "these", "tables"]
    db.additional_options = ["-xc", "-E=utf8"]
  end

  ##
  # Amazon Simple Storage Service [Storage]
  #
  store_with S3 do |s3|
    # AWS Credentials
    s3.access_key_id     = "access_key_id"
    s3.secret_access_key = "secret_access_key"
    # Or, to use a IAM Profile:
    # s3.use_iam_profile = true

    s3.region            = "us-east-1"
    s3.bucket            = "bucket-name"
    s3.path              = "path/to/backups"
  end

  ##
  # Gzip [Compressor]
  #
  compress_with Gzip

  ##
  # Mail [Notifier]
  #
  # The default delivery method for Mail Notifiers is 'SMTP'.
  # See the documentation for other delivery options.
  #
  notify_by Mail do |mail|
    mail.on_success           = true
    mail.on_warning           = true
    mail.on_failure           = true

    mail.from                 = "sender@email.com"
    mail.to                   = "receiver@email.com"
    mail.address              = "smtp.gmail.com"
    mail.port                 = 587
    mail.domain               = "your.host.name"
    mail.user_name            = "sender@email.com"
    mail.password             = "password"
    mail.authentication       = "plain"
    mail.encryption           = :starttls
  end

end

 

You can view more in Generator to get more options.

 

3. Check your configuration

Before running your backup, you should check your configuration:

$ backup check

If everything is okie, it'll show message:

[info] Configuration Check Succeeded.

 

4. Running your backup

backup perform --trigger your_backup

After running completed, you'll receive an email notification and see your backup in the path (S3 path) you specified.

 

5. Auto backup using 'whenever'

Step 1: Add gem

gem 'whenever'

 

Step 2: Generate schedule.rb file

$ cd /your-project
$ wheneverize .

This will create an initial config/schedule.rb file for you.

 

Step 3: Add config to schedule.rb

every :day, at: '1:00 am' do
  command "backup perform --trigger your_backup"
end

 

6. References