Centos6.5下rsync+inotify的配置详解

Centos 6.5配置rsync+inotify实现文件实时同步

1.安装rsync(两台机器执行相同的步骤)
yum install gcc 
yum install rsyncd xinetd -y
因为rsync是由xinetd启动的,所以需要修改一个配置
vim /etc/xinetd.d/rsync
disable = yes --修改为-->> disable = no
启动并查看是否启动成功
/etc/init.d/xinetd restart
netstat -atnp |grep 873
tcp        0      0 :::873                      :::*                        LISTEN      1503/xinetd
2.服务部署
yum install wget -y
wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
注意:需要检查你的系统是否支持inotify
内核需要在2.6.13以上
uname -r
2.6.32-431.el6.x86_64
并且有如下三个文件
ll /proc/sys/fs/inotify/
total 0
-rw-r--r-- 1 root root 0 Nov 13 19:38 max_queued_events
-rw-r--r-- 1 root root 0 Nov 13 19:38 max_user_instances
-rw-r--r-- 1 root root 0 Nov 13 19:38 max_user_watches
开始编译安装inotify
tar zxvf inotify-tools-3.14.tar.gz
cd inotify-tools-3.14
./configure --prefix=/usr/local/inotify && make && make install
cd /usr/local/inotify
查看一下下面的目录及文件	
ls -l
total 16
drwxr-xr-x. 2 root root 4096 Nov 13 19:42 bin ##inotify执行命令(二进制)
drwxr-xr-x. 3 root root 4096 Nov 13 19:42 include ##inotify程序所需用的头文件
drwxr-xr-x. 2 root root 4096 Nov 13 19:42 lib ##动态链接的库文件
drwxr-xr-x. 4 root root 4096 Nov 13 19:42 share ##帮助文档

工具集合介绍:

一共安装了2个工具(命令),即inotifywait和inotifywatch

inotifywait:在被监控的文件或目录上等待特定文件系统事件(open、close、delete等)发生,执行后处于阻塞状态,适合在shell脚本中使用。

inotifywatch:收集被监视的文件系统使用度统计数据,指定文件系统事件发生的次数统计。

创建一个软连接
ln -s /usr/local/inotify/bin/inotifywait /bin/

常用参数:
-r	##递归查询目录
-q  ##打印很少的信息,仅仅打印监控相关的信息
-m  ##始终保持事件监听状态
--excludei ##排除文件或目录时,不区分大小写
--timefmt  ##指定时间的输出格式

配置实时同步目录:

1.在这我们通过ssh秘钥的方法来认证,当然也可以用rsync指定密码的形式
  在同步端生成ssh秘钥对
ssh-keygen   (直接回车就行)

将生成的公钥同步到你的被同步机器
scp /root/.ssh/id_rsa.pub 192.168.2.11:/root/.ssh/ 	
需要在被同步机器上将公钥改名
ls -l /root/.ssh
total 8
-rw------- 1 root root 404 Nov 13 13:48 authorized_keys

或者可以直接用(这种方法不用改名,详情可百度密钥对的配置)
ssh-copy-id -i /root/.ssh/id_rsa.pub 192.168.2.11

2.同步端编写同步的脚本
cat /shell/auto_rsync.sh 
#!/bin/bash
/usr/local/inotify/bin/inotifywait -mrq -e modify,delete,create,attrib,move /usr/local/src |while read events
do
    rsync -avzP --delete -e 'ssh -p 60820' /usr/local/src/ 172.17.33.188:/usr/local/src
    echo "`date +'%Y-%m-%d %H:%M:%S'` 出现事件:$events" >> /shell/auto_rsync.log
done

3.在同步端创建一个abc的文件测试
touch /usr/local/src/abc

4.查看记录的log日志会有你刚刚创建的文件
5.在被同步机器上面查看文件或目录是否同步过去
posted @ 2017-11-13 14:24  于欢水  阅读(687)  评论(0编辑  收藏  举报