Homelab, Linux & ABAP (~˘▾˘)~

[Terminal] Using rsync with –backup and –delete together

I’m using rsync to create backups from my NAS to an external HDD. The command looks like this:

rsync -azP --delete --exclude=/.zfs -b --backup-dir=Backup /mnt/nfs/photos/ /media/nocin/externalBackup/photos/ 
rsync parameterdescription
-a –archiveThis is equivalent to -rlptgoD. It is a quick way of saying you want recursion and
want to preserve almost everything.
-z –compressWith this option, rsync compresses the file data as it is sent to the destination machine, which reduces the amount of data being transmitted
-PThe -P option is equivalent to –partial –progress. Its purpose is to make it much easier to specify these two options for a long transfer that may be interrupted.
–deleteThis tells rsync to delete extraneous files from the receiving side (ones that aren’t on the sending side)
–excludeexclude files matching PATTERN
-b –backup-dirWith this option preexisting destination files are renamed with a ~ extension as each file is transferred. You can control where the backup file goes and what (if any) suffix gets appended using the –backup-dir and –suffix options.

But somehow it always created the Backup folder recursively again inside the Backup folder. So the first run created the /Backup folder, after the second run I’ve got /Backup/Backup, after the third run /Backup/Backup/Backup and so on..

The solution was to exclude the Backup directory using the --exclude command.

rsync -azP --delete --exclude=/.zfs --exclude=Backup -b --backup-dir=Backup /mnt/nfs/photos/ /media/nocin/externalBackup/photos/ 

I found a good explanation for this behaviour here: https://www.jveweb.net/en/archives/2011/02/using-rsync-and-cron-to-automate-incremental-backups.html

“If we are storing backups in the destination folder, or in a directory inside of the destination folder, the --delete parameter is going to delete old backups, as they are not in the source folder. Or attempt to as in the following situation:

Say, we already have a folder called backup inside of the destination directory, and we use rsync again, using --backup-dir=backup one more time. As rsync is going to attempt to delete every file and folder that is not in the source directory, it would backup the backup folder, which would create a backup folder inside our already existing backup folder, and then it would attempt to delete the backup folder and fail because it is using it to backup files.”

Leave a Reply

Your email address will not be published. Required fields are marked *