Rsync+Inotify架构实现实时同步

 

CentOS7 Rsync服务搭建-Rsync+Inotify架构实现实时同步

关于centos7版本上面搭建rsync服务并且实现实时同步
之前一直是在6版本上面搭建rsync服务,在7版本上面折腾了半天。此处总结下
inotify下载地址:http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz

环境说明:

centos7版本上面已经默认安装了rsync和xinetd服务

[ root@rh6 ~ ]# rpm -ql rsync           --在6版本上面执行此命令可以看到xinetd服务配置文件下面有rsync的子配置文件

/etc/xinetd.d/rsync

[ root@rh6 ~ ]# ls /etc/xinetd.d/sync

/etc/xinetd.d/rsync

[ root@centos7_3 ~ ]# rpm -ql rsync           --在7版本上面执行此命令却找不到这样的配置文件,ls查看提示没有此文件

[ root@centos7_3 ~ ]# ls /etc/xinetd.d/sync

ls: cannot access /etc/xinetd.d/rsync: No such file or directory

————————————————————————————————————————————————————————

实验环境:两台centos7版本的宿主机

1. 客户端:172.16.27.25

2. 服务端:172.16.27.26

具体步骤:

· 服务端

· [root@cc4 ~]# rpm -aq |grep xinetd
xinetd-2.3.15-13.el7.x86_64
[root@cc4 ~]# rpm -aq |grep rsync
rsync-3.0.9-17.el7.x86_64

· [root@cc4 ~]# vim /etc/xinetd.d/rsync    --说明:修改xinetd服务下面的配置文件,将rsync交给xinetd管理。此配置文件在7版本默认上面是没有的,我是直接从6版本复制过来的,也可以手写下面内容
# default: off
# description: The rsync server is a good addition to an ftp server, as it \
#       allows crc checksumming etc.
service rsync
{
        disable              = no      --将原来的yes改为no
        flags                  = IPv6
        socket_type      = stream
        wait                   = no
        user                   = root
        server                = /usr/bin/rsync
        server_args       = --daemon
        log_on_failure  += USERID
}

· [root@cc4 ~]# vim /etc/rsyncd.conf       --修改配置文件发布共享目录
[test]
        path = /test                 --本地共享目录
        auth user = user1      --指定用户同步
        secrets file = /etc/rsyncd.secrets      --指定保存用户密码文件

· [root@cc4 ~]# systemctl restart xinetd    --启动服务

· [root@cc4 ~]# vim /etc/rsyncd.secrets    --创建安全用户
user1:123

· [root@cc4 ~]# chmod 600 /etc/rsyncd.secrets    --设置安全用户文件的权限,不能被其他人查看

· [root@cc4 ~]# mkdir /test     --创建共享目录
[root@cc4 ~]# touch /test/file{1..10}    --此处为了测试,创建几个文件
[root@cc4 ~]# ls /test/
file1  file10  file2  file3  file4  file5  file6  file7  file8  file9

· 

· 客户端测试:

· [root@cc3 ~]# rsync -a 172.16.27.26::
test    

· [root@cc3 date]# ls
[root@cc3 date]# rsync -av user1@172.16.27.26::test /date
receiving incremental file list
./
file1
file10
file2
file3
file4
file5
file6
file7
file8
file9
sent 219 bytes  received 524 bytes  1486.00 bytes/sec
total size is 0  speedup is 0.00
[root@cc3 date]# ls        --可以看见成功从上面同步过来了。
file1  file10  file2  file3  file4  file5  file6  file7  file8  file9

· 

· 通过inotify+rsync架构实现实时同步

· 环境说明:要求node1主机上面的/node1目录发生新的创建,删除,移动,以及文件属性信息改变时,自动同不到node2主机的/node2目录

· node1主机: IP  172.16.27.25 
node2主机: IP  172.16.27.26

· 具体步骤:

· node1端:

· [root@cc3 tmp]# ls /tmp/inotify-tools-3.14.tar.gz
/tmp/inotify-tools-3.14.tar.gz

