inotify+rsync架构实现实时同步

本文介绍了一种使用inotify和rsync结合xinetd实现的文件实时同步方案,详细步骤包括在客户端安装inotify-tools,编写实时同步Shell脚本,以及在服务端配置rsync daemon和xinetd服务。通过该方案,客户端的文件操作能够实时反映到服务端。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前序

使用inotify+rsync架构实现(文件夹/文件)实时同步, 双机之间需要ssh免密码配置(两步完成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 : chengkenlee@sina.com
#         > 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下面

 

转载于:https://www.cnblogs.com/chenglee/p/10401610.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值