您的位置:首页 > 其它

rsync+inotify-tools+ssh实现触发式远程实时同步

2012-04-25 22:36 736 查看
一、环境:
外网IP:X.X.X.69

内网IP:10.20.220.247

外网目录:/data/apps/ksoftmgr/ksafe/client/update

内网目录:/data/appsdata/softmgr-v3/static-file/update

二、项目需求:

外网需要同步内网的数据,从114.112.36.69拉10.20.220.247肯定是实现不了,只能把内网的数据推到外网服务器了,

三、实现步骤:

1.安装需要的软件

114.112.36.69上:

[root@localhost ~]# yum install rsync -y

10.20.220.247上:

[root@localhost ~]# yum install rsync inotify-tools -y

2.新建用来同步的数据的用户(两台机器上做同样的操作)
[root@localhost ~]# useradd wcm
[root@localhost ~]# passwd wcm
3.配置ssh无密钥登录
在10.20.220.247上:
[root@localhost ~]# su - wcm

[wcm@localhost ~]$ ssh-keygen -t dsa (一路回车)
[wcm@localhost ~]$ cd .ssh/

[wcm@localhost .ssh]$ ssh-copy-id -i id_dsa.pub 114.112.36.69
4.撰写脚本
[root@localhost ~]# cat /etc/init.d/inotify_rsync1.sh

#!/bin/sh

SRC=/data/appsdata/softmgr-v3/static-file/update

DST=wcm@114.112.36.69:/data/apps/ksoftmgr/ksafe/client

/usr/bin/inotifywait -mrq -e modify,delete,create,attrib ${SRC} | while read D E F

do

/usr/bin/rsync -ahqzt --delete $SRC $DST

done

5.执行脚本
[root@localhost ~]# /etc/init.d/inotify_rsync.sh &
6.加入开机启动
[root@localhost ~]# echo "/etc/init.d/inotify_rsync.sh &" >> /etc/rc.local
7.测试

在10.20.220.247的upte目录下写文件,看114.112.36.69对应的目录下是否会也生成。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: