免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 658 | 回复: 1
打印 上一主题 下一主题

[网络管理] Centos6.3下rsync+inotify安装配置笔记 [复制链接]

求职 : Linux运维
论坛徽章:
203
拜羊年徽章
日期:2015-03-03 16:15:432015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:57:092015小元宵徽章
日期:2015-03-06 15:58:182015年亚洲杯之约旦
日期:2015-04-05 20:08:292015年亚洲杯之澳大利亚
日期:2015-04-09 09:25:552015年亚洲杯之约旦
日期:2015-04-10 17:34:102015年亚洲杯之巴勒斯坦
日期:2015-04-10 17:35:342015年亚洲杯之日本
日期:2015-04-16 16:28:552015年亚洲杯纪念徽章
日期:2015-04-27 23:29:17操作系统版块每日发帖之星
日期:2015-06-06 22:20:00操作系统版块每日发帖之星
日期:2015-06-09 22:20:00
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2016-01-14 11:46 |只看该作者 |倒序浏览
inotify是一种强大的,细粒度的,异步文件系统时间监控机制,它可以替代crond实现与rsync的触发式文件同步,从而监控文件系统中添加,删除,修改,移动等细粒事件,从LINUX 内核2.6.13起,就已加入了对inotify的支持,所以我们只需要安装一个第三方软件inotify-tools即可管理此服务。


   之前利用的rsync+crond来触发实现同步的瓶颈在于,rsync在同步数据时,需要先扫描所有文件后进行比对,而后进行差异传输,如果文件数量级别很大而且变化会很快,扫描所有文件会非常耗时,而且会存在漏同步的问题,造成效率低下。
   而rsync+inotify则会弥补前者先扫描后同步的效率问题,采用系统级别监控各种变化,当文件发生任何变化,就会触发rsync同步,解决效率与实时性问题。
LINUX操作系统:  centOS6.3 64bit
rsync:          系统自带
inotify-tools:  inotify-tools-master
www1(rsync server):192.168.7.73
www2(rsync client):192.168.7.74
拓扑图:

1
(server)表示仅服务端配置
(client)表示仅客户端配置
(server,client)表示客户端与服务端都需配置
环境搭建:(server,client)
1.关闭iptables和SELINUX
# service iptables stop
# setenforce 0
# vi /etc/sysconfig/selinux
---------------
SELINUX=disabled
---------------
判断LINUX系统内核是否达到2.6.13以上:
# uname -a
-------------
Linux www1.example.com 2.6.32-279.el6.x86_64 #1 SMP Fri Jun 22 12:19:21 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux
-------------
查看inotify目录是否存在:
# ls -lsart /proc/sys/fs/inotify/
------------------
总用量 0
0 dr-xr-xr-x 0 root root 0 6月   4 14:04 ..
0 dr-xr-xr-x 0 root root 0 6月   4 17:35 .
0 -rw-r--r-- 1 root root 0 6月   4 17:35 max_user_watches
0 -rw-r--r-- 1 root root 0 6月   4 17:35 max_user_instances
0 -rw-r--r-- 1 root root 0 6月   4 17:35 max_queued_events
------------------
若返回以上内容,则系统支持inotify.
一.安装rsyncserver,client)
传送门:http://www.showerlee.com/archives/419
配置:(server)
# vi /etc/rsyncd.conf
--------------------
uid = root
gid = root
use chroot = no
max connections = 10
strict modes = yes
port = 873
address = 192.168.7.73
[test]
path = /test
comment = mirror for test
ignore errors
read only = no
list = no
auth users = user
secrets file = /etc/rsync.pas
hosts allow = *
# hosts deny = 0.0.0.0/0
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
--------------------
启动rsync
# rsync --daemon --config=/etc/rsyncd.conf
重启xinetd使其配置生效:
# /etc/init.d/xinetd restart
二.安装inotify-tools:(server,client)
可以到https://github.com/rvoicilas/inotify-tools/下载zip包,然后传到系统进行编译安装:
# unzip inotify-tools-master.zip
# cd inotify-tools-master
# ./autogen.sh
# ./configure --prefix=/usr/local/inotify
# make && make install
配置client端的inotify:(client)
# vi /etc/rc.d/inotify.sh
该脚本在做客户端目录下文件若发生变化,则向服务端做同步上传操作,也就是保持客户端目录文件发生变化,服务端也相应改变。
------------------
#!/bin/bash
src=/test
des=test
ip=192.168.7.73
/usr/local/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y/%H:%M' --format '%T%w%f' -e modify,delete,create,attrib $src | while read file
  do
       rsync -vzrtopg --delete --progress $src user@$ip:des --password-file=/etc/rsync.pas &&
   echo "$src has been resynced"
  done
