rsync+inotify 实时文件同步_第1张图片

Web主机

1.rsync使用的是xinet作为其守护进程,我们首先配置其为开启

# vim /etc/xinetd.d/rsync
service rsync
{
disable = no    //改为no
flags           = IPv6
socket_type     = stream
wait            = no
user            = root
server          = /usr/bin/rsync
server_args     = --daemon
log_on_failure  += USERID
}

2,配置rsync服务

编辑文件/etc/rsyncd.conf

uid = nobody
gid = nobody
use chroot = no
max connections = 3
strict modes = yes
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsyncd.lock
log file = /var/log/rsyncd.log
[web1]
path = /www/htdocs
ignore errors = yes
read only = no
write only = no
hosts allow = 172.16.0.0/16 //允许可以同步过来的主机
hosts deny = *
list = false
uid = root
gid = root
auth users = cyb  //认证的用户名
secrets file = /etc/rsync.passwd //使用的秘钥文件所在的地方

 

3,创建密码文件

 

#vim /etc/rsync.passwd
cyb:123     //同步需要的用户名和密码
# chmod 600 /etc/rsync.passwd

 

启动rsync

 

# service xinetd start

 

两台web 主机配置内容近似

 

SOURCE主机:

这边也安装rsync ,rsync 以服务器模式运行

安装inotify-tools inotify-devel

# yum install inotify*

书写密码文件

# vim /etc/rsync_client.pwd
123
# chmod 600 /etc/rsync_client.pwd

创建脚本

 

# vim rsync.sh
#!/bin/bash
host1=172.16.11.10
host2=172.16.11.11
src=/www/web/
dst1=web1
dst2=web2
user1=cyb
user2=lyn
/usr/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 --delete --progress --password-file=/etc/rsync_client.pwd $src $user1@$host1::$dst1
/usr/bin/rsync -vzrtopg --delete --progress --password-file=/etc/rsync_client.pwd $src $user2@$host2::$dst2
echo "${files} was rsynced" >>/tmp/rsync.log 2>&1
done
~

在后台运行

# bash /tmp/rsync.sh

 

最后,将此脚本加入系统自启动文件开机自己启动

echo“/tmp/rsync.sh&”>>/etc/rc.local

至此两遍已经同步