1. 程式人生 > >rsync+inotify實現代碼實時同步

rsync+inotify實現代碼實時同步

rsync inotify 實時同步

rsync

rsync是lunix系統下的數據鏡像備份工具。使用快速增量備份工具Remote Sync可以遠程同步,支持本地復制,或者與其他SSH、rsync主機同步。

優點:

1)、可以鏡像保存整個目錄樹和文件系統。

2)、可以很容易做到保持原來文件的權限、時間、軟硬鏈接等等。

3)、無須特殊權限即可安裝。

4)、快速:第一次同步時 rsync 會復制全部內容,但在下一次只傳輸修改過的文件。rsync 在傳輸數據的過程中可以實行壓縮及解壓縮操作,因此可以使用更少的帶寬。

5)、安全:可以使用scp、ssh等方式來傳輸文件,當然也可以通過直接的socket連接。6)、支持匿名傳輸,以方便進行網站鏡象。

缺點:

1)、同步數據,需要掃描所有文件進行對比,才進行差量傳輸。如果文件數量達到百萬甚至千萬級,掃描文件對比文件將非常耗時,降低了rsync效率。

2)、rsync不能實時地區監測、同步數據。雖然可以通過守護進程方式觸發同步,但兩次動作間有時間差,導致數據不一致,無法應對出現故障時完全恢復數據。


inotify

inotify 是Linux 的一個內核特性,是一種強大的、細粒度的、異步的文件系統事件監控機制。它可以監控文件系統中的添加、刪除、修改、移 動等各種細微事件,並且及時向專門的應用程序發出相關的事件警告,比如刪除、讀、寫和卸載操作等。


rsync+inotify

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



搭建環境

拓撲圖如圖1所示

技術分享圖片

圖1


開始配置


1、先把兩臺機的防火墻和selinux關閉,兩臺機之間配置ssh免密鑰通信,並配置時間同步

##配置ssh免密鑰通信

[root@centos1 ~]# ssh-keygen 
[root@centos1 ~]# ssh-copy-id 192.168.15.12
##配置時間同步
[root@centos1 ~]# yum install -y ntpdate
[root@centos1 ~]# crontab -e
* * * * * ntpdate -u 0.pool.ntp.org


2、在兩臺服務器上安裝rsync

[root@centos1 ~]# yum install -y rsync
[root@centos2 ~]# yum install -y rsync


3、配置rsync文件

(1)、在CentOS1上配置


[root@centos1 ~]# mkdir /rsync     ##創建目錄(需要同步的內容)
[root@centos1 ~]# vim /etc/rsyncd.conf   ##配置主配置文件
uid = root
gid = root
usechroot = no
max connections = 20
timeout = 600
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
[web_log]
path = /rsync/  ##修改為剛剛創建的目錄
ignore errors
read only = false
writeonly = false
list = false
hosts allow = 192.168.15.0/24
hosts deny = 0.0.0.0/32
auth users = backuser
secrets file = /etc/rsync.passwd


[root@centos1 ~]# cat /etc/rsync.passwd       ##配置密碼文件
123456
[root@centos1 ~]# chmod 600 /etc/rsync.passwd   ##給密碼文件授權
[root@centos1 ~]# rsync --daemon        ##啟動rsync
[root@centos1 ~]# ps -ef | grep rsync     ##查看rsync是否啟動成功
root       1304      1  0 09:25 ?        00:00:00 rsync --daemon
root       1330   1083  0 09:29 pts/0    00:00:00 grep --color=auto rsync


(2)、在CentOS2上配置

[root@centos1 ~]# mkdir /rsync
[root@centos1 ~]# vim /etc/rsyncd.conf
uid = root                            
gid = root                            
usechroot = no                    
max connections = 20           
timeout = 600                      
pid file = /var/run/rsyncd.pid           
lock file = /var/run/rsync.lock          
log file = /var/log/rsyncd.log            
[web_log]                           
path = /rsync/            
ignore errors                        
read only = false                   
writeonly = false                   
list = false                           
hosts allow = 192.168.15.0/24    
hosts deny = 0.0.0.0/32                    
auth users = backuser               
secrets file = /etc/rsync.password

[root@centos2 ~]# cat /etc/rsync.password     ##創建密碼
backuser:123456
[root@centos2 ~]# chmod 600 /etc/rsync.password    ##授權
[root@centos2 ~]# rsync --daemon       ##啟動
[root@centos2 ~]# ps -ef | grep rsync
root      10732      1  0 09:29 ?        00:00:00 rsync --daemon
root      10734   1085  0 09:29 pts/0    00:00:00 grep --color=auto rsync


(3)、推送測試

##在CentOS1上創建文件
[root@centos1 ~]# cd /rsync/
[root@centos1 rsync]# touch {1..10}.txt
[root@centos1 rsync]# ls
10.txt  1.txt  2.txt  3.txt  4.txt  5.txt  6.txt  7.txt  8.txt  9.txt
##到CentOS2上查看
[root@centos2 ~]# cd /rsync/
[root@centos2 rsync]# ls

推送測試如圖2所示

技術分享圖片

圖2

到CentOS2上查看,如圖3所示

技術分享圖片

圖3

至此,rsync就已經配置完成了,接下來配置inotify了


4、配置inotify

(1)、安裝inotify

[root@centos1 src]# wget http://cloud.github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz     ##下載安裝包
[root@centos1 src]# tar -xf inotify-tools-3.14.tar.gz 
[root@centos1 src]# cd inotify-tools-3.14
[root@centos1 inotify-tools-3.14]# ./configure --prefix=/usr/local/inotify
[root@centos1 inotify-tools-3.14]# make && make install
[root@centos1 inotify-tools-3.14]# ln -s /usr/local/src/inotify-tools-3.14 /usr/local/inotify


(2)、配置監控腳本

[root@centos1 src]# cat inotify.sh 
#!/bin/bash
/usr/local/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e modify,delete,create,attrib /rsync/ | while read file
do
/usr/bin/rsync -vzrtopg --progress /rsync/ [email protected]::web_log --password-file=/etc/rsync.password
/usr/bin/rsync -vzrtopg --delete --progress /rsync/ [email protected]::web_log --password-file=/etc/rsync.password
echo "${files} was rsynced" >> /var/log/rsync.log 2>&1
done

[root@centos1 src]# chmod a+x inotify.sh     ##給執行權限
[root@centos1 src]# crontab -e               ##加入計劃任務
* * * * * bash /usr/local/src/inotify.sh


5、測試腳本

(1)、添加文件

在CentOS1上執行腳本,再另外開一個終端登錄到CentOS1上,創建文件,如圖4和圖5所示;在CentOS2上查看,如圖6所示

技術分享圖片

圖4

技術分享圖片

圖5

技術分享圖片

圖6


(2)、刪除文件

在CentOS1上執行腳本,再另外開一個終端登錄到CentOS1上刪除文件,如圖7和圖8所示;在CentOS2上查看,如圖9所示

技術分享圖片

圖7

技術分享圖片

圖8

技術分享圖片

圖9

通過對比,我們發現當CentOS1添加文件時,同時也會把文件同步到CentOS2,當CentOS1刪除某個文件時,CentOS2上相對應的文件也會被刪除。


6、測試

在CentOS1上創建文件名為1-5的txt文本,如圖10所示,同時在CentOS2上查看,如圖11所示,我們可以發現添加的計劃任務已經成功了,可以實現兩臺機之間的實時同步。

技術分享圖片

圖10

技術分享圖片

圖11
























rsync+inotify實現代碼實時同步