Linux RSync:我如何在两个方向同步?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1602324/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
RSync: How do I synchronize in both directions?
提问by java.is.for.desktop
I want to use rsync to synchronizetwo directories in both directions.
我想使用 rsync在两个方向上同步两个目录。
I refer to synchronization in classical sense
(nothow it is meant in rsync manuals):
I want to update the directories in both directions,
depending on which of them is newer.
我指的是经典意义上的同步(而不是rsync 手册中的意思):
我想在两个方向上更新目录,具体取决于它们中的哪个较新。
Can this be done by rsync (preferable in a Linux-way)?
If not, what other solutions exist?
这可以通过 rsync 完成吗(最好以 Linux 方式)?
如果没有,还有哪些其他解决方案?
采纳答案by jsight
Just run it twice, with "newer" mode (-u or --update flag) plus -t (to copy file modified time), -r (for recursive folders), and -v (for verbose output to see what it is doing):
只需运行两次,使用“较新”模式(-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
This won't handle deletes, but I'm not sure there is a good solution to that problem with only periodic sync'ing.
这不会处理删除,但我不确定只有定期同步才能解决这个问题。
回答by MaD70
Do you know Unison File Synchronizer?
你知道Unison 文件同步器吗?
Unison is a file-synchronization tool for Unix and Windows. It allows two replicas of a collection of files and directories to be stored on different hosts (or different disks on the same host), modified separately, and then brought up to date by propagating the changes in each replica to the other. ...
Unison 是一个用于 Unix 和 Windows 的文件同步工具。它允许文件和目录集合的两个副本存储在不同主机(或同一主机上的不同磁盘)上,分别修改,然后通过将每个副本中的更改传播到另一个副本来更新。...
Note also that it is resilient to failure:
另请注意,它对失败具有弹性:
Unison is resilient to failure. It is careful to leave the replicas and its own private structures in a sensible state at all times, even in case of abnormal termination or communication failures.
Unison 对失败具有弹性。即使在异常终止或通信失败的情况下,也要小心地让副本及其自己的私有结构始终处于合理状态。
回答by Mayank Agarwal
Thanks jsight
感谢 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
回答by Bohdan
Use rsync <OPTIONS> [hostname:]source-dir [hostname:]dest-dir
用 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.
将 xxx-files 同步到 different-stuff/xxx-files 。如果 different-stuff/xxx-files 不存在,它将创建它 - 即复制它。
-pogtEtv
- just bunch of options to preserve file metadata, plus v - verbose and r - recursive
-pogtEtv
- 只是一堆保留文件元数据的选项,加上 v - 详细和 r - 递归
--progress
- show progress of syncing in real time - super useful if you copy big files
--progress
- 实时显示同步进度 - 如果您复制大文件,非常有用
--bwlimit=2000
- sets maximum speed of copying/syncing (bw = bandwidth)
--bwlimit=2000
- 设置复制/同步的最大速度(bw = 带宽)
P.S.rsync is critically important when you work over network in case of local machine you can use commands like cp.
PSrsync 在您通过网络工作时非常重要,以防在本地机器上您可以使用cp 之类的命令。
Good Luck!
祝你好运!
回答by iadd
I'm using rsync
with inotifywait
.
When you change any file, rsync
will be executed.
我正在rsync
与inotifywait
. 当您更改任何文件时,rsync
将被执行。
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
您需要inotifywait
在两个主机上运行。请检查示例inotifywait
回答by user1461607
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.
你需要的是Rclone。Rclone(“用于云存储的 rsync”)是一个命令行 Linux 程序,用于在不同的云存储提供商(box、dropbox、ftp 等)和本地文件系统之间同步文件和目录。Rlone 仅支持镜像同步。
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.
另一个包含实时同步的更图形化的解决方案是使用FreeFileSync,其中包含程序 RealTimeSync。FreefileSync 支持 2 路双向同步,包括处理删除。
回答by Mecki
You need to run rsync
twice and I recommend to run it with -au
:
您需要运行rsync
两次,我建议使用以下命令运行它-au
:
rsync -au /local/source/* /remote/destination
rsync -au /remote/destination/* /local/source
-a
(a for archive) is a shortcut for -rlptgoD
:
-a
(a for archive) 是一个快捷方式-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
-r
递归到子目录-l
还同步符号链接-p
还同步文件权限-t
还同步文件修改时间-g
还同步文件组-o
还同步文件所有者-D
还同步特殊(非常规/元)文件
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.
基本上,每当您想使用 来创建相同的一对一副本时rsync
,您都应该始终使用,-a
因为这是大多数用户在谈论“同步”时所期望的。这里的其他答案似乎忽略了有时文件的内容保持不变,但其所有者可能已更改或其访问权限可能已更改,在这种情况下rsync
不会同步文件,这可能是致命的。
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.
但是您还需要-u
因为它告诉rsync
完全保留任何文件/文件夹,以防它已经存在于目的地并且具有更新的最后修改日期。如果没有-u
rsync
将同步,如果一个文件/文件夹是新的或不不管。
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
.
请注意,此解决方案无法处理已删除的文件。处理删除并不容易,因为考虑到以下情况:一个文件在源头被删除,现在如何rsync
知道该文件是否曾经存在并已被删除(在这种情况下,它也必须在目的地被删除)或者是否它从不存在于源(在这种情况下,它必须从目标复制)。这两种情况看起来相同,rsync
因此它不知道如何正确反应。反过来同步也无济于事,因为这可能会导致相同的情况:文件存在于源中,但不存在于目标中。为什么?它从未存在于目的地或已被删除?两种情况看起来都相同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.
可以可靠地同步已删除文件的同步工具通常管理有关所有过去同步操作的同步日志。如果该日志显示曾经有一个文件并已同步但现在丢失了,则很明显它已被删除。如果根据日志从来没有这样的文件,它必须被同步。通过使用时间戳存储所有日志条目,甚至有可能删除的文件回来并被多次删除,但同步工具将始终知道要做什么并且结果始终是正确的。rsync
没有这样的日志,它只依赖于操作两侧的当前文件状态。