scripts:rsync:one_way_sync_delete_files_on_target

Use Rsync to do a full one-way sync

Rsync by default will act like a copy, and add new files from the source to the target, but not delete.

Sometimes, you want to make a full one-way sync, meaning, make the target look exactly like the source, including deleting files in the target if they no longer exist in the source.

This is really simple as follows:

rsync -avhP --delete /path/to/source/ tony@desthostname:/path/to/target/

In this example:

  • -a is to use archive mode, preserving file attributes (as well as setting a bunch of other flags),
  • -v tells rsync to be verbose and list files (your choice if you want this!)
  • -h uses human-readable values (optional, this uses normal units instead of bytes)
  • -P enables progress printing (again, optional)
  • –delete is what tells it to delete target files if they don't exist in the source
    • You can also use –delete-before or –delete-after and several others. This just changes the order of whether items should be copied first, or deleted first. (eg: if low disk space for a backup, you can delete the old files before copying the new ones, or, if you want to be safe and wait for the new copy to be finished, delete only after the copying is done)

As always, rsync will keep track and save progress, so you can interrupt the copy and resume it anytime.

Remember that rsync cares about whether you type /source/ or /source.
/source will create a directory called “source” in your target folder.
/source/ will copy the contents of the source directory into the target directory.

You should dry-run your changes with rsync -n to ensure you don't accidentally delete files!

  • scripts/rsync/one_way_sync_delete_files_on_target.txt
  • Last modified: 2023-01-15 22:40
  • by Tony