我想使用 rsync 在两个方向上同步两个目录。
我指的是经典意义上的同步(不是rsync 手册中的意思):
我想在两个方向上更新目录,具体取决于其中哪个更新。
这可以通过 rsync 来完成吗(最好以 Linux 方式)?
如果没有,还有哪些其他解决方案?
我想使用 rsync 在两个方向上同步两个目录。
我指的是经典意义上的同步(不是rsync 手册中的意思):
我想在两个方向上更新目录,具体取决于其中哪个更新。
这可以通过 rsync 来完成吗(最好以 Linux 方式)?
如果没有,还有哪些其他解决方案?
只需运行两次,使用“更新”模式(-u 或 --update 标志)加上 -t(复制文件修改时间)、-r(用于递归文件夹)和 -v(用于详细输出以查看它是什么)正在做):
rsync -rtuv /path/to/dir_a/* /path/to/dir_b
rsync -rtuv /path/to/dir_b/* /path/to/dir_a
这不会处理删除,但我不确定只有定期同步才能解决这个问题。
你知道Unison 文件同步器吗?
Unison 是适用于 Unix 和 Windows 的文件同步工具。它允许将文件和目录集合的两个副本存储在不同的主机(或同一主机上的不同磁盘)上,分别修改,然后通过将每个副本中的更改传播到另一个来更新。...
另请注意,它对失败具有弹性:
Unison 能够适应失败。即使在异常终止或通信失败的情况下,也要小心地将副本及其自己的私有结构始终保持在合理状态。
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) filesBasically 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.
感谢 jsight
rsync -urv --progress dir_a dir_b && rsync -urv --progress dir_b dir_a
This would result in the second sync happening immediately after 1st sync is over. In case the directory structure is huge, this will save time, as one does not need to sit before the pc. If the structure is huge, remove the verbose and progress stuff
rsync -ur dir_a dir_b && rsync -ur dir_b dir_a
Use rsync <OPTIONS> [hostname:]source-dir [hostname:]dest-dir
for example:
rsync -pogtEtvr --progress --bwlimit=2000 xxx-files different-stuff
Will sync xxx-files to different-stuff/xxx-files .If different-stuff/xxx-files did not exist, it will create it - i.e. copy it.
-pogtEtv
- just bunch of options to preserve file metadata, plus v - verbose and r - recursive
--progress
- show progress of syncing in real time - super useful if you copy big files
--bwlimit=2000
- sets maximum speed of copying/syncing (bw = bandwidth)
P.S. rsync is critically important when you work over network in case of local machine you can use commands like cp.
Good Luck!
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.
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