如何在两个方向上同步?

发布于 2024-08-08 16:42:32 字数 224 浏览 3 评论 0原文

我想使用 rsync 在两个方向同步两个目录。

我指的是经典意义上的同步 (不是 rsync 手册中的含义):
我想双向更新目录, 取决于其中哪个较新

这可以通过 rsync 来完成(最好以 Linux 方式)吗?
如果没有,还有哪些其他解决方案?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

血之狂魔 2024-08-15 16:42:32

只需运行两次,使用“较新”模式(-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

这不会处理删除,但我不确定是否有一个很好的解决方案可以仅通过定期同步来解决该问题。

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):

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.

枉心 2024-08-15 16:42:32

您知道Unison 文件同步器吗?

Unison 是一个文件同步工具
适用于 Unix 和 Windows。它允许两个
文件集合的副本以及
目录存放在不同的目录
主机(或同一主机上的不同磁盘)
host),单独修改,然后
通过传播来更新
每个副本对另一个副本的更改。 ...

还要注意,它对失败具有弹性:

Unison 能够抵御失败。这是
小心留下复制品及其
拥有合理的私人结构
始终保持状态,即使在以下情况下
异常终止或通信故障。

Do you know Unison File Synchronizer?

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. ...

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.

梦途 2024-08-15 16:42:32

您需要运行 rsync 两次,我建议使用 -au 运行它:

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

-a(a 表示存档)是 的快捷方式>-rlptgoD:

  • -r 递归到子目录
  • -l 同时同步符号链接
  • -p 同时同步文件权限
  • -t 还同步文件修改时间
  • -g 还同步文件组
  • -o 还同步文件所有者
  • -D 还同步特殊 ( 的

基本上,每当您想使用 rsync 创建相同的一对一副本时,您应该始终使用 -a,因为这是大多数用户所期望 当他们谈论“同步”时就会发生。这里的其他答案似乎忽略了有时文件的内容保持不变,但其所有者可能已更改或其访问权限可能已更改,在这种情况下, rsync 将不会同步文件,这可能是致命的。

但您还需要 -u 因为它告诉 rsync 完全保留任何文件/文件夹,以防它已经存在于目的地并且具有较新的最后修改日期。如果没有 -u rsync 将会同步 无论文件/文件夹是否较新或不

请注意,此解决方案无法处理已删除的文件。处理删除并不容易,因为考虑以下情况:文件已在源处被删除,现在rsync如何知道该文件是否曾经存在并已被删除(在这种情况下必须删除它)也存在于目标中)或者它是否在源中从未存在过(在这种情况下,必须从目标复制它)。这两种情况看起来与 rsync 相同,因此它不知道如何正确反应。反过来同步也无济于事,因为这可能会导致相同的情况:文件存在于源但不存在于目标。为什么?它在目的地是否从未存在过或已被删除?这两种情况看起来与 rsync 相同。

可以可靠地同步已删除文件的同步工具通常管理有关所有过去同步操作的同步日志。如果该日志显示曾经有一个文件并且已同步,但现在丢失了,则很明显该文件已被删除。如果根据日志从来没有这样的文件,则必须对其进行同步。通过存储带有时间戳的所有日志条目,甚至可能已删除的文件会返回并被删除多次,但同步工具将始终知道要做什么,并且结果始终是正确的。 rsync没有这样的日志,它只依赖于操作两侧的当前文件状态。

不过,您可以使用 rsync 和一点 POSIX shell 脚本为自己构建一个同步命令,这已经非常接近如上所述的同步工具。由于我自己需要这样的工具,这里有一个 Stackoverflow 上的答案,它可以指导您创建这样的脚本。

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.

烙印 2024-08-15 16:42:32

谢谢 jsight

rsync -urv --progress dir_a dir_b && rsync -urv  --progress dir_b dir_a

这将导致第二次同步在第一次同步结束后立即发生。如果目录结构很大,这将节省时间,因为不需要坐在电脑前。如果结构很大,请删除冗长和进度的内容

rsync -ur dir_a dir_b && rsync -ur dir_b dir_a

Thanks 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
水水月牙 2024-08-15 16:42:32

您需要的是 Rclone。 Rclone(“云存储的 rsync”)是一个命令行 Linux 程序,用于在不同的云存储提供商(box、dropbox、ftp 等)和本地文件系统之间同步文件和目录。 Rlone 仅支持镜像同步。

另一个包含实时同步的图形化解决方案是使用 FreeFileSync,其中包含程序 RealTimeSync。 FreefileSync 支持 2 路双向同步,其中包括处理删除。

FreFileSync gui

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

○愚か者の日 2024-08-15 16:42:32

使用 rsync <选项> [hostname:]source-dir [hostname:]dest-dir

例如:

rsync -pogtEtvr --progress --bwlimit=2000 xxx-files different-stuff

将同步 xxx-files 到 different-stuff/xxx-files 。如果 different-stuff/xxx-files 不存在,它将创建它 - 即复制它。

-pogtEtv - 只是保留文件元数据的一堆选项,加上 v - 详细和 r - 递归

--progress - 显示实时同步进度 - 如果您喜欢的话,超级有用复制大文件

--bwlimit=2000 - 设置复制/同步的最大速度(bw = 带宽)

PS 当您在本地计算机上通过网络工作时,rsync 至关重要您可以使用cp等命令。

祝你好运!

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!

萧瑟寒风 2024-08-15 16:42:32

我有同样的问题并最终使用 git 。它可能不适合您的情况,但如果有人找到这个主题并有同样的问题,您可以考虑使用版本控制系统。

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.

辞别 2024-08-15 16:42:32

我将 rsyncinotifywait 结合使用。
当您更改任何文件时,rsync 将被执行。

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

您需要在两台主机上运行 inotifywait。请检查示例 inotifywait

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文