DEV Community

Kayabyte
Kayabyte

Posted on

Why one-way folder sync wipes backups, and four guards that stop it

Every few months someone posts the same story: a nightly mirror script ran, and the next morning the backup drive was empty, or the backup was fine but the internal disk was full of a second copy. The script was usually one line of rsync -a --delete, and it had worked for a year.

One-way sync is simple in principle. You make the destination match the source. The trouble is that "match the source" is exactly the wrong thing to do when the source is not what you think it is. Below are the failure modes I have hit or reproduced on a Mac, and the guards I now put in front of any mirror job. The commands were run on macOS 27.2 against scratch folders.

First, which rsync do you have?

Recent macOS versions ship openrsync as /usr/bin/rsync, not the GNU rsync many guides assume:

$ rsync --version
openrsync: protocol version 29
rsync version 2.6.9 compatible
Enter fullscreen mode Exit fullscreen mode

Most flags are the same, but not all behaviour is. I point out one difference below. If you installed GNU rsync with Homebrew, check which -a rsync so you know which one your script calls.

Failure 1: the source is an empty folder

This is the classic. Your source lives on an external disk, say /Volumes/Photos/. The disk is unplugged, but a folder with that name still exists, maybe because an earlier script ran mkdir -p while the disk was away. Now the source is an empty folder, and --delete dutifully makes the destination empty too.

You can see it coming with a dry run:

$ mkdir src dst; echo a > dst/a; echo b > dst/b
$ rsync -a --delete --dry-run -i src/ dst/
*deleting b
*deleting a
Enter fullscreen mode Exit fullscreen mode

A related trap: if /Volumes/Backup already exists as an ordinary folder when the real disk is connected, macOS mounts the disk at /Volumes/Backup 1 instead. Your script keeps writing to /Volumes/Backup, which is now a folder on the internal disk. Nothing is deleted, but your backup silently stops being a backup and your startup disk fills up.

Failure 2: the destination is a different disk

You swapped backup drives, or two drives have the same name. The path resolves, the files are there, and --delete starts trimming a disk that was never meant to be a mirror of this source. The path alone tells you nothing about which disk is behind it.

Failure 3: a case-only rename on a case-insensitive disk

This one surprised me. APFS on a Mac is case-insensitive by default, and so is exFAT. Rename Photo.JPG to photo.jpg in the source and run a mirror with --delete-after:

$ mkdir src dst
$ echo hello > src/Photo.JPG
$ rsync -a src/ dst/
$ mv src/Photo.JPG src/tmp && mv src/tmp src/photo.jpg
$ rsync -a --delete-after src/ dst/
$ ls dst
$
Enter fullscreen mode Exit fullscreen mode

The destination is empty. Here is why: rsync asks for photo.jpg on the destination, the case-insensitive file system hands back Photo.JPG, size and date match, so nothing is copied. Then the delete pass compares names exactly, decides Photo.JPG is not in the source, and deletes it. That was the only copy.

With plain --delete (deletes during the transfer) the same test left photo.jpg in place on my Mac, because the old name was deleted before the new one was checked. So the result depends on a flag many people add for safety. The general lesson: a tool that compares names exactly while the file system compares them loosely will eventually delete the wrong thing.

The reverse case also exists. A NAS or Linux share can hold Report.pdf and report.pdf side by side. Mirror that to an APFS disk and one of them overwrites the other. You can find such pairs before you sync:

find "$SRC" | tr '[:upper:]' '[:lower:]' | sort | uniq -d
Enter fullscreen mode Exit fullscreen mode

Any output means two paths collapse into one on the destination.

Guard 1: check that the right disk is mounted

Don't check that the path exists. Check that a volume is mounted there and that it is the volume you expect. Get the UUID once:

diskutil info -plist /Volumes/Backup | plutil -extract VolumeUUID raw -o - -
Enter fullscreen mode Exit fullscreen mode

Then refuse to run on anything else:

#!/bin/bash
set -euo pipefail
SRC="$HOME/Pictures/Photos-2026/"
VOL="/Volumes/Backup"
DST="$VOL/Photos-2026/"
EXPECTED_UUID="PUT-THE-UUID-HERE"

if ! mount | grep -q " on $VOL ("; then
  echo "Backup disk not mounted at $VOL, stopping." >&2; exit 1
