How to keep two folders automatically synchronized?

前端 未结 5 1090
醉话见心
醉话见心 2021-01-29 19:26

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 th

相关标签:
5条回答
  • 2021-01-29 20:04

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

    0 讨论(0)
  • 2021-01-29 20:05

    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.

    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.

    0 讨论(0)
  • 2021-01-29 20:06

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

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

    If you don't have inotifywait on your system, run sudo apt-get install inotify-tools

    0 讨论(0)
  • 2021-01-29 20:07

    Just simple modification of @silgon answer:

    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)

    0 讨论(0)
  • 2021-01-29 20:12

    You need something like this: https://github.com/axkibe/lsyncd It 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:

    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!

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

    0 讨论(0)
提交回复
热议问题