News
Faster Speeds: Bandwidth for vStack Servers in Kazakhstan Increased to 200 Mbps
JH
Joe Harris
January 31 2021
Updated June 27 2025

How to Set Up Incremental Periodic Backups with Rsync on Ubuntu 20.04 – Step-by-Step Guide

Backup Linux Ubuntu

Using another OS?

Select the desired version or distribution.

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:

#!/bin/bash
# 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:

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/.

Conclusion

In this tutorial, you learned how to set up incremental periodic backups on Ubuntu 20.04 using Rsync and automate the process with cron jobs. By maintaining a full backup folder and incrementally saving changed or deleted files with timestamps, this approach ensures that you always have an up-to-date backup as well as historical versions for recovery. The retention period configuration helps you manage disk space by automatically removing old backups. This solution is reliable, efficient, and ideal for routine server backup tasks, providing both data protection and version history without manual intervention.

FAQ

  • Q: What is the advantage of using incremental backups with Rsync?
    A: Incremental backups save only the changed or deleted files after the initial full backup, reducing storage space and backup time while keeping a history of changes.
  • Q: How often should I run the backup script?
    A: It depends on your data change frequency and backup needs. You can schedule the script hourly, daily, or weekly by placing it in the corresponding cron folder (/etc/cron.hourly, /etc/cron.daily, etc.).
  • Q: Can I change the retention period for old backups?
    A: Yes, simply modify the retention variable in the script to the desired number of days for which you want to keep incremental backups.
  • Q: How do I ensure the backup script runs properly?
    A: Make sure the script file has execute permissions (chmod 0744 /etc/cron.hourly/backup), and check cron logs to verify scheduled execution.
  • Q: Is it safe to store backup passwords in /etc/rsyncd.passwd?
    A: For security, ensure that the permissions of the password file restrict access only to the root or the backup user (chmod 600 /etc/rsyncd.passwd). Consider additional security measures like SSH keys or VPN tunnels.
  • Q: Can I backup multiple source servers using this script?
    A: Yes, you can modify the script or create multiple scripts with different IP addresses and destinations to back up multiple servers.
  • Q: What if I want to restore files from a specific incremental backup?
    A: You can browse the dated folders inside /opt/destination/increment/ to locate and restore previous versions of files manually.
Vote:
3 out of 5
Аverage rating : 3.8
Rated by: 5
1101 CT Amsterdam The Netherlands, Herikerbergweg 292
+31 20 262-58-98
700 300
ITGLOBAL.COM NL
700 300

You might also like...

We use cookies to make your experience on the Serverspace better. By continuing to browse our website, you agree to our
Use of Cookies and Privacy Policy.