fi
uuid=$(diskutil info -plist "$VOL" | plutil -extract VolumeUUID raw -o - -)
if [ "$uuid" != "$EXPECTED_UUID" ]; then
  echo "A different disk is mounted at $VOL ($uuid), stopping." >&2; exit 1
fi
Enter fullscreen mode Exit fullscreen mode

If the source is on an external disk too, do the same check for it. That covers failure 1 and failure 2. A simpler alternative is a marker file such as .backup-target in the root of the right disk, checked with [ -f "$VOL/.backup-target" ]. It is less strict than a UUID but survives reformatting.

Guard 2: compare counts before you delete anything

Even on the right disk, a mirror that would remove most of the destination is almost always a mistake. Count first, using rsync's own dry run so the numbers match what it would do:

MAX_PCT=10
src_count=$(find "$SRC" -type f | wc -l | tr -d ' ')
dst_count=$(find "$DST" -type f | wc -l | tr -d ' ')
if [ "$src_count" -eq 0 ]; then
  echo "Source is empty, refusing to mirror it." >&2; exit 1
fi
deletes=$(rsync -a --delete --dry-run -i "$SRC" "$DST" | grep -c '^\*deleting' || true)
if [ "$dst_count" -gt 0 ] && [ $((deletes * 100 / dst_count)) -gt "$MAX_PCT" ]; then
  echo "Run would delete $deletes of $dst_count files, stopping." >&2; exit 1
fi
Enter fullscreen mode Exit fullscreen mode

Against a source with 3 files and a destination with 20, this prints Run would delete 17 of 20 files, stopping. and exits. With an empty source it stops at the first check.

rsync also has --max-delete=N as a hard ceiling. In my test, openrsync deleted up to the limit, printed Deletions stopped due to --max-delete limit, and exited with status 25. That is a useful backstop, but note that it still performs the first N deletions. The dry-run check above stops before anything happens.

Guard 3: archive instead of delete

The safest delete is a move. With GNU rsync, --backup --backup-dir moves files that would be replaced or deleted into a separate folder:

rsync -a --delete --backup --backup-dir="/Volumes/Backup/_archive/$(date +%F_%H%M%S)" "$SRC" "$DST"
Enter fullscreen mode Exit fullscreen mode

Use an absolute path outside the destination tree, or the archive itself becomes subject to --delete.

Here is the openrsync difference I mentioned. Its man page describes --backup-dir only for files being replaced. In my test on macOS 27.2, a changed file was archived as expected, but a file that existed only in the destination was neither deleted nor archived; it stayed in place. That is safe, but it is not a mirror. If you rely on archive-on-delete, test it with the binary your script actually runs, or do the archive step yourself: list the *deleting lines from a dry run and mv those files into a dated folder before the real run.

Then prune the archive on your own schedule, for example anything older than 90 days, once you trust the job.

Guard 4: copy to a temporary name, then rename

A copy that dies half-way should never leave a half file where the good one used to be. rsync already does this by default: it writes into a temporary file and renames it into place when the transfer finishes. The openrsync man page lists --inplace as the option that turns this off, with the trade-offs spelled out. So the guard here is mostly "don't add --inplace or --append to a backup job".

If you write your own copy step (with cp, ditto or a script), do the same thing by hand:

tmp="$dst_dir/.$name.partial.$$"
cp -p "$src_file" "$tmp" && mv -f "$tmp" "$dst_dir/$name"
Enter fullscreen mode Exit fullscreen mode

mv within the same volume is a rename, so the old file is replaced in one step. If the disk is pulled during cp, you are left with a stray .partial file and the previous version intact.

Putting it together

My mirror jobs now follow this order:

  1. Check that both sides are mounted and are the expected volumes (UUID or marker file).
  2. Refuse an empty source.
  3. Look for case collisions if either side might be case-sensitive.
  4. Dry run, count deletions, stop above a threshold. Scheduled jobs stop and notify; they never "just go ahead".
  5. Run with replaced and removed files moved to a dated archive.
  6. Leave rsync's temp-file-then-rename behaviour alone.

None of this is clever. It is just refusing to trust a path, and treating "delete" as the one operation that needs a second opinion.


Disclosure: I make Pairfold, a Mac app for folder mirror and two-way sync that is built around these guards: a preview of every change, a dated archive instead of deletes, a stop when a disk is missing or a different disk is at the same place, and temp-name copies. It is free to use for one folder pair, with a one-time licence for schedules and more pairs: Pairfold.

Written with help from an AI assistant; code and commands were tested on macOS.

Top comments (0)