1.安装软件包
# yum install inotify-tools
# yum -y install rsync

2.同步机器相互添加信任

[root@host-10-0-100-106 ~]# ssh-keygen  #一路回车
[root@host-10-0-100-106 ~]# ssh-copy-id -i /root/.ssh/
[root@host-10-0-100-106 ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@10.0.100.139

3.两台机器上分别放置同步脚本

vim /shell/inotify.sh
#!/bin/bash
watch_dir=/data/wwwroot/www.ikmak.com/data/attachment
push_to=10.0.100.104 #另一台机器放置修改ip地址
inotifywait -mrq -e delete,close_write,moved_to,moved_from,isdir --timefmt '%Y-%m-%d %H:%M:%S' --format '%w%f:%e:%T' $watch_dir \
--exclude=".*.swp" |\
while read line;do
# logging some files which has been deleted and moved out
if echo $line | grep -i -E "delete|moved_from";then
echo "$line" >> /etc/inotify_away.log
fi
# from here, start rsync's function
rsync -az --delete --exclude="*.swp" --exclude="*.swx" $watch_dir $push_to:/data/wwwroot/www.ikmak.com/data/
if [ $? -eq 0 ];then
echo "sent $watch_dir success"
else
echo "sent $watch_dir failed"
fi
done

优化脚本

[root@xuexi tmp]# cat ~/inotify.sh
#!/bin/bash
watch_dir=/www
push_to=172.16.10.5 # First to do is initial sync
rsync -az --delete --exclude="*.swp" --exclude="*.swx" $watch_dir $push_to:/tmp inotifywait -mrq -e delete,close_write,moved_to,moved_from,isdir --timefmt '%Y-%m-%d %H:%M:%S' --format '%w%f:%e:%T' $watch_dir \
--exclude=".*.swp" >>/etc/inotifywait.log & while true;do
if [ -s "/etc/inotifywait.log" ];then
grep -i -E "delete|moved_from" /etc/inotifywait.log >> /etc/inotify_away.log
rsync -az --delete --exclude="*.swp" --exclude="*.swx" $watch_dir $push_to:/tmp
if [ $? -ne 0 ];then
echo "$watch_dir sync to $push_to failed at `date +"%F %T"`,please check it by manual" |\
mail -s "inotify+Rsync error has occurred" root@localhost
fi
cat /dev/null > /etc/inotifywait.log
rsync -az --delete --exclude="*.swp" --exclude="*.swx" $watch_dir $push_to:/tmp
else
sleep 1
fi
done

4.配置后台执行脚本

# nohup /shell/inotify.sh &
# crontab -e
* * * * * nohup /shell/inotify.sh > /dev/null 2>&1 &

  

  

 

 

Rsync+Inotify 搭建实时同步数据的更多相关文章

  1. rsync简介与rsync+inotify配置实时同步数据

    rsync简介 rsync是linux系统下的数据镜像备份工具.使用快速增量备份工具Remote Sync可以远程同步,支持本地复制,或者与其他SSH.rsync主机同步. rsync特性 rsync ...

  2. rsync+inotify实现实时同步案例--转

    转自:http://chocolee.blog.51cto.com/8158455/1400596 随着应用系统规模的不断扩大,对数据的安全性和可靠性也提出的更好的要求,rsync在高端业务系统中也逐 ...

  3. linux rsync +inotify 实现 实时同步

    前言:     rsync可以实现触发式的文件同步,但是通过crontab守护进程方式进行触发,同步的数据和实际数据会有差异,而inotify可以监控文件系统的各种变化,当文件有任何变动时,就触发rs ...

  4. rsync+inotify实现实时同步案例【转】

    1.1 inotify介绍 inotify是一种强大的.细粒度的.异步的文件系统事件控制机制.linux内核从2.6.13起,加入了inotify支持,通过inotify可以监控文件系统中添加.删除. ...

  5. rsync+inotify实现实时同步案例

    转自:http://chocolee.blog.51cto.com/8158455/1400596 随着应用系统规模的不断扩大,对数据的安全性和可靠性也提出的更好的要求,rsync在高端业务系统中也逐 ...

  6. Rsync+inotify实现实时同步

    1.1 inotify介绍 inotify是一种强大的.细粒度的.异步的文件系统事件控制机制.linux内核从2.6.13起,加入了inotify支持,通过inotify可以监控文件系统中添加.删除. ...

  7. CentOS 7 rsync+inotify实现实时同步

    测试环境如下: inotify-slave IP : 172.16.0.222 inotify-master IP : 172.16.0.233 对两台机的要求: 安装依赖包gcc: yum inst ...

  8. rsync+inotify实现实时同步,自动触发同步文件

    本文参考来自:http://chocolee.blog.51cto.com/8158455/1400596 我的需求和他的略有不同,同时做了一下更改,如下: 需求:两台机器相互为主备,搭建相同的两个服 ...

  9. rsync+inotify 实现实时同步

    inotify:这个可以监控文件系统中的添加,修改,删除,移动等事件 inotify的特性需要linux内核2.6.13以上的支持 [root@test1 inotify-tools-3.13]# u ...

随机推荐

  1. 注册google账号时出现手机号的问题

    注册谷歌账号时出现此电话号码无法用于进行验证 这主要是86和手机号之间有一个空格造成的,把这个空格删除就可以了

  2. linux shell脚本攻略笔记

    前一阵子系统学习了下<linux shell脚本攻略>这本书.在此记录下自己的学习笔记 1. 输出颜色字符  echo -e "\e[1:41m" 1表示背景色   2 ...

  3. 通过psexec实现远程安装软件包

    1.在管理机上下载和安装psexec https://docs.microsoft.com/en-us/sysinternals/downloads/psexec 2.在管理机上编写bat脚本,存放在 ...

  4. Python to list users in AWS

    code import boto3 c1=boto3.client('iam') #list_users will be a dict users=c1.list_users() #transfer ...

  5. 51Nod 1095 Anigram单词 | Hash

    Input示例 5 add dad bad cad did 3 add cac dda Output示例 1 0 2 题意:一系列字符串,查询字符串S,能通过其他字符串交换串内字符顺序得到的字符串个数 ...

  6. C11简洁之道:函数绑定

    1.  可调用对象 在C++中,有“可调用对象”这么个概念,那么什么是调用对象呢?有哪些情况?我们来看看: 函数指针: 具有operator()成员函数的类对象(仿函数): 可以被转换为函数指针的类对 ...

  7. cookie与session的区别与应用

    通常我们所说的浏览器自动保存密码,下次不用登陆,提示一次就不再出现的内容,大部分通过cookie或者session来实现的. cookie的概念 cookie是浏览器(User Agent)访问一些网 ...

  8. 重复代码Duplicated Code---要重构的信号

    什么时候需要重构,当你在项目代码里面嗅到这个味道的时候,就要进行重构.   首个介绍的味道是重复代码的味道.   它表现出来的特征是这些:   1.一个类里面,两个函数中,含有相同的代码,类似的代码: ...

  9. 省队集训 Day3 吴清华

    [题目大意] 给网格图,共有$n * n$个关键节点,横向.纵向距离均为$d$,那么网格总长度和宽度均为$(n+1) * d + 1$,最外围一圈除了四角是终止节点.要求每个关键节点都要通过线连向终止 ...

  10. 【BZOJ】2705: [SDOI2012]Longge的问题

    [题意]给定n,求∑gcd(i,n),(1<=i<=n),n<=2^32 [算法]数论(欧拉函数,gcd) [题解]批量求gcd的题目常常可以反过来枚举gcd的值. 记f(g)为gc ...