【rsync的单向——实时同步】_企业打工人的博客-爱代码爱编程
inotify 简介
inotify 是一个Linux 内核特性,它监控文件系统,并且及时发出警告,比如删除、读、写和卸载操作等。
inotify两个监控命令:
inotifywait:用于持续监控,实时输出结果(常用)
inotifywatch:用于短期监控,任务完成后再出结果
inotify 部署
环境准备:gcc、inotify-tools等软件
[root@localhost filesrc]# yum -y install gcc* inotify-tools
如果安装时,没有相关软件包,则需要下载epel源
[root@localhost filesrc]# yum -y install epel-release
inotifywait 命令格式
格式: inotifywait -mrq -e 监控动作 1,监控动作 2 /监控目录 &
实例: inotifywait -mrq -e create,delete,modify /filesrc &
-m:始终保持事件监听状态
-r:递归查询目录
-q:只打印监控事件的信息
监控动作:modify(内容),create,attrib(权限),move,delete
利用rsync + inotifywait 结合脚本实现单向实时同步
#!/bin/bash
a="inotifywait -mrq -e create,delete,modify /filesrc"
b="rsync -avz /filesrc/* root@192.168.28.149:/filedst"
$a | while read directory event file
do
$b
done
注:用户登录时要求免密码验证!!(利用ssh的openssl)
实验测试
两台服务器,src和dst,以src为基准服务器。
1)首先两个服务器的目录内没用任何文件
2)在src上运行我们写好的脚本,再在rsync服务器上,创建一个叫zlf.txt的目录,而此时脚本执行了,备份服务器上,也出现了对应的目录
[root@localhost filesrc]# bash /root/src.sh &
[3] 12123
[root@localhost filesrc]# mkdir zlf.txt
[root@localhost filesrc]# sending incremental file list
zlf.txt/
sent 61 bytes received 20 bytes 162.00 bytes/sec
total size is 0 speedup is 0.00
sending incremental file list
sending incremental file list
sent 58 bytes received 17 bytes 150.00 bytes/sec
total size is 0 speedup is 0.00
sent 58 bytes received 17 bytes 150.00 bytes/sec
total size is 0 speedup is 0.00
ok,实验测试成功!!