brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: rsync --max-delete stops a mirror that suddenly wants to delete too much

Backup
~/tips/tip-backup-rsync-max-delete

rsync -av --delete --max-delete=50 /mnt/user/photos/ [email protected]:/mnt/user/backup/photos/

Caps how many files rsync may delete in one run, and exits with an error when the limit is hit.

An rsync mirror with --delete is only as safe as its source. If the source folder is empty because a disk did not mount, the next run happily empties the backup as well. --max-delete puts a ceiling on that.

rsync -av --delete --max-delete=50 /mnt/user/photos/ [email protected]:/mnt/user/backup/photos/

The man page says it tells rsync not to delete more than NUM files or directories. When the limit is exceeded, all further deletions are skipped through the end of the transfer, rsync prints a warning with a count, and it exits with code 25 (unless a more serious error came first). So a mirror that was about to remove thousands of files removes fifty at most, and your script gets a non-zero exit status it can mail you about.

A special value is useful during setup. --max-delete=0 deletes nothing at all and only warns about extraneous files in the destination, which makes it a quick way to see how far two folders have drifted apart without touching either:

rsync -av --delete --max-delete=0 /mnt/user/photos/ [email protected]:/mnt/user/backup/photos/

The man page adds a warning about old versions: a client rsync older than 3.0.0 (March 2008) treats --max-delete=0 as unlimited, and for such clients you should use --max-delete=-1. That only matters on very old systems, but a NAS with ancient firmware is exactly where it might show up, so check rsync --version on both ends.

There are limits to what this protects against. The number is a count of items, not a size, and it counts every file and directory, so pick it a bit above your normal daily churn. The rest of the transfer continues as usual, so new and changed files still go over while deletions are held back. I would still run a new job with -n (dry run) first, and think of --max-delete as the seatbelt rather than the plan.

Further reading: rsync man page

More Backup tips restic snapshots --group-by host,paths mysqldump --routines --events mydb > mydb.sql rsync -a --files-from=list.txt /home/me/ backup:/backup/ restic find "*.txt" restic stats All Backup tips (45) All topics

Link