rsync是linux系统下文件同步和数据传输工具,
他有四种应用模式:1.本地模式2.远程shell模式3.rsync列表模式4.服务器模式
这里我们搭建的是服务器模式,这种模式是基于C/S模式,需要rsync在后台启用一个守护进程,这个进程在rsync服务器端永久进行,用于接收文件传输请求。
举个例子,假设有个网站,数据每天会更新,为保数据安全,建立一个远程容灾系统。但,rsync需要触发才能更新,虽然可以使用linux守护进程crontab定时触发,但两次触发动作是有一定时间差,可能导致服务端与客户端数据不一致,无法在出现灾害时,完全恢复数据。
在这里我们用到inotify工具,它是一个异步的文件监控文件系统,监控文件系统的各种变化,当文件有任何变化,就会触发rsync同步,这刚好解决数据实时同步问题。

节点名称
|
用途
|
IP
|
网页路径
|
server
|
内容发布节点
|
193.168.0.20
|
/webdata
|
web2
|
服务节点
|
193.168.0.22
|
/web2data
|
web3
|
服务节点
|
193.168.0.23
|
/web3data
|
inotify是用来监控文件系统的,所以一定安装在内容发布节点,服务节点无需安装,只需安装rsync。
1.在服务器节点上配置rsync
- [root@web2 ~]# tar xzf apt/rsync-3.0.4.tar.gz
- [root@web2 ~]# cd rsync-3.0.4/
- [root@web2 rsync-3.0.4]# ./configure
- [root@web2 rsync-3.0.4]# make && make install
1.2.安装好rsync时没有rsyncd.con配置文件的,我们自行建一个就好
web2节点的rsyncd.conf
- [root@web2 ~# vim /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 = /web2data
- comment = web2 file
- ignore errors
- read only = no
- write only = no
- hosts allow = *
- list = false
- uid = root
- gid = root
- auth users = web2user
- secrets file = /etc/web2.pass
1.3.web3节点的rsyncd.conf
- [root@web3 ~# vim /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
-
- [web3]
- path = /web3data
- comment = web3 file
- ignore errors
- read only = no
- write only = no
- hosts allow = *
- list = false
- uid = root
- gid = root
- auth users = web3user
- secrets file = /etc/web3.pass
auth user 是定义的用户,与linux系统用户无关,用来连接该模块的用户名。
secretes file 是指定包含“用户名:密码”格式文件,用户名就是auth user定义的用户名
- [root@web2 ~]# more /etc/web2.pass
- web2user:admin
- [root@web2 ~]# chmod 600 /etc/web2.pass
-
- [root@web3 ~]# more /etc/web3.pass
- web3user:admin
- [root@web3 ~]# chmod 600 /etc/web3.pass
1.4.启动rsync守护进程(web2,web3)
- [root@web2 ~]# /usr/local/bin/rsync –daemon
- [root@web2 ~]# ps -ef |grep rsync
- root 38588 1 0 Mar23 ? 00:00:00 /usr/local/bin/rsync --daemon
- [root@web2 ~]# echo "/usr/local/bin/rsync --daemon" >> /etc/rc.local #加入自启动文件
2.配置内容发布点
2.1.发布点主要是将静态文件实时同步到两个服务器节点,这个过程我们用一个shell脚本实现
- #!/bin/bash
- host1=193.168.0.22
- host2=193.168.0.23
-
- src=/webdata/
- dst1=web2
- dst2=web3
- user1=web2user
- user2=web3user
-
- /usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e close_write,delete,cre
- ate,attrib $src | while read files
- do
- /usr/local/bin/rsync -vzrtopg --delete --progress --password-file=/etc/server.pass $src $user1@$h
- ost1::$dst1
- /usr/local/bin/rsync -vzrtopg --delete --progress --password-file=/etc/server.pass $src $user2@$h
- ost2::$dst2
- echo "${files} was rsynced" >> /tmp/rsync.log 2>&1
- done
2.2.连接备份模块密码
- [root@redhat6 ~]# more /etc/server.pass
- admin
2.3.这里为了方便把shell脚本就放在webdata目录下,命名为inotify.sh,并给予执行权限,放到后台运行
- [root@redhat6 ~]# chmod 755 /webdata/inotify.sh
- [root@redhat6 ~]# nohup /webdata/inotify.sh &
2.4.加入系统自启动文件
- [root@redhat6 ~]# echo "/webdata/inotify.sh &" >> /etc/rc.local
完成所有配置后,就可以在网站发布节点添加,删除,写入文件了,会看到2个服务器节点
跟随发布节点跟更新文件。(初次如果没出现同步,稍等片刻,内容发布节点需要跟服务器节点模块对接,出现同步后,后面的更新就会快多了。)
- 有时候会遇到这样情况:向inotify监控目录写入一个很大的文件,当写入文件需要一段时间,inotify会持续不断输出该文件被更新的信息,这样就会持续触发rsync同步,占用大量系统资源,针对这样情况,最好方法是等待文件完全写入完,再触发更新,可以修改“-e close_write,delete,create,atrrib”