遠程同步工具之rsync實戰(後臺服務方式)


一、簡介(摘自百度百科):

rsync是linux系統下的數據鏡像備份工具。

使用快速增量備份工具Remote Sync可以遠程同步,

支持本地複製,或者與其他SSHrsync主機同步


二、配置rsync源

配置rsync源服務器大致分爲三步:

(1)創建rsync配置文件

(2)創建同步賬戶數據文件

(3)啓動rsync服務

----------

(1)創建rsync配置文件(實驗虛擬機爲CentOS7)


CentOS7之前的版本,rsyncd.conf配置文件需要自己創建,(安裝:yum install -y rsync)

CentOS7之後已經預裝rsync,且rsyncd.conf中有簡單的模板參考信息,如下圖:

圖片.png

編輯該文件,配置模塊信息,加入如下配置:

address = 192.168.100.101
port = 873
uid = root
gid = root
use chroot = yes
max connections = 10
transfer logging = yes
log format = %t %a %m %f %b
syslog facility = local3
timeout = 300
pid file = /var/run/rsync.pid 
lock file=/var/run/rsync.lock
log file = /var/log/rsync.log
[Anker]
path = /home/anker/rsync-test
list = false
read only = yes 
hosts allow = 192.168.100.100
comment = rsync test
auth users = anker
secrets file = /etc/rsyncd.password
exclude =  bin/ lib/ conf/

(2)添加同步賬戶及密碼

echo anker:123456 > rsyncd.password         (密碼爲該用戶的密碼)

用戶名與密碼之間用:隔開,且賦予該文件600權限

chmod 600 rsyncd.password

(3)啓動rsync服務

rsync --daemon


三、遠程同步

在同步服務器執行命令  rsync -av [email protected]::Anker .

(未做免密需要輸入101服務器anker用戶的密碼,配合crond計劃任務時需做免密登錄)

圖片.png


rsync常用選項

-a 歸檔模式,表示以遞歸方式傳輸文件,並保持所有屬性,等同於  -rlptgoD,-a選項後面可以跟一個 --no-OPTION 這個表示關閉 -rlptgoD中的某一個,

例如  -a--no-l 等同於 -rptgoD

-r 對子目錄以遞歸模式處理,主要針對目錄,若傳輸一個單獨文件不需要加 -r,但傳輸目錄必須加-r選項

-v  打印一些信息出來,比如速率、數量等

-l 保留軟鏈接

-L 像對待常規文件一樣處理軟鏈接,如果是SRC中有軟鏈接文件,則加上該選項後,將會把軟鏈接指向的目標文件拷貝到DST

-p 保持文件權限

-o 保持文件屬主信息

-g 保持文件屬組信息

-D 保持設備文件信息

-t 保持文件時間信息

--delete 刪除那些DST中SRC沒有的文件

--exclude=PATTERN 指定排除不需要傳輸的文件,等號後面跟文件名,可以是通配符模式(*.txt)

--progress 在同步的過程中可以看到同步的過程狀態,比如統計要同步的文件數量、同步的文件傳輸速度等等

-u 加上這個選項後將會把DST中比SRC還新的文件排除掉,不會覆蓋


常用選項:    -a -v  --delete --exclude


傳輸限速(防止rsync耗帶寬)

rsync -arvuztopg --port=873 --bwlimit=10240  --password-file=/home/anker/rsync/rsync.passwd  [email protected]::m2 /home/anker/.m2/repository/

文件刪除(需要先創建一個空目錄,如下 anker爲空目錄 test爲目標目錄,清空test目錄):

rsync --delete-before -d anker/  test/                      #在刪除包含大量文件目錄的時候,使用該方式比rm速度快



rsync 常見錯誤與解決方法

