Linux服务器间文件实时同步的实现

2019-01-16 20:52:58丽君

inotify-tools的详细介绍请点击: https://github.com/rvoicilas/inotify-tools/wiki

inotify-tools的安装

对于centos7系统,依次执行:

yum install -y epel-release yum --enablerepo=epel install inotify-tools

使用inotifywait命令进行事件监听

监听脚本如下(inotifywait-rsync.sh):

inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e modify,delete,create,attrib /home/paul/rsync/ | while read file do rsync -avPz --progress /home/paul/rsync/ 192.168.100.130:/home/paul/rsync/ rsync -avPz --delete /home/paul/rsync/ 192.168.100.130:/home/paul/rsync/ echo "${file} was synchronized" done

参数解析

-m 保持持续监听状态,如果不写该参数,inotifywait会在监听到一次事件之后退出。 -r 递归方式监听目录。 -q 安静模式,打印输出较少的内容。 --timefmt 指定时间的输出格式。 --format 指定事件输出的格式。 -e 设置监听的事件类型。这里监听增删改和metadata的变更。

对于每次触发的监听时间,inotifywait会执行do和done之间的代码。在这里,我们调用之前所说的rsync命令进行文件同步。

监听脚本加入crontab

crontab -e * * * * * sh /home/paul/inotifywait-rsync.sh

参考资料
https://rsync.samba.org
https://github.com/rvoicilas/inotify-tools/wiki

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易采站长站。

您可能感兴趣的文章:

linux系统中rsync+inotify实现服务器之间文件实时同步Linux 服务器同步 Rsync同步服务器文件