rsync 配置遠程同步

      昨天終於拿到服務器權限,今天第一次登上線上的服務器協助開發人員做遠程備份。由於是第一次,心裏多少有點負擔,生怕做不好(其實更怕做出什麼驚天動地的事...)

      開發人員要求將積分的目錄備份到兩臺服務器上,用到的工具是rsync,本來挺簡單的一件事,結果由於太久沒用,還有不夠細心,過程中出現了一些小問題,還好leader幫忙指導一二最終也算是順利交付任務了,下面記錄下今天的過程和遇到的問題。


任務具體要求如下:

   協助開發人員A將積分副本同步到目標主機下。

   源服務器:192.168.1.1

   目標服務器:192.168.1.2 192.168.1.3

   服務器平臺Ubuntu


配置過程

==============目標服務器==============

目標服務器上,配置文件(默認不存在),需要手動添加

wuguihong1@ubuntu:~$ vim /etc/rsyncd.conf
uid = www
gid = www
pid file=/tmp/rsyncd.pid
log file = /var/log/rsyncd.log
[jifen]
path=/data/app-jifen/
auth users=jifen
read only = no
strict modes = yes
secrets file = /etc/rsyncd.pass
hosts allow= 192.168.1.1
hosts deny = *


創建密碼文件
wuguihong1@ubuntu:~$ sudo vim /etc/rsyncd.pass
jifen:jifen@passwd


確保密碼文件權限爲600,否則會報錯

wuguihong1@ubuntu:~$ sudo chmod 600 /etc/rsyncd.pass


確保同步的目標目錄存在,否則會報錯

wuguihong1@ubuntu:~$ sudo mkdir /data/app-jifen/


確保目錄屬主與 rsyncd.conf 中的配置一致

wuguihong1@ubuntu:~$ sudo chown www.www /data/app-jifen/


==============源服務器==============

root@ubuntu:/home/wuguihong1# echo "jifen@passwd" > /etc/rsync.pass
root@ubuntu:/home/wuguihong1# chmod 600 /etc/rsync.pass


測試

root@ubuntu:/etc# rsync -vzrtopg --delete /tmp/testrsync [email protected]::jifen --password-file=/etc/rsync.pass
sending incremental file list
testrsync
sent 70 bytes received 27 bytes 194.00 bytes/sec

total size is 0 speedup is 0.00


root@ubuntu:/etc# rsync -vzrtopg --delete /tmp/testrsync jifen@192.168.1.3::jifen --password-file=/etc/rsync.pass
sending incremental file list
testrsync
sent 70 bytes received 27 bytes 64.67 bytes/sec
total size is 0 speedup is 0.00


       到這裏配置就完成了,實際上不是什麼難事,只不過原本上面就跑了一個進程(/usr/local/rsync),我一上去就往這個目錄上配,一直出錯,後來問過才知道是要自己搞多一個進程...wtf,看來得事先多做做溝通才行。


    PS: 配置完成後,最好隨手加上對進程的監控,一旦進程死掉,則自動拉起來;加上開機自啓動;

#監控進程

root@ubuntu:/home/wuguihong1# /etc/default/rsync
RSYNC_ENABLE=false
修改爲

RSYNC_ENABLE=true


root@ubuntu:/home/wuguihong1# vim /etc/crontab  //加上下面一行

*/1 * * * * root ps -C rsync || /etc/init.d/rsync start


#開機自啓動

root@ubuntu:/home/wuguihong1# cd /etc/rc2.d

root@ubuntu:/home/wuguihong1# ln -s ../init.d/rsync S50rsync


由於後來需要配置好幾臺機器,乾脆寫了腳本來處理:

#!/bin/bash
ip_allow = "192.168.1.1"
backdir = "/data/webapps"
user = "jifen"
#配置rsyncd.conf
cat << EOF >> /etc/rsyncd.conf
uid = www-data
gid = www-data
pid file=/tmp/rsyncd.pid
log file = /var/log/rsyncd.log
[jifen]
path=$backdir
auth users=$user
read only = no
strict modes = yes
secrets file = /etc/rsyncd.pass
hosts allow= $ip_allow
hosts deny = *
EOF
#配置rsync密碼文件
echo "jifen:jifen@rsync" > /etc/rsyncd.pass
chmod 600 /etc/rsyncd.pass
#監控進程存活
sed -i "s/RSYNC_ENABLE=false/RSYNC_ENABLE=true/" /etc/default/rsync
cd /etc/rc2.d
ln -s ../init.d/rsync S50rsync
cd
echo "*/1 * * * * root ps -C rsync || /etc/init.d/rsync start" >> /etc/crontab
/etc/init.d/rsync start



操作過程中遇到的問題:

問題一
root@ubuntu:/etc# rsync -vzrtopg --delete /tmp/testrsync [email protected]::jifen --password-file=/etc/rsync.pass
@ERROR: auth failed on module jifen

rsync error: error starting client-server protocol (code 5) at main.c(1503) [receiver=3.0.6]


解決辦法:
(1) 檢查服務、客戶端密碼文件是否正確:服務端密碼文件(這裏爲/etc/rsync.pass) 的格式爲 用戶:密碼; 客戶端密碼文件爲:密碼(沒有用戶名)
(2)檢查密碼文件的權限是否正確

wuguihong1@ubuntu:~$ ps aux|grep rsync
root 3978 0.0 0.0 12576 752 ? Ss 16:44 0:00 rsync --daemon --config /etc/rsyncd.conf
wuguihong1@ubuntu:~$ ll /etc/rsyncd.*
-rw-r--r-- 1 root root 298 Apr 25 17:04 /etc/rsyncd.conf
-rw------- 1 root root 18 Apr 25 17:05 /etc/rsyncd.pass


問題二
root@ubuntu:/etc# rsync -vzrtopg --delete /tmp/testrsync [email protected]::jifen --password-file=/etc/rsync.pass
sending incremental file list
testrsync
rsync: mkstemp "/.testrsync.k4hMJP" (in jifen) failed: Permission denied (13)
sent 70 bytes received 27 bytes 64.67 bytes/sec
total size is 0 speedup is 0.00
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1058) [sender=3.0.5]
解決辦法:
檢查服務器端的目錄(備份目錄)是否存在,並檢查其權限。創建目錄並修正權限可解決問題。

問題三
root@ubuntu:/etc# rsync -vzrtopg --delete /tmp/testrsync [email protected]::jifen --password-file=/etc/rsync.pass
password file must not be other-accessible
continuing without password file
Password:
解決辦法:
檢查服務端和客戶端上的密碼配置文件權限是否爲600(只能爲600),若不是可以通過命令 chmod 600 rsync.pass 修改即可



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章