After installing my blog, the remaining part is to set a proper backup policy. The easiest way is to use the backup gem which provides anything i could want for backups. It’s very well documented on the gem website so i will only describe my setup as a reminder.

backup

The first step is to install the backup gem.

gem install --no-rdoc --no-ri backup

Generate a configuration model :

backup generate:model --trigger my_backup --databases="postgresql" --storages="ftp" --encryptor="ssl" --compressor="gzip" --notifiers="mail"

It will generate the following configuration :

  • backup postgresql databases
  • transfer backup by ftp
  • compress data with gzip
  • send email notifications

After that, the last steps are to configure the backup model file : ~/Backup/models/my_backup.rb

Database backup configuration :

  database PostgreSQL do |db|
    # To dump all databases, set `db.name = :all` (or leave blank)
    db.name = "djouxblog_production"
    db.username = "username"
    db.password = "password"
    db.host = "localhost"
    db.port = 5432
    db.additional_options = ["-xc", "-E=utf8"]
  end

I used sendmail to send email notifications :

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

    mail.delivery_method = :sendmail
    mail.from = "backup@test.com"
    mail.to = "user@test.com"
  end

And the configuration for ftp transfer :

  store_with FTP do |server|
    server.username = "user"
    server.password = "password"
    server.ip = "ftpserver"
    server.port = 21
    server.path = "~/"
    server.keep = 5
    server.passive_mode = true

Last step is to backup :

[2014/06/14 10:20:10][info] Performing Backup for 'Description for my_backup (my_backup)'!
[2014/06/14 10:20:10][info] [ backup 4.0.1 : ruby 2.1.2p95 (2014-05-08 revision 45877) [x86_64-linux] ]
[2014/06/14 10:20:10][info] Database::PostgreSQL Started...
[2014/06/14 10:20:10][info] Using Compressor::Gzip for compression.
[2014/06/14 10:20:10][info] Command: '/bin/gzip'
[2014/06/14 10:20:10][info] Ext: '.gz'
[2014/06/14 10:20:10][info] Database::PostgreSQL Finished!
[2014/06/14 10:20:10][info] Packaging the backup files...
[2014/06/14 10:20:10][info] Using Encryptor::OpenSSL to encrypt the archive.
[2014/06/14 10:20:10][info] Packaging Complete!
[2014/06/14 10:20:10][info] Cleaning up the temporary files...
[2014/06/14 10:20:10][info] Storage::FTP Started...
[2014/06/14 10:20:12][info] Storing 'ftpserver:my_backup/2014.06.14.10.20.10/my_backup.tar.enc'...
[2014/06/14 10:20:12][info] Cycling Started...
[2014/06/14 10:20:12][info] Storage::FTP Finished!
[2014/06/14 10:20:12][info] Cleaning up the package files...
[2014/06/14 10:20:12][info] Backup for 'Description for my_backup (my_backup)' Completed Successfully in 00:00:02
[2014/06/14 10:20:12][info] Sending notification using Notifier::Mail...

To schedule the backup, the recommended way is to use whenever.

Install the gem :

gem install --no-rdoc --no-ri whenever

Creating configuration file

mkdir config
wheneverize
[add] writing `./config/schedule.rb'
[done] wheneverized!

The configuration file is config/schedule.rb :

every 1.day, :at => '4:30 am' do
  command "backup perform -t my_backup"
end

Enable the job in crontab :

whenever --update-crontab
[write] crontab file updated
rails@krystalia:~$ crontab -l

# Begin Whenever generated tasks for: /home/rails/config/schedule.rb
30 4 * * * /bin/bash -l -c 'backup perform -t my_backup'

# End Whenever generated tasks for: /home/rails/config/schedule.rb

the end

That’s all :) It works pretty well and is easy to configure. It can too handle flat files or directories to provides a full backup solution.