Backup & Recovery (Rsync/Tar/Dd)
Stop letting rsync delete your files on a bad destination path
đź§© The Challenge
You’ve set up a backup script using rsync –delete and a variable for the destination directory, but then the variable accidentally evaluates to empty. Next thing you know, rsync decides the best way to sync an empty path is to wipe your entire root partition.
đź’ˇ The Fix
Use the –dry-run flag combined with –delete-excluded, or better yet, force a safety check on your variables before executing the sync. I’ve personally lost a morning to this disaster, and it’s a mistake you only make once before becoming paranoid.
[ -d "$DEST" ] && [ "$(ls -A $DEST)" ] && rsync -av --delete --dry-run /source/ "$DEST/" || echo "Backup destination is suspicious, aborting."
⚙️ Why It Works
Adding the check for the destination directory existence and ensuring it isn’t empty acts as a guardrail against empty variable expansion. It’s simple, but it saves your bacon when a script goes rogue.
🚀 Pro-Tip: Always include the –dry-run flag during testing; it’s the only thing standing between you and a resume-generating event.
Linux Tips & Tricks | © ngelinux.com | 9/26/2026
