File Server Synchronization with rsync

You can use rsync with the --delete option to synchronize two drives and remove any extra files from the target drive that aren’t on the source drive! Let’s break it down.

Basic command:

rsync -av --delete /path/to/source/ /path/to/target/

✨ Explanation:

  • -a (archive): Preserves symbolic links, file permissions, modification times, and more.
  • -v (verbose): Shows what’s happening during the sync.
  • --delete: Removes files from the target that no longer exist in the source.
    • Important: Use with care—this option deletes extra files on the target!

Dry run (recommended before running for real):

rsync -av --delete --dry-run /path/to/source/ /path/to/target/

This shows what would happen without making any changes.

Excluding files:
If you want to avoid deleting certain files on the target:

rsync -av --delete --exclude='folder_or_file_to_keep' /path/to/source/ /path/to/target/

Logging the sync:

rsync -av --delete /path/to/source/ /path/to/target/ --log-file=/path/to/rsync.log

Automating with a cron job:
Edit your crontab with crontab -e and add:

0 2 * * * rsync -a --delete /path/to/source/ /path/to/target/

This runs the sync daily at 2 AM.

Would you like to fine-tune it, like setting up email notifications if the sync fails? I can help with that too! 🚀

Leave a comment