inotify
定義
Inotify是一種強(qiáng)大的眉睹、細(xì)粒度的柑肴、異步的文件系統(tǒng)時(shí)間監(jiān)控機(jī)制,通過Inotify可以監(jiān)控文件系統(tǒng)中添加、刪除癣猾、修改敛劝、移動(dòng)等各時(shí)間
Inotify程序原理是一種事件驅(qū)動(dòng)機(jī)制,它為應(yīng)用程序監(jiān)控文件系統(tǒng)事件提供了實(shí)時(shí)響應(yīng)事件的機(jī)制
備份原理:
通俗點(diǎn)說纷宇,如想實(shí)時(shí)同步/data目錄夸盟,則inotify或sersync會(huì)實(shí)時(shí)監(jiān)控/data下的block數(shù)量和inode數(shù)量,若發(fā)生變化像捶,則會(huì)執(zhí)行rsync去備份上陕。
inotify實(shí)施準(zhǔn)備
大前提rsync daemon服務(wù)配置成功,可以在rsync客戶端推送拉取數(shù)據(jù)拓春,然后才能配置inotify服務(wù)释簿。
關(guān)鍵參數(shù)說明
在/proc/sys/fs/inotify目錄下有三個(gè)文件,對(duì)inotify機(jī)制有一定的限制
max_queued_events:設(shè)置inotifywait或inotifywatch命令可以監(jiān)視的文件數(shù)量(單進(jìn)程)
max_user_instances:設(shè)置每個(gè)用戶可以運(yùn)行的inotifywait或inotifywatch命令的進(jìn)程數(shù)
max_user_watches:設(shè)置inotify實(shí)例事件(event)隊(duì)列可容納的時(shí)間數(shù)量
inotify優(yōu)點(diǎn):
1)監(jiān)控文件系統(tǒng)時(shí)間變化硼莽,通過同步工具實(shí)現(xiàn)實(shí)時(shí)數(shù)據(jù)同步
inotify缺點(diǎn):
1)并發(fā)如果大于200個(gè)文件(10-100K)庶溶,同步就會(huì)有延遲。
2)我們前面寫的腳本懂鸵,每次都是全部推送一次偏螺,但確實(shí)是增量的。也可以只同步變化的文件匆光,不變化的置之不理套像。
3)監(jiān)控到事件后,調(diào)用rsync同步是單進(jìn)程的(也可加&并發(fā))殴穴,sersync是多進(jìn)程同步
=====================================================================
環(huán)境準(zhǔn)備
實(shí)時(shí)復(fù)制實(shí)踐:
前提:backup rsync服務(wù)端部署好凉夯。
1)部署NFS客戶端
[root@nfs01 ~]# echo 'export RSYNC_PASSWORD=oldboy' >>/etc/bashrc
[root@nfs01 ~]# source /etc/bashrc
[root@nfs01 ~]# echo $RSYNC_PASSWORD
oldboy
測(cè)試推送
[root@nfs01 ~]# rsync -avz /data rsync_backup@172.16.1.41::backup/
sending incremental file list
sent 164 bytes? received 25 bytes? 126.00 bytes/sec
total size is 0? speedup is 0.00
2)查看inotify支持情況
[root@nfs01 ~]# uname -r
3.10.0-957.5.1.el7.x86_64
[root@nfs01 ~]#? ls -l /proc/sys/fs/inotify/
總用量 0
-rw-r--r-- 1 root root 0 4月? 19 09:45 max_queued_events
-rw-r--r-- 1 root root 0 4月? 19 09:45 max_user_instances
-rw-r--r-- 1 root root 0 4月? 19 09:45 max_user_watches
3)安裝inotify-tools
yum install epel-release -y
yum install inotify-tools -y
[root@nfs01 ~]# rpm -ql inotify-tools|head -2
/usr/bin/inotifywait
/usr/bin/inotifywatch
[root@nfs01 ~]# rpm -qa inotify-tools
inotify-tools-3.14-8.el7.x86_64
4)命令參數(shù)和事件知識(shí)
max_queued_events:設(shè)置inotifywait或inotifywatch命令可以監(jiān)視的文件數(shù)量(單進(jìn)程)
max_user_instances:設(shè)置每個(gè)用戶可以運(yùn)行的inotifywait或inotifywatch命令的進(jìn)程數(shù)
max_user_watches:設(shè)置inotify實(shí)例事件(event)隊(duì)列可容納的時(shí)間數(shù)量
5)測(cè)試實(shí)踐
inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e create /data
6)增 改 刪 需要監(jiān)控
[root@nfs01 ~]# inotifywait -mrq --format '%w%f' -e close_write,delete /data
7)編寫腳本
mkdir /server/scripts -p
rsync -az --delete /data/ rsync_backup@172.16.1.41::backup
vim /server/scripts/monitor1.sh
#!/bin/sh
cmd="/usr/bin/inotifywait"
$cmd -mrq? --format '%w%f' -e close_write,delete /data|\
while read line
do
? #刪除事件發(fā)生
? [ ! -e? "$line" ] && cd /data &&\
? rsync -az --delete ./ rsync_backup@172.16.1.41::backup && continue
? #處理增改事件
? rsync -az --delete $line rsync_backup@172.16.1.41::backup
done
[root@nfs01 /server/scripts]# /bin/sh /server/scripts/monitor1.sh &[2] 9199? <==后臺(tái)執(zhí)行
加到開機(jī)自啟
[root@nfs01 /server/scripts]# tail -2 /etc/rc.local
########################
/bin/sh /server/scripts/monitor1.sh &
sersync實(shí)踐:
1)客戶端推送成功
rsync -az /data rsync_backup@172.16.1.41::backup
2)部署sersync服務(wù)(NFS)
wget https://github.com/wsgzao/sersync/blob/master/sersync2.5.4_64bit_binary_stable_final.tar.gz
[root@nfs01 /server/tools]# mkdir /application -p
[root@nfs01 /server/tools]# mv application/sersync /application/
[root@nfs01 /server/tools]# tree /application/
/application/
└── sersync
? ? ├── bin
? ? │?? └── sersync
? ? ├── conf
? ? │?? ├── confxml.xml
? ? │?? └── confxml.xml.ori
? ? ├── logs
? ? │?? └── rsync_fail_log.sh
? ? └── readme.txt
4 directories, 5 files
[root@nfs01 /server/tools]# cd /application/sersync/
[root@nfs01 /application/sersync]# ls
bin? conf? logs? readme.txt
[root@nfs01 /application/sersync]# cd conf/
[root@nfs01 /application/sersync/conf]# ls
confxml.xml? confxml.xml.ori
3)sersync配置文件:干兩件事:
vim confxml.xml
1)完成監(jiān)控配置:
inotifywait -mrq --format '%w%f' -e createFolder,close_write,delete,moveFrom,moveTo /data
2)完整命令拼接:
rsync -az /data --timeout=100 rsync_backup@172.16.1.41::backup
4)啟動(dòng)服務(wù)
[root@nfs01 /application/sersync/conf]# ../bin/sersync -h
set the system param
execute:echo 50000000 > /proc/sys/fs/inotify/max_user_watches
execute:echo 327679 > /proc/sys/fs/inotify/max_queued_events
parse the command param
_______________________________________________________
參數(shù)-d:啟用守護(hù)進(jìn)程模式
參數(shù)-r:在監(jiān)控前,將監(jiān)控目錄與遠(yuǎn)程主機(jī)用rsync命令推送一遍
c參數(shù)-n: 指定開啟守護(hù)線程的數(shù)量采幌,默認(rèn)為10個(gè)
參數(shù)-o:指定配置文件劲够,默認(rèn)使用confxml.xml文件
參數(shù)-m:單獨(dú)啟用其他模塊,使用 -m refreshCDN 開啟刷新CDN模塊
參數(shù)-m:單獨(dú)啟用其他模塊休傍,使用 -m socket 開啟socket模塊
參數(shù)-m:單獨(dú)啟用其他模塊征绎,使用 -m http 開啟http模塊
不加-m參數(shù),則默認(rèn)執(zhí)行同步程序
________________________________________________________________
[root@nfs01 /application/sersync/conf]# /application/sersync/bin/sersync -d -n 10 -o /application/sersync/conf/confxml.xml
最終
/application/sersync/bin/sersync -d
pkill sersync
二進(jìn)制程序磨取,不是yum安裝的人柿,所以不能systemctl start sersync
5)/application/sersync/bin/sersync -d 加到/etc/rc.local開機(jī)自啟
[root@nfs01 /data]# tail -1 /etc/rc.local
/application/sersync/bin/sersync -d
配置:systemctl start sersync啟動(dòng)方案
https://blog.51cto.com/oldboy/2155931
[root@nfs01 /data]# cat /etc/rc.d/init.d/sersync
#!/bin/bash
# chkconfig: 2345 21 81
# description: rsync service start and stop scripts
# Author: oldboy
# Organization: www.oldboyedu.com
start(){
? ? /application/sersync/bin/sersync -d -o /application/sersync/conf/confxml.xml &>/dev/null
}
stop(){
? ? killall sersync 2>/dev/null
}
case "$1" in
? ? start)
? ? ? ? start
? ? ? ? ;;
? ? stop)
? ? ? ? stop
? ? ? ? ;;
? ? restart)
? ? ? ? stop
? ? ? ? sleep 2
? ? ? ? start
? ? ? ? ;;
? ? *)
? ? ? ? echo $"Usage:$0 {start|stop|restart}"
? ? ? ? exit 1
esac
chmod +x /etc/rc.d/init.d/sersync
[root@nfs01 /data]# cat /usr/lib/systemd/system/sersync.service
[Unit]
Description=sersyncd service
After=network.target
[Service]
Type=forking? ? ? ? ?
ExecStart=/etc/rc.d/init.d/sersync start?
ExecReload=/etc/rc.d/init.d/sersync restart
ExecStop=/etc/rc.d/init.d/sersync stop? ?
PrivateTmp=true
[Install]
WantedBy=multi-user.target
chmod +x /usr/lib/systemd/system/sersync.service
[root@nfs01 /data]# systemctl enable sersync.service
Created symlink from /etc/systemd/system/multi-user.target.wants/sersync.service to /usr/lib/systemd/system/sersync.service.
[root@nfs01 /data]# systemctl status sersync.service
● sersync.service - sersyncd service
? Loaded: loaded (/usr/lib/systemd/system/sersync.service; enabled; vendor preset: disabled)
? Active: inactive (dead)
[root@nfs01 /data]# systemctl stop sersync
[root@nfs01 /data]# ps -ef|grep sersync|grep -v grep
[root@nfs01 /data]# systemctl start sersync