rsync+inotify实现实时同步

一、rsync基本操作
rsync常用选项:
-n : 测试同步过程,不做实际修改
–delete :删除目标文件夹内多余的文档
-a :归档模式,相当于-rlptgoD
-v :显示详细操作信息
-z :传输过程中启用压缩/解压
用法:
rsync [选项…] 本地目录1 本地目录2
rsync [选项…] 本地目录1/ 本地目录2/

rsync -a /shell /test //将目录1作为目录2的子目录
rsync -a /shell/ /test/ //将目录1下的内容同步到目录2下
rsync -a --delete /shell/ /test/ 将目录1下的内容同步到目录2下,并删除目录2下多余的文件
二、rsync+ssh同步
用法:
rsync [选项…] user@host:远程目录/ 本地目录/ //将远程目录下的内容同步到本地目录下
rsync [选项…] 本地目录/ user@host:远程目录/ //将本地目录下的内容同步到远程目录下

rsync -a root@192.168.84.130:/root/shell/ /test/ //将远程目录下的内容同步到本地目录下
rsync -a test/ root@192.168.84.130:/root/shell/ //将本地目录下的内容同步到远程目录下
三、inotifywait工具
用法:
inotifywait [选项] 目标文件夹

inotifywait常用命令选项:
-m :持续监控(捕获一个事件后不退出)
-r :递归监控,包括子目录及文件
-q : 减少屏幕输出信息
-e :指定监控的modify(改变)、move(移动)、create(新建)、delete(删除)、attrib(修改权限)等事件类别
安装inotify-tools软件包:
1、配置centos6系统 yum源
wget -O /etc/yum.repos.d/CentOS6.5.repo http://mirrors.aliyun.com/repo/Centos-6.repo
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
2、执行命令进行安装
yum -y install inotify-tools
3、测试inotifywait监控
inotifywait -mrq -e modify,move,attrib,delete,create /root/test & //监控/root/test目录的变化,并置入后台
touch /root/test/test.txt //在目录/root/test下创建文件进行测试,观察屏幕输出信息。
四、配置网页同步(以httpd为例)
1、安装httpd服务
yum -y install httpd
2、部署ssh免密
ssh-keygen //生成密钥文件,一直回车即可
ssh-copy-id root@192.168.84.130 //将公钥文件上传至目标主机
3、编写同步脚本,进行实时同步
vim /root/rsync.sh

#!/bin/bash
dir="/var/www/"
cmd="rsync -az --delete $dir root@192.168.84.130:/var/www/"
while inotifywait -rqq -e modify,move,create,delete,attrib $dir
do
        $cmd
done &
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值