Public API for Service Management

How to Set Up Incremental Periodic Backups using Rsync on Ubuntu 20.04

Joe Harris
January 31, 2021

Earlier we looked at installing Rsync on Ubuntu 20.04 and creating a one-time backup. To complete the setup from the current guide, follow the steps in the first article.

For many tasks, it is enough to add the execution of such a backup to cron, but these tools allow you to make and store multiple copies of files and have a history of changes over a certain period of time. In this tutorial, we will take a look at how to set up an incremental periodic backup using Rsync on Ubuntu 20.04.

Backup logic

When backing up for the first time, all target files are placed in the full folder. On subsequent launches, the script updates all the files in this folder, and places the old versions of the changed files in the increment folder with the corresponding date. Thus, an up-to-date full backup is constantly maintained, as well as a list of changed files for each date. The retention period is configured, as well as the frequency of the task launch.

We performed the basic configuration for the interaction of servers using Rsync in the first part of the manual. We are now going to create a script for regular incremental backups.

Creating a backup script

Create folders to store your backups:

sudo mkdir -p /opt/destination/full
sudo mkdir -p /opt/destination/increment

When creating a script file, you can select a location folder. If you put it in the /etc/cron.hourly folder, synchronization will happen hourly, and if in the /etc/cron.daily, then daily, etc. Let’s create a script file:

sudo nano /etc/cron.hourly/backup

Avoid dots in the script file name in cron scheduler folders. Learn more about Cron.

In the following script, you must at least specify the correct IP address of the source server. The rest of the values can be left as is. Paste the following lines into the open file:

# Path to folder for backups
# Source server IP address
# Rsync user on source server
# The resource we configured in the /etc/rsyncd.conf file on the source server
# Set the retention period for incremental backups in days
# Start the backup process
rsync -a --delete --password-file=/etc/rsyncd.passwd ${user}@${ip}::${src} ${dest}/full/ --backup --backup-dir=${dest}/increment/`date +%Y-%m-%d`/
# Clean up incremental archives older than the specified retention period
find ${dest}/increment/ -mindepth 1 -maxdepth 2 -type d -mtime +${retention} -exec rm -rf {} ;

Save the script file and add launch rights:

sudo chmod 0744 /etc/cron.hourly/backup

Now the script will synchronize data in the source and destination every hour, adding old versions of deleted files to the corresponding folder in /opt/destination/increment/.

Start Your Cloud Journey Migration made simplified. Take the first step right now.
We use cookies to provide our services and for analytics and marketing. To find out more about our use of cookies, please see our Privacy Policy. By continuing to browse our website, you agree to our use of cookies.