Rsync, The Best Linux Backup Tool


What are the properties that we can expect from a backup system?

  • Local and remote copies.
  • Incremental backups.
  • Complete backups.
  • Low bandwidth in case of remote copies.
  • Backups easy to restore.
  • Backups for Windows (Deltacopy) and Linux systems.

"Rsync" tool is the Swiss army knife that will allow us to do all of these tasks effortlessly, simply by digging into its syntax a little bit.

To save you a bit of work, in this post, I present a simple script that will allow you to make daily backups on a USB flash drive or external disk, being able to configure how many days we want to retain the information.

How does it work?

In this script, we are going to use a combination of the following rsync options:

  • -a: File mode. Mix of options that make files copying and their properties easier (-rlptgoD (not -H, -A, -X))
  • -v: Enable verbosity to see the entire process on the screen.
  • --delete: Force deletion of files that no longer exist.
  • --link-des: It will create a physical link instead of copying the whole file, in case the file is not modified since last backup. This is the option that creates the magic of rsync.

The useful variables in this script are:

  • DAYS: Number of days to recycle the backups.
  • SOURCE_DIR: Directory that contains the files we want to copy.
  • BACKUP_DIR: Mount point path of the external drive and directory where we want the copy.

This script will create as many directories as days we have configured daily.0 -> daily.N. In each of the directories we will find all the same day's copy, with the advantage that the files that have not been modified between the directories of the different days, there will only be one real file, and all the others will be physical links to the original that do not take up disk space.

As soon as a file is modified, it will be copied again instead of creating a physical link, so we will never have 2 identical files taking up space in our backup disk.


readonly DAYS=30
readonly SOURCE_DIR="/folder/to/backup"
readonly BACKUP_DIR="/path/to/my/external/memory"

rm -rf "${BACKUP_DIR}/daily.${DAYS}"

for (( c=DAYS-1; c>=0; c--))
    plusone=$(expr $c + 1)
    mv "${BACKUP_DIR}/daily.${c}" "${BACKUP_DIR}/daily.${plusone}"

rsync -av --delete "$SOURCE_DIR" --link-dest "${BACKUP_DIR}/daily.1" "${BACKUP_DIR}/daily.0"

Cron Task

In order for the script to run daily, we will create a cron task.

I usually launch the backup task daily at 10:05 p.m., after finishing my daily work for sure.

Open the Cron task editor from the Linux terminal:

crontab -e 

Assuming that our script is called, we will write the following line in the task list:

5 22 * * * /path/to/script/

If everything went well, we would already have our backups up and running. I hope this is useful to you.