rsync实现文件同步
rsync是類unix系統下的數據鏡像備份工工具,一般linux系統都自帶了 [可以確認一下:shell>rpm -qa|grep rsync]
服務端:192.168.1.2 ?同步目錄:/home/source
客戶端:192.168.1.3 ?同步目錄:/home/receive
1、服務端配置
shell>vi /etc/rsyncd.conf ? #創建配置文件rsyncd.conf
配置文件內容:
uid=nobody gid=nobody use chroot = no max connections = 10 pid file = /var/run/rsyncde.pid lock file = /var/run/rsync.lock log file = /var/log/rsyncd.log Timeout = 300 Log format = %t %a %m %f %b [backup] path=/a/system_build/leia/ ignore errors read only = yes list = no auth users = rsync secrets file = /etc/rsyncd.secrets hosts allow = 9.115.249.158 hosts deny = 0.0.0.0/0
shell>vi etc/rsyncd.secrets #創建密碼文件
內容:
rsync:rsync //這里用戶名和密碼都定義為rsyncshell>chmod 0600 /etc/rsyncd.secrets ?#更改密碼文件權限
shell>rsync --daemon; ?#啟動服務,默認在873端口監聽(可以自己修改) ?or ?/usr/bin/rsync --daemon
?
檢查進程是否存在,
ps -aux |grep rsync root 4406 0.0 0.0 4228 588 ? Ss May14 0:00 /usr/local/bin/rsync --daemon netstat -an |grep 873 tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN出現以上結果,表明rsync服務器端已經啟動。
?
打開防火墻
iptables -i INPUT -p tcp --dport 873 -j ACCEPT
iptables -L
結果如下
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT tcp -- anywhere anywhere tcp dpt:rsync
?
rsync開機啟動
echo "/usr/bin/rsync --daemon" >> /etc/rc.local
(開機自動啟動rsync服務)
注意事項
1) 提示密碼文件不能讀,需要手工輸入密碼時,可能就是密碼文件權限不是600,或者格式不對,或者是路徑不對。
2) 提示要創建新目錄或文件傳輸失敗時,可能是欲同步的目錄沒有權限,最好把欲同步的目錄權限修改為744。
3) 從客戶端同步文件到服務端時,最好單個文件目錄傳輸,否則易出錯。
?
2、客戶端
客戶端就不用啟動rsync服務了
shell>vi /etc/rsyncd.secrets #這里也是創建密碼文件,文件名字自定義,但內容只需要填目標服務授權密碼
內容:
rsync //這里是服務端rsync服務授權密碼shell>chmod 0600 /etc/rsyncd.secrets #更改文件權限
?
測試:(在服務端/home/source/ 先創建test文件 )
shell>rsync -vzrtopg --delete --progress --password-file=/etc/rsyncd.secrets?rsync@192.168.1.2::backup?/home/receive
v:傳輸時的進度等信息, z:表示壓縮, r:是遞歸, t:保持文件原有時間, o:保持文件原有屬主, P:傳輸進度, g:保持文件原有用戶組
--progress 指顯示
--delete 指如果服務器端刪除了這一文件,那么客戶端也相應把文件刪除,保持真正的一致
--password-file=/etc/rsyncd.secrets 認證密碼
rsync 認證用戶
backup 認證模塊
如果客戶端/home/receive/下產生了test文件代表同步成功
下一步寫一個腳本文件實現真正的同步......
啟動腳本:rsync.sh?
shell>vi /home/rsync.sh
| #!/bin/sh rsync -vzrtopg --delete --progress --password-file=/etc/rsyncd.secrets rsync@192.168.1.2::backup /home/receive |
關閉腳本:killrsync.sh
shell>vi /home/killrsync.sh
#!/bin/shRSYNC_PID=`ps auxww|grep rsync |grep -v grep|awk '{print $2}'`
kill -9 $RSYNC_PID
設置定時任務 (crontab?google can help you!)
shell>crontab -e
SHELL=/bin/shPATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
0 3 * * * /home/rsync.sh //每天晚上3點執行一次同步
0 6 * * * /home/killrsync.sh //每天早上6點強制終止同步(如果還沒完成)
shell>crontab -l #可以查看任務設置情況
shell>service crond status #查看crond是否已啟動,若啟動了能看到PID
shell>service crond start #啟動crond服務
?
?
--------------------------------------------------------
rsync常見錯誤排錯
1.
rsync: failed to connect to 118.244.216.177: No route to host (113)
rsync error: error in socket IO (code 10) at clientserver.c(124) [receiver=3.0.6]
原因:防火墻屏蔽了端口
解決:打開873段考
iptables -i INPUT -p tcp --dport 873 -j ACCEPT
iptables -L
如果以上指令不行,可以直接停掉防火墻
/etc/init.d/iptables stop
2.
@ERROR: auth failed on module backup
rsync error: error starting client-server protocol (code 5) at main.c(1506) [Receiver=3.0.7]
那估計是密碼文件沒有設置權限哦: chmod 600 /home/admin/security/rsync.pass
應該差不多就可以了
3.@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)
這是因為密碼設錯了, 無法登入成功, 請檢查一下 rsyncd.scrt 中的密碼, 二端是否一致?
4.password file must not be other-accessible
continuing without password file
Password:
這表示 rsyncd.scrt 的檔案權限屬性不對, 應設為 600。
5.@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)
這通常是您的 rsyncd.conf 中的 path 路徑所設的那個目錄并不存在所致.請先用 mkdir開設好要備份目錄
6.@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)
最后原因終于找到了。因為有兩個網段都需要同步該文件夾內容,但沒有在hosts allow 后面添加另一個IP段
hosts allow = 192.168.1.0/24
改為
hosts allow = 192.168.1.0/24 192.168.2.0/24
重新啟動rsync服務,問題解決
7.@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.scrt
8.rsync: recv_generator: mkdir "/teacherclubBackup/rsync……" failed: No space left on device (28)
*** Skipping any contents from this failed directory ***
磁盤空間滿了
9.rsync: opendir "/kexue" (in dtsChannel) failed: Permission denied (13)
同步目錄的權限設置不對,改為755
10.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]
未啟動xinetd守護進程
[root@CC02 /]# service xinetd start
11.rsync: unable to open configuration file "/etc/rsyncd.conf": No such file or directory
xnetid查找的配置文件位置默認是/etc下,在/etc下找不到rsyncd.conf文件
12.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測試
13.我需要在防火墻上開放哪些端口以適應rsync?
視情況而定。rsync可以直接通過873端口的tcp連接傳文件,也可以通過22端口的ssh來進行文件傳遞,但你也可以通過下列命令改變它的端口:
rsync --port 8730 otherhost::
或者
rsync -e 'ssh -p 2002' otherhost:
14.我如何通過rsync只復制目錄結構,忽略掉文件呢?
rsync -av --include '*/' --exclude '*' source-dir dest-dir
15.為什么我總會出現"Read-only file system"的錯誤呢?
看看是否忘了設"read only = no"了
16.@ERROR: chroot failed
rsync error: error starting client-server protocol (code 5) at main.c(1522) [receiver=3.0.3]
原因:
服務器端的目錄不存在或無權限。創建目錄并修正權限可解決問題。
17.@ERROR: auth failed on module tee
rsync error: error starting client-server protocol (code 5) at main.c(1522) [receiver=3.0.3]
原因:
服務器端該模塊(tee)需要驗證用戶名密碼,但客戶端沒有提供正確的用戶名密碼,認證失敗。提供正確的用戶名密碼解決此問題。
18.@ERROR: Unknown module ‘tee_nonexists’
rsync error: error starting client-server protocol (code 5) at main.c(1522) [receiver=3.0.3]
原因:
服務器不存在指定模塊。提供正確的模塊名或在服務器端修改成你要的模塊以解決問題。
19.權限無法復制。去掉同步權限的參數即可。(這種情況多見于Linux向Windows的時候)
總結
以上是生活随笔為你收集整理的rsync实现文件同步的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 微信小号一般隐藏在哪
- 下一篇: 网友花2499元就买到了努比亚Z50:系