inotify+rsync架构实现实时同步

前序

使用inotify+rsync架构实现(文件夹/文件)实时同步, 双机之间需要ssh免密码配置

环境

客户端:192.168.137.176 (rsync + inotify-tools-3.14 + 实时shell脚本)

服务端:192.168.137.177 (rsync + xinetd)

操作

客户端

工具:inotify-tools-3.14.tar.gz

工具:rsync + xinetd

tar -zxvf inotify-tools-3.14.tar.gz
cd inotify-tools-3.14
./configure && make && make install

实时shell(csync.sh)

#*************************************************************************
#         > File Name: /tmp/1.sh
#         > Author: chenglee
#         > Main : [email protected]
#         > Blog : http://www.cnblogs.com/chenglee/
#         > Created Time : 2019年02月19日 星期二 19时30分51秒
#*************************************************************************
#!/bin/bash
serverip="192.168.137.177"
serverdir="/home/test"
clientdir="/home/test"
module="test"

/usr/local/bin/inotifywait -mrq -e modify,create,move,delete,attrib ${clientdir} | while read events
    do
    rsync -a --delete ${serverdir} ${serverip}::${module}
    echo "`date +'%F %T'` 出现事件 $events" >>rsync.log 2>&1
    done 

服务端

vim /etc/rsyncd.conf

# /etc/rsyncd: configuration file for rsync daemon mode
# See rsyncd.conf man page for more options.
# configuration example:
# uid = nobody
# gid = nobody
# use chroot = yes
# max connections = 4
# pid file = /var/run/rsyncd.pid
# exclude = lost+found/
# transfer logging = yes
# timeout = 900
# ignore nonreadable = yes
# dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2

[test]
    path = /home/
    read only = false
    uid = root
    gid = root

启动xinetd(没有的安装一下)

systemctl restart xinetd

启动rsync

rsync --daemon

测试

1.新建资源文件夹(客户端个服务端都需要建立)

mkdir /home/test

2.启动实时shell

nohup ./csync.sh &

3.在客户端/home/test文件夹下建立资源

touch /home/test/file{1..10}

日志

2019-02-19 21:39:52 出现事件 /home/test/ CREATE file6
2019-02-19 21:39:52 出现事件 /home/test/ ATTRIB file6
2019-02-19 21:39:52 出现事件 /home/test/ CREATE file7
2019-02-19 21:39:52 出现事件 /home/test/ ATTRIB file7
2019-02-19 21:39:53 出现事件 /home/test/ CREATE file8
2019-02-19 21:39:53 出现事件 /home/test/ ATTRIB file8
2019-02-19 21:39:53 出现事件 /home/test/ CREATE file9
2019-02-19 21:39:53 出现事件 /home/test/ ATTRIB file9
2019-02-19 21:39:53 出现事件 /home/test/ CREATE file10
2019-02-19 21:39:53 出现事件 /home/test/ ATTRIB file10

服务端展示

只要实时监控脚本启动, 无论你在客户端机器的/home/test下做任何操作都会自动同步到服务端的/home/test下面

猜你喜欢

转载自www.cnblogs.com/chenglee/p/10401610.html