通过rsync+inotify实现数据的实时备份配置

2019-10-14 21:42:20王旭


系统环境

这里所有服务器均采用Linux操作系统,系统内核版本与节点信息如表1 所示:

表1



1 安装rsync与inotify-tools

inotify-tools是用来监控文件系统变化的工具,因此必须安装在内容发布节点,服务节点无需安装inotify-tools,另外需要在web1、web2、web3、webserver节点上安装rsync,由于安装非常简单,这里不在讲述。
在这个案例中,内容发布节点(即server)充当了rsync客户端的角色,而三个服务节点充当了rsync服务器端的角色,整个数据同步的过程,其实就是一个从客户端向服务端推送数据的过程。这点与上面我们讲述的案例刚好相反。

2 在三个服务节点配置rsync

 这里给出三个服务节点的rsync配置文件,以供参考,读者可根据实际情况自行修改。

Web1节点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.12.134
hosts deny = *
list = false
uid = root
gid = root
auth users = web1user
secrets file = /etc/web1.pass
Web2节点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.12.134
hosts deny = *
list = false
uid = root
gid = root
auth users = web2user
secrets file = /etc/web2.pass
Web3节点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 = /web3/wwwroot/
comment = web3 file
ignore errors
read only = no
write only = no
hosts allow = 192.168.12.134
hosts deny = *
list = false
uid = root
gid = root
auth users = web3user
secrets file = /etc/web3.pass

在三台服务节点rsyncd.conf文件配置完成后,依次启动rsync守护进程,接着将rsync服务加入到自启动文件中:
echo  “/usr/local/bin/rsync --daemon” >>/etc/rc.local
到此为止,三个web服务节点已经配置完成。

3 配置内容发布节点

 配置内容发布节点的主要工作是将生成的静态网页实时的同步到集群中三个服务节点,这个过程可以通过一个shell脚本来完成,脚本内容大致如下:
#!/bin/bash
host1=192.168.12.131