rsync 做檔案同步

u013378306發表於2017-05-25



rsync 是一個遠端同步工具,可以用來備份資料庫,或者圖片伺服器的圖片

修改檔案內容也會同步

注意:這裡設定的賬戶密碼和 系統賬戶密碼沒有任何關係,時rsync 賬戶密碼,需要確定開啟873埠,預設使用873埠


系統:centos6.4

主伺服器IP:192.168.1.136

備份伺服器IP:192.168.1.137

一、安裝

檢視是否安裝,沒有安裝就yum 安裝一下

[plain] view plain copy
 print?
  1. rpm -qa | grep rsync  

[plain] view plain copy
 print?
  1. yum install rsync  

二、伺服器端配置


為方便統一管理,將rsync 相關檔案放在一個資料夾下【 rsyncd 】

[plain] view plain copy
 print?
  1. mkdir /etc/rsyncd  

建立 伺服器端 配置檔案

[plain] view plain copy
 print?
  1. vim /etc/rsyncd/rsyncd.conf  
內容/引數註釋如下

[plain] view plain copy
 print?
  1. # 使用者和使用者組  
  2. uid = root  
  3. gid = root  
  4.   
  5. # 允許訪問的客戶機  
  6. hosts allow = 192.168.1.137  
  7. #拒絕訪問的  
  8. #hosts deny = 0.0.0.0/32  
  9.   
  10. # 能否切換目錄  
  11. use chroot = no  
  12. # 最大連結數  
  13. max connections = 10  
  14.   
  15. # pid檔案的存放  
  16. pid file = /etc/rsyncd/rsyncd.pid  
  17. # max connections 引數的鎖檔案的存放位置  
  18. lock file = /etc/rsyncd/rsync.lock  
  19. # 使 rsync 伺服器將傳輸操作記錄到傳輸日誌檔案  
  20. transfer logging = true  
  21. log format = "%o %h [%a] %m (%u) %f %l"  
  22. ##########################################################  
  23. #   可以使用的日誌格式定義符如下所示:  
  24. #   %a - 遠端IP地址  
  25. #   %h - 遠端主機名  
  26. #   %l - 檔案長度字元數  
  27. #   %p - 該次 rsync 會話的 PID  
  28. #   %o - 操作型別:”send” 或 “recv”  
  29. #   %f - 檔名  
  30. #   %P - 模組路徑  
  31. #   %m - 模組名  
  32. #   %t - 當前時間  
  33. #   %u - 認證的使用者名稱(匿名時是 null)  
  34. #   %b - 實際傳輸的位元組數  
  35. #   %c - 當傳送檔案時,記錄該檔案的校驗碼  
  36. ##########################################################  
  37. # 日誌記錄檔案的存放  
  38. log file = /etc/rsyncd/rsyncd.log  
  39. # 歡迎資訊  
  40. # motd file = /etc/rsyncd/rsyncd.motd  
  41.   
  42. ## 模組  
  43. # 模組名 自定義  
  44. [rsyncd]  
  45. # 指定檔案目錄所在位置 [必須]  
  46. path = /home/wwwroot/attachments  
  47. # 註釋  
  48. comment = rsync files  
  49. # 指定在 rsync 伺服器上執行 delete 操作時是否忽略 I/O 錯誤。  
  50. # 一般來說 rsync 在出現 I/O 錯誤時將跳過 –delete 操作,以防止因為暫時的資源不足或其它 I/O 錯誤導致的嚴重問題  
  51. ignore errors = true  
  52. # 指定是否允許客戶上傳檔案。若為 true 則不允許上傳;若為 false 並且伺服器目錄也具有讀寫許可權則允許上傳  
  53. read only = true   
  54. # 指定當客戶請求列出可以使用的模組列表時,該模組是否應該被列出  
  55. list = no   
  56. # 同步驗證時用的賬號,這裡的使用者和系統使用者沒有任何關係。  
  57. # 使用者名稱和口令以明文方式存放在 secrets file 引數指定的檔案中  
  58. auth users = rsync   
  59. # 指定認證檔案  
  60. secrets file = /etc/rsyncd/rsyncd.secrets  
  61. # 指定是否監測口令檔案的許可權,若為 true 則口令檔案只能被 rsync 伺服器執行身份的使用者訪問,其他任何使用者不可以訪問該檔案  
  62. strict modes = true  

配置檔案預設要在 /etc下

[plain] view plain copy
 print?
  1. ln -s /etc/rsyncd/rsyncd.conf /etc/rsyncd.conf  

建立認證檔案 格式 驗證名:驗證密碼 一行一個,不必是系統使用者

[plain] view plain copy
 print?
  1. vim /etc/rsyncd/rsyncd.secrets  

內容為  賬戶:密碼

[plain] view plain copy
 print?
  1. rsync:rsync123456  

設定認證檔案的許可權,不然會報錯

[plain] view plain copy
 print?
  1. chown root:root /etc/rsyncd/rsyncd.secrets  
  2. chmod 600 /etc/rsyncd/rsyncd.secrets  

開啟預設埠 873

