bash 如何保持两个文件夹自动同步?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12460279/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 22:42:31  来源:igfitidea点击:

How to keep two folders automatically synchronized?

bashsynchronization

提问by Luca Borrione

I would like to have a synchronized copy of one folder with all its subtree.

我想要一个文件夹及其所有子树的同步副本。

It should work automatically in this way: whenever I create, modify, or delete stuff from the original folder those changes should be automatically applied to the sync-folder.

它应该以这种方式自动工作:每当我从原始文件夹中创建、修改或删除内容时,这些更改应该自动应用于同步文件夹。

Which is the best approach to this task?

哪种方法是完成这项任务的最佳方法?

BTW: I'm on Ubuntu 12.04

顺便说一句:我在 Ubuntu 12.04

Final goal is to have a separated real-time backup copy, without the use of symlinks or mount. I used Ubuntu One to synchronize data between my computers, and after a while something went wrong and all my data was lost during a synchronization.

最终目标是拥有一个独立的实时备份副本,而不使用符号链接或挂载。我使用 Ubuntu One 在我的计算机之间同步数据,一段时间后出现问题,同步过程中我的所有数据都丢失了。

So I thought to add a step further to keep a backup copy of my data:

所以我想进一步添加一个步骤来保留我的数据的备份副本:

  • I keep my data stored on a "folder A"
  • I need the answer of my current question to create a one-way sync of "folder A" to "folder B" (crona script with rsync? could be?). I need it to be one-way only from A to B any changes to B must not be applied to A.
  • The I simply keep synchronized "folder B" with Ubuntu One

    In this manner any change in A will be appled to B, which will be detected from U1 and synchronized to the cloud. If anything goes wrong and U1 delete my data on B, I always have them on A.
  • 我将我的数据存储在“文件夹 A”中
  • 我需要我当前问题的答案来创建“文件夹 A”到“文件夹 B”的单向同步(cron带有rsync?的脚本可能是?)。我需要它只是从 A 到 B 的单向,对 B 的任何更改都不能应用于 A。
  • 我只是与 Ubuntu One 保持同步“文件夹 B”

    。这样,A 中的任何更改都将应用到 B,这将从 U1 检测到并同步到云。如果出现任何问题并且 U1 删除我在 B 上的数据,我总是将它们放在 A 上。

Inspired by lanzz's comments, another idea could be to run rsync at startup to backup the content of a folder under Ubuntu One, and start Ubuntu One only after rsync is completed.

受lanzz评论的启发,另一个想法可能是在启动时运行rsync来备份Ubuntu One下文件夹的内容,并在rsync完成后才启动Ubuntu One。

What do you think about that? How to know when rsync ends?

你怎么看?如何知道 rsync 何时结束?

回答by silgon

You can use inotifywait(with the modify,create,delete,moveflags enabled) and rsync.

您可以使用inotifywaitmodify,create,delete,move启用标志)和rsync.

while inotifywait -r -e modify,create,delete,move /directory; do
    rsync -avz /directory /target
done

回答by Martin

You need something like this: https://github.com/axkibe/lsyncdIt is a tool which combines rsync and inotify - the former is a tool that mirrors, with the correct options set, a directory to the last bit. The latter tells the kernel to notify a program of changes to a directory ot file. It says:

你需要这样的东西:https: //github.com/axkibe/lsyncd这是一个结合了 rsync 和 inotify 的工具——前者是一个镜像工具,设置了正确的选项,一个目录到最后一位。后者告诉内核通知程序目录 ot 文件的更改。它说:

It aggregates and combines events for a few seconds and then spawns one (or more) process(es) to synchronize the changes.

它聚合和组合事件几秒钟,然后生成一个(或多个)进程以同步更改。

But - according to Digital Ocean at https://www.digitalocean.com/community/tutorials/how-to-mirror-local-and-remote-directories-on-a-vps-with-lsyncd- it ought to be in the Ubuntu repository!

但是 - 根据https://www.digitalocean.com/community/tutorials/how-to-mirror-local-and-remote-directories-on-a-vps-with-lsyncd上的 Digital Ocean - 它应该在Ubuntu 存储库!

I have similar requirements, and this tool, which I have yet to try, seems suitable for the task.

我有类似的要求,这个我还没有尝试过的工具似乎适合这项任务。

回答by Denis Kuzin

Just simple modification of @silgon answer:

只是对@silgon 答案的简单修改:

while true; do 
  inotifywait -r -e modify,create,delete /directory
  rsync -avz /directory /target
done

(@silgon version sometimes crashes on Ubuntu 16 if you run it in cron)

(@silgon 版本有时会在 Ubuntu 16 上崩溃,如果你在 cron 中运行它)

回答by Denis Kuzin

You can take advantage of fschange. It's a Linux filesystem change notification. The source code is downloadable from the above link, you can compile it yourself. fschangecan be used to keep track of file changes by reading data from a proc file (/proc/fschange). When data is written to a file, fschange reports the exact interval that has been modified instead of just saying that the file has been changed. If you are looking for the more advanced solution, I would suggest checking Resilio Connect. It is cross-platform, provides extended options for use and monitoring. Since it's BitTorrent-based, it is faster than any other existing sync tool. It was written on their behalf.

您可以利用fschange。这是一个 Linux 文件系统更改通知。源代码可以从上面的链接下载,你可以自己编译。fschange可用于通过从 proc 文件 (/proc/fschange) 读取数据来跟踪文件更改。将数据写入文件时,fschange 会报告已修改的确切时间间隔,而不仅仅是说明文件已更改。如果您正在寻找更高级的解决方案,我建议您查看Resilio Connect。它是跨平台的,为使用和监控提供了扩展选项。因为它是基于 BitTorrent 的,所以它比任何其他现有的同步工具都要快。这是代表他们写的。

回答by Petas

I use this free program to synchronize local files and directories: https://github.com/Fitus/Zaloha.sh. The repository contains a simple demo as well.

我使用这个免费程序来同步本地文件和目录:https: //github.com/Fitus/Zaloha.sh。该存储库还包含一个简单的演示。

The good point: It is a bash shell script (one file only). Not a black box like other programs. Documentation is there as well. Also, with some technical talents, you can "bend" and "integrate" it to create the final solution you like.

优点:它是一个 bash shell 脚本(只有一个文件)。不像其他程序那样是黑匣子。文档也在那里。另外,借助一些技术人才,您可以“弯曲”和“集成”它以创建您喜欢的最终解决方案。