------------------
赋予执行权限
# chmod +x /etc/rc.d/inotify.sh
执行脚本并做开机启动:
# /etc/rc.d/inotify.sh
# echo "/etc/rc.d/inotify.sh" >> /etc/rc.local
注:这个脚本的作用是通过inotify监控文件目录的变化,进而触发rsync进行同步操作,由于这是通过内核完成的主动式触发操作,所以比rsync遍历整个目录的扫描方式效率要高很多。
验证:
在客户端创建5个文件,到服务端查看文件是否实时同步?
(client)
# cd /test
# touch 1 2 3 4 5
(server)
# cd /test
# ls
-------------
1  2  3  4  5
-------------
验证成功,client端的目录发生变化会实时同步到server端,类似一个网络raid-1
总结:
rsync+inotify比较适用于轻量级文件即时同步,如果量大建议还是使用共享存储方法解决。

求职 : Linux运维
论坛徽章:
203
拜羊年徽章
日期:2015-03-03 16:15:432015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:57:092015小元宵徽章
日期:2015-03-06 15:58:182015年亚洲杯之约旦
日期:2015-04-05 20:08:292015年亚洲杯之澳大利亚
日期:2015-04-09 09:25:552015年亚洲杯之约旦
日期:2015-04-10 17:34:102015年亚洲杯之巴勒斯坦
日期:2015-04-10 17:35:342015年亚洲杯之日本
日期:2015-04-16 16:28:552015年亚洲杯纪念徽章
日期:2015-04-27 23:29:17操作系统版块每日发帖之星
日期:2015-06-06 22:20:00操作系统版块每日发帖之星
日期:2015-06-09 22:20:00
2 [报告]
发表于 2016-01-14 14:31 |只看该作者
最核心其实就是rsync.sh脚本,并开机后天运行
vi /usr/local/inotify/rsync.sh   #编辑,添加以下代码

======================================

#!/bin/sh

srcdir=/home/www.osyunwei.com/

dstdir=home_www.osyunwei.com

excludedir=/usr/local/inotify/exclude.list

rsyncuser=home_www.osyunwei.com_user

rsyncpassdir=/etc/passwd.txt

dstip="192.168.21.127 192.168.21.128"

for ip in $dstip

do

rsync -avH --port=873 --progress --delete  --exclude-from=$excludedir  $srcdir $rsyncuser@$ip:dstdir --password-file=$rsyncpassdir

done

/usr/local/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e close_write,modify,delete,create,attrib,move $srcdir |  while read file

do

for ip in $dstip

do

rsync -avH --port=873 --progress --delete  --exclude-from=$excludedir  $srcdir $rsyncuser@$ip:dstdir --password-file=$rsyncpassdir

echo "  ${file} was rsynced" >> /tmp/rsync.log 2>&1

done

done

======================================

chmod +x /usr/local/inotify/rsync.sh   #添加脚本执行权限



============================================
6、设置脚本开机自动执行

vi /etc/rc.d/rc.local  #编辑,在最后添加一行

sh /usr/local/inotify/rsync.sh & #设置开机自动在后台运行脚本
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP