rsync+inotify实时同步数据

一、无差异同步数据

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

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


服务端上操作:

多模块共享配置,只需在服务器端/etc/rsyncd.conf新增模块目录地址即可,其他
rsync+inotify实时同步数据


inotify参考资料wiki:https://github.com/rvoicilas/inotify-tools/wiki
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 #队列大小,生产中调大500000000

然后从互联网上下载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
文件属性:attrib
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数据同步脚本(rsync客户端上操作)

创建脚本存放路径:mkdir -p /server/scripts
进入脚本存放目录:cd /server/scripts
[root@localhost scripts]# vim inotify.sh

#!/bin/bash```

#para
host01=172.17.0.112
src=/backup
dst=backup
user=backup
rsync_passfile=/etc/rsync.pwd
inotify_home=/usr/local/inotify-tools-3.14/

#judge
if [ ! -e "$src" ]
[ ! -e "${rsync_passfile}" ]
[ ! -e "${inotify_home}bin/inotifywait" ]
[ ! -e "/usr/bin/rsync" ];
then
echo "Check File and Folder"
exit 9
fi

${inotify_home}/bin/inotifywait -mrq --timefmt '%y%m%d %H:%M' --format '%T %w%f' -e close_write,delete,create,attrib $src \
| while read file
do
注释:# rsync -avzP --delete --timout=100 --password-file=${rsync_passfile} $src $user@$host01::$dst >/dev/null 2>&1
cd $src && rsync -aruz -R --delete ./ --timeout=100 $user@$host01::$dst --password-file=${rsync_passfile} >/dev/null 2>&1
done
exit 0


##############################################################
![](http://i2.51cto.com/images/blog/201806/28/7236e98b80e0b85a2b6d998cff15afb9.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)

执行脚本文件sh -x inotify.sh,观察远端共享文件目录
最后,按照需求将脚本添加到定时任务。

小知识点:

实时同步并发测试:(使用paste命令并列显示)
[root@localhost backup]# seq 10 >a.log
[root@localhost backup]# echo {a..j}|tr " " "\n" >b.log
[root@localhost backup]# paste a.log b.log >c.log
[root@localhost backup]# cat c.log
1 a
2 b
3 c
4 d
5 e
6 f
7 g
8 h
9 i
10 j
###########################################
ps:命令解释:
1.seq按序排列
2.|tr 替换(\n是换行符)
3.paste按列合并

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章