Automatic Scheduled backups with Backup Gem

Automatic Backup Plan with Backup Gem , Setting up automatic schedules backup plan for a single server using backup ruby gem and local storage or you can use amazon was s3 buckets there are many choices including dropbox but in this post I will only cover local storage mode , you can use cloud storage sync and backup providers to sync backups eg. copy.com and mega.nz etc.
so let’s get started to ” Automatic backups with Backup Gem “.

Please Note: This is intended for single , or personal server . please learn backup gem documentation before using in production . for large and huge distributed applications please see some-other backup solution .

automatic-backup

Automatic Backup WorkFlow

  • We will use backup ruby gem , and write some ruby scripts to perform backups of files and database .
  • after that it will send notification email .
  • we will set crontab to perform this job every day .

Intro to Backup Gem

Backup is ruby gem written by Michael van Rooijen . its provide simple ruby DSL for backup . its easy to use and feature rich . overall its great for backup if you don’t have any ruby background d. don’t worry its simple as writing 5 lines in human language , additionally for security you can encrypt all backup files .

Backup allows you to model your backup jobs using a Ruby DSL

Github
Documentation

Codes

okay lets setup BackUp Gem
Backup gem is ruby gem so lets install ruby first then we will setup backup gem .

sudo apt-get install curl build-essential
curl -sSL https://get.rvm.io | bash -s stable --ruby --gems=backup,whenever
source $HOME/.rvm/scripts/rvm
rvm use ruby
gem install backup

okay lets setup first backup model . in this model we are going to backup nginx files .

backup generate:model --trigger nginx_backup
--archives --storages='local' --compressor='gzip' --notifiers='mail'

output will look like this

Generated configuration file: '/home/alok/Backup/config.rb'.
Generated model file: '/home/alok/Backup/models/nginx_backup.rb'.

Models

okay lets create our first model to backup nginx config files on daily basis . you can use this kind of models according to your requirements like backing up your WordPress based blog and other stuffs .
you can also add mysql block in model to backup your mysql database .

Lets edit nginx_backup.rb to backup nginx configurations

# encoding: utf-8

##
# Backup Generated: nginx_backup
# Once configured, you can run the backup with the following command:
#
# $ backup perform -t nginx_backup [-c ]
#
# For more information about Backup's components, see the documentation at:
# http://meskyanichi.github.io/backup
#
Model.new(:nginx_backup, 'Description for nginx_backup') do
##
# Archive [Archive]
#
# Adding a file or directory (including sub-directories):
# archive.add "/path/to/a/file.rb"
# archive.add "/path/to/a/directory/"
#
# Excluding a file or directory (including sub-directories):
# archive.exclude "/path/to/an/excluded_file.rb"
# archive.exclude "/path/to/an/excluded_directory
#
# By default, relative paths will be relative to the directory
# where `backup perform` is executed, and they will be expanded
# to the root of the filesystem when added to the archive.
#
# If a `root` path is set, relative paths will be relative to the
# given `root` path and will not be expanded when added to the archive.
#
# archive.root '/path/to/archive/root'
#
archive :my_archive do |archive|
# Run the `tar` command using `sudo`
# archive.use_sudo
archive.add "/etc/nginx/"
end

##
# Local (Copy) [Storage]
#
store_with Local do |local|
local.path = "/home/alok/backup/nginx"
local.keep = 5
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 = "my_password"
mail.authentication = "plain"
mail.encryption = :starttls
end

end

adjust email setting accordingly .

you can generate and use these kind of models to backup your wordpress , forums or any kind of files .

Cron Jobs

Cron is a time based job scheduler for unix like operating systems . we can use cron to schedule to run backup script accordingly .
there is awesome gem in ruby called “whenever ” is provide great dsl for writing cronjobs . and great thing about whenever is its supported by backup gem . means you can use whenever gem with backup script .

gem install whenever

cd ~/Backup

#whenever gem expect a file named `schedule.rb` inside `config` directory so lets create it

mkdir -p config/
touch config/schedule.rb

wheneverize

okay lets setup crontab using whenever gem to trigger nginx_bakup daily at 12AM

edit ~/Backup/config/schedule.rb

every 1.day , :at => '12 am' do
command "backup perform -t nginx_backup"
end

okay lets check syntax and all other configs

backup check

# Sample output
[2014/10/05 07:51:02][info] Configuration Check Succeeded.

now lets write cron using whenever gem

cd ~/Backup
whenver -w

Now relax , you can use any cloud based storage and sync service to sync your backup files ( eg. copy.com, mega.nz etc ).
🙂

Tagged With : - - -
Add Comment