Backing up with Rsync

rsync can be used to syncronise files and directories between two locations on the same device or across different devices.

It’s a great way to backup the content of your computer with a Linux server.

Synchronising Files

Typical command format to retrieve files

rsync -avz neil@192.168.0.55:/home/neil/home/backup somewhere

Where the parameters are:

  • a – archive, synchronises recursively and maintains symbolic links
  • v – verbose, output as each file is actioned. Handy if you wish to monitor the actions.
  • z – compress files during the transfer

Add a slash on the end of the source location to copy the files and directories within to the nominated destination. As shown without the folder itself will be copied into the destination directory.

Exclude Files and Directories

As the command stands it also transfers files which have been deleted and are currently residing in the directory called .Trash.

To exclude a directory add the following

–exclude=‘directory or file name’

Matches to the above naming will be excluded from the copying. Repeat for each exclusion.

rsync -avz --exclude='/.Trash-1000' neil@192.168.0.55:/home/neil/abc /home/vntweb/backup/mar2024

Deletion

As given the older files in the destination aren’t deleted.

to check this I created a directory called fruit and rsnc’d it to a directory called oldfruit. Just a couple of text files inside. I then deleted one of the files, edited the remaining one and rsync’d the directory once more. The deleted file was still in the destination folder.

To resolve this add the parameter –delete, giving:

rsync -avz --delete --exclude='/.Trash-1000' neil@192.168.0.55:/home/neil/abc /home/vntweb/backup/mar2024

Deleted file now disappears in the destination folder.

Automating Rsync

rsync can be added to crontab, or included within a file which is referenced by crontab.

09 23 * * * root /usr/bin/rsync -avz --exclude='/.Trash-1000' /home/neil/abc /home/vntweb/backup/mar2024

The above is fine where the source and destination are on the same computer.

The alternative is to add the command in a file, which is then referenced under crontab.

#! /bin/bash

echo "completed 1 starting VNTweb backup"
/usr/bin/rsync -az -P --delete --exclude=.Trash-1000 --exclude=lost+found /home/neil/project/abc neil@192.168.0.55:/home/backup/2024  --rsh="/usr/bin/sshpass -p pWordValue ssh -o StrictHostKeyChecking=no -l neil"

Where pWordValue is the password in plain text to be used.

In order to allow automation to work the password is required. In this example the password is passed in the command. Dependant upon the circumstances this simple approach may not be appropriate.

More on parameters

  • r – recursive
  • a – archive
  • t – maintain modification times
  • l – maintain symlinks
  • z – compression
  • P – implies partial. Ensuring larger files are copied and then moved into place, helping avoid broken files where the transfer is incomplete.

Other options:

  • –rsh – run the following command, in this case handle the password