Backup and storage

How to copy a directory with rsync without deleting files by mistake

The short version

Always start with --dry-run. The slash at the end of the source changes what gets copied. Also, --delete can remove files from the destination.

Copy safely, without deleting files

rsync -aHAX --info=progress2 --dry-run \
  /source/directory/ /destination/directory/

If the list looks correct, run the command again without --dry-run:

rsync -aHAX --info=progress2 \
  /source/directory/ /destination/directory/

Why the slash matters

With /source/directory/, rsync copies the contents of the directory into the destination. Without the slash, it may create a directory folder inside the destination.

When to use --delete

Use --delete only when the destination must be an exact copy of the source:

rsync -aHAX --delete --dry-run /source/directory/ /destination/directory/

Check the list of deleted files before removing --dry-run. For backups, versioned copies or snapshots are usually safer than a mirror which deletes files immediately.

Sources