rsync+inotify檔案實時同步
1、rsync
與傳統的cp、tar備份方式相比,rsync具有安全性高、備份迅速、支援增量備份等優點,通過rsync可以解決對實時性要求不高的資料備份需求,例如定期的備份檔案伺服器資料到遠端伺服器,對本地磁碟定期做資料映象等。
隨著應用系統規模的不斷擴大,對資料的安全性和可靠性也提出的更好的要求,rsync在高階業務系統中也逐漸暴露出了很多不足,首先,rsync同步資料 時,需要掃描所有檔案後進行比對,進行差量傳輸。如果檔案數量達到了百萬甚至千萬量級,掃描所有檔案將是非常耗時的。而且正在發生變化的往往是其中很少的 一部分,這是非常低效的方式。其次,rsync不能實時的去監測、同步資料,雖然它可以通過linux守護程序的方式進行觸發同步,但是兩次觸發動作一定 會有時間差,這樣就導致了服務端和客戶端資料可能出現不一致,無法在應用故障時完全的恢復資料。基於以上原因,rsync+inotify組合出現了!
2、inotify
Inotify 是一種強大的、細粒度的、非同步的檔案系統事件監控機制,linux核心從2.6.13起,加入了Inotify支援,通過Inotify可以監控檔案系統 中新增、刪除,修改、移動等各種細微事件,利用這個核心介面,第三方軟體就可以監控檔案系統下檔案的各種變化情況,而inotify-tools就是這樣 的一個第三方軟體。
在上面章節中,我們講到,rsync可以實現觸發式的檔案同步,但是通過crontab守護程序方式進行觸發,同步的資料和實際資料會有差異,而inotify可以監控檔案系統的各種變化,當檔案有任何變動時,就觸發rsync同步,這樣剛好解決了同步資料的實時性問題。
具體大家可以參照http://www.ibm.com/developerworks/cn/linux/l-ubuntu-inotify/index.html來進行學習。
接下面我們來開始進行rsync與inotify的安裝、配置、測試。
下面是2個伺服器的結構,分別為主機名、ip、狀態、核心、位數、同步的目錄,並將2臺伺服器均是redhat5.4發行版本。 一、主伺服器(server端,我這裡是nginx) 其中主伺服器需要安裝rsync與inotify,主伺服器作為server,向備份伺服器client傳輸檔案 1、安裝rsync- [root@nginx ~]# cd /usr/src/
- [root@nginx src]# ll
- total 16
- drwxr-xr-x 2 root root 4096 Jan 26 2010 debug
- drwxr-xr-x 2 root root 4096 Jan 26 2010 kernels
- [root@nginx src]# wget http://rsync.samba.org/ftp/rsync/src/rsync-3.0.9.tar.gz
- [root@nginx src]# tar zxvf rsync-3.0.9.tar.gz
- [root@nginx src]# cd rsync-3.0.9
- [root@nginx rsync-3.0.9]# ./configure --prefix=/usr/local/rsync
- [root@nginx rsync-3.0.9]# make
- [root@nginx rsync-3.0.9]# make install
- [root@nginx rsync-3.0.9]# cd /usr/local/rsync/
- [root@nginx rsync]# echo "rsync-pwd" >/usr/local/rsync/rsync.passwd
- [root@nginx rsync]# chmod 600 rsync.passwd
- password file must not be other-accessible
- continuing without password file
- [root@nginx rsync]# cd /usr/src/
- [root@nginx src]# wget http://cloud.github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
- [root@nginx src]# tar zxvf inotify-tools-3.14.tar.gz
- [root@nginx src]# cd inotify-tools-3.14
- [root@nginx inotify-tools-3.14]# ./configure --prefix=/usr/local/inotify
- [root@nginx inotify-tools-3.14]# make
- [root@nginx inotify-tools-3.14]# make install
- #!/bin/bash
- host=192.168.10.221
- src=/tmp/
- des=web
- user=webuser
- /usr/local/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib $src \
- | while read files
- do
- /usr/bin/rsync -vzrtopg --delete --progress --password-file=/usr/local/rsync/rsync.passwd $src $user@$host::$des
- echo "${files} was rsynced" >>/tmp/rsync.log 2>&1
- done
- [root@nginx tmp]# chmod 764 rsync.sh
- [root@nginx tmp]# sh /tmp/rsync.sh &
- rsync: failed to connect to 192.168.10.221: Connection refused (111)
- rsync error: error in socket IO (code 10) at clientserver.c(107) [sender=2.6.8]
- [root@nginx tmp]# echo "/tmp/rsync.sh" >> /etc/rc.local
二、備份伺服器(client,我這裡為nginx-backup)
1、安裝rsync(備份伺服器只安裝rsync)
- [root@nginx-backup ~]# cd /usr/src/
- [root@nginx-backup src]# ll
- total 16
- drwxr-xr-x 2 root root 4096 Jan 26 2010 debug
- drwxr-xr-x 2 root root 4096 Jan 26 2010 kernels
- [root@nginx-backup src]# wget http://rsync.samba.org/ftp/rsync/src/rsync-3.0.9.tar.gz
- [root@nginx-backup src]# tar zxvf rsync-3.0.9.tar.gz
- [root@nginx-backup src]# cd rsync-3.0.9
- [root@nginx-backup rsync-3.0.9]# ./configure --prefix=/usr/local/rsync
- [root@nginx-backup rsync-3.0.9]# make
- [root@nginx-backup rsync-3.0.9]# make install
- [root@nginx-backup rsync-3.0.9]# echo "webuser:rsync-pwd" > /usr/local/rsync/rsync.passwd
- [root@nginx-backup rsync]# chmod 600 rsync.passwd
- uid = root
- gid = root
- 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
- [web]
- path = /tmp/
- comment = web file
- ignore errors
- read only = no
- write only = no
- hosts allow = 192.168.10.220
- hosts deny = *
- list = false
- uid = root
- gid = root
- auth users = webuser
- secrets file = /usr/local/rsync/rsync.passwd
- [root@nginx-backup rsync]# /usr/local/rsync/bin/rsync --daemon --config=/usr/local/rsync/rsync.conf
- /usr/local/rsync/bin/rsync: error while loading shared libraries: libiconv.so.2: cannot open shared object file: No such file or directory,
- [root@nginx-backup rsync]# whereis libiconv.so.2
- libiconv.so: /usr/local/lib/libiconv.so.2 /usr/local/lib/libiconv.so
- [root@nginx-backup rsync]# echo "/usr/local/lib/" >> /etc/ld.so.conf
- [root@nginx-backup rsync]# ldconfig
- [root@nginx-backup rsync]# echo "/usr/local/rsync/bin/rsync --daemon --config=/usr/local/rsync/rsync.conf" >> /etc/rc.local