阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

rsync+inotify 实现实时同步

136次阅读
没有评论

共计 2980 个字符,预计需要花费 8 分钟才能阅读完成。

rsync+inotify 实现实时同步

1、安装 rsync

可以通过 yum 源安装 rsync 服务
[root@dg test]# yum install rsync

也可以通过安装源码包
 wget  http://rsync.samba.org/ftp/rsync/src/rsync-3.0.9.tar.gz 
[root@dg test]#tar zxvf rsync-3.0.9.tar.gz 
[root@dg test]#cd rsync-3.0.9 
[root@dg test]#./configure –prefix=/usr/local/rsync 
[root@dg test]# make 
[root@dg test]#make install 

make -j 4 # make 编译,将源代码编译成二进制,可执行的文件 -j 4 使用 4 个进程同时编译,考虑 cpu 个数,不要超过 cpu 的个数

例如:在源端有个目录 /tmp/test 下有文件,都会同步目标端 10.10.6.82:/data 下,但是此方式只能手工同步,不能实现实时同步
[root@dg test]# rsync -azP –delete /tmp/test root@10.10.6.82:/data

注:rsync 是基于 ssh 协议,需要知道对服务器端的 root 和密码
-a,archive(存档)归档模式,表示以递归的方式传输文件,并且保持稳健的属性,等同于加了参数 -riptgoD
-z –compress 表示压缩
-P 显示传输速度
–delete 删除那些目标端位置有而原始位置没有的文件

添加 ssh 密钥服务,下次可以不用输入密码
[root@dg bin]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
1e:5f:6e:fe:15:a6:c1:12:d7:1d:4c:79:c5:9f:a3:9d root@dg

拷贝信任关系到目标端

[root@dg .ssh]# ssh-copy-id root@10.10.6.82

 

查看目标段密钥已经过去
[root@rac2 .ssh]# ls
authorized_keys  known_hosts

不用输入密码登录 10.10.6.82,此方式同步不用输入密码
[root@dg .ssh]# ssh root@10.10.6.82
Last login: Wed Jan 20 10:42:22 2016 from 10.10.8.54
[root@rac2 ~]#

2 安装 inotify

[root@dg test]#tar zxvf inotify-tools-3.14.tar.gz 
[root@dg test]# cd inotify-tools-3.14 
[root@dg test]# ./configure –prefix=/usr/local/inotify 
[root@dg test]# make 
[root@dg test]# make install

inotifywait -mrq -e create,move,delete,modify /tmp/test
-e 用来指定要监控哪些事件 这些事件包括 create,创建,move 移动,delete 删除,modify 修改内容 attrib 属性的更改
-m 表示持续监控
- r 表示递归监控
- q 表示简化输出信息

另外开了一个窗口:
root@dg test]# cp /etc/group  /tmp/test
[root@dg ~]# inotifywait -mrq -e create,move,delete,modify /tmp/test
/tmp/test/ CREATE group
/tmp/test/ MODIFY group

从此可以看出 inotifywait 可以捕捉到创建文件的消息

3 创建 rsync 复制脚本

此项功能主要是将 server 端的目录 /tmp 里的内容,如果修改了(无论是添加、修改、删除文件)能够通过 inotify 监控到,并通过 rsync 实时的同步给 client 的 /tmp 里,下面是通过 shell 脚本实现的。

[root@dg test]# cat rsync.sh
#!/bin/bash 
host=10.10.6.82
src=/data 
des=/data2
user=root
/usr/local/inotify/bin/inotifywait -mrq –timefmt ‘%d/%m/%y %H:%M’ –format ‘%T %w%f%e’ -e modify,delete,create,attrib $src  |while read files 
do 
/usr/bin/rsync -vzrtopg –delete  $src $user@$host:$des 
echo “${files} was rsynced” >>/rsync.log 2>&1 
done

其中 host 是 client 的 ip,src 是 server 端要实时监控的目录,des 是认证的模块名,需要与 client 一致,user 是建立密码文件里的认证用户。
把这个脚本命名为 rsync.sh,放到监控的目录里,比如我的就放到 /tmp 下面,并给予 764 权限,建议各位吧 rsync 的日志放到其他的目录下(非备份目录)。
[root@dg test]# chmod 764 rsync.sh

然后运行这个脚本
[root@dg test]#  sh /tmp/rsync.sh &

我们还可以把 rsync.sh 脚本加入到开机启动项里
echo “/tmp/rsync.sh” >> /etc/rc.local

在源端目录添加或者删除文件,目标端的文件夹也会改变

RSync 实现文件备份同步详解   http://www.linuxidc.com/Linux/2014-09/106967.htm

利用 inotifywait 监控主机文件和目录 http://www.linuxidc.com/Linux/2013-03/81075.htm

利用 inotify+rsync 实现 Linux 文件批量更新 http://www.linuxidc.com/Linux/2012-01/52132.htm

inotify-tools+rsync 实时同步文件安装和配置 http://www.linuxidc.com/Linux/2012-06/63624.htm

rsync 同步完整配置 http://www.linuxidc.com/Linux/2013-06/85781.htm

CentOS 6.5 下 Rsync 远程同步 http://www.linuxidc.com/Linux/2014-05/101084.htm

Rsync 的详细介绍 :请点这里
Rsync 的下载地址 :请点这里

本文永久更新链接地址 :http://www.linuxidc.com/Linux/2016-01/127803.htm

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-21发表,共计2980字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中