Round Robin Backup

Data backup is very important in managing an application. Without data backup, the continuity of an application-based service is threatened, especially when a disaster occurs. I’m sure all System Administrators believe in the importance of data backup, it’s just that sometimes they forget to do that and that is human. For this reason, it is necessary to have a data backup mechanism that can run automatically, so that problems caused by the absence of data backup can be minimized. One such automatic backup method is Round Robin Backup.

The working principle of Round Robin Backup is how we can backup the database automatically and periodically (eg per day) in a certain cycle (eg weekly cycle). For example, if we were to do a Daily Round Robin Backup, the cycle would be something like this.

From the picture above, we can see that the system will backup every day, starting from Monday to Sunday, then the next day’s backup (Monday) will overwrite last week’s (Monday) backup, and so on. Thus, our system will have a database backup last week.

To do the above the key is in naming the backup file. The naming of the backup file must be based on the day the backup was performed (remember, the day is not the date), for example: backup_senin.sql, backup_Tuesday.sql, and so on.

To facilitate the implementation of this concept, we will convert the name of the day to a number, as below.

By using a file naming format like the following:

backup_daily_db_[database name]#[day number].sql

For example, the database that we are going to backup is called a service, then we will get backup results like the following:

With a file naming format like that, the system will be easy to overwrite our backup file last week with the latest backup file.

For that, there are two things we need to do, namely creating a script and assigning a cronjob.

Scripting

Making a script is needed to get the name of our backup file as described above. Here is the full script to do the Daily Round Robin Backup:

#!/bin/bash

today=$(date +%u)
fname=backup_daily_db_service’#’$today.sql

mysqldump -u [username] -p[password] [database_name] > $fname

Note:
Replace [username] with the database username, [password] with the password, and [database_name] with the name of the database to be backed up, in this case the service.

Save the script with the name daily_backup.sh
Then do chmod so that the script can be executed.

$ sudo chmod 754 daily_backup.sh

Do a test of the script.

$ ./daily_backup.sh

If the script execution produces the desired file, it means the script is correct.

More info about MySQL database backup, can be seen at this link .

Cronjob Assignment

The next step is to assign a cronjob for the execution of the script in question by the system on a scheduled basis.
Here are the steps:

$ crontab -e

Then fill it with the following command:

# Daily Backup at 00:00 AM
0 0 * * * [directory]daily_backup.sh

Note:
[directory] is filled with the full path of the directory where the script file is located, for example: /home/user/backup/scripts/daily_backup.sh

The cronjob command above will execute the backup script every 00.00 every day.

More info about cronjobs, can be seen at this link .

This is an easy way to use Round Robin Backup for automatic database backups.

By using the Round Robin Backup method, we can get several advantages, including the following:
1. We can perform backups automatically;
2. We can get the last backup in a certain cycle (eg, for daily backups we have the last 7 daily backups);
3. We can save storage space, because we only save one particular cycle (for example, for daily backups, we only store 7 backup copies).

Thank you for visiting, hopefully useful.

Leave a Reply

Your email address will not be published.