linux rsync +inotify 實現 實時同步

Dus發表於2015-06-22

前言:
     rsync可以實現觸發式的檔案同步,但是通過crontab守護程式方式進行觸發,同步的資料和實際資料會有差異,而inotify可以監控檔案系統的各種變化,當檔案有任何變動時,就觸發rsync同步,這樣剛好解決了同步資料的實時性問題。

一、基本環境

系統:CentOS 2.6.32-220.el6.x86_64
軟體包版本:rsync-3.0.6-12.el6.x86_64 
                   inotify-tools-3.14 
下載連結:百度   inotify-tools-3.14  

服務端(server):172.16.1.1
客服端(client1):172.16.1.2
         (client2):172.16.1.3

二、客戶端配置(172.16.1.2、172.16.1.3)

1. client1 172.16.1.2配置

安裝rsync
#yum install -y rsync xinetd

在/etc/目錄下建立rsyncd.conf配置檔案進行編輯
#vim /etc/rsyncd.conf 

uid = nobody         //rsyncd 守護程式執行系統使用者全域性配置,也可在具體的塊中獨立配置,
gid = nobody         //rsyncd 守護程式執行系統使用者全域性配置,也可在具體的塊中獨立配置,
use chroot = no    //允許 chroot,提升安全性,客戶端連線模組,首先chroot到模組path引數指定的目錄下 #chroot為yes時必須使用root許可權,且不能備份path路徑外的連結檔案
max connections = 10  //最大連線數
strict modes = yes   //是否檢查口令檔案的許可權
pid file = /var/run/rsyncd.pid  //pid檔案的存放位置
lock file = /var/run/rsync.lock  //支援max connections引數的鎖檔案
log file = /var/log/rsyncd.log   //日誌檔案位置,啟動rsync後自動產生這個檔案,無需提前建立
[lixuan]   //自定義名稱
path = /data/lixuan/          #本地自定義路徑
comment = client file   //模組名稱與自定義名稱相同
ignore errors             //忽略錯誤
read only = no         //設定rsync服務端檔案為讀寫許可權
write only = no        //設定rsync服務端檔案為讀寫許可權
hosts allow = 172.16.1.1       #服務端IP地址
hosts deny = *                    //止資料同步的客戶端IP地址,可以設定多個,用英文狀態下逗號隔開
list = false                           //不顯示rsync服務端資源列表
uid = root                           //設定rsync執行許可權為root
gid = root                          //設定rsync執行許可權為root
auth users = root               //模組驗證使用者名稱稱,可使用空格或者逗號隔開多個使用者名稱
secrets file = /etc/client1.pass     #自動同步密碼檔案

新建/etc/client1.pass  檔案
#echo "root:123456" >>/etc/client1.pass
#chmod -R 600 /etc/client1.pass   #這個 必須是 600許可權  要不就會提示 找不到檔案


啟動rsync服務
#/etc/init.d/xinetd start

#rsync --daemon --config=/etc/rsync.conf

2. client2 172.16.1.3配置

安裝rsync
#yum install -y rsync xinetd

在/etc/目錄下建立rsyncd.conf配置檔案進行編輯
#vim /etc/rsyncd.conf 

uid = nobody
gid = nobody
use chroot = no
max connections = 10
strict modes = yes
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
[lixuan]
path = /data/lixuan/          #本地自定義路徑
comment = client file
ignore errors
read only = no
write only = no
hosts allow = 172.16.32.204      #服務端IP地址
hosts deny = *
list = false
uid = root
gid = root
auth users = root
secrets file = /etc/client2.pass     #自動同步密碼檔案

儲存退出!

新建/etc/client2.pass  檔案
#echo "root:123456" >>/etc/client2.pass
chmod -R 600 /etc/client2.pass #這個 必須是 600許可權  要不就會提示 找不到檔案


啟動rsync服務
#/etc/init.d/xinetd start

#rsync --daemon --config=/etc/rsync.conf

三、服務端配置(172.16.1.1)

1.安裝rsync 

#yum install -y rsync xinetd


在/etc/目錄下建立rsyncd.conf配置檔案進行編輯
#vim /etc/rsyncd.conf 

uid = root
gid = root
use chroot = no
max connections = 100
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
secrets file = /etc/server.pass
[lixuan]
path = /data/lixuan/
auth users = root
list = no
read only = no
secrets file = /etc/servers.pass
comment = server directory

儲存退出!

新建/etc/server.pass  檔案
#echo "root:123456" >>/etc/server.pass
#chmod -R 600 /etc/server.pass #這個 必須是 600許可權  要不就會提示 找不到檔案


啟動rsync服務
#/etc/init.d/xinetd start

2. 安裝inotify

驗證核心是否支援inotify
#uname -r
2.6.32-220.el6.x86_64

#ll /proc/sys/fs/inotify
total 0
-rw-r--r-- 1 root root 0 Jun 11 10:15 max_queued_events    #表示呼叫inotify_init時分配給inotify instance中可排隊的event的數目的最大值
-rw-r--r-- 1 root root 0 Jun 11 10:15 max_user_instances     #表示每一個real user ID可建立的inotify instatnces的數量上限
-rw-r--r-- 1 root root 0 Jun 11 10:15 max_user_watches      #表示每個inotify instatnces可監控的最大目錄數量

 


配置服務端內容釋出指令碼
#vim /data/sh/inotifyrsync.sh

#!/bin/bash
client1=172.16.1.2
client2=172.16.1.3
src=/data/lixuan/
dst=lixuan
user=root

/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e close_write,modify,delete,create,attrib $src | while read files
        do
  /usr/bin/rsync -vzrtopg --delete --progress --password-file=/etc/client.pass $src $user@$client1::$dst
  /usr/bin/rsync -vzrtopg --delete --progress --password-file=/etc/client.pass $src $user@$client2::$dst
                echo "${files} was rsynced" >>/tmp/rsync.log 2>&1
         done

儲存退出!

新建/etc/client.pass  檔案
#echo "123456" >>/etc/client.pass
#chmod -R 600 /etc/client.pass #這個 必須是 600許可權  要不就會提示 找不到檔案

                                                                
賦予指令碼執行許可權
#chmod 755 /data/sh/inotifyrsync.sh

後臺執行指令碼
#sh /data/sh/inotifyrsync.sh &

將此指令碼加入開機自啟動檔案中
#echo "/data/sh/inotifyrsync.sh &"  >> /etc/rc.local

#rsync --daemon --config=/etc/rsync.conf

四、測試rsync+inotify資料實時同步

      在服務端(172.16.1.1)的/data/lixuan/目錄中新增刪除目錄或檔案,然後進入客戶端(172.16.1.1、172.16.1.2)的/data/lixuan/目錄中檢視是否和服務端資料實時保持一致。

 

參考文章 http://www.tuicool.com/articles/IB3I7rm

相關文章