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文件裏面寫入文件測試是否是會同步