rsync
一款快速增量備份工具
Remote Sync,遠程同步
支持本地復(fù)制,或者與其他SSH伴挚、rsync主機同步, 功能類似于scp,但是要比scp豐富。
官方網(wǎng)站: http//:rsync.samba.org
特點
1缩滨、可以鏡像保存整個目錄樹和文件系統(tǒng)呐萨。
2、可以很容易做到保持原來文件的權(quán)限底哥、時 間咙鞍、軟硬接等等,做到特殊權(quán)限即可安裝趾徽。
3续滋、快速:第一次同步時rsync會復(fù)制全部內(nèi)容,但在下一次只傳輸修改過的文件孵奶。rsync在傳輸數(shù)據(jù)的過程中可以實行壓縮及解壓縮操作疲酌,因此可以使用更少的寬帶。
4了袁、安全:可以使用scp朗恳、ssh等方式來傳輸文件,當然也可以通過直接的socket連接载绿。支持匿名傳輸粥诫,以方便進行網(wǎng)站鏡像。
環(huán)境
操作系統(tǒng):Centos 7us
目標服務(wù)器:192.168.232.10
源服務(wù)器:192.168.232.20
目的:把源服務(wù)器上/a目錄實時同步到目標服務(wù)器的/b目錄下崭庸。
關(guān)閉SELinux怀浆,關(guān)閉防火墻
vim /etc/selinux/config
->SELINUX=disabled
setenforce 0 #立即生效
systemctl stop/disable firewalld
安裝rsync
yum -y install rsync xinetd
systemctl start rsyncd
#注意:兩臺服務(wù)器都需要做
源服務(wù)器:192.168.232.10
打開配置文件/etc/rsyncd.conf
創(chuàng)建rsync服務(wù)端數(shù)據(jù)?錄路徑和模塊
mkdir /home_test
touch /home_test/home_test
創(chuàng)建用戶認證文件
設(shè)置?件權(quán)限
chmod 600 /etc/rsyncd.conf
chmod 600 /etc/rsync.pass
重啟rsync
目標服務(wù)器192.168.232.20
創(chuàng)建密碼?件
vim /etc/passwd.txt
123456 #密碼
注意:這?的密碼和客戶端的密碼是?樣的
chmod 600 /etc/passwd.txt #設(shè)置?件權(quán)限,只設(shè)置?件所有者具有讀取怕享、寫?權(quán)限
配置完成
rsync -avH --port=873 --progress --delete /home_test user1@192.168.232.10::home_test --password-file=/etc/passwd.txt
/home_test 是指服務(wù)端數(shù)據(jù)?錄
user1 是客戶端設(shè)置好的賬號
home_test 是指客戶端設(shè)置的模塊名稱
rsync 不能實時的去監(jiān)測执赡、同步數(shù)據(jù),雖然它可以通過 linux 守護進程的方式進行觸發(fā)同步熬粗,但是兩次觸發(fā)動作一定會有時間差搀玖,這樣就導(dǎo)致了服務(wù)端和客戶端數(shù)據(jù)可能出現(xiàn)不一致,無法在應(yīng)用故障時完全的恢復(fù)數(shù)據(jù)驻呐」嘧纾可以使用 rsync+inotify 的組合來解決,可以實現(xiàn)數(shù)據(jù)的實時同步
安裝Inotify-tools?具含末,實時觸發(fā)rsync進?同步
1猜拾、查看服務(wù)器內(nèi)核是否?持inotify (下?輸出說明?持)
ll /proc/sys/fs/inotify
-rw-r--r-- 1 root root 0 Mar 7 02:17 max_queued_events
-rw-r--r-- 1 root root 0 Mar 7 02:17 max_user_instances
-rw-r--r-- 1 root root 0 Mar 7 02:17 max_user_watches
2、安裝inotify-tools
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
設(shè)置系統(tǒng)環(huán)境變量佣盒,添加軟連接
echo "PATH=/usr/local/inotify/bin:$PATH" >>/etc/profile.d/inotify.sh
source /etc/profile.d/inotify.sh
echo "/usr/local/inotify/lib" > /etc/ld.so.conf.d/inotify.conf
ln -s /usr/local/inotify/include /usr/include/inotify
ln -s /usr/local/inotify/lib/libnotifytools.so.0 /usr/lib64
修改inotify默認參數(shù)(inotify默認內(nèi)核參數(shù)值太?)
查看系統(tǒng)默認參數(shù)值
sysctl -a | grep max_queued_events
結(jié)果是:fs.inotify.max_queued_events = 16384
sysctl -a | grep max_user_watches
結(jié)果是:fs.inotify.max_user_watches = 8192
sysctl -a | grep max_user_instances
結(jié)果是:fs.inotify.max_user_instances = 128
修改參數(shù):
sysctl -w fs.inotify.max_queued_events="99999999"
sysctl -w fs.inotify.max_user_watches="99999999"
sysctl -w fs.inotify.max_user_instances="65535"
參數(shù)說明
max_queued_events:
inotify隊列最?度挎袜,如果值太?,會出現(xiàn)"** Event Queue Overflow **"錯誤,導(dǎo)致監(jiān)控?件不準確
max_user_watches:
要同步的?件包含多少?錄盯仪,可以?:find /home_test -
type d | wc -l 統(tǒng)計紊搪,必須保證max_user_watches值?于統(tǒng)
計結(jié)果(這?/home_test為同步?件?錄)
max_user_instances:
每個?戶創(chuàng)建inotify實例最?值
創(chuàng)建腳本,實時觸發(fā)rsync進?同步
vim /usr/local/inotify/rsync.shsh /usr/local/inotify/rsync.sh
設(shè)置腳本開機自動執(zhí)行
vim /etc/rc.d/rc.local #編輯全景,在最后添加??
sh /usr/local/inotify/rsync.sh &
#設(shè)置開機自動在后臺運行腳本
chmod +x /etc/rc.d/rc.local
systemctl enable /etc/rc.d/rc.local
錯誤排查
問題?: @ERROR: chroot failed rsync error: error starting client-server protocol (code 5) at main.c(1522) [receiver=3.0.3]
原因: 服務(wù)器端的?錄不存在或?權(quán)限耀石,創(chuàng)建?錄并修正權(quán)限可解決問題。
問題?: @ERROR: auth failed on module tee rsync error: error starting client-server protocol (code 5) at main.c(1522) [receiver=3.0.3]
原因: 服務(wù)器端該模塊(tee)需要驗證?戶名密碼爸黄,但客戶端沒有提供正確 的?戶名密碼滞伟,認證失敗。 提供正確的?戶名密碼解決此問題炕贵。
問題三: @ERROR: Unknown module ‘tee_nonexists' rsync error: error starting client-server protocol (code 5) at main.c(1522) [receiver=3.0.3]
原因: 服務(wù)器不存在指定模塊梆奈。提供正確的模塊名或在服務(wù)器端修改成你要 的模塊以解決問題。