Rsync拉取

实验条件

RsyncClient:内容发布节点(192.168.71.133

RsyncServer1:服务节点1(192.168.71.134)

RsyncServer2:服务节点2(192.168.71.132)


同步目录

RsyncClient /web/wwwroot/

RsyncServer1/web1/wwwroot/

RsyncServer2/web2/wwwroot/

无线网密码:123qazwsx

实验需求

内容发布节点负责将用户发布的数据生成静态页面,同时将静态页面的更新情况实时更新到服务节点,服务节点2

网络拓扑图:

Rsync实时同步之rsync+inotify 拉取_第1张图片

实验步骤

1:内容发布节点(192.168.71.133

[root@localhost ~]#cd Desktop/
[root@localhost Desktop]#tar zxfrsync-3.0.9.tar.gz -C /opt/
[root@localhost Desktop]#cd/opt/rsync-3.0.9/
[root@localhost inotify-tools-3.13]#./configure;make&& make install
[root@localhost ~]# cd Desktop/
[root@localhost Desktop]# tar xfz  inotify-tools-3.13.tar.gz -C /opt/
[root@localhost Desktop]# cd/opt/inotify-tools-3.13/
[root@localhost inotify-tools-3.13]#./configure;make && make install

有些时候你在编译的时候可能会提示说Gcc不在path路径中

解决办法:yum -y  install  gcc*

现在你再重新编译安装就没事了。

[root@localhost ~]# cat/web/wwwroot/inotifyrsync.sh
#!/bin/bash
host1=192.168.71.134
src=/web/wwwroot/
dst1=web1
user1=web1user
host2=192.168.71.132
src=/web/wwwroot/
dst2=web2
user2=web2user
/usr/local/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/server1.pass$src $user1@$host1::$dst1
       /usr/bin/rsync -vzrtopg --delete --progress--password-file=/etc/server2.pass $src $user2@$host2::$dst2
                 echo " was  rsynced" >> /tmp/rsync.log2>&1
       done

该脚本放在需要主动同步的服务器(或发布服务器),我称其为rsyncClient

该脚本也可以分开写,需要注意的是,/etc/server.pass/etc/server2.pass分别是两个rsyncServer存放在rysncClient端的密码文件,我曾尝试把两个密码放在同一个配置文件,但是发现不行。于是我就分开写了


[root@localhost ~]# mkdir  /web/wwwroot/ -p
[root@localhost ~]# chmod 755  /web/wwwroot/inotifyrsync.sh
[root@localhost ~]# cat /etc/server1.pass
123
[root@localhost ~]# cat /etc/server2.pass
321
[root@localhost ~]# chmod 600  /etc/server1.pass
[root@localhost ~]# chmod 600  /etc/server2.pass
[root@localhost ~]# ll  /etc/server1.pass
-rw------- 1 root root 4 Mar 24 01:31 /etc/server1.pass
[root@localhost ~]# ll  /etc/server2.pass
-rw------- 1 root root 4 Mar 24 01:31 /etc/server2.pass
[root@localhost ~]# /web/wwwroot/inotifyrsync.sh &    调入后台作为守护进程
[root@localhost ~]#echo  “/web/wwwroot/inotifyrsync.sh &” >> /etc/rc.local  开机自动运行


2:服务节点1192.168.71.134

cd Desktop/
tar zxf rsync-3.0.9.tar.gz -C /opt/
cd /opt/rsync-3.0.9/
./configure;make && make install
[root@localhost ~]# cat /etc/rsyncd.conf
uid = nobody
gid = nobody
use chroot = no
max connections =10
strict modes = yes
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
[web1]
path = /web1/wwwroot/
comment = web1 file
ignore errors
read only = no
write only = no
hosts allow = 192.168.71.133
hosts deny = *
list = false
uid = root
gid = root
auth users = web1user
secrets file = /etc/web1.pass
[root@localhost ~]#mkdir  /web1/wwwroot/  -p
[root@localhost ~]#chmod 755  /web1/wwwroot/
[root@localhost ~]# touch  /etc/web1.pass
[root@localhost ~]# vi  /etc/web1.pass
web1user:321
[root@localhost ~]# chmod  600  /etc/web1.pass
[root@localhost ~]# /usr/local/bin/rsync –-daemon    开启rsync守护进程
[root@localhost ~]# ps -ef|grep rsync|grep-v grep     查看守护进程是否开启
root    16592     1  0 00:46 ?        00:00:00/usr/local/bin/rsync --daemon
[root@localhost ~]# lsof -i:873                                查看默认端口873是否开启
COMMAND  PID USER   FD   TYPE DEVICE SIZE NODE NAME
rsync  16592 root    4u  IPv6 65197       TCP *:rsync (LISTEN)
rsync  16592 root    5u  IPv4 65198       TCP *:rsync (LISTEN)
设置开机自动运行
[root@localhost ~]#echo “/usr/local/bin/rsync–daemon” >> /etc/rc.local


3:服务节点2192.168.71.132

cd Desktop/
tar zxf rsync-3.0.9.tar.gz -C /opt/
cd /opt/rsync-3.0.9/
./configure;make && make install
[root@localhost ~]# cat /etc/rsyncd.conf
uid = nobody
gid = nobody
use chroot = no
max connections =10
strict modes = yes
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
[web2]
path = /web2/wwwroot/
comment = web2 file
ignore errors
read only = no
write only = no
hosts allow = 192.168.71.133
hosts deny = *
list = false
uid = root
gid = root
auth users = web2user
secrets file = /etc/web2.pass
[root@localhost ~]#mkdir  /web2/wwwroot/  -p
[root@localhost ~]#chmod 755  /web2/wwwroot/
[root@localhost ~]# touch  /etc/web2.pass
[root@localhost ~]# vi  /etc/web2.pass
web2user:321
[root@localhost ~]# chmod  600  /etc/web2.pass
[root@localhost ~]# /usr/local/bin/rsync –daemon       开启rsync守护进程
[root@localhost ~]# ps -ef|grep rsync                  查看守护进程是否开启
root     16592     1  0 00:46 ?        00:00:00 /usr/local/bin/rsync --daemon
[root@localhost ~]# lsof -i:873                    查看默认端口873是否开启
COMMAND   PID USER   FD   TYPE DEVICE SIZE NODE NAME
rsync   16592 root    4u  IPv6  65197       TCP *:rsync (LISTEN)
rsync   16592 root    5u  IPv4  65198       TCP *:rsync (LISTEN)
[root@localhost ~]#echo “/usr/local/bin/rsync –daemon” >> /etc/rc.local  设置开机自动运行

4:测试

在内容发布节点的/web/wwwroot/下创建文件,在服务节点观察,看是否可以实时同步

注意

1:测试的时候,当密码文件(如/etc/web2.pass)的权限不对时会提示以下错误

rsync error: error starting client-serverprotocol (code 5) at main.c(1296) [sender=2.6.8]

2:若报下面的错误,则很可能是iptables防火墙的问题(我所遇到的都是防火墙的问题)

wKioL1MyGn-jA4V9AACN1OJ2k1A477.jpg

你可以先关闭这三台机器的防火墙确认是否是防火墙在作怪

关掉防火墙后,若可以实现实时更新,就是数据包被防火墙的拦截

因此需要设置防火墙,以便rsync可以通过