How do I synchronize in both directions?

Viewed 71761

I want to use rsync to synchronize two directories in both directions.

I refer to synchronization in classical sense (not how it is meant in rsync manuals):
I want to update the directories in both directions, depending on which of them is newer.

Can this be done by rsync (preferable in a Linux-way)?
If not, what other solutions exist?

8 Answers

You need to run rsync twice and I recommend to run it with -au:

rsync -au /local/source/* /remote/destination
rsync -au /remote/destination/* /local/source

-a (a for archive) is a shortcut for -rlptgoD:

  • -r Recurse into sub directories
  • -l Also sync symbolic links
  • -p Also sync file permissions
  • -t Also sync file modification times
  • -g Also sync file groups
  • -o Also sync file owner
  • -D Also sync special (not regular/meta) files

Basically whenever you want to create an identical one-to-one copy using rsync, you should always use -a as that's what most users expect to happen when they talk about "syncing". Other answers here seem to overlook that sometimes the content of a file stays unchanged but its owner may have changed or its access permissions may have changed and in that case rsync would not sync the file which could be fatal.

But you also require -u as that tells rsync to completely leave any file/folder alone, in case it exists already at the destination and has a newer last modification date. Without -u rsync would sync regardless if a file/folder is newer or not.

Please note that this solution cannot handle deleted files. Handling deletes is not easily possible as consider the following situation: A file has been deleted at the source, now how shall rsync know if that file once existed and has been deleted (in that case it must be deleted at the destination as well) or whether it never existed at the source (in that case it must be copied from the destination). These two situations look identical to rsync thus it cannot know how to react correctly. It won't help to sync the other way round as that can lead to the same situation: A file exists at the source but not at the destination. Why? Has it never existed at the destination or has it been deleted? Both cases look identical to rsync.

Sync tools that can reliably sync deleted files usually manage a sync log about all past sync operations. If that log reveals that there once was a file and has been synced but now it is missing, it's clear that it has been deleted. If there never was such a file according to the log, it must be synced. By storing all log entries with timestamps, it's even possible that a deleted file comes back and gets deleted multiple times yet the sync tool will always know what to do and the result is always correct. rsync has no such log, it only relies on the current file state of two sides of the operation.

You can however build yourself a sync command using rsync and a bit POSIX shell scripting which gets already very close to a sync tool as described above. As I needed such a tool myself, here is an answer on Stackoverflow that guides you through the creation of such a script.

What you need is Rclone. Rclone ("rsync for cloud storage") is a command line Linux program to sync files and directories to and from different cloud storage providers (box,dropbox,ftp etc) and local filesystems. Rlone supports mirror syncing only.

Another more graphical solution which includes real-time syncing would be to use FreeFileSync, which includes the program RealTimeSync. FreefileSync support 2-way bidirectional syncing which includes handling deletes.

FreFileSync gui

I was having the same question and end up using git. It might not fit your situation, but if anyone find this topic and have the same question, you may consider a version control system.

I'm using rsync with inotifywait. When you change any file, rsync will be executed.

inotifywait -m --exclude "$_LOG_FILE" -r -e create,delete,delete_self,modify,moved_to --format "%w%f" "$folder"

You need run inotifywait on both host. Please check example inotifywait

Related