#实时同步inotify

1、inotify简介
inotify是一种强大的,细腻度的,异步的文件系统事件监控机制,linux内核从2.6.13起,加入了inotify支持,通过INOTIFY可以监控文件系统中添加、删除、修改、移动等各种事件,利用这个内核接口,第三方软件就可以监控文件系统下文件的各种变化情况,而inotify-tloos就是实施这样监控的软件。

2、inotify实施
检查rsync daemon服务是否服务正常,可以推送数据实施同步
ps -ef |grep rsync|grep -v grep
root 5959 1 0 18:52 ? 00:00:00 rsync --daemon
1)坚持当前系统是否支持inotify
uname -r 版本在2.6.13以上才支持
2.6.32-504.el6.x86_64
# ls -l /proc/sys/fs/inotify
-rw-r--r-- 1 root root 0 Apr 4 20:23 max_queued_events
-rw-r--r-- 1 root root 0 Apr 4 20:23 max_user_instances
-rw-r--r-- 1 root root 0 Apr 4 20:23 max_user_watches
#显示这三个文件则证明支持INOTIFY
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可监控的最大目录数量。如果监控的文件数目巨大,需要根据情况,适当增加此值的大小。
例如: echo 30000000 > /proc/sys/fs/inotify/max_user_watches

2)下载inotify源码包,编译安装
wget https://jaist.dl.sourceforge.net/project/inotify-tools/inotify-tools/3.13/inotify-tools-3.13.tar.gz
tar zxvf inotify-tools-3.13.tar.gz
cd inotify-tools-3.13
./configure --prefix=/usr/local/inotify-tools-3.13
make && make install
ln -s /usr/local/inotify-tools-3.13/ /usr/local/inotify
cd /usr/local/inotify

./inotifywait -help
inotifywait 3.13
Wait for a particular event on a file or set of files.
Usage: inotifywait [ options ] file1 [ file2 ] [ file3 ] [ ... ]
Options:
-h|--help Show this help text.
@<file> Exclude the specified file from being watched.
--exclude <pattern>
Exclude all events on files matching the
extended regular expression <pattern>.
--excludei <pattern>
Like --exclude but case insensitive. #排除文件或目录时,不区分大小写
-m|--monitor Keep listening for events forever. Without
this option, inotifywait will exit after one
event is received. #始终保持事件监听状态
-r|--recursive Watch directories recursively. #递归查询目录
--fromfile <file>
Read files to watch from <file> or - for stdin.
-q|--quiet Print less (only print events). #打印监控事件的信息
-qq Print nothing (not even events).
--format <fmt> Print using a specified printf-like format
string; read the man page for more details.
--timefmt <fmt> strftime-compatible format string for use with
%T in --format string. #指定时间输出的格式
-c|--csv Print events in CSV format.
-t|--timeout <seconds>
When listening for a single event, time out after
waiting for an event for <seconds> seconds.
If <seconds> is 0, inotifywait will never time out.
-e|--event <event1> [ -e|--event <event2> ... ]
Listen for specific event(s). If omitted, all events are
listened for. #通过此参数可以指定需要监控的事件,如下:

Exit status:
0 - An event you asked to watch for was received.
1 - An event you did not ask to watch for was received
(usually delete_self or unmount), or some error occurred.
2 - The --timeout option was given and no events occurred
in the specified interval of time.

Events:
access file or directory contents were read #文件或目录被读取
modify file or directory contents were written #文件或目录内容被修改
attrib file or directory attributes changed #文件或目录属性被修改
close_write file or directory closed, after being opened in
writeable mode
close_nowrite file or directory closed, after being opened in
read-only mode
close file or directory closed, regardless of read/write mode #文件或目录封闭,无论读/写模式
open file or directory opened #文件或目录被打开
moved_to file or directory moved to watched directory #文件或目录被移动至另外一个目录
moved_from file or directory moved from 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 #文件或目录被删除
delete_self file or directory was deleted
unmount file system containing file or directory unmounted #文件或目录被卸载
实时监控命令:
/usr/local/inotify/bin/inotifywait -mrq --timefmt '%d%m%y %H:%M' --format '%T %w%f' -e create,delete,close_write,attrib /data

inotify缺点:
1)并发不能大于200个文件

实时监控inotify脚本

#!/bin/bash
#inotify jiankong
cmd="/usr/local/inotify/bin/inotifywait"
src="/data"
mod="test"
user="rsyncback"
ip=192.168.233.129
passfile="/etc/rsync.password"
rsyc="/usr/bin/rsync"
#judge
if [ -f "$cmd" ] && [ -e "$src" ] && [ -n "$user" ] && [ -n "$mod" ] && [ -f "$passfile" ] && [ -f "$rsyc" ];then
echo "file path is ok"
else
exit
fi
while true
do
$cmd -mrq --format '%w%f' -e create,delete,modify,attrib,close_write,move $src|\
while read line
do
[ ! -e "$line" ] && break || \
echo $line >> /root/inotiity.log
$rsyc -az --delete $line ${user}@${ip}::$mod --password-file=$passfile
done
cd $src && $rsyc -az --delete ./ ${user}@${ip}::$mod --password-file=$passfile
done

