/ s3

Backing up Databases and Directories

Backup your Database OR any of your personal stuff like directories with [backup][1]

Backup's Components

  • Archives and Databases
  • Compressors and Encryptors
  • Storages
  • Syncers
  • Notifiers

Its a handy backup tool with plenty of storage options (for example: S3, Dropbox, FTP, SCP and many more)

For installation(Dependencies should be installed if you are working with shell environment)

gem install backup

backup dependencies --install net-ssh

backup dependencies --install net-scp

backup dependencies --install excon

backup dependencies --install fog

Now execute the @backup generate@ command

 backup generate:model -t backup [-c ] --archives \
 --storages='local,s3' --compressors='gzip' --notifiers='mail,twitter'

This will create two files a configuration file @~/Backup/config.rb@ and Backup model @~/Backup/models/backup.rb@

Copy the backup.rb to you /config directory (if path not specified while generation)

[a sample backup model][2]

More complex generators can also be used per your requirement.

To use s3 as your backup location you need some extra configurations to load the S3 credentials and database.yml settings(in case used)

Backup::Model.new(:backup, 'BackUp To S3') do

  split\_into\_chunks\_of 250

  database\_yml = File.expand_path('../database.yml',  _\_FILE\__)
  s3\_yml       = File.expand_path('../amazon_s3.yml',  _\_FILE\__)
  RAILS\_ENV    = ENV['RAILS_ENV'] || 'development'

  require 'yaml'
  config = YAML.load_file(database_yml)

  storage = YAML.load_file(s3_yml)[RAILS_ENV]

  # PostgreSQL [Database]

  database PostgreSQL do |db|
    db.name               = config[RAILS_ENV]["database"]
    db.username           = config[RAILS_ENV]["username"]
    db.password           = config[RAILS_ENV]["password"]
    db.host               = config[RAILS_ENV]["host"]
    db.skip_tables        = []
  end

  store\_with S3 do |s3|
    s3.access_key_id     = storage['connection'][:access_key_id]
    s3.secret_access_key = storage['connection'][:secret_access_key]
    s3.region            = "ap-southeast-1"
    s3.bucket            = storage['bucket']
    s3.path              = "/daily-backup"
    s3.keep              = 50
  end

  # Gzip [Compressor]

  compress\_with Gzip

  # Mail [Notifier]

  notify\_by Mail do |mail|
    mail.on_success           = true
    mail.on_warning           = true
    mail.on_failure           = true

    mail.from                 = "from@example.com"
    mail.to                   = "recepient@example.com"
    mail.address              = "<smtp_settings>"
    mail.port                 = 587
    mail.domain               = "example.com"
    mail.user_name            = "<user_name>"
    mail.password             = "<password>"
    mail.authentication       = "plain"
    mail.encryption = true
  end
end

Now to run the backup execute following command on rails root

 backup perform --trigger backup --config_file config/backup.rb \
 --data-path db --log-path log --tmp-path tmp

How about scheduling the backup command ?
The backup should be triggered automatically and needs to be scheduled

Use [whenever][3] OR [rufus-scheduler][4]

With whenever:

every 1.day, at: Time.zone.parse('01:00') do
  command "cd #{path} && RAILS_ENV=#{environment} backup perform --trigger backup \
 --config_file config/backup.rb --data-path db --log-path log --tmp-path tmp"
end

A rake task would be awesome here, [backup-task][5]
This is not up-to-date but can be easily customized.

So you can just do

every 1.day, at: Time.zone.parse('01:00') do
  rake 'db:backup'
end

This would be good to go for scheduled backup.

PS Note: Restore is missing worth to look at [backup2s3][6]

Recommended [s3db-backup][7] but has limitation on mysql can't be used with PG
[1]: https://github.com/meskyanichi/backup
[2]: https://github.com/meskyanichi/backup/wiki/Getting-Started#generating-your-first-backup-model
[3]: https://github.com/javan/whenever
[4]: http://knowbuddy.kiprosh-app.com/kyu_entries/rufus-scheduler
[5]: https://github.com/edebill/backup-task
[6]: https://github.com/aricwalker/backup2s3
[7]: https://github.com/mmarschall/s3db-backup