rsync + lsyncd 檔案實時同步/備份

u013378306發表於2017-05-25

lsyncd 在哪臺伺服器啟動,這臺伺服器就像當於是同步伺服器,其他伺服器如果多了檔案就會刪除,少了檔案就會從這臺伺服器同步下來。

修改檔案內容也會同步

rsync 可實現檔案的同步/備份,安裝配置移步linux 下 rsync 備份/同步檔案

lsyncd 實現了觸發式或定時通知事件,可以近實時的同步檔案(封裝了rsync),github地址:https://github.com/axkibe/lsyncd


系統:centos6.4

主伺服器:192.168.1.136

同步伺服器:192.168.1.137


在 192.168.1.136 上安裝 lsyncd 

安裝依賴(lsyncd 使用 lua 語言寫的)

[plain] view plain copy
 print?
  1. yum install lua lua-devel  

在github 上下載 lsyncd-master.zip 

[plain] view plain copy
 print?
  1. unzip lsyncd-master.zip   
  2. cd lsyncd-master  
  3. cmake -DCMAKE_INSTALL_PREFIX=/usr/local/lsyncd  
  4. make && make install  

建立配置檔案

[plain] view plain copy
 print?
  1. cd /usr/local/lsyncd/  
  2. mkdir ./etc  
  3. mkdir ./var  
  4. vi etc/lsyncd.conf  

內容為( -- 是 lua 中的註釋符)

[plain] view plain copy
 print?
  1. -- 全域性配置   
  2. settings {  
  3.     -- 日誌檔案存放位置  
  4.     logfile ="/usr/local/lsyncd/var/lsyncd.log",  
  5.   
  6.     -- 狀態檔案存放位置  
  7.     statusFile ="/usr/local/lsyncd/var/lsyncd.status",  
  8.   
  9.     -- 將lsyncd的狀態寫入上面的statusFile的間隔,預設10秒  
  10.     --statusInterval = 10  
  11.   
  12.     -- 是否啟用守護模式,預設 true  
  13.     --nodaemon=true   
  14.   
  15.     -- inotify監控的事件 ,預設是 CloseWrite,還可以是 Modify 或 CloseWrite or Modify  
  16.     inotifyMode = "CloseWrite",  
  17.   
  18.     -- 最大同步程式  
  19.     maxProcesses = 8,  
  20.   
  21.     --累計到多少所監控的事件啟用一次同步,即使後面的delay延遲時間還未到  
  22.     --maxDelays = 1  
  23. }  
  24.   
  25. -- 遠端目錄同步  
  26. sync {  
  27.     -- rsync , rsyncssh , direct 三種模式  
  28.     default.rsync,  
  29.   
  30.     -- 同步的源目錄,使用絕對路徑。  
  31.     source = "/home/wwwroot/attachments",  
  32.   
  33.     -- 定義目的地址.對應不同的模式有幾種寫法,這裡使用遠端同步的地址,rsync中的地址  
  34.     target = "rsync137@192.168.1.137::rsyncd",  
  35.   
  36.     -- 預設 true ,允許同步刪除。還有 false, startup, running 值  
  37.     --delete = true,  
  38.   
  39.     -- 哪些檔案不同步  
  40.     exclude = { ".*" },  
  41.   
  42.     -- 累計事件,等待rsync同步延時時間,預設15秒,最大累計到1000個不可合併的事件(1000個檔案變動),  
  43.     delay = 15,  
  44.   
  45.     -- 預設 true 當init = false ,只同步程式啟動以後發生改動事件的檔案,原有的目錄即使有差異也不會同步  
  46.     --init = true,  
  47.   
  48.     -- rsync 的配置  
  49.     rsync = {  
  50.         -- rsync 的二進位制處理檔案  
  51.         binary = "/usr/bin/rsync",  
  52.   
  53.     -- 歸檔模式  
  54.         archive = true,  
  55.   
  56.     -- 壓縮傳輸  
  57.         compress = true,  
  58.   
  59.     -- 增量  
  60.         verbose   = true,  
  61.   
  62.     -- 密碼檔案  
  63.         password_file = "/etc/rsyncd.pwd",  
  64.   
  65.     -- 其他 rsync 的配置引數, 限速(--bwlimit KBPS),使用 rsync -v 檢視詳細引數  
  66.         -- _extra    = {"--bwlimit=200"}  
  67.     }  
  68. }  

更多詳細配置,請看文件 https://github.com/axkibe/lsyncd/wiki/Lsyncd%202.1.x%20%E2%80%96%20The%20Configuration%20File

建立密碼檔案(內容為192.168.1.137 上使用者‘rsync137’對應的密碼 123456)

[plain] view plain copy
 print?
  1. vim /etc/rsyncd.pwd  
  2. chmod 600 /etc/rsyncd.pwd  

將 lsyncd 加入到環境目錄(做個軟鏈)

[plain] view plain copy
 print?
  1. ln -s /usr/local/lsyncd/bin/lsyncd /usr/bin/lsyncd  


在192.168.1.137安裝配置 rsync ,參考這裡linux 下 rsync 備份/同步檔案

配置可簡單點

[plain] view plain copy
 print?
  1. uid = root  
  2. gid = root  
  3.   
  4. hosts allow = 192.168.1.136  
  5.   
  6. use chroot = no  
  7. # 最大連結數  
  8. max connections = 10  
  9.   
  10. # pid檔案的存放  
  11. pid file = /etc/rsyncd/rsyncd.pid  
  12. # max connections 引數的鎖檔案的存放位置  
  13. lock file = /etc/rsyncd/rsync.lock  
  14.   
  15. # 模組名 自定義  
  16. [rsyncd]  
  17. path = /home/wwwroot/attachments  
  18. ignore errors = true  
  19. # 這裡要允許寫入  
  20. read only = false   
  21.   
  22. list = no   
  23. auth users = rsync137  
  24. # 指定認證檔案  
  25. secrets file = /etc/rsyncd/rsyncd.secrets  
  26. strict modes = true  

認證檔案內容為(/etc/rsyncd/rsyncd.secrets)

rsync137:123456


啟動 lsyncd 

[plain] view plain copy
 print?
  1. lsyncd -log Exec /usr/local/lsyncd/etc/lsyncd.conf  


測試

192.168.1.136 上建立了一個t1的資料夾,和上傳了張 t1.jpg圖片


還未同步時(配置的15秒延遲),192.168.1.137 上結構如下圖



同步後如下



相關文章