linux 实时同步inotify的更多相关文章

  1. 实时同步inotify+rsync

    目的,要求 nfs储存服务器与backup备份服务器,数据同步,万一nfs储存服务器挂了,数据还在 实时同步备份软件服务 1)inotify 实时同步软件 2)sersync 实时同步软件 实时同步原 ...

  2. centos文件实时同步inotify+rsync

    我的应用场景是重要文件备份 端口:873,备份端打开即可 下载地址:https://rsync.samba.org/ftp/rsync/src/ 服务端和客户端要保持版本一致 网盘链接:https:/ ...

  3. inotify和rsync实现数据实时同步

    数据的实时同步 实现实时同步 要利用监控服务(inotify),监控同步数据服务器目录中信息的变化 发现目录中数据产生变化,就利用rsync服务推送到备份服务器上 实现实时同步的方法 ino ...

  4. rsync 与 inotify 的使用 & 实现实时同步备份

    今日内容 rsync 内容详细 上一篇内容问题 1.yum源问题 2.VPN链接正常,但是没办法通过172 3.VPN链接时,出现了DNS错误 4.掩码不对 5.openvpn开启错误 复制的命令 1 ...

  5. rsync 远程同步 实时同步备份 两种免交互的方式实现实时备份

    rsync 远程同步: 一款快速增量备份工具 Remote Sync,远程同步 支持本地复制,或者与其他SSH.rsync主机同步 作用:做数据备份 备份方式:      完全备份      增量备份 ...

  6. linux系统中rsync+inotify实现服务器之间文件实时同步

    最近需要对服务器上的文件实施动态备份,我又不想每次都手动来进行备份,在网上找了挺多资料,发现使用rsync就可以实现,如果想要实现实时同步,还可以使用rsync+inotify组合,本文就是以组合方式 ...

  7. linux设置rsync+inotify实时同步文件

    linux设置rsync+inotify实时同步文件   应用场景: 同步接收方:test01 接收目录:/opt/software/test/a/ 同步发起方:test02 同步目录:/opt/so ...

  8. (转)Linux下通过rsync与inotify(异步文件系统事件监控机制)实现文件实时同步

    Linux下通过rsync与inotify(异步文件系统事件监控机制)实现文件实时同步原文:http://www.summerspacestation.com/linux%E4%B8%8B%E9%80 ...

  9. Linux学习-利用inotify和rsync实现数据的实时同步

    一.inotify简介 1.inotify介绍 异步的文件系统事件监控机制,利用事件驱动机制,而无须通过诸如cron等的 轮询机制来获取事件,linux内核从2.6.13起支持 inotify,通过i ...

随机推荐

  1. 基于MATLAB的人脸识别算法的研究

    基于MATLAB的人脸识别算法的研究 作者:lee神 现如今机器视觉越来越盛行,从智能交通系统的车辆识别,车牌识别到交通标牌的识别:从智能手机的人脸识别的性别识别:如今无人驾驶汽车更是应用了大量的机器 ...

  2. Hibernate学习(二补充)关系映射----基于外键的双向一对一

    刚刚写的是基于外键的单向一对一.  那么双向一对一就是在单向一对一的基础上稍微改动就可以了. account.java和account.hbm.xml都不用变动  只要我们小小的变动address.j ...

  3. 主次设备号 Device Major and Minor Numbers

    对于一个设备文件而言真正重要的标志是它的主次设备号(major and minor device numbers).如果我们用ls命令列出/dev下的一个设备: frank@under:~$ ls - ...

  4. JavaScript操作符(一元操作符)

    JavaScript操作符包括算术操作符.位操作符.关系操作符和相等操作符.只能操作一个值的操作符叫做一元操作符. 递增和递减操作符 递增和递减操作符有两个版本:前置型和后置型.前置型操作符位于要操作 ...

  5. MVC+EF 入门教程(三)

    一.前言 上一节,我们将了生成数据库,那么这张我就将操作设计库. 二.在 Aplication 定义服务 在 Application 中添加文件夹(Blog)和 操作类(BlogServer).实例如 ...

  6. 以List为例浅谈C#的学习方法

    前言:关于学习方法的讨论其实是个比较模糊的概念,对于List的介绍的资料其实已经很多了,但是一般是介绍List本身,我打算分享的是,以温故List为例,来获取新知识的这么一个过程.这里的新知识也不是什 ...

  7. 4. sudo,PATH环境变量,修改字符集,ntpserver,加大文件描述符,隐藏内核版本,锁定关键系统文件

    1   命令:  visudo              98gg      98行             yy           复制当前行             p              ...

  8. 小白的Python之路 day1 数据类型,数据运算

    一.数据类型初识 1.数字 2 是一个整数的例子.长整数 不过是大一些的整数.3.23和52.3E-4是浮点数的例子.E标记表示10的幂.在这里,52.3E-4表示52.3 * 10-4.(-5+4j ...

  9. 伪列:Oracle显示查询结果前几条记录用rownum<=。去掉重复记录,保留最早录入记录:取出最小ROWID

    显示6-10行记录: 去掉重复记录,保留最早录入记录:取出最小ROWID SELECT deptno,dname,loc,min(ROWID) FROM dept GROUP BY deptno,dn ...

  10. Swift3.0 自定义tableView复用cell 的写法,与CollectionViewCell的不同,数据model

    Model数据 class HospitalModel: NSObject { //后边不赋值 会报错 var imgurl :String = "" var introducti ...