Rsync+Inotify 实时同步

需求:实时同步A机器文件到B机器

1
2
A 192.168.1.225
B 192.168.1.226

基本架构

rsync

A

1
yum install rsync

然后修改配置文件
/etc/rsyncd.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
pid file = /var/run/rsync.pid
log file = /var/log/rsync.log
lock file=/var/run/rsync.lock
secrets file = /etc/rsync.pwss
transfer logging = yes
address=192.168.1.226
[data]
path = /home/joy/imkefu/apache-tomcat-8.0.45/webapps/ROOT/WEB-INF/classes
comment = data
port = 873
uid = root
gid = root
timeout = 600
max connections = 200
use chroot = no
read only = no
hosts allow = 192.168.1.225

设置验证文件

1
2
3
4
#/etc/rsync.pass
123456

#chmod 600 /etc/rsync.pass

启动服务

1
systemctl start rsyncd

B

1
yum install rsync

设置验证文件

1
2
3
4
#/etc/rsync.pass
123456

#chmod 600 /etc/rsync.pass

Inotify

安装inotify

1
2
3
4
5
6
cd /usr/src
wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
tar zxf inotify-tools-3.14.tar.gz
cd inotify-tools-3.14
./configure –prefix=/usr/local/inotify-3.14
make && make install

同步脚本

A

1
2
3
4
5
6
7
8
9
10
11
#!/bin/bash
#currentdate=`date +%Y%m%d%H%M`

src=/home/joy/imkefu/apache-tomcat-8.0.45/webapps/ROOT/WEB-INF/classes/
des=data
user=root
host=192.168.1.226
/usr/local/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib $src | while read files
do
/usr/bin/rsync -vzrtopg --progress --delete --password-file=/etc/rsyncd.pass $src $user@$host::$des
done

然后执行该脚本。。。

备注

  • 可以先拿无关目录测试
  • 检查pass文件权限必须是600
  • module名字必须保持一致
------ 本文结束 ------

版权声明

Medivh's Notes by Medivh is licensed under a Creative Commons BY-NC-ND 4.0 International License.
Medivh创作并维护的Medivh's Notes博客采用创作共用保留署名-非商业-禁止演绎4.0国际许可证
本文首发于Medivh 博客( http://www.mknight.cn ),版权所有,侵权必究。