· [root@cc3 tmp]# tar -xf inotify-tools-3.14.tar.gz 
[root@cc3 tmp]# cd inotify-tools-3.14/
[root@cc3 inotify-tools-3.14]# ls
aclocal.m4  ChangeLog     config.h.in  configure     COPYING  INSTALL     libinotifytools  Makefile.am  man      NEWS    src
AUTHORS     config.guess  config.sub   configure.ac  depcomp  install-sh  ltmain.sh        Makefile.in  missing  README
[root@cc3 ~]# ./configure && make && make install    --安装软件

· [root@cc3 ~]# vim /tmp/1.sh
#!/bin/bash
/usr/local/bin/inotifywait -mrq -e modify,create,move,delete,attrib /node1 |while read events
        do
        rsync -a --delete /node1 172.16.27.26::test
        echo "`date +'%F %T'` 出现事件 $events" >>/tmp/rsync.log 2>&1
        done[root@cc3 ~]#mkdir /node1
[root@cc3 ~]# touch /node1/testa{1..5}

· node2端:

· [root@cc4 ~]# vim /etc/rsyncd.conf 
[test]
        path = /node2/
        read only = false
        uid = root
        gid = root
[root@cc4 ~]# mkdir /node2
[root@cc4 ~]# ls /node2/        --此时查看,该目录里面什么文件都没有
[root@cc4 ~]# systemctl restart xinetd

· 测试验证:
node1端执行脚本并且放在后台执行,并且创建文件,再看tmp下面是否有创建文件后脚本执行报的日志信息
[root@cc3 ~]# nohup sh /tmp/1.sh &
[1] 14720
[root@cc3 ~]# nohup: ignoring input and appending output to ‘nohup.out’   --此处直接回车

[root@cc3 ~]# echo haha >>/node1/file1
[root@cc3 ~]# ls /tmp/rsync.log 
/tmp/rsync.log
[root@cc3 ~]# cat /tmp/rsync.log 
2017-10-10 15:55:01 出现事件 /node1/ CREATE file1
2017-10-10 15:55:01 出现事件 /node1/ MODIFY file1

node2端检查验证
[root@cc4 ~]# ls /node2/
file1  testa1  testa2  testa3  testa4  testa5
[root@cc4 ~]# cat /node2/file1 
haha
---------测试发现在node1端创建的文件实时同步过来到node2上面了。

· 

· 总结:其实在6系统和7系统上面搭建rsync服务是一样的,只是在7上面改版了,系统默认没有/etc/xinetd.d/rsync配置文件,需要自己手动创建。其余配置完全一样

 

