Rsync+inotify搭建实时同步系统
Rsync 優(yōu)點(diǎn)
rsync具有安全性高、備份迅速、支持增量備份等優(yōu)點(diǎn),通過rsync可以解決對(duì)實(shí)時(shí)性要求不高的數(shù)據(jù)備份需求,例如定期地備份文件服務(wù)器數(shù)據(jù)到遠(yuǎn)程服務(wù)器上,對(duì)本地磁盤定期進(jìn)行數(shù)據(jù)鏡像等。
Rsync 不足
隨著應(yīng)用系統(tǒng)規(guī)模的不斷擴(kuò)大,對(duì)數(shù)據(jù)的安全性和可靠性提出了更高的要求,rsync便暴露出了很多不足之處。首先,rsync同步數(shù)據(jù)時(shí),需要掃描所有文件后進(jìn)行比對(duì),然后進(jìn)行差量傳輸。如果文件數(shù)量達(dá)到了百萬量級(jí)及以上時(shí),掃描文件就非常耗費(fèi)時(shí)間,有時(shí)還會(huì)出現(xiàn)只是很少一部分?jǐn)?shù)據(jù)發(fā)生了變化,因此rsync就變得非常低效了。其次,rsync不能夠?qū)崟r(shí)監(jiān)測、同步數(shù)據(jù),雖然它可以通過Linux守護(hù)進(jìn)程的方式觸發(fā)同步,但是兩次觸發(fā)動(dòng)作一定會(huì)出現(xiàn)時(shí)間差,可能導(dǎo)致服務(wù)器端和客戶端的數(shù)據(jù)出現(xiàn)不一致,后期無法在出現(xiàn)故障時(shí)完全恢復(fù)數(shù)據(jù)。
Rsync+inotify組合
為解決以上問題而出現(xiàn)的,實(shí)現(xiàn)了數(shù)據(jù)的實(shí)時(shí)同步
inotify是什么 ?
inotify是一種強(qiáng)大的、細(xì)粒度的、異步的文件系統(tǒng)事件監(jiān)控機(jī)制,Linux內(nèi)核從2.6.13版本起,加入了對(duì)inotify的支持。通過inotify可以監(jiān)控文件系統(tǒng)中添加、刪除、修改、移動(dòng)等各種事件,利用這個(gè)內(nèi)核接口,inotify-tools便可以監(jiān)控文件系統(tǒng)下文件的各種變化情況了。
檢查內(nèi)核是否支持inotify:
實(shí)驗(yàn)案例:rsync+inotify搭建實(shí)時(shí)同步系統(tǒng)
案例描述:在前文Haproxy搭建高可用Web集群的網(wǎng)站拓?fù)浼軜?gòu)圖基礎(chǔ)上,實(shí)現(xiàn)兩臺(tái)Web站點(diǎn)數(shù)據(jù)的實(shí)時(shí)同步,web1服務(wù)器為內(nèi)容發(fā)布節(jié)點(diǎn),web2服務(wù)器為同步節(jié)點(diǎn)(本文主要說明rsync+inotify實(shí)時(shí)同步數(shù)據(jù)技術(shù),其他請(qǐng)閱讀前文,謝謝!)。
系統(tǒng)環(huán)境
| 負(fù)載調(diào)度服務(wù)器: | CentOS 7.0 x86_64 | 192.168.100.25 | 無 | haproxy-1.5.19.tar.gz |
| web1節(jié)點(diǎn)服務(wù)器: | CentOS 7.0 x86_64 | 192.168.100.26 | /www/wwwroot | nginx-1.12.0.tar.gz、rsync-3.1.3.tar.gz、inotify-tools-3.14.tar.gz |
| web2節(jié)點(diǎn)服務(wù)器: | CentOS 7.0 x86_64 | 192.168.100.27 | /web2/wwwroot | nginx-1.12.0.tar.gz、rsync-3.1.3.tar.gz |
| Win 7 客戶端: | Windows 7 | 192.168.100.30 | 無 | 無 |
關(guān)閉防火墻及Selinux
systemctl stop firewalld setenforce 0軟件包:rsync+inotify 密碼:do45
開始部署
inotify-tools是用來監(jiān)控文件系統(tǒng)變化的工具,因此必須安裝在內(nèi)容發(fā)布系統(tǒng)上,服務(wù)節(jié)點(diǎn)無需安裝它,而web2服務(wù)器上需要安裝rsync實(shí)現(xiàn)文件傳輸。
一、web2服務(wù)器(待同步數(shù)據(jù):rsync)
1.安裝rsync軟件包
#解壓 tar zxvf rsync-3.1.3.tar.gz -C /opt#切換目錄 cd /opt/rsync-3.1.3#配置 ./configure#編譯及安裝 make && make install2.修改rsync配置文件:
vim /etc/rsyncd.conf
#/etc/rsyncd: configuration file for rsync daemon mode #See rsyncd.conf man page for more options. #configuration example: uid = nobody gid = nobody use chroot = yes max connections = 10 strict mode=yes pid file = /var/run/rsyncd.pid lock file=/var/run/rsync.lock log file=/var/log/rsyncd.log [web1]path = /web1/wwwrootcomment = web1 fileignore errrorsread only=nowrite only=nohosts allow=*hosts deny=192.168.100.10list=falseuid=rootgid=rootauth users=web1usersecrets file=/etc/web1.pass3.創(chuàng)建密碼文件(文件格式:user:pass)
/etc/web1.passweb1user:www123
修改密碼文件權(quán)限
chmod 600 /etc/web1.pass4.啟動(dòng)rsync守護(hù)進(jìn)程
/usr/local/bin/rsync --daemon5.加入系統(tǒng)自啟動(dòng)文件
echo "/usr/local/bin/rsync --daemon" >> /etc/rc.local6.查看rsync進(jìn)程
ps -ef | grep rsync二、web1服務(wù)器(內(nèi)發(fā)發(fā)布節(jié)點(diǎn):rsync+inotify)
1.安裝rsync軟件包
#解壓 tar zxvf rsync-3.1.3.tar.gz -C /opt#切換目錄 cd /opt/rsync-3.1.3#配置 ./configure#編譯及安裝 make && make install2.新建密碼文件,需和節(jié)點(diǎn)服務(wù)器密碼一致
vim /etc/server.passwww123
3.修改密碼文件權(quán)限
chmod 600 /etc/server.pass4.啟動(dòng)rsync守護(hù)進(jìn)程
/usr/local/bin/rsync --daemon5.加入系統(tǒng)自啟動(dòng)文件
echo "/usr/local/bin/rsync --daemon" >> /etc/rc.local6.安裝inotify-tools
#解壓 tar zxvf inotify-tools-3.14.tar.gz -C /opt#切換目錄 cd -C /opt/inotify-tools-3.14#配置 ./configure#編譯及安裝 make && make install7.編寫shell腳本來配置內(nèi)容發(fā)布節(jié)點(diǎn)
vim /web/inotifyrsync.sh
#!/bin/bash host1=192.168.100.26 src=/web/wwwroot/ dst1=web1 user1=web1user /usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e close_write,delete,create,attrib $src \ | while read files do/usr/bin/rsync -vzrtopg --delete --progress --password-file=/etc/server.pass $src $user1@$host1::$dst1 > /dev/null 2>&1echo "${files} was rsynced." >> /tmp/rsync.log 2>&1 done8.為其指定可執(zhí)行權(quán)限,然后放入后臺(tái)運(yùn)行
chmod 755 /web/inotifyrsync.sh /web/inotifyrsync.sh &9.將腳本加入系統(tǒng)自啟動(dòng)文件
echo "/web/inotifyrsync.sh &" >> /etc/rc.local測試
在web1服務(wù)器節(jié)點(diǎn)的/web/wwwroot目錄下添加、刪除、修改文件,然后到web2服務(wù)器節(jié)點(diǎn)對(duì)應(yīng)目錄去查看文件是否跟隨發(fā)布節(jié)點(diǎn)同步變化。
web1服務(wù)器為內(nèi)容發(fā)布節(jié)點(diǎn):
web2服務(wù)器進(jìn)行數(shù)據(jù)同步:
轉(zhuǎn)載于:https://blog.51cto.com/10316297/2136440
總結(jié)
以上是生活随笔為你收集整理的Rsync+inotify搭建实时同步系统的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 配置Tomcat监听80端口配置Tomc
- 下一篇: 等重构完这系统,我就辞职