rsync+inotify实现实时同步,自动触发同步文件
本文参考来自:http://chocolee.blog.51cto.com/8158455/1400596
我的需求和他的略有不同,同时做了一下更改,如下:
需求:两台机器相互为主备,搭建相同的两个服务,实现数据同步,并长期运行脚本,不受hup影响
1.安装Rsync
http://www.cnblogs.com/liuquan/p/5413132.html
2.安装 inotify-slave部署
inotify是rsync客户端安装和执行的
企业场景压力测试200-300个同步限制,受网卡,磁盘,带宽等的制约。
[root@JD-BJ-747 ~]#ll /proc/sys/fs/inotify/
total 0
-rw-r--r-- 1 root root 0 Apr 26 14:20 max_queued_events
-rw-r--r-- 1 root root 0 Apr 26 14:20 max_user_instances
-rw-r--r-- 1 root root 0 Apr 26 14:20 max_user_watches
http://pan.baidu.com/s/1jIRLiei
tar -zxvf inotify-tools-3.14.tar.gz
./configure --prefix=/usr/local/inotify-3.14 make && make install
[root@inotify-master inotify-tools-3.14]# cd /usr/local/inotify-3.14/
[root@inotify-master inotify-3.14]# ./bin/inotifywait --help
-r|--recursive Watch directories recursively. #递归查询目录
-q|--quiet Print less (only print events). #打印监控事件的信息
-m|--monitor Keep listening for events forever. Without this option, inotifywait will exit after one event is received. #始终保持事件监听状态
--excludei <pattern> Like --exclude but case insensitive. #排除文件或目录时,不区分大小写。
--timefmt <fmt> strftime-compatible format string for use with %T in --format string. #指定时间输出的格式
--format <fmt> Print using a specified printf-like format string; read the man page for more details.
#打印使用指定的输出类似格式字符串
-e|--event <event1> [ -e|--event <event2> ... ] Listen for specific event(s). If omitted, all events are listened for. #通过此参数可以指定需要监控的事件,如下所示:
Events:
access file or directory contents were read #文件或目录被读取。
modify file or directory contents were written #文件或目录内容被修改。
attrib file or directory attributes changed #文件或目录属性被改变。
close file or directory closed, regardless of read/write mode #文件或目录封闭,无论读/写模式。
open file or directory opened #文件或目录被打开。
moved_to file or directory moved to watched directory #文件或目录被移动至另外一个目录。
move file or directory moved to or from watched directory #文件或目录被移动另一个目录或从另一个目录移动至当前目录。
create file or directory created within watched directory #文件或目录被创建在当前目录
delete file or directory deleted within watched directory #文件或目录被删除
unmount file system containing file or directory unmounted #文件系统被卸载
#/bin/bash
host01=$IP //写其他机器IP
src=/letv/fe
t/rsync
dst=/test/rsync
user=root
inotify_home=/usr/local/inotify
if [ ! -e "$src" ] \
||[ ! -e "${inotify_home}/bin/inotifywait" ] \
||[ ! -e "/usr/bin/rsync" ];
then
echo "Check File and Folder"
exit 9
fi
${inotify_home}/bin/inotifywait -mrq --timefmt '%d/%m/%y %H/%M' --format '%T %w%f' -e close_write,delete,create,attrib $src | while read files;
do
#rsync -aczP --delete --timeout=100 $src $user@$host01:$dst >/dev/null 2>&1
cd $src && /usr/local/bin/rsync -vrtapogL -R --delete ./ --timeout=100 $user@$host01:$dst
done
exit 0
5. 脚本后台执行,并开机自启,并且不能受到HUP影响而中断服务。
[root@cdn ~]# nohup sh inotify.sh &
[1] 4907
[root@cdn ~]# nohup: ignoring input and appending output to `nohup.out' [root@cdn ~]# echo "nohup sh inotify.sh &" >>/etc/rc.local
[root@cdn ~]#
我们知道,当用户注销(logout)或者网络断开时,终端会收到 HUP(hangup)信号从而关闭其所有子进程。因此,我们的解决办法就有两种途径:要么让进程忽略 HUP 信号,要么让进程运行在新的会话里从而成为不属于此终端的子进程。
nohup 示例
[root@pvcent107 ~]# nohup ping www.ibm.com &
[1] 3059
nohup: appending output to `nohup.out'
[root@pvcent107 ~]# ps -ef |grep 3059
root 3059 984 0 21:06 pts/3 00:00:00 ping www.ibm.com
root 3067 984 0 21:06 pts/3 00:00:00 grep 3059
[root@pvcent107 ~]#
6.测试
去/TEST/RSYNC目录下面创建文件,看看是否能自动触发同步过去。
另外一台机器操作与上方一致既可以实现互为主备,哪台机器备份目录优先有改动哪台即为主,就会执行同步脚本。
rsync+inotify实现实时同步,自动触发同步文件的更多相关文章
- linux rsync +inotify 实现 实时同步
前言: rsync可以实现触发式的文件同步,但是通过crontab守护进程方式进行触发,同步的数据和实际数据会有差异,而inotify可以监控文件系统的各种变化,当文件有任何变动时,就触发rs ...
- rsync+inotify 实现实时同步
inotify:这个可以监控文件系统中的添加,修改,删除,移动等事件 inotify的特性需要linux内核2.6.13以上的支持 [root@test1 inotify-tools-3.13]# u ...
- rsync + inotify 数据实时同步
一.rsync介绍 rsync英文全称为Remote synchronization,从软件的名称就可以看出来,Rsync具有可是本地和远程两台主机之间的数据快速复制同步镜像.远程备份的功能,这个功能 ...
- rsync简介与rsync+inotify配置实时同步数据
rsync简介 rsync是linux系统下的数据镜像备份工具.使用快速增量备份工具Remote Sync可以远程同步,支持本地复制,或者与其他SSH.rsync主机同步. rsync特性 rsync ...
- centos 配置rsync+inotify数据实时同步
何为rsync? 定义: rsync是一个开源的快速备份工具,可以在不同主机之间镜像同步整个目录树,支持增量备份,保持链接和权限,非常适用于异地备份 何为源端和发起端? 在远程同步过程中,负责发起rs ...
- rsync+inotify实现实时同步案例--转
转自:http://chocolee.blog.51cto.com/8158455/1400596 随着应用系统规模的不断扩大,对数据的安全性和可靠性也提出的更好的要求,rsync在高端业务系统中也逐 ...
- rsync+inotify实现实时同步案例【转】
1.1 inotify介绍 inotify是一种强大的.细粒度的.异步的文件系统事件控制机制.linux内核从2.6.13起,加入了inotify支持,通过inotify可以监控文件系统中添加.删除. ...
- centos 配置rsync+inotify数据实时同步2
一.Rsync服务简介 1. 什么是Rsync 它是一个远程数据同步工具,它在同步文件的同时,可通过LAN/WAN快速同步多台主机间的文件.Rsync使用所谓的“rsync算法”来使本地和远程两个主机 ...
- rsync+inotify实现实时同步案例
转自:http://chocolee.blog.51cto.com/8158455/1400596 随着应用系统规模的不断扩大,对数据的安全性和可靠性也提出的更好的要求,rsync在高端业务系统中也逐 ...
随机推荐
- OS--lab0+lab1+lab4+lab5+lab6+lab7
URL:https://github.com/Chasssser/MytestOR(Linux) git clone https://github.com/Chasssser/Mytest
- 关于document.write()加载JS等静态资源 和 异步async加载JS
现流行浏览器对于静态资源的预加载 传统的浏览器,对于静态资源加载,会阻塞 HTML 解析器的线程进行,无论内联还是外链. 例如: <script src="test1.js" ...
- Java三种代理模式
本文转自:https://mp.weixin.qq.com/s/nBmbNP2mR7ei-lDsuOxjWg 代理模式 代理(Proxy)是一种设计模式,提供了对目标对象另外的访问方式;即通过代理对象 ...
- JavaScript6里出现了哪些新语法、新特征?
ES5是2009年就出来的,目前来说在我写这篇文章的时候基本上ES6在浏览器上面还没有普及,不过Google浏览器是支持ES6语法的,谁让Google是美国生产的呢... ES6现在使用的地方其实还是 ...
- 06 day小数据池
1.小数据池,id() 小数据池针对的是: int ,str,bool-----都是不可变的数据类型 a.int 类型 a = 1000 b = 1000 print(id(a), id(b)) ...
- Appointment Helper
using System; using Microsoft.Xrm.Sdk; using Microsoft.Crm.Sdk.Messages; using Microsoft.Xrm.Sdk.Que ...
- Spark在实际项目中分配更多资源
Spark在实际项目中分配更多资源 Spark在实际项目中分配更多资源 性能调优概述 分配更多资源 性能调优问题 解决思路 为什么调节了资源以后,性能可以提升? 性能调优概述 分配更多资源 性能调优的 ...
- html 的radio单选框如何实现互斥------radio只是input的type属性
先看看没有互斥的情况: <html> <body> 男性:<input type="radio" id="male" /> ...
- jz_2440_电阻屏触摸驱动
驱动中: 入口函数init内: /* 1. 分配一个input_dev结构体 */ s3c_ts_dev = input_allocate_device(); /*------------------ ...
- win10 禁用自动更新
管理员身份运行CMD,输入REG add "HKLM\SYSTEM\CurrentControlSet\Services\UsoSvc" /v "Start" ...