(摘自https://www.jb51.net/article/31920.htm)


問題一:
@ERROR: chroot failed
rsync error: error starting client-server protocol (code 5) at main.c(1522) [receiver=3.0.3]

原因:
服務器端的目錄不存在或無權限,創建目錄並修正權限可解決問題。

問題二:
@ERROR: auth failed on module tee
rsync error: error starting client-server protocol (code 5) at main.c(1522) [receiver=3.0.3]

原因:
服務器端該模塊(tee)需要驗證用戶名密碼,但客戶端沒有提供正確的用戶名密碼,認證失敗。
提供正確的用戶名密碼解決此問題。

問題三:
@ERROR: Unknown module ‘tee_nonexists'
rsync error: error starting client-server protocol (code 5) at main.c(1522) [receiver=3.0.3]

原因:
服務器不存在指定模塊。提供正確的模塊名或在服務器端修改成你要的模塊以解決問題。


問題1:
在client上遇到問題:
rsync -auzv --progress --password-file=/etc/rsync.pas [email protected]::backup /home/
rsync: could not open password file "/etc/rsync.pas": No such file or directory (2)
Password:
@ERROR: auth failed on module backup
rsync error: error starting client-server protocol (code 5) at main.c(1506) [Receiver=3.0.7]
遇到這個問題:client端沒有設置/etc/rsync.pas這個文件,而在使用rsync命令的時候,加了這個參數--
password-file=/etc/rsync.pas

問題2:
rsync -auzv --progress --password-file=/etc/rsync.pas [email protected]::backup /home/
@ERROR: auth failed on module backup
rsync error: error starting client-server protocol (code 5) at main.c(1506) [Receiver=3.0.7]
遇到這個問題:client端已經設置/etc/rsync.pas這個文件,裏面也設置了密碼111111,和服務器一致,但是
服務器段設置有錯誤,服務器端應該設置/etc/rsync.pas ,裏面內容root:111111 ,這裏登陸名不可缺少

問題3:
rsync -auzv --progress --password-file=/etc/rsync.pas [email protected]::backup /home/
@ERROR: chdir failed
rsync error: error starting client-server protocol (code 5) at main.c(1506) [Receiver=3.0.7]
遇到這個問題,是因爲服務器端的/home/backup 其中backup這個目錄並沒有設置,所以提示:chdir failed

問題4:
rsync: write failed on "/home/backup2010/wensong": No space left on device (28)
rsync error: error in file IO (code 11) at receiver.c(302) [receiver=3.0.7]
rsync: connection unexpectedly closed (2721 bytes received so far) [generator]
rsync error: error in rsync protocol data stream (code 12) at io.c(601) [generator=3.0.7]
磁盤空間不夠,所以無法操作。
可以通過df /home/backup2010 來查看可用空間和已用空間

問題5:網絡收集問題
1、權限問題
類似如下的提示:rsync: opendir "/kexue" (in dtsChannel) failed: Permission denied (13)注意查看同步的目錄權限是否爲755
2、time out
rsync: failed to connect to 203.100.192.66: Connection timed out (110)
rsync error: error in socket IO (code 10) at clientserver.c(124) [receiver=3.0.5]
檢查服務器的端口netstat –tunlp,遠程telnet測試。
可能因爲客戶端或者服務端的防火牆開啓 導致無法通信,可以設置規則放行 rsync(873端口) 或者直接關閉防火牆。

還有一種在同步過程中可能會提示沒有權限 (將同步目錄加上SvcwRsync全部權限即可,更簡單的方法就是將SvcwRsync設爲管理員即可)


3、服務未啓動
rsync: failed to connect to 10.10.10.170: Connection refused (111)
rsync error: error in socket IO (code 10) at clientserver.c(124) [receiver=3.0.5]
啓動服務:rsync --daemon --config=/etc/rsyncd.conf
4、磁盤空間滿
rsync: recv_generator: mkdir "/teacherclubBackup/rsync……" failed: No space left on device (28)
*** Skipping any contents from this failed directory ***
5、Ctrl+C或者大量文件
rsync error: received SIGINT, SIGTERM, or SIGHUP (code 20) at rsync.c(544) [receiver=3.0.5]
rsync error: received SIGINT, SIGTERM, or SIGHUP (code 20) at rsync.c(544) [generator=3.0.5]
說明:導致此問題多半是服務端服務沒有被正常啓動,到服務器上去查查服務是否有啓動,然後查看下 /var/run/rsync.pid 文件是否存在,最乾脆的方法是殺死已經啓動了服務,然後再次啓動服務或者讓腳本加入系統啓動服務級別然後shutdown -r now服務器

6、xnetid啓動
rsync: read error: Connection reset by peer (104)
rsync error: error in rsync protocol data stream (code 12) at io.c(759) [receiver=3.0.5]
查看rsync日誌
rsync: unable to open configuration file "/etc/rsyncd.conf": No such file or directory
xnetid查找的配置文件位置默認是/etc下,根據具體情況創建軟鏈接。例如:
ln -s /etc/rsyncd/rsyncd.conf /etc/rsyncd.conf
或者更改指定默認的配置文件路徑,在/etc/xinetd.d/rsync配置文件中。

Rsync configure:
配置一:
ignore errors
說明:這個選項最好加上,否則再很多crontab的時候往往發生錯誤你也未可知,因爲你不可能天天去看每時每刻去看log,不加上這個出現錯誤的機率相對會很高,因爲任何大點的項目和系統,磁盤IO都是一個瓶頸

Rsync error:
錯誤一:
@ERROR: auth failed on module xxxxx
rsync: connection unexpectedly closed (90 bytes read so far)
rsync error: error in rsync protocol data stream (code 12) at io.c(150)
說明:這是因爲密碼設置錯了,無法登入成功,檢查一下rsync.pwd,看客服是否匹配。還有服務器端沒啓動rsync 服務也會出現這種情況。

錯誤二:
password file must not be other-accessible
continuing without password file
Password:
說明:這是因爲rsyncd.pwd rsyncd.sec的權限不對,應該設置爲600。如:chmod 600 rsyncd.pwd

錯誤三:
@ERROR: chroot failed
rsync: connection unexpectedly closed (75 bytes read so far)
rsync error: error in rsync protocol data stream (code 12) at io.c(150)
說明:這是因爲你在 rsync.conf 中設置的 path 路徑不存在,要新建目錄才能開啓同步

錯誤四:
rsync: failed to connect to 218.107.243.2: No route to host (113)
rsync error: error in socket IO (code 10) at clientserver.c(104) [receiver=2.6.9]
說明:防火牆問題導致,這個最好先徹底關閉防火牆,排錯的基本法就是這樣,無論是S還是C,還有ignore errors選項問題也會導致

錯誤五:
@ERROR: access denied to www from unknown (192.168.1.123)
rsync: connection unexpectedly closed (0 bytes received so far) [receiver]
rsync error: error in rsync protocol data stream (code 12) at io.c(359)
說明:此問題很明顯,是配置選項host allow的問題,初學者喜歡一個允許段做成一個配置,然後模塊又是同一個,致使導致

錯誤六:
rsync error: received SIGINT, SIGTERM, or SIGHUP (code 20) at rsync.c(244) [generator=2.6.9]
rsync error: received SIGUSR1 (code 19) at main.c(1182) [receiver=2.6.9]
說明:導致此問題多半是服務端服務沒有被正常啓動,到服務器上去查查服務是否有啓動,然後查看下 /var/run/rsync.pid 文件是否存在,最乾脆的方法是殺死已經啓動了服務,然後再次啓動服務或者讓腳本加入系統啓動服務級別然後shutdown -r now服務器

錯誤七:
rsync: read error: Connection reset by peer (104)
rsync error: error in rsync protocol data stream (code 12) at io.c(604) [sender=2.6.9]
說明:原數據目錄裏沒有數據存在

rsync 錯誤與解決方法整理補充篇

由於我們經常使用rsync進行服務器文件的同步工作,但在配置過程中,會出現很多問題,下面的錯誤基本上都是通過客戶端返回的錯誤進行分析。

      另外查找錯誤過程中也學到了一個查看錯誤的方法-查看系統日誌,我們操作的每步都會在日誌上有說明,有時候報錯看不懂,日誌上都寫的清清楚楚。

      命令:cat /var/log/messages

問題一:

@ERROR: auth failed on module bachup
 rsync error: error starting client-server protocol (code 5) at main.c(1522) [receiver=3.0.3]

原因:服務器端該模塊(backup)需要驗證用戶名密碼,但客戶端沒有提供正確的用戶名密碼,認證失敗。  提供正確的用戶名密碼解決此問題。 
這個問題比較蠢,如果可能你和我一樣明明在模塊裏寫了密碼文件,怎麼都不通,這個時候我勸你先檢查一下所寫模塊中是不是和下圖一樣。比如,某處多了個s。

問題二:110錯誤 -time out

rsync: failed to connect to 203.100.192.66: Connection timed out (110) 
rsync error: error in socket IO (code 10) at clientserver.c(124) [receiver=3.0.5]

原因:

檢查服務器的端口netstat –tunlp,遠程telnet測試。 可能因爲客戶端或者服務端的防火牆開啓 導致無法通信,可以設置規則放行 rsync(873端口) 或者直接關閉防火牆。 
在這裏博主是重啓過服務器,然後就報110了,直接開啓rsync服務就行。

命令:service rsyncd start

問題三:113錯誤 -防火牆
rsync: failed to connect to 218.107.243.2: No route to host (113) rsync error: error in socket IO (code 10) at clientserver.c(104) [receiver=2.6.9]
原因:
防火牆問題導致,這個很簡單,關閉防火牆就可以。不過博主還是被困在這裏幾個小時,因爲linux防火牆分兩種:

-iptable和-firewall
認識到這個問題首先查看系統的防火牆種類,查看防火牆狀態即可
service iptable status
firewall-cmd --state

確定防火牆種類後再按對應方法將防火牆關閉即可。

問題四:1516錯誤

@ERROR: auth failed on module backup
rsync error: error starting client-server protocol (code 5) at main.c(1516) [Receiver=3.0.9]

解決:這個問題錯誤差別很大,可查看系統日誌查看錯誤,再搜索解決方法。

注意:一般這種情況都建立在可以連通的基礎。去服務端查看系統日誌,可以查看相關錯誤。

命令:cat /var/log/massages

cat /var/log/rsyncd.log



















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