用pc做網(wǎng)站服務(wù)器為什么不如云主機(jī)百度輸入法下載
引言
????????使用inotify通知接口,可以用來(lái)監(jiān)控文件系統(tǒng)的各種變化情況,如文件存取、刪除、移動(dòng)、修改等。利用這一機(jī)制,可以非常方便地實(shí)現(xiàn)文件異動(dòng)告警、增量備份,并針對(duì)目錄或文件的變化及時(shí)作出響應(yīng)。
????????將inotify機(jī)制與rsync工具相結(jié)合,可以實(shí)現(xiàn)觸發(fā)式備份(實(shí)時(shí)同步),即只要原始位置的文檔發(fā)生變化,則立即啟動(dòng)增量備份操作;否則處于靜默等待狀態(tài)。這樣,就避免了按固定周期備份時(shí)存在的延遲性、周期過(guò)密等問(wèn)題。因?yàn)?inotify 通知機(jī)制由 Linux 內(nèi)核提供,因此主要做本機(jī)監(jiān)控,在觸發(fā)式備份中應(yīng)用時(shí)更適合上行同步。
1、修改 rsync 源服務(wù)器配置文件(rsync)
vim /etc/rsyncd.conf?#修改 read only 為 noread only = nokill $(cat /var/run/rsyncd.pid)rm -rf /var/run/rsyncd.pidrsync --daemonnetstat -natp | grep rsyncchmod 777 /var/www/html/txy
2、調(diào)整 inotify 內(nèi)核參數(shù)(backuper)
在 Linux 內(nèi)核中,默認(rèn)的 inotify 機(jī)制提供了三個(gè)調(diào)控參數(shù)
max_queued_events ? ?監(jiān)控事件隊(duì)列,默認(rèn)值為 16384
max_user_instances ? ? 最多監(jiān)控實(shí)例數(shù),默認(rèn)值為 128
max_user_watches 每個(gè)實(shí)例最多監(jiān)控文件數(shù),默認(rèn)值為 8192
當(dāng)要監(jiān)控的目錄、文件數(shù)較多或者變化較頻繁時(shí),建議加大這三個(gè)參數(shù)的值
cat /proc/sys/fs/inotify/max_queued_events
cat /proc/sys/fs/inotify/max_user_instances
cat /proc/sys/fs/inotify/max_user_watches
可通過(guò) /etc/sysctl.conf 文件進(jìn)行配置,設(shè)定參數(shù)應(yīng)符合服務(wù)器性能以及服務(wù)需要
vim /etc/sysctl.conf
#添加如下配置
fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576
sysctl -p
3、安裝 inotify-tools(backuper)
用 inotify 機(jī)制需要安裝 inotify-tools,以便提供 inotifywait、inotifywatch 輔助工具程序,用來(lái)監(jiān)控、匯總改動(dòng)情況。
inotifywait:可監(jiān)控modify(修改)、create(創(chuàng)建)、move(移動(dòng))、delete(刪除)、attrib(屬性更改)等各種事件,一旦變動(dòng)立即輸出結(jié)果。
inotifywatch:可用來(lái)收集文件系統(tǒng)變動(dòng)情況,并在運(yùn)行結(jié)果后輸出匯總的變化情況。
[root@localhost ~]# cd /opt[root@localhost opt]# rz -E#傳入 inotify-tools 安裝包rz waiting to receive.tar zxvf inotify-tools-3.14.tar.gz -C /optcd inotify-tools-3.14/./configuremake -j 2 && make install
可以先執(zhí)行 inotifywait 命令,然后另外再開(kāi)啟一個(gè)新終端向 /var/www/html 目錄下添加文件、移動(dòng)文件,在原來(lái)的終端上跟蹤屏幕輸出結(jié)果。
新終端操作
[root@localhost ~]# cd /var/www/html/txy[root@localhost txy]# touch test.php[root@localhost txy]# mv test.php test.txt[root@localhost txy]# echo 'test' > test.txt[root@localhost txy]# rm -rf test.txt
原終端操作
[root@localhost inotify-tools-3.14]# inotifywait -mrq -e modify,create,move,delete /var/www/html/txy
4、實(shí)驗(yàn)驗(yàn)證
在新終端的/var/www/html/txy的目錄下新建文件夾
?在原終端上顯示該內(nèi)容
inotifywait 選項(xiàng)說(shuō)明如下
常用選項(xiàng) ? ? ? 說(shuō)明
-e ? 用來(lái)指定要監(jiān)控哪些事件
-m ?表示持續(xù)監(jiān)控
-r ? ?表示遞歸整個(gè)目錄
-q ? 簡(jiǎn)化輸出信息
5、另一個(gè)終端的觸發(fā)式同步腳本
[root@localhost txy]# vim /opt/inotify.sh#!/bin/bashINOTIFY_CMD="inotifywait -mrq -e modify,create,attrib,move,delete /var/www/html/txy"RSYNC_CMD="rsync -azH --delete --password-file=/etc/server.pass /var/www/html/txy backuper@192.168.10.23::rsync/"$INOTIFY_CMD | while read DIRECTORY EVENT FILE
##while判斷是否接收到監(jiān)控記錄doif [ $(pgrep rsync | wc -l) -le 0 ] ; then$RSYNC_CMDfidone
[root@localhost opt]# chmod +x inotify.sh[root@localhost opt]# chmod 777 /var/www/html/[root@localhost opt]# chmod +x /etc/rc.d/rc.local[root@localhost opt]# echo '/opt/inotify.sh' >> /etc/rc.d/rc.local
##加入開(kāi)機(jī)自動(dòng)執(zhí)行
上述腳本用來(lái)檢測(cè)本機(jī) /var/www/html/txy 目錄的變動(dòng)情況,一旦有更新觸發(fā) rsync 同步操作,上傳備份至服務(wù)器 192.168.10.23 的 rsync 共享目錄下。
6、驗(yàn)證
6.1 在本機(jī)運(yùn)行 /opt/inotify_rsync.sh 腳本程序
[root@backuper opt]# ./inotify.sh &? ? ? ? //后臺(tái)運(yùn)行該腳本
6.2 切換到本機(jī)的 /var/www/html 目錄,執(zhí)行增加、刪除、修改文件等操作
[root@localhost opt]# cd /var/www/html/txy[root@localhost txy]# lstest.html ?test.txt[root@localhost txy]# rm -rf *[root@localhost txy]# touch test.html[root@localhost txy]# echo 'this is inotify_rsync test!' > test.html
6.3 查看遠(yuǎn)端服務(wù)器中的 rsync 目錄下的變化情況
[root@rsync ~]# cd /data[root@rsync data]# lstest.html[root@rsync data]# cat test.htmlthis is inotify_rsync test!