How to Set Up Incremental Periodic Backups using Rsync on Ubuntu 20.04
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/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:
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
dest=/opt/destination
# Source server IP address
ip=10.5.5.10
# Rsync user on source server
user=backup-user
# The resource we configured in the /etc/rsyncd.conf file on the source server
src=data
# Set the retention period for incremental backups in days
retention=30
# 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:
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/.