27.02.2021

How to Set Up Incremental Periodic Backups using Rsync on CentOS 8

Rsync in collaboration with Cron lets you set up periodic incremental backups on CentOS 8. In this tutorial, we’ll show you how to do this. First, you need to install and configure Rsync, as we did in this article.
Here we will show you how to use the power of Rsync to create and store multiple copies of files so you can have a history of changes over a period of time.

Location of backup files

The full folder contains all the backup files. This location is the actual copy of the content at the time of the backup.
In the increment folder, catalogs are created for each date. They contain the old versions of the modified files.

Setting up regular incremental backups

Let’s first create folders for storing backups.

mkdir -p /opt/backup/full
mkdir -p /opt/backup/increment

The regularity of running the backup will be ensured by cron. For example, we will place the script in the /etc/cron.hourly folder, the task will run every hour. Use /etc/cron.daily, /etc/cron.weekly or /etc/cron.monthly folder to select a different frequency. Learn more about setting up cron.

nano /etc/cron.hourly/backup-task

Paste the following lines here. If you used the file name (/etc/secret.passwd), username (rsync-user), data source name (data) are the same as in our first tutorial, now it remains to replace source-server-ip with the IP address of the data source server. Otherwise, you must additionally make the appropriate replacement of parameters in the script.

# Backup
rsync -a --delete --password-file=/etc/secret.passwd rsync-user@source-server-ip::data /opt/backup/full/ --backup --backup-dir=/opt/backup/increment/`date +%Y-%m-%d`/
# Clean up
find /opt/backup/increment -mindepth 1 -maxdepth 2 -type d -mtime +30 -exec rm -rf {} \;

In the Clean up section, you can configure how old incremental copies are deleted using the -mtime parameter. In this case, it is more than 30 days.

Now it remains to configure the necessary rights for our script.

chmod 0744 /etc/cron.hourly/backup-task