[plain] view plain copy
 print?
  1. vim /etc/sysconfig/iptables  

加入

[plain] view plain copy
 print?
  1. -A INPUT -m state --state NEW -m tcp -p tcp --dport 873 -j ACCEPT  

重啟生效

[plain] view plain copy
 print?
  1. service iptables restart  

啟動 rsync

[plain] view plain copy
 print?
  1. rsync --daemon  

檢視是否啟動

[plain] view plain copy
 print?
  1. lsof -i:873  

看見這樣的資訊就是成功啟動了

[plain] view plain copy
 print?
  1. COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME  
  2. rsync   17978 root    4u  IPv4  93140      0t0  TCP *:rsync (LISTEN)  
  3. rsync   17978 root    5u  IPv6  93141      0t0  TCP *:rsync (LISTEN)  

三、客戶器端配置

按上述步驟安裝好rsync

無須啟動,直接執行語句 ,會把伺服器端的內容更新到本地,如果本地有多的檔案,會自動刪除

誰執行這個命令,誰就相當於客戶端。每臺機器配置都一樣。

rsync@192.168.1.136::rsyncd 中 ::後面的rsyncd為對方伺服器rsyncd.conf 中配置的 

## 模組  
# 模組名 自定義  
[rsyncd]  


[plain] view plain copy
 print?
  1. rsync -avz --progress --delete rsync@192.168.1.136::rsyncd /home/wwwroot/attachments --password-file=/etc/rsyncd.pwd  

--password-file 指定密碼檔案,存放密碼

rsyncd.pwd 內容為 (只需要密碼,此帳號密碼和系統賬戶密碼沒有關係)


[plain] view plain copy
 print?
  1. rsync123456  

更改rsyncd.pwd 的許可權,不然會報錯


chmod 600 /etc/rsyncd/rsyncd.secrets 


-a archive mode; equals -rlptgoD,歸檔模式,相當於-rlptgoD,保證檔案的屬性,建立時間,使用者組等資訊

-v 進度資訊

-z 壓縮傳輸

--delete 保證客戶端和伺服器端的資料完全一致,若伺服器檔案被刪除了,客戶端也會刪除

詳細的引數列表執行 rsync -h 檢視,一般來說 -avz 和 --delete 就可以滿足需求了


四、定時備份/同步

在客戶端設定定時執行同步

利用crontab 

若沒安裝 crontab 執行如下命令安裝

[plain] view plain copy
 print?
  1. yum install crontabs  


新增自啟動
[plain] view plain copy
 print?
  1. chkconfig crond on  

啟動

[plain] view plain copy
 print?
  1. service crond start  
新增定時任務
[plain] view plain copy
 print?
  1. crontab -e  

加入(一分鐘執行一次同步)

[plain] view plain copy
 print?
  1. */1 * * * * rsync -avz --progress --delete rsync@192.168.1.136::rsyncd /home/wwwroot/attachments --password-file=/etc/rsyncd.pass  

具體時間根據自己要求更改,比如每天凌晨1點執行一次同步,0 1 * * *

下圖是136 和137伺服器同步前和同步後的對比

192.168.1.136

192.168.1.137


在137 上執行同步後


images 檔案已被刪除,t6-6.jpg已同步下來


附:rsync 常用引數的具體解釋如下:

-v, --verbose 詳細模式輸出
-q, --quiet 精簡輸出模式
-c, --checksum 開啟校驗開關,強制對檔案傳輸進行校驗
-a, --archive 歸檔模式,表示以遞迴方式傳輸檔案,並保持所有檔案屬性,等於-rlptgoD
-r, --recursive 對子目錄以遞迴模式處理
-b, --backup 建立備份,也就是對於目的已經存在有同樣的檔名時,將老的檔案重新命名為~filename。可以使用--suffix選項來指定不同的備份檔案字首。
-suffix=SUFFIX 定義備份檔案字首
-u, --update 僅僅進行更新,也就是跳過所有已經存在於DST,並且檔案時間晚於要備份的檔案。(不覆蓋更新的檔案)
-l, --links 保留軟鏈結
-p, --perms 保持檔案許可權
-o, --owner 保持檔案屬主資訊
-g, --group 保持檔案屬組資訊
-t, --times 保持檔案時間資訊
-e, --rsh=COMMAND 指定使用rsh、ssh方式進行資料同步
--delete 刪除那些DST中SRC沒有的檔案
--delete-excluded 同樣刪除接收端那些被該選項指定排除的檔案
--delete-after 傳輸結束以後再刪除
--ignore-errors 及時出現IO錯誤也進行刪除
--force 強制刪除目錄,即使不為空
--timeout=TIME IP超時時間,單位為秒
--progress 顯示備份過程
-z, --compress 對備份的檔案在傳輸時進行壓縮處理
--exclude=PATTERN 指定排除不需要傳輸的檔案模式
--include=PATTERN 指定不排除而需要傳輸的檔案模式
--exclude-from=FILE 排除FILE中指定模式的檔案
--include-from=FILE 不排除FILE指定模式匹配的檔案

原文:http://blog.csdn.net/jam00/article/details/50923671

相關文章