CentOS 7 部署inotify實時監控(NFS服務器上部署,rsync服務器測試)
1.1inotify
一個 Linux 內核特性,它監控文件系統,並且及時向專門的應用程序發出相關的事件警告,比如刪除、讀、寫和卸載操作等。
2.環境準備
[root@nfs01 ~]# cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)
[root@nfs01 ~]# uname -r
3.10.0-327.el7.x86_64
[root@nfs01 ~]# getenforce
Disabled
[root@nfs01 ~]# systemctl status firewalld.service
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
Active: inactive (dead)
[root@nfs01 ~]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.0.0.31 netmask 255.255.255.0 broadcast 10.0.0.255
inet6 fe80::20c:29ff:fe7a:66a4 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:7a:66:a4 txqueuelen 1000 (Ethernet)
RX packets 1195 bytes 283633 (276.9 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 198 bytes 24332 (23.7 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
eth1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.16.1.31 netmask 255.255.255.0 broadcast 172.16.1.255
inet6 fe80::20c:29ff:fe7a:66ae prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:7a:66:ae txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 10 bytes 744 (744.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 0 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
3.NFS服務器部署inotify服務
3.1安裝inotify
[root@nfs01 ~]# yum install -y inotify-tools
3.2編寫實時同步腳本
[root@nfs01 ~]# cat /server/scripts/inotify.sh
#!bin/bash
inotifywait -mrq --format "%w%f" -e create,close_write,delete,moved_to /data/|\
while read zy
do
rsync -az /data/ --delete [email protected]::backup --password-file=/etc/rsync.password
done
4.測試
4.1運行腳本
[root@nfs01 ~]# sh /server/scripts/inotify.sh
4.2在NFS服務器的/data目錄創建測試文檔
[root@nfs01 data]# touch test.txt
[root@nfs01 data]# ls
test.txt
4.3rsync服務器的/backup目錄查看
[root@backup ~]# ls /backup/
test.txt
CentOS 7 部署inotify實時監控(NFS服務器上部署,rsync服務器測試)