利用rsync+inotify实现源服务器与目标服务器的实时同步

服务器

IP

同步目录

环境

       源服务器

    10.234.145.146

       /tmp

Linux centos7系统

      目标服务器

10.234.145.55

       /tmp

Linux centos7虚拟机

--操作源服务器--

1. 安装rsync:

2. 建立密码认证文件(这里我把rsync-pwd改为了‘123456’,这里的rsync-pwdrsync.passwd文件名都是可以自定义的)

3. 然后给rsync.passwd文件600的权限:

无论是为了安全,还是为了避免以下错误,密码文件都需要给600权限

4. 安装inotify

 [root@nginx rsync]# cd /usr/src/

[root@nginx src]# wgethttp://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz 

[root@nginx src]# tar zxvf inotify-tools-3.14.tar.gz

[root@nginx src]# cd inotify-tools-3.14

[root@nginx inotify-tools-3.14]# ./configure --prefix=/usr/local/inotify

[root@nginx inotify-tools-3.14]# make

[root@nginx inotify-tools-3.14]# make install

5. 创建rsync复制脚本

#!/bin/sh

srcdir=/tmp/

dstdir=web

#excludedir=/usr/local/inotify/exclude.list

rsyncuser=webuser

rsyncpassdir=/usr/local/rsync/rsync.passwd

dstip="10.234.145.55"

/usr/local/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e close_write,modify,delete,create,attrib,move $srcdir | while read file

do

rsync -avH --port=873 --progress --delete --exclude-from=$excludedir $srcdir $rsyncuser@$dstip::$dstdir --password-file=$rsyncpassdir

echo "${file} was rsynced" >>/var/log/rsync.log 2>&1

done

我发现如果把rsync.log的放到tmp(备份的目录)或发送一直复制的问题,所以建议各位吧rsync的日志放到其他的目录下(非备份目录)

其中host是目标服务器ip,src是源服务器端要实时监控的目录,des是认证的模块名,需要与client一致,user是建立密码文件里的认证用户。
把这个脚本命名为rsync.sh,放到监控的目录里,比如我的就放到/tmp下面,并给予764权限

[root@nginx tmp]# chmod 764 rsync.sh

为了避免出现以下错误,我们要在目标服务器上安装完rsync并且运行rsync后,才可以运行源服务器上的rsync.sh脚本

rsync: failed to connect to 192.168.10.221: Connection refused (111) 
rsync error: error in socket IO (code 10) at clientserver.c(107) [sender=2.6.8]

这个错误会满屏出现。

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

[root@nginx tmp]# echo "/tmp/rsync.sh" >> /etc/rc.local

 

--操作目标服务器--

1. 安装rsync(目标服务器只安装rsync)

 

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

 [root@nginx-backup rsync-3.0.9]# echo "webuser:rsync-pwd" > /usr/local/rsync/rsync.passwd

请记住,在源服务器端建立的密码文件,只有密码,没有用户名;而在目标服务端client里建立的密码文件,用户名与密码都有。

[root@nginx-backup rsync]# chmod 600 rsync.passwd 需要给密码文件600权限

3. 建立rsync配置文件

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 = 10.234.145.146

hosts deny = *

list = false

uid = root

gid = root

auth users = webuser

secrets file = /usr/local/rsync/rsync.passwd

其中web是服务端里的认证模块名称,需要与主服务器里的一致,以上的配置我的自己服务器里的配置,以供参考。

把配置文件命名为rsync.conf,放到/usr/local/rsync/目录里

启动rsync
[root@nginx-backup rsync]# /usr/local/rsync/bin/rsync --daemon --config=/usr/local/rsync/rsync.conf

 

注意:关闭防火墙或者tcp udp 的873端口打开,就是在firewalld配置文件中添加

-A INPUT -m state --state NEW -m tcp -ptcp --dport873 -jACCEPT

-A INPUT -m state --state NEW -m udp -pudp --dport873 -jACCEPT

 

这时再运行脚本:在源服务器上执行:sh /tmp/rsync.sh &

测试:在源服务器上/tmp目录下创建一个1.php,然后在目标服务器上查看/tmp下是否有1.php,有则成功。

猜你喜欢

转载自blog.csdn.net/small_33/article/details/78978376
今日推荐