配置前检查
1.首先确定Rsync已经配置完成,客户端可以向服务端推送文件
[test@C64-6-B ~]$ rsync -avzP ./syncdir rsync_backup@2.2.2.5::syner --password-file=/etc/rsync.password
sending incremental file list
syncdir/
sent 52 bytes received 12 bytes 42.67 bytes/sec
total size is 0 speedup is 0.00
具体配置可以参考以下博客
https://blog.csdn.net/Paul_George/article/details/82805496
2.查看当前系统是否支持Inotify
[test@C64-5-S ~]$ ls -l /proc/sys/fs/inotify/
total 0
-rw-r--r-- 1 root root 0 Sep 25 13:25 max_queued_events
-rw-r--r-- 1 root root 0 Sep 25 13:25 max_user_instances
-rw-r--r-- 1 root root 0 Sep 25 13:25 max_user_watches
如果出现上面三个文件,说明系统支持Inotify
3.下载安装Inotify
执行以下命令(由于信息过多,这里就不贴出来了)
yum install -y epel-release && yum update
yum install inotify-tools
配置Inotify
1.编写Inotify实时监控脚本
[syner@C64-5-S ~]$ mkdir -p /home/syner/r_inotify/
vi /server/scripts/inotify/inotify.sh
#!/bin/bash
#para
host01=2.2.2.6
src=/home/syner/
dst=syner
user=rsync_backup
rsync_passfile=/etc/rsync.password
inotify_home=/usr/src/kernels/2.6.32-754.3.5.el6.x86_64/
#judge
if [ ! -e "$src" ] \
|| [ ! -e "${rsync_passfile}" ] \
|| [ ! -e "/usr/bin/inotifywait" ] \
|| [ ! -e "/usr/bin/rsync" ];
then
echo "Check File and Folder"
exit 9
fi
/usr/bin/inotifywait -mrq --timefmt '%d%m%y %H:%M' --format '%T %w%f' -e close_write,delete,cr
eate,attrib $src | while read file
do
cd $src && rsync -aruzR --delete ./r_inotify --timeout=100 -e 'ssh -p 52113' $dst@$host01:/home/syner/ >> /home/syner/inotify.log 2>&1
done
exit 0
2.运行程序
[syner@C64-5-S ~]$ sh /server/scripts/inotify/inotify.sh &
[1] 8509
[syner@C64-5-S ~]$ ps -ef | grep inotify
syner 8509 8440 0 19:01 pts/2 00:00:00 sh /server/scripts/inotify/inotify.sh
syner 8510 8509 0 19:01 pts/2 00:00:00 /usr/bin/inotifywait -mrq --timefmt %d%m%y %H:%M --format %T %w%f -e close_write,delete,create,attrib /home/syner/
syner 8511 8509 0 19:01 pts/2 00:00:00 sh /server/scripts/inotify/inotify.sh
syner 8513 8440 0 19:01 pts/2 00:00:00 grep inotify
测试是否成功
[syner@C64-5-S r_inotify]$ echo "test inotify" > test.txt
[syner@C64-6-B r_inotify]$ ll
total 4
-rw-rw-r-- 1 syner syner 13 Sep 25 19:02 test.txt
[syner@C64-6-B r_inotify]$ more test.txt
test inotify
成功同步过去了
[syner@C64-5-S r_inotify]$ rm test.txt
[syner@C64-6-B r_inotify]$ ll
total 0
也可以同步删除