rsync+inotify实现实时同步

一、无差异同步数据

1.首先,在实践实时同步之前我们先来了解一下rsync无差异同步
无差异推送数据加--delete
1)备份 --delete风险
本地有,远端就有。本地没有远端有也会删除,服务器端的目录数据可能会丢失

无差异拉取数据
2)代码发布、下载 --delete风险
远端有,本地就有。远端没有的本地有也会删除,本地的目录数据可能丢失
rsync+inotify实现实时同步
ps:上图会将远程端的/hzftp/test里的文件完全克隆到客户端的/data1目录中


多模块共享配置,只需新增模块目录地址即可,其他


inotify实施(切记在客户端上安装)

前提:需要rsync daemon已经搭建完成的情况下,可以在服务端上推拉数据
inotifu安装:
检查服务端的rsync服务是正常启用的,并且可以在客户端往服务端推送文件
检查内核版本:uname -r
[root@localhost inotify]# ls
max_queued_events max_user_instances max_user_watches
[root@localhost inotify]# ll /proc/sys/fs/inotify
总用量 0
-rw-r--r--. 1 root root 0 6月 27 22:58 max_queued_events
-rw-r--r--. 1 root root 0 6月 27 22:58 max_user_instances
-rw-r--r--. 1 root root 0 6月 27 22:58 max_user_watches

然后从互联网上下载inotify源码安装包
下载好后,解压:tar xf inotify-tools-3.14.tar.gz
cd inotify-tools-3.14
./configure --prefix=/usr/local/inotify-tools-3.14
make && make install
ln -s /usr/local/inotify-tools-3.14 /usr/local/inotify
[root@localhost inotify]# ll /usr/local/inotify
总用量 4
drwxr-xr-x. 2 root root 43 6月 27 23:10 bin #inotify执行命令
drwxr-xr-x. 3 root root 25 6月 27 23:10 include #inotify所需头文件
drwxr-xr-x. 2 root root 4096 6月 27 23:10 lib #动态链接库文件
drwxr-xr-x. 4 root root 26 6月 27 23:10 share #帮助文档

使用inotify来监控目录文件,当被监控的目录发生变化会产生输出:
监听创建:create
监听删除:delete
监听写入:close_write
ps:当create和close_write同时使用时,创建文件会被监控两遍
======create,delete,close_write同时使用用逗号隔开=======
/usr/local/inotify-tools-3.14/bin/inotifywait -mrq --timefmt '%d%m%y %H:%M' --format '%T %w%f' -e create /backup
rsync+inotify实现实时同步


开发inotify数据同步脚本
创建脚本存放路径:mkdir -p /server/scripts
进入脚本存放目录:cd /server/scripts

猜你喜欢

转载自blog.51cto.com/13233089/2133301