rsync+inotify实现服务器之间文件实时同步(分享十三)

下面是2个服务器的结构,分别为主机名、ip、同步的目录,并将2台服务器均是CentOS 6.7发行版本。

Server 192.168.0.1    /tmp  

Client   192.168.0.2   /tmp

1、安装rsync(提前查看是否系统自带rsync)

#安装rsync和xinetd,并创建目录:
yum install rsync xinetd

#配置xinetd:
vi /etc/xinetd.d/rsync
#disable = yes修改为
disable = no

启动xinetd服务:
service xinetd start

2、建立密码认证文件

echo "test" >/etc/rsync.passwd
chmod 777 /etc/rsync.passwd 
3、安装inotify
cd /usr/src/ 
wget http://cloud.github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz 
tar zxvf inotify-tools-3.14.tar.gz 
cd inotify-tools-3.14 
./configure --prefix=/usr/local/inotify 
make && make install 
通过shell脚本rsync.sh(服务端)
#!/bin/bash 
host=192.168.0.2
src=/tmp/     
des=web 
user=webuser 
/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 --delete --progress --password-file=/usr/local/rsync/rsync.passwd $src $user@$host::$des 
echo "${files} was rsynced" >>/tmp/rsync.log 2>&1 
done
chmod 764 rsync.sh
sh /tmp/rsync.sh &

我们还可以把rsync.sh脚本加入到开机启动项里

echo "/tmp/rsync.sh" >> /etc/rc.local 
 
客户端:
1、安装rsync(备份服务器只安装rsync)
#安装rsync和xinetd,并创建目录:
yum install rsync xinetd

#配置xinetd:
vi /etc/xinetd.d/rsync
#disable = yes修改为
disable = no

启动xinetd服务:
service xinetd start

2、建立用户与密码认证文件

echo "webuser:test" > /etc/rsync.passwd

3、建立rsync配置文件

vim /etc/rsyncd.conf

uid = root 
gid = root 
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 
[web] 
path = /tmp/ 
comment = web file 
ignore errors 
read only = no 
write only = no 
hosts allow = 192.168.0.2 
hosts deny = * 
list = false 
uid = root 
gid = root 
auth users = webuser 
secrets file = /usr/local/rsync/rsync.passwd 

4、启动rsync

service xinetd start
chkconfig xinetd on #设置开机自启动

三、测试

现在rsync与inotify在server端安装完成,rsync在备份服务器client端也安装完成。接下来就可以来测试一下:

在server里创建个test-rsync文件,看看client是否能收到

[root@Server tmp]# touch test-rsync

再看client端是否有test-rsync文件,同时client端的tmp目录文件是否与server端的文件完全一致。如果一致,则表示已经设置成功。

原文地址:https://www.cnblogs.com/zywu-king/p/8000163.html