Rsyncserver:IP:192.168.5.129 /rsync CentOS release 6.9

Rsyncclient:IP:192.168.5.131 /rsync CentOS release 6.9

一、主服务器(server端,我这里是nginx)

其中主服务器需要安装rsync与inotify,主服务器作为server,向备份服务器client传输文件

1、安装rsync

1. [root@centos-1 ~]# cd /usr/local/src/  

2.  [root@centos-1 src]# wget http://rsync.samba.org/ftp/rsync/src/rsync-3.0.9.tar.gz  

3. [root@centos-1 src]# tar zxvf rsync-3.0.9.tar.gz  

4. [root@centos-1 src]# cd rsync-3.0.9  

5. [root@centos-1 rsync-3.0.9]# ./configure --prefix=/usr/local/rsync  

6. [root@centos-1 rsync-3.0.9]# make && make install

2、建立密码认证文件

1. [root@centos-1 rsync-3.0.9]# cd /usr/local/rsync/  

2. [root@centos-1 rsync]# echo "redhat" >/usr/local/rsync/rsync.passwd   

其中rsync-pwd可以自己设置密码,rsync.passwd名字也可以自己设置

1. [root@centos-1 rsync]# chmod 600 rsync.passwd

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

1. password file must not be other-accessible  

2. continuing without password file  

3、安装inotify

1. [root@centos-1 rsync]# cd /usr/local/src/  

2. [root@centos-1 src]# wget http://cloud.github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz  

3. [root@centos-1 src]# tar zxvf inotify-tools-3.14.tar.gz  

4. [root@centos-1 src]# cd inotify-tools-3.14  

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

6. [root@centos-1 inotify-tools-3.14]# make && make install 

4、创建rsync复制脚本

此项功能主要是将server端的目录/rsync里的内容,如果修改了(无论是添加、修改、删除文件)能够通过inotify监控到,并通过rsync实时的同步给client的/rsync里,下面是通过shell脚本实现的。

[root@centos-1 rsync]# vim rsync.sh

#!/bin/bash  

host=192.168.5.131

src=/rsync/        

des=web

user=root  

/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  

最好把日志文件放在其他目录不然不经常提醒同步目录。

其中host是client的ip,src是server端要实时监控的目录,des是认证的模块名,需要与client一致,user是建立密码文件里的认证用户。

把这个脚本命名为rsync.sh,放到监控的目录里,比如我的就放到/tmp下面,并给予764权限

[root@centos-1 tmp]# chmod 764 rsync.sh

然后运行这个脚本

[root@centos-1 tmp]# sh /tmp/rsync.sh &

请记住,只有在备份服务器client端的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@centos-1 tmp]# echo "/tmp/rsync.sh" >> /etc/rc.local

二、备份服务器(client,我这里为nginx-backup)

1、安装rsync(备份服务器只安装rsync)

[root@centos-2 ~]# cd /usr/src/   

[root@centos-2 src]# wget  http://rsync.samba.org/ftp/rsync/src/rsync-3.0.9.tar.gz  

[root@centos-2 src]# tar zxvf rsync-3.0.9.tar.gz  

[root@centos-2 src]# cd rsync-3.0.9  

[root@centos-2 rsync-3.0.9]# ./configure --prefix=/usr/local/rsync  

[root@centos-2 rsync-3.0.9]# make  

[root@centos-2 rsync-3.0.9]# make install  

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

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

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

[root@centos-2 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 = /rsync/  

comment = web file  

ignore errors  

read only = no

write only = no

hosts allow = 192.168.5.129  

hosts deny = *  

list = false

uid = root

gid = root

auth users = root 

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

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

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

启动rsync

[root@centos-2 rsync]# /usr/local/rsync/bin/rsync --daemon --config=/usr/local/rsync/rsync.conf

也可以安装xintd来接管一类网络服务在后台启动:接管了rsync

如果出现以下问题:

/usr/local/rsync/bin/rsync: error while loading shared libraries: libiconv.so.2: cannot open shared object file: No such file or directory,

可以采用下面方法解决

[root@centos-2 rsync]# whereis libiconv.so.2  

libiconv.so: /usr/local/lib/libiconv.so.2 /usr/local/lib/libiconv.so  

找到所需模块所在的目录,然后把此目录添加到/etc/ld.so.conf里,并更新库文件

[root@centos-2 rsync]# echo "/usr/local/lib/" >> /etc/ld.so.conf  

 [root@centos-2 rsync]# ldconfig  

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

[root@centos-2 rsync]# echo "/usr/local/rsync/bin/rsync --daemon --config=/usr/local/rsync/rsync.conf" >> /etc/rc.local

现在rsync与inotify在server端安装完成,rsync在备份服务器client端也安装完成

测试:

1、现在服务端创建1.txt到4.txt文件看看client端是否同步更新:

[root@centos-1 rsync]# ls

rsync.sh

[root@centos-1 rsync]# touch {1..4}.txt

[root@centos-1 rsync]# sending incremental file list

./

1.txt

           0 100%    0.00kB/s    0:00:00 (xfer#1, to-check=4/6)

2.txt

           0 100%    0.00kB/s    0:00:00 (xfer#2, to-check=3/6)

3.txt

           0 100%    0.00kB/s    0:00:00 (xfer#3, to-check=2/6)

4.txt

           0 100%    0.00kB/s    0:00:00 (xfer#4, to-check=1/6)

 

sent 246 bytes  received 87 bytes  666.00 bytes/sec

total size is 397  speedup is 1.19

sending incremental file list

 

sent 99 bytes  received 8 bytes  71.33 bytes/sec

total size is 397  speedup is 3.71

sending incremental file list

 

sent 99 bytes  received 8 bytes  214.00 bytes/sec

total size is 397  speedup is 3.71

sending incremental file list

 

sent 99 bytes  received 8 bytes  214.00 bytes/sec

total size is 397  speedup is 3.71

sending incremental file list

 

sent 99 bytes  received 8 bytes  214.00 bytes/sec

total size is 397  speedup is 3.71

sending incremental file list

 

sent 99 bytes  received 8 bytes  214.00 bytes/sec

total size is 397  speedup is 3.71

sending incremental file list

 

sent 99 bytes  received 8 bytes  214.00 bytes/sec

total size is 397  speedup is 3.71

sending incremental file list

 

sent 99 bytes  received 8 bytes  214.00 bytes/sec

total size is 397  speedup is 3.71

[root@centos-2 rsync]# ls

1.txt  2.txt  3.txt  4.txt  rsync.sh

2、测试server删除1.txt文件client是否会删除同步/rsync目录:

[root@centos-1 rsync]# rm -rf 1.txt

[root@centos-1 rsync]# sending incremental file list

./

deleting 1.txt

 

sent 88 bytes  received 11 bytes  198.00 bytes/sec

total size is 397  speedup is 4.01

[root@centos-2 rsync]# ls

2.txt  3.txt  4.txt  rsync.sh

3、在server端2.txt文件里面写入文件测试是不是会同步