Auto Backup of Database in Rails Application | Hire ROR Developers
loader image
Ruby on Rails Technical

Auto Backup of database in Rails Application

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:


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:


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:


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.


If it is right, following is the display message:


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:


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:


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:


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


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.

The following two tabs change content below.
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