<think>好的,我现在需要解决用户提到的rsync配合inotify进行双向同步时的死循环问题,以及inotify进程崩溃的问题。首先,我得回忆一下之前给出的解决方案,确保这次回答一致且更深入。 用户提到参考了引用[1],但看起来是LVS的配置,可能用户误贴了,但按照系统提示,我应该忽略不相关的引用,专注于用户的实际需求。用户的主要问题是双向同步导致的死循环和进程挂掉,需要详细的解决步骤。 首先,死循环问题通常是因为同步操作触发了另一边的inotify事件,导致无限循环。之前的回答提到通过排除特定文件或目录来过滤这些事件,可能需要更具体的例子,比如排除临时文件或.rsync部分。 然后,关于inotify进程崩溃,可能的原因包括资源限制(如文件描述符不足)、脚本逻辑问题(比如没有错误恢复机制),或者系统参数设置不当。需要检查系统日志,调整max_user_watches和max_user_instances,以及使用守护进程或systemd服务来监控和重启进程。 用户可能希望得到更具体的排查步骤,比如如何检查当前inotify限制,如何修改sysctl.conf,以及脚本中的具体过滤规则。同时,用户可能需要知道如何验证解决方案是否有效,比如压力测试方法。 另外,用户可能没有意识到使用专用工具如Unison的好处,可以再次建议作为替代方案,但重点还是在rsync+inotify的优化上。需要确保回答结构清晰,步骤详细,并且给出具体的命令和配置示例,方便用户直接操作。 最后,确保语言口语化,避免使用技术术语过多,但又要准确。检查是否有遗漏的排查点,比如网络问题、权限问题,以及如何通过日志分析定位问题。可能还需要强调双向同步的配置细节,比如双脚本设计或单脚本处理双向事件时的注意事项。</think>#### 一、双向同步死循环问题的核心原因与解决方案 **根本原因**: 双向同步中,A端同步到B端的文件变更会再次触发B端的`inotify`监控事件,导致B端向A端反向同步,形成`A→B→A→B`的无限循环[^4]。 --- #### 二、针对性解决方案(附详细实现) 1. **文件变更标记过滤法** **原理**:通过文件特征标识同步操作自身产生的变更,避免二次触发 **实现步骤**: -同步命令中添加`--exclude`参数过滤临时标记文件 ```bash rsync -azP --delete --exclude='*.rsync_lock' /source/ user@remote:/dest/ ``` - 在触发同步前创建标记文件,同步完成后删除 ```bash touch /source/.rsync_lock # 创建标记 rsync [参数] # 执行同步 rm -f /source/.rsync_lock # 删除标记 ``` 2. **事件类型区分法** **原理**:`inotifywait`监控特定事件类型,如仅响应`CREATE,DELETE,MOVED_TO` **配置示例**: ```bash inotifywait -m -r --format '%w%f' -e create,delete,moved_to /source | while read FILE do rsync -azP --delete /source/ user@remote:/dest/ done ``` *说明*:忽略`MODIFY`事件可减少触发频率,但需根据业务需求调整监控事件类型。 3. **双向同步目录隔离法** **架构设计**: ``` A端监控目录:/data/a_primary B端监控目录:/data/b_primary 实际同步路径: A→B同步到 /data/b_replica B→A同步到 /data/a_replica ``` *优势*:物理隔离监控目录与接收目录,彻底避免循环触发。 --- #### 三、进程稳定性保障措施 1. **资源限制优化** - **内核参数调整**(需root权限): ```bash echo "fs.inotify.max_user_watches=1048576" >> /etc/sysctl.conf echo "fs.inotify.max_user_instances=1024" >> /etc/sysctl.conf sysctl -p ``` - **用户级限制调整**: ```bash echo "* soft nofile 65535" >> /etc/security/limits.conf echo "* hard nofile 65535" >> /etc/security/limits.conf ``` 2. **进程守护方案** **方案选择**: | 方案 | 优势 | 实现命令 | |-------------|---------------------------|-----------------------------------| | systemd托管 | 自动重启、日志管理 | `systemctl enable inotify-sync` | | screen会话 | 零配置快速部署 | `screen -dmS sync ./sync.sh` | | 脚本自循环 | 无依赖基础环境 | 添加`while true; do ... done` | **推荐systemd配置**: ```ini # /etc/systemd/system/inotify-sync.service [Unit] Description=Bidirectional Sync Service After=network.target [Service] Type=simple User=rsync_user ExecStart=/opt/sync_script.sh Restart=on-failure RestartSec=30s [Install] WantedBy=multi-user.target ``` 3. **异常中断捕获** **增强脚本示例**: ```bash #!/bin/bash trap "echo '进程被中断,10秒后重启' >> /var/log/sync_errors.log; sleep 10" SIGTERM SIGINT while true; do inotifywait -r -q -e create,delete,move /source if rsync -azP --exclude='*.tmp' /source/ user@remote:/dest/; then echo "$(date): 同步成功" >> /var/log/sync.log else echo "$(date): 同步失败,错误码$?" >> /var/log/sync_errors.log sleep 120 fi done ``` --- #### 四、问题排查流程图 ```mermaid graph TD A[发现死循环/进程崩溃] --> B{检查系统日志} B -->|Too many open files| C[调整max_user_watches] B -->|inotify watch limit| D[增加max_user_instances] B -->|权限拒绝| E[统一目录权限] A --> F{检查同步日志} F -->|连续同步记录| G[添加标记文件过滤] F -->|网络错误| H[测试rsync连通性] A --> I{压力测试} I -->|进程崩溃| J[部署systemd守护] I -->|资源不足| K[优化内核参数] ``` --- #### 五、验证方法 1. **循环触发测试** ```bash # 在A端生成测试文件 for i in {1..100}; do touch /source/test${i}.txt; done # 观察B端是否仅同步一次 watch -n1 'ls /dest | wc -l' ``` 2. **进程稳定性测试** ```bash # 模拟进程崩溃 killall -9 inotifywait # 检查自动恢复时间 journalctl -u inotify-sync --since "1 minute ago" ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值