At Agira, Technology Simplified, Innovation Delivered, and Empowering Business is what we are passionate about. We always strive to build solutions that boost your productivity.

,

Auto Backup of database in Rails Application

  • By Agira Technologies
  • October 12, 2016
  • 3438 Views

We are going to see how to set up an auto backup of database in Rails application.
A few weeks back we faced a serious threat. One of our clients has an interesting and popular app to their repertoire, which is heavily interactive with public online consumers. The app has a high volume of data processing, transmission and storage requirements. When such an app went through a critical failure situation, it became a serious threat. Such failures are feared by any organization.
We had to immediately resolve the issue and rectify the situation. And the first step involved was to back up the entire data. In this article, I shall go through the steps in the process of taking an auto backup of database in Rails application.
To perform a backup of database in Rails, we need to install the gem backup. This gem should not be added to a Rails application’s gem file, since it is meant for backing up.

Step 1: Installation

Install backup gem with the following command:

gem install backup

 

Step 2: Configuration

We need to configure and generate a model backup file. In this example, we are using a PostgreSQL database, Gzip format compressor, store in S3, and send an email to notify once the backup process is over. Following is the command to achieve this:

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

 
We have configured to notify us via email once the backup of database in Rails application is successfully done. This will generate a model backup file in the following path: ~/Backup/models/your_backup.rb, and this is how the backup file looks like:

Model.new(:your_backup , 'Description for your_backup') do
# Database in PostgreSQL
database PostgreSQL do |db|
# `db.name` has to be set to  `db.name = :all` if you want to dump all databases. If not, the attribute can be left blank
db.name               = "databasename"
db.username           = "username"
db.password           = "password"
db.host               = "localhost"
db.port               = 5432
db.socket             = "/tmp/pg.sock"
db.skip_tables        = ["skip", "these", "tables"]
db.only_tables        = ["only", "these", "tables"]
# If you choose to dump all databases, the `skip_tables` and `only_tables` are ignored.
db.additional_options = ["-xc", "-E=utf8"]
end
# Storage will be Amazon Simple Storage Service
store_with S3 do |s3|
# Let us give the AWS Credentials
s3.access_key_id     = "access_key_id"
s3.secret_access_key = "secret_access_key"
# If you use an IAM Profile, set as following: s3.use_iam_profile = true
s3.region            = "us-east-1"
s3.bucket            = "bucket-name"
s3.path              = "path/to/backups"
end
# Now to set the compressor, which is Gzip in this example.
compress_with Gzip
# 'SMTP' is the default delivery method for Mail Notifiers. To set the notifier to Email:
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   #With this, all necessary configurations as desired, have been set.

 
There are several other options that can be set in the Generator, which is a tool to set up the backups faster.

Step 3: Check Configuration

Next, we have to check if the configuration we set, is right. This is to be done prior to running our configuration.

$ backup check    #This check the configuration is set.

 
If it is right, following is the display message:

[info] Configuration Check Succeeded.

 
If wrong, we will need to recheck the steps we went through till now.

Step 4: Run Backup Commands

We finally can now run our backup with the following command:

backup perform --trigger your_backup
#To perform the backup of database in Rails

 
Once successfully back up, we will receive a notification via email for the same, since that is our configuration, and also the backup of our database will be store in the path s3 provided by us.

Step 5: Auto Backup Database

Now for the final crown on the backup process… we need to auto backup the database. To do this, we shall use the gem whenever.

Step 5a: Add the gem whenever using the following command:

gem 'whenever'

 
Step 5b: We need to generate a schedule for the auto backup to take place. We shall do this by generating a schedule.rb file with the following code:

$ cd /your-project
$ wheneverize . #This will create an initial config/schedule.rb file.

 
Step 5c: Next is to configure this file according to our needs. Example:

every :day, at: '12:00 am' do
command "backup perform --trigger your_backup"
end         #With this, DB Backup code ends.

 
This will auto backup the database at 12.00 a.m. everyday.
This is all that is require to set up auto backup of database in Rails application.
In the scenario we faced at Agira, with our client’s app failure, once this backup was set up for the database, we were ready to resolve the issue and get it running without any loss or corruption of data.

Agira Technologies

AgiraTech is a technology company whose business services and domain solutions supports global clients who comprise the current world economy. Services we offer : Web development, Mobile App development, Blockchain, IoT and DevOps Consulting