各主機(jī)IP
172.16.1.5 lb01
172.16.1.6 lb02
172.16.1.7 web02
172.16.1.8 web01
172.16.1.51 db01
172.16.1.31 nfs01
172.16.1.41 backup
172.16.1.61 m01
在backup主機(jī)上rsync.conf中增加一個(gè)[nfsbkup] path = /nfsbackup
本篇主要在nfs01主機(jī)上操作
inotify是一種強(qiáng)大的惰瓜、細(xì)粒度的、異步的文件系統(tǒng)事件監(jiān)控機(jī)制。
rpm -qa inotify-tools
# 查看是否安裝
yum -y install inotify-tools
# 在nfs01主機(jī)上(需要epel源)
inotify-tools軟件環(huán)境如下
/usr/bin/inotifywait
/usr/bin/inotifywatch
/usr/lib64/libinotifytools.so.0
/usr/lib64/libinotifytools.so.0.4.1
/usr/share/doc/inotify-tools-3.14
/usr/share/doc/inotify-tools-3.14/AUTHORS
/usr/share/doc/inotify-tools-3.14/COPYING
/usr/share/doc/inotify-tools-3.14/ChangeLog
/usr/share/doc/inotify-tools-3.14/NEWS
/usr/share/doc/inotify-tools-3.14/README
/usr/share/man/man1/inotifywait.1.gz
/usr/share/man/man1/inotifywatch.1.gz
inotify優(yōu)化配置(可選):
/proc/sys/fs/inotify/max_queued_events
inotify實(shí)例事件隊(duì)列可容納的事件數(shù)量
/proc/sys/fs/inotify/max_user_watches
可監(jiān)視的文件數(shù)量
/proc/sys/fs/inotify/max_user_instances
每個(gè)用戶可運(yùn)行的inotifywait或inotifywatch命令的進(jìn)程數(shù)
inotifywait 基本用法如下
Usage: inotifywait [ options ] file1 [ file2 ] [ file3 ] [ ... ]
options:
-m|--monitor: 一直保持監(jiān)聽(tīng)事件,沒(méi)有此選項(xiàng)的話inotify監(jiān)聽(tīng)到一個(gè)事件后即退出
-d|--daemon : 以后臺(tái)運(yùn)行模式監(jiān)聽(tīng)
-t|--timeout <seconds>: 指定監(jiān)聽(tīng)等待多少秒后停止,0表示不停止監(jiān)聽(tīng)
-e|--event <event1> [ -e|--event <event2> ... ]:
監(jiān)聽(tīng)明確的事件,沒(méi)有此項(xiàng)默認(rèn)會(huì)監(jiān)聽(tīng)所有事件
-q|--quiet : 只打印(輸出)事件
-r|--recursive: 遞歸目錄
events: 事件類型如下
access 文件或目錄內(nèi)容被讀即被訪問(wèn)
modify 文件或目錄內(nèi)容被修改
attrib 文件或目錄屬性被修改
close_write 文件或目錄以寫模式打開(kāi)之后關(guān)閉
close_nowrite 文件或目錄以只讀模式打開(kāi)后關(guān)閉
close 文件或目錄關(guān)閉(不管之前是以什么模式打開(kāi))
open 文件或目錄被打開(kāi)
moved_to 文件或目錄移動(dòng)到'監(jiān)視'的目錄中
moved_from 文件或目錄從'監(jiān)視'的目錄中移出
move 等于上述兩條并集
create 文件或目錄在指定'監(jiān)視'的目錄中被創(chuàng)建
delete 文件或目錄在指定'監(jiān)視'的目錄中被刪除
delete_self 文件或目錄被刪除
unmount 文件系統(tǒng)包含未掛載的文件或目錄
Exit status:
0 - 接收到指定監(jiān)聽(tīng)的一個(gè)事件
1 - 接收到一個(gè)未指定監(jiān)聽(tīng)的事件
(usually delete_self or unmount), or some error occurred.
2 - The --timeout option was given and no events occurred
in the specified interval of time.
使用示例:
inotifywait -mqr --timefmt '%Y-%m-%d %H:%M' --format '%T %w%f' -e create,delete /backup # 新建文件時(shí)會(huì)有如下輸出
2017-02-05 17:30 /backup/a
2017-02-05 17:30 /backup/b
..
腳本實(shí)現(xiàn):"/server/scripts/inotify_rsync.sh"
#!/bin/bash
# Program: backup local dir "/backup" to BACKUP Server in real time
# author: Huangjian
# date: 2017-2-5
DIR="/data"
/usr/bin/inotifywait -mqr --format '%w%f' -e create,delete,close_write $DIR | \
while read file; do
if [ -f $file ];then
rsync -az $file --delete rsync_backup@172.16.1.41::nfsbkup/ --password-file=/etc/rsync.pa
ssword
else
cd $DIR &&
rsync -az ./ --delete rsync_backup@172.16.1.41::nfsbkup/ --password-file=/etc/rsync.passw
ord
fi
done
執(zhí)行此腳本,往NFS Server的/data目錄中添加 刪除或修改文件時(shí)會(huì)將此目錄的內(nèi)容與backup主機(jī)中的/nfsbackup目錄保持一致
inotify優(yōu)缺點(diǎn)
- 優(yōu)點(diǎn)
1.監(jiān)控文件系統(tǒng)事件變化,通過(guò)同步工具實(shí)現(xiàn)實(shí)時(shí)數(shù)據(jù)同步.
- 缺點(diǎn)
- 并發(fā)如果大于200個(gè)文件(10-100K),同步就會(huì)有延遲.
- 監(jiān)控到事件后調(diào)用rsync同步是單進(jìn)程的.