1.
兩台服務器AB
A做客戶端,B做備機,A --》 B同步數據
進入A
如果是centos系統可以使用以下命令查看是否安裝了rsync
# rpm -qa | grep rsync
如果有安裝則可跳過以下源碼安裝
a.rsync源碼安裝
# wget http://www.samba.org/ftp/rsync/src/rsync-3.0.7.tar.gz
# tar zxvf rsync-3.0.7.tar.gz
# cd rsync-3.0.7
# ./configure --prefix=/usr/local/rsync
# make
# make install
b.創建密碼認證文件:
# cd /usr/local/rsync/
# echo "111111" >/usr/local/rsync/rsync1.passwd
c.給密碼文件賦予600權限:
# chmod 600 rsync1.passwd
d.安裝inotify:
# wget http://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
e、創建監控腳本:
#!/bin/bash
host=172.16.10.134
src=/root/test/
des=web
user=rsync
/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
#同步后 --delete 刪除文件,同步增量把 --delete刪除
/usr/bin/rsync -vzurtopg --delete --progress --password-file=/usr/local/rsync/rsync1.passwd $src $user@$host::$des
echo "${files} was rsynced" >>/tmp/rsync.log 2>&1
done
注意:host為第二台服務器IP,src為要監控的路徑,web是認證模塊名稱,最后把監控腳本命名為rsync1.sh放在要監控的路徑下。
f、給監控腳本賦予764權限:
# chmod 764 rsync1.sh
!!!進入B服務器
查看是否安裝了rsync
# rpm -qa | grep rsync
如果有安裝則可跳過以下源碼安裝
a.rsync源碼安裝
# wget http://www.samba.org/ftp/rsync/src/rsync-3.0.7.tar.gz
# tar zxvf rsync-3.0.7.tar.gz
# cd rsync-3.0.7
# ./configure --prefix=/usr/local/rsync
# make
# make install
b.創建密碼認證文件:
# cd /usr/local/rsync/
# echo "rsync:111111" >/usr/local/rsync/rsync1.passwd
c.給密碼文件賦予600權限:
# chmod 600 rsync1.passwd
d、創建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 = /root/test/
comment = web file
ignore errors
read only = no
write only = no
hosts allow = 172.16.10.134
hosts deny = *
list = false
uid = root
gid = root
auth users = rsync
secrets file = /usr/local/rsync/rsync1.passwd
該配置文件是用來接收另一台服務器的文件。其中web是server服務端(134服務器)的認證模塊名稱,需要與134服務器里的一致。把配置文件命名為rsync1.conf,放到/usr/local/rsync/目錄里
h、啟動該配置文件
# /usr/local/rsync/bin/rsync --daemon --config=/usr/local/rsync/rsync1.conf
需要開機啟動的話:
# echo "/usr/local/rsync/bin/rsync --daemon --config=/usr/local/rsync/rsync1.conf" >> /etc/rc.local
二、A是備機,B做為客戶端 B --》A
172.16.10.207
進入B服務器(rsync已安裝)
安裝inotify:
# wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
# tar zxvf inotify-tools-3.13.tar.gz
# cd inotify-tools-3.13
# ./configure --prefix=/usr/local/inotify
# make
# make install
創建密碼
b.創建密碼認證文件:
# cd /usr/local/rsync/
# echo "111111" >/usr/local/rsync/rsync2.passwd
c.給密碼文件賦予600權限:
# chmod 600 rsync2.passwd
e、創建監控腳本:
#!/bin/bash
host=172.16.10.207
src=/root/test/
des=web2
user=rsync2
/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/rsync2.passwd $src $user@$host::$des
echo "${files} was rsynced" >>/tmp/rsync.log 2>&1
done
注意:host為第二台服務器IP,src為要監控的路徑,web是認證模塊名稱,最后把監控腳本命名為rsync2.sh放在要監控的路徑下。
f、給監控腳本賦予764權限:
# chmod 764 rsync2.sh
!!!進入客戶端A服務器
.創建密碼認證文件:
# cd /usr/local/rsync/
# echo "rsync:111111" >/usr/local/rsync/rsync2.passwd
chmod 600 rsync2.passwd
g、創建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
[web2]
path = /root/test/
comment = web file
ignore errors
read only = no
write only = no
hosts allow = 172.16.10.207
hosts deny = *
list = false
uid = root
gid = root
auth users = rsync2
secrets file = /usr/local/rsync/rsync2.passwd
該配置文件是用來接收另一台服務器的文件。其中web是server服務端(207服務器)的認證模塊名稱,需要與134服務器里的一致。把配置文件命名為rsync2.conf,放到/usr/local/rsync/目錄里
h、啟動該配置文件
# /usr/local/rsync/bin/rsync --daemon --config=/usr/local/rsync/rsync2.conf
需要開機啟動的話:
# echo "/usr/local/rsync/bin/rsync --daemon --config=/usr/local/rsync/rsync2.conf" >> /etc/rc.local
最后啟動兩台服務器的監控腳本:
# sh /root/test/rsync1.sh &
需要開機啟動的話:
# echo "/root/test/rsync1.sh" >> /etc/rc.local
sh /root/test/rsync2.sh &
需要開機啟動的話:
# echo "/root/test/rsync2.sh" >> /etc/rc.local
測試
任意在其中一台服務器/root/test/路徑下,新增一個文件,你會發現另一台服務器也同步了該文件。
同步之后,兩台服務器下都會有rsync1.sh和rsync2.sh監控腳本,注意勿刪!
如果需要3台或3台以上的服務器之間進行同步,則需要更改shell腳本,方法類似。
如果出現錯誤

yum -y install xinetd
service xinetd start
ps -ef|grep rsync
kill進程 然后刪除/var/run 目錄下的rsyncd.pid,重啟進程
