rsync组合inotify-tools完成实时同步[转]
一. 什么是inotify
inotify是一种强大的、细粒度的、异步的文件系统事件监控机制,Linux内核从2.6.13开始引入,允许监控程序打开一个独立文件描述符,并针对事件集监控一个或者多个文件,例如打开、关闭、移动/重命名、删除、创建或者改变属性。
CentOS6自然已经支持:
使用ll /proc/sys/fs/inotify命令,是否有以下三条信息输出,如果没有表示不支持。
total 0
-rw-r--r-- 1 root root 0 Dec 11 15:23 max_queued_events
-rw-r--r-- 1 root root 0 Dec 11 15:23 max_user_instances
-rw-r--r-- 1 root root 0 Dec 11 15:23 max_user_watches
/proc/sys/fs/inotify/max_queued_evnets表示调用inotify_init时分配给inotify instance中可排队的event的数目的最大值,超出这个值的事件被丢弃,但会触发IN_Q_OVERFLOW事件。
/proc/sys/fs/inotify/max_user_instances表示每一个real user ID可创建的inotify instatnces的数量上限。
/proc/sys/fs/inotify/max_user_watches表示每个inotify instatnces可监控的最大目录数量。如果监控的文件数目巨大,需要根据情况,适当增加此值的大小。
inotify-tools:
inotify-tools是为linux下inotify文件监控工具提供的一套C的开发接口库函数,同时还提供了一系列的命令行工具,这些工具 可以用来监控文件系统的事件。 inotify-tools是用c编写的,除了要求内核支持inotify外,不依赖于其他。inotify-tools提供两种工具,一是inotifywait,它是用来监控文件或目录的变化,二是inotifywatch,它是用来统计文件系统访问的次数。
下载inotify-tools-3.14-1.el6.x86_64.rpm,通过rpm包安装:
# rpm -ivh /apps/crm/soft_src/inotify-tools-3.14-1.el6.x86_64.rpm
warning: /apps/crm/soft_src/inotify-tools-3.14-1.el6.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 4026433f: NOKEY
Preparing... ########################################### [100%]
1:inotify-tools ########################################### [100%]
# rpm -qa|grep inotify
inotify-tools-3.14-1.el5.x86_64
二.inotifywait使用示例
监控/root/tmp目录文件的变化:
/usr/bin/inotifywait -mrq --timefmt '%Y/%m/%d-%H:%M:%S' --format '%T %w %f' \
-e modify,delete,create,move,attrib /root/tmp/
上面的命令表示,持续监听/root/tmp目录及其子目录的文件变化,监听事件包括文件被修改、删除、创建、移动、属性更改,显示到屏幕。执行完上面的命令后,在/root/tmp下创建或修改文件都会有信息输出:
2014/12/11-15:40:04 /root/tmp/ new.txt
2014/12/11-15:40:22 /root/tmp/ .new.txt.swp
2014/12/11-15:40:22 /root/tmp/ .new.txt.swx
2014/12/11-15:40:22 /root/tmp/ .new.txt.swx
2014/12/11-15:40:22 /root/tmp/ .new.txt.swp
2014/12/11-15:40:22 /root/tmp/ .new.txt.swp
2014/12/11-15:40:23 /root/tmp/ .new.txt.swp
2014/12/11-15:40:31 /root/tmp/ .new.txt.swp
2014/12/11-15:40:32 /root/tmp/ 4913
2014/12/11-15:40:32 /root/tmp/ 4913
2014/12/11-15:40:32 /root/tmp/ 4913
2014/12/11-15:40:32 /root/tmp/ new.txt
2014/12/11-15:40:32 /root/tmp/ new.txt~
2014/12/11-15:40:32 /root/tmp/ new.txt
...
三.组合rsync实现实时同步
这一步的核心其实就是在客户端创建一个脚本rsync.sh,适用inotifywait监控本地目录的变化,触发rsync将变化的文件传输到远程备份服务器上。为了更接近实战,我们要求一部分子目录不同步,如/root/tmp/log和临时文件。
1. 创建排除在外不同步的文件列表
排除不需要同步的文件或目录有两种做法,第一种是inotify监控整个目录,在rsync中加入排除选项;第二种是inotify排除部分不监控的目录,rsync自然也不会触发。我们选择第二种。
这个操作在客户端进行,/root/tmp/log以及/root/tmp/src目录下的js、css和一些临时文件不同步,所以不需要inotifywait,/root/tmp下的其他文件和目录都同步。(其实对于打开的临时文件,可以不监听modify时间而改成监听close_write)
# vi /etc/inotify_rsync_exclude.lst:
@/root/tmp/cache??*
@/root/tmp/src/js
@/root/tmp/src/css
@/root/tmp/src
@/root/tmp/src/20*/20*/20*/.??*
@/root/tmp/log/
目前测试看到只能用绝对路径。
2.客户端同步到远程的脚本rsync.sh
rsync.sh文件:
#variables
current_date=$(date +%Y%m%d_%H%M%S)
source_path=/home/guotmp/
log_file=/var/log/rsync_client.log
#rsync
rsync_server=192.168.1.84
rsync_user=ant5
rsync_pwd=/etc/rsync-84.pas
rsync_module=tmp
#rsync client pwd check
if [ ! -e ${rsync_pwd} ];then
echo -e "rsync client passwod file ${rsync_pwd} does not exist!"
exit 0
fi
#inotify_function
inotify_fun(){
/usr/bin/inotifywait -mrq --timefmt '%Y/%m/%d-%H:%M:%S' --format '%T %w %f' \
--fromfile /etc/inotify_rsync_exclude.lst -e modify,delete,create,move,attrib ${source_path} \
| while read file
do
/usr/bin/rsync -auvrtzopgP --progress --bwlimit=200 --password-file=${rsync_pwd} ${source_path} ${rsync_user}@${rsync_server}::${rsync_module}
done
}
--bwlimit=200用于限制传输速率最大200kb,因为在实际应用中发现如果不做速率限制,会导致巨大的CPU消耗。
在客户端运行脚本# ./rsync.sh即可实时同步目录。
本文摘自:http://segmentfault.com/a/1190000002427568
rsync组合inotify-tools完成实时同步[转]的更多相关文章
- CentOS 6.5 rsync+inotify实现数据实时同步备份
CentOS 6.5 rsync+inotify实现数据实时同步备份 rsync remote sync 远程同步,同步是把数据从缓冲区同步到磁盘上去的.数据在内存缓存区完成之后还没有写入到磁盘 ...
- sersync基于rsync+inotify实现数据实时同步
一.环境描述 需求:服务器A与服务器B为主备服务模式,需要保持文件一致性,现采用sersync基于rsync+inotify实现数据实时同步 主服务器A:192.168.1.23 从服务器B:192. ...
- Rsync+lsync实现触发式实时同步
使用rsync+lsync实现触发式实时同步 服务器信息 centos6.5 主:192.168.5.4 搭建lsync 从:192.168.5.3 搭建rsync 1.1 从服务器设置 # yum ...
- Rsync+inotify实现文件实时同步#附shell脚本
强烈推荐先仔细看此文 https://segmentfault.com/a/1190000002427568 实验环境 centos 7.3 vm2:192.168.221.128 同步服务器 vm1 ...
- 实战:rsync+inotify实现数据实时同步
Linux 内核从 2.6.13 版本开始提供了 inotify 通知接口,用来监控文件系统的各种变化情况,如文件存取.删除.移动等.利用这一机制,可以非常方便地实现文件异动告警.增量备份,并针对目录 ...
- Rsync + inotify 实现文件实时同步
Rsync 用来实现触发式的文件同步. Inotify-tools是一套组件,Linux内核从2.6.13版本开始提供了inotify通知接口,用来监控文件系统的各种变化情况,如文件存取.删除.移动等 ...
- Centos 6.5配置rsync+inotify实现文件实时同步
1.安装rsync(两台机器执行相同的步骤)yum install gcc yum install rsyncd xinetd -y因为rsync是由xinetd启动的,所以需要修改一个配置vim / ...
- rsync+inotify磁盘数据实时同步
一.rsync+inotify主服务器部署 1.1安装rsync [root@nginx ~]# cd /usr/src/ [root@nginx src]# tar zxvf rsync-3.0.9 ...
- rsync+inotify安装配置 实时同步文件
安装 #安装inotify 工具 [root@localhost ~]# yum install inotify-tools -y 常用命令 [root@localhost ~]# inotifywa ...
- rsync + inotify-tools实现文件的实时同步
文章摘自:http://lxw66.blog.51cto.com/5547576/1331048 rsync 帮助文档:http://man.linuxde.net/rsync 最近有个想法就是部署一 ...
随机推荐
- JS(获得当前时间并且用2015-01-01格式表示)
一个简单的小例子,实现获得当前时间,js代码如下: function getdate() {var date = new Date();var mon = date.getMonth() + 1; ...
- SpringMVC中向服务器传递时间参数时出现的问题
1. 问题描述: 今天在SpringMVC应用中上传参数的时候遇到如下问题: The request sent by the client was syntactically incorrect 这说 ...
- SPFA(建图) HDOJ 4725 The Shortest Path in Nya Graph
题目传送门 题意:有两种路径,每个点会分别在某一层,层相邻之间权值c.还有直接两点传送,花费w.问1到n的最短距离. 分析:1~n正常建边.然后n + a[i]表示i点在第a[i]层.然后再优化些就不 ...
- ASP.NET 操作Cookie详解 增加,修改,删除
Cookie,有时也用其复数形式Cookies,指某些网站为了辨别用户身份而储存在用户本地终端上的数据(通常经过加密).定义于RFC2109.它是网景公司的前雇员Lou Montulli在1993年3 ...
- ccc 函数中写函数
attackOnTarget: function (atkDir, targetPos) { var self = this; let deg = cc.radiansToDegrees(cc.pAn ...
- 洛谷 P1379 八数码难题 Label:判重&&bfs
特别声明:紫书上抄来的代码,详见P198 题目描述 在3×3的棋盘上,摆有八个棋子,每个棋子上标有1至8的某一数字.棋盘中留有一个空格,空格用0来表示.空格周围的棋子可以移到空格中.要求解的问题是:给 ...
- 去掉inline-block元素默认间距的几种方法
方法1:使用负margin值一般是-3px,部分浏览器可能不同,不太推荐使用. 方法2:去掉多余空格将元素紧挨着写去掉多余空格,但降低了可读性. 方法3:使用font-size:0在外层父元素加上fo ...
- db2代理和优化
DB2 的代理 (agent) 是位于 DB2 服务器中的服务于应用程序请求的一些进程或线程.当有外部应用程序连接至 DB2 实例提出访问请求时,DB2 的代理就会被激活去应答这些请求.一般 DB2 ...
- CF 706B 简单二分,水
1.CF 706B Interesting drink 2.链接:http://codeforces.com/problemset/problem/706/B 3.总结:二分 题意:给出n个数,再给 ...
- C语言-结构体
#include<stdio.h> struct stu //定义结构体类型 { int num; char *name; char *sex; float score; } boy[]= ...