在CentOS7上配置rsync源服務器+inotify實時同步

在CentOS7上配置rsync源服務器+inotify實時同步

2018年08月07日 11:21:45 沙克頓巴斯 閱讀數:93 標籤: rsync inotifyhtml

概述c++

rsync是一個開源的快速備份工具,能夠再不一樣主機之間鏡像同步整個目錄樹,支持增量備份,保持連接和權限,且採用優化的同步算法,再傳輸前執行壓縮,所以很是適用於異地備份、鏡像服務器等應用。算法

原理vim

再遠程同步任務中,負責發起rsync同步操做的客戶機稱爲發起端,而負責響應來自客戶機的rsync同步操做的服務器稱爲同步源。再同步過程當中,同步源負責提供文檔的原始位置,而發起端對該位置具備讀取權限,如圖所示:bash

配置rsync源服務器服務器

1.檢查rsync是否安裝tcp

 
  1. [root@localhost ~]# rpm -q rsync工具

  2. rsync-3.0.9-18.el7.x86_64優化

2.修改rsync默認配置文件,位於/etc/rsyncd.conf。ui

 
  1. 插入如下內容

  2. uid = nobody

  3. gid = nobody

  4. use chroot = yes //禁錮在源目錄//

  5. address = 192.168.126.138 //監聽地址//

  6. port 873 //監聽端口//

  7. log file = /var/log/rsyncd.log //日誌文件位置//

  8. pid file = /var/run/rsyncd.pid //存放進程ID的文件位置//

  9. hosts allow = 192.168.126.0/24 //容許訪問的客戶機地址//

  10. # max connections = 4

  11. # pid file = /var/run/rsyncd.pid

  12. # exclude = lost+found/

  13. # transfer logging = yes

  14. # timeout = 900

  15. # ignore nonreadable = yes

  16. # dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2

  17. [wwwroot] //共享模塊名稱//

  18. path = /var/www/html //源目錄的實際路徑//

  19. read only = no //是否爲只讀//

  20. dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2 //同步時再也不壓縮的文件類型//

  21. auth users = backuper //受權帳戶//

  22. secrets file = /etc/rsyncd_users.db //存放帳戶信息的數據文件//

3.爲備份帳戶建立數據文件

根據上一步的設置,建立帳號數據文件,添加一行用戶記錄,以冒號分隔,用戶名稱爲backup,密碼爲abc123。因爲帳號信息採起明文存放,所以應調整文件權限,避免帳號信息泄露。

 
  1. [root@localhost ~]#vim /etc/rsyncd_users.db

  2. backuper:abc123

  3. [root@localhost ~]#chmod 600 /etc/rsyncd_users.db

4.開啓rsync服務,運行參數爲 --daemon。

 
  1. [root@localhost opt]# systemctl stop firewalld.service

  2. [root@localhost opt]# setenforce 0

  3. [root@localhost opt]# rsync --daemon

  4. [root@localhost opt]# netstat -ntap | grep rsync

  5. tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 2934/rsync

使用rsync備份工具

配置源的方法:

在執行運程同步任務時,rsync命令須要指定同步源服務器中的資源位置。rsync同步源的資源表示方式爲「用戶名@主機地址::共享模塊名」或者「rsync://用戶名@主機地址/共享模塊名」,前者爲兩個冒號分隔形式,後者爲URL地址形式。

 
  1. rsync -avz backuper@192.168.175.129::wwwroot /opt/

  2. rsync -avz rsync://backuper@192.168.175.129/wwwroot /opt/

1.執行如下操做將源服務器中的wwwroot共享模塊,下載到客戶機的本地/var/www/html目錄下。

 
  1. 源服務器:

  2. [root@localhost ~]# cd /var/www/html/

  3. [root@localhost html]# echo "123" > 111.txt

  4. [root@localhost html]# echo "456" > 222.txt

  5. [root@localhost html]# ls

  6. 111.txt 222.txt

  7.  
  8. 客戶端:

  9. [root@localhost opt]# rsync -avz backuper@192.168.126.138::wwwroot/ ./ //下載到當前目錄//

  10. Password:

  11. receiving incremental file list

  12. ./

  13. 111.txt

  14. 222.txt

  15.  
  16. sent 102 bytes received 221 bytes 23.93 bytes/sec

  17. total size is 8 speedup is 0.02

  18. [root@localhost html]# ls

  19. 111.txt 222.txt

  20.  

2.在客戶端上傳文件到源服務器

 
  1. [root@localhost opt]#mkdir b1 b2 b3 b4

  2. [root@localhost opt]# ls

  3. b1 b2 b3 b4 b5 rh

  4. [root@localhost opt]# rsync -avz backuper@192.168.126.138::wwwroot/ ./ #上傳

  5. Password:

  6. receiving incremental file list

  7. ./

  8. 111.txt

  9. 222.txt

  10.  
  11. sent 102 bytes received 221 bytes 23.93 bytes/sec

  12. total size is 8 speedup is 0.02 #上傳成功

  13.  
  14. 源服務器上查看:

  15. [root@localhost ~]# cd /var/www/html/

  16. [root@localhost html]# ls

  17. b1 b2 b3 b4 b5 rh

  18.  
 
  1. 注意:

  2. 上傳前須要把源服務器rsync的配置文件rsyncd.conf中的uid、gid修改成root

配置rsync+inotify實施同步

將rsync工具與inotify機制相結合,能夠實現觸發式備份(實時同步)——只要原始位置的文檔發生變化,就當即啓動增量備份操做,如圖所示,不然處於靜默等待狀態。這樣,就避免了按固定週期備份時存在的延遲性、週期過密等問題。

正由於inotify通知機制由Linux內核提供,所以要作本機監控,在觸發式備份中應用時更適合上行同步。下面一次介紹其配置過程。

1.調整inotify內核參數

當要監控的目錄、文件數量較多或者變化較頻繁時,建議加大這三個參數的值。能夠直接修改/etc/sysctl.conf的配置文件,將管理隊列、實例數、監控數進行設置。

 
  1. [root@localhost html]# vim /etc/sysctl.conf

  2. # For more information, see sysctl.conf(5) and sysctl.d(5). //添加//

  3. fs.inotify.max_queued_events = 16384

  4. fs.inotify.max_user_instances = 1024

  5. fs.inotify.max_user_watches = 1048576

  6. [root@localhost html]# sysctl -p //啓動//

  7. fs.inotify.max_queued_events = 16384

  8. fs.inotify.max_user_instances = 1024

  9. fs.inotify.max_user_watches = 1048576

2.安裝inotifi-tools

使用inotify機制還須要安裝inotifi-tools,以便提供inotifywait和inotifywatch輔助工具程序,用來監控和彙總改動狀況。

 
  1. [root@localhost rs]# tar zxvf inotify-tools-3.14.tar.gz -C /opt/ #解包

  2. [root@localhost opt]# cd inotify-tools-3.14/

  3. [root@localhost inotify-tools-3.14]# yum install gcc gcc-c++ make -y #安裝編譯軟件

  4. [root@localhost inotify-tools-3.14]# ./configure

  5. [root@localhost inotify-tools-3.14]#make && make install

3.編寫觸發式同步腳本

 
  1. root@localhost opt]# vim inotify.sh

  2.  
  3. #!/bin/bash

  4. INOTIFY_CMD="inotifywait -mrq -e modify,create,attrib,move,delete /var/www/html/"

  5. RSYNC_CMD="rsync -azH --delete --password-file=/etc/server.pass /var/www/html/ backuper@192.168.126.138::wwwroot/"

  6. $INOTIFY_CMD | while read DIRECTORY EVENT FILE

  7. do

  8. if [ $(pgrep rsync | wc -l) -le 0 ] ; then

  9. $RSYNC_CMD

  10. fi

  11. done

上述腳本用來檢測本機/var/www/html目錄的變更狀況,一旦有更新觸發rsync同步操做,上傳備份至服務器192.168.126.138的/var/www/html目錄下。

4.驗證

1).在源服務器運行inotifywait -mrq -e modify,create,move,delete /var/www/html/

[root@localhost html]# inotifywait -mrq -e modify,create,move,delete /var/www/html/ //靜默等待狀態//

2)打開源服務器的另外一個窗口在/var/www/html目錄下建立新的文件

 
  1. [root@localhost ~]# cd /var/www/html/

  2. [root@localhost html]# ls

  3. [root@localhost html]# echo "this is 111" > 111.txt //寫入文件//

  4. [root@localhost html]# echo "this is 222" > 222.txt

  5. [root@localhost html]# rm -rf 111.txt //刪除文件//

3)查看源服務器的更新觸發狀態

 
  1. [root@localhost html]# inotifywait -mrq -e modify,create,move,delete /var/www/html/

  2. /var/www/html/ CREATE 111.txt

  3. /var/www/html/ MODIFY 111.txt

  4. /var/www/html/ CREATE 222.txt

  5. /var/www/html/ MODIFY 222.txt

  6. /var/www/html/ DELETE 111.txt

  7. //監控端已有反饋//

4)查看服務器中的/var/www/html目錄下的變化狀況

 
  1. [root@localhost html]# ls

  2. 111.txt 222.txt

  3. [root@localhost html]# ls

  4. 222.txt

實驗成功

相關文章
相關標籤/搜索