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在高端业务系统中也逐 ...
随机推荐
- Zabbix——自动发现
前提条件: Zabbix版本为4.0 固定网段寻找网络设备,并添加组.添加模板.添加proxy. 设置完毕,等待~~ 如果没有问题,将会直接出现在host中.
- mysql碰到的问题总结
1.问题描述: 连接数据库出现大约10s延迟后才能连接,排除网络问题 解决方案: 通过抓包工具tcpdump抓包分析mysql在连接开始有近10s的空白请求,问题原因就在这 ,不知道在执行什么请求,后 ...
- [异常笔记]启动DFS报错:Cannot find configuration directory: /etc/hadoop
[hadoop@master ~]$ start-dfs.sh Incorrect configuration: namenode address dfs.namenode.servicerpc-ad ...
- jquery实现表单验证简单实例
/* 描述:基于jquery的表单验证插件. */ (function ($) { $.fn.checkForm = function (options) { var root = this; //将 ...
- mysql 操作表结构
整理一下对mysql表结构的简单操作,dos窗口模式的,现在基本上都是些图形化操作mysql,像这种命令形式的少了很多,暂时记忆点,以防以后忘记. 从数据库的创建开始: show databases; ...
- Flink基本概念
Flink基本概念 1.The history of Flink? 2.What is Flink? Apache Flink是一个开源的分布式.高性能.高可用.准确的流处理框架,主要由Java代码实 ...
- python学习笔记:第12天 列表推导式和生成器
目录 1. 迭代器 2. 推导式 1. 迭代器 什么是生成器呢,其实生成器的本质就是迭代器:在python中有3中方式来获取生成器(这里主要介绍前面2种) 通过生成器函数获取 通过各种推导式来实现生成 ...
- python爬xx图代码
今日 好热,照样是挖洞挖不到,看了几天的python爬虫,学会了xpath解析 撸一个代码玩玩] 不要说什么,优化之类的,刚学完,跑了一阵 ,还可以 挺稳定 # -*- coding:utf-8 - ...
- 我和Python的Py交易》》》》》》函数
一 函数是什么? 是数学中的函数? Python中 函数是指将一组语句的集合通过一个名字(函数名)封装起来的一段代码.(所以这里的函数是subroutine子程序) 那要函数干嘛.不都是代码吗?只不 ...
- 【8086汇编-Day6】关于loop的实验
实验内容 因为是要复制代码,所以常规来做是取代码段地址来用,所以把cs值mov给ax,但是这只是临时的,ax之后还有别的用途,那就把指令当作数据来存(把ax值 mov给ds,表示这一段地址用作代码段, ...