1.简介
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
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
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
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
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
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10
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