Rsyncserver:IP:192.168.5.129 /rsync CentOS release 6.9nginx
Rsyncclient:IP:192.168.5.131 /rsync CentOS release 6.9 git
1、主服务器(server端,我这里是nginx)github
其中主服务器须要安装rsync与inotify,主服务器做为server,向备份服务器client传输文件web
一、安装rsyncshell
1. [root@centos-1 ~]# cd /usr/local/src/ vim
2. [root@centos-1 src]# wget http://rsync.samba.org/ftp/rsync/src/rsync-3.0.9.tar.gz centos
3. [root@centos-1 src]# tar zxvf rsync-3.0.9.tar.gz 安全
4. [root@centos-1 src]# cd rsync-3.0.9 bash
5. [root@centos-1 rsync-3.0.9]# ./configure --prefix=/usr/local/rsync 服务器
6. [root@centos-1 rsync-3.0.9]# make && make install
二、创建密码认证文件
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
三、安装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
四、建立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
2、备份服务器(client,我这里为nginx-backup)
一、安装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
二、创建用户与密码认证文件
[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权限
三、创建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.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
二、测试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
三、在server端2.txt文件里面写入文件测试是否是会同步