linux 实时同步inotify
#实时同步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的更多相关文章
- 实时同步inotify+rsync
目的,要求 nfs储存服务器与backup备份服务器,数据同步,万一nfs储存服务器挂了,数据还在 实时同步备份软件服务 1)inotify 实时同步软件 2)sersync 实时同步软件 实时同步原 ...
- centos文件实时同步inotify+rsync
我的应用场景是重要文件备份 端口:873,备份端打开即可 下载地址:https://rsync.samba.org/ftp/rsync/src/ 服务端和客户端要保持版本一致 网盘链接:https:/ ...
- inotify和rsync实现数据实时同步
数据的实时同步 实现实时同步 要利用监控服务(inotify),监控同步数据服务器目录中信息的变化 发现目录中数据产生变化,就利用rsync服务推送到备份服务器上 实现实时同步的方法 ino ...
- rsync 与 inotify 的使用 & 实现实时同步备份
今日内容 rsync 内容详细 上一篇内容问题 1.yum源问题 2.VPN链接正常,但是没办法通过172 3.VPN链接时,出现了DNS错误 4.掩码不对 5.openvpn开启错误 复制的命令 1 ...
- rsync 远程同步 实时同步备份 两种免交互的方式实现实时备份
rsync 远程同步: 一款快速增量备份工具 Remote Sync,远程同步 支持本地复制,或者与其他SSH.rsync主机同步 作用:做数据备份 备份方式: 完全备份 增量备份 ...
- linux系统中rsync+inotify实现服务器之间文件实时同步
最近需要对服务器上的文件实施动态备份,我又不想每次都手动来进行备份,在网上找了挺多资料,发现使用rsync就可以实现,如果想要实现实时同步,还可以使用rsync+inotify组合,本文就是以组合方式 ...
- linux设置rsync+inotify实时同步文件
linux设置rsync+inotify实时同步文件 应用场景: 同步接收方:test01 接收目录:/opt/software/test/a/ 同步发起方:test02 同步目录:/opt/so ...
- (转)Linux下通过rsync与inotify(异步文件系统事件监控机制)实现文件实时同步
Linux下通过rsync与inotify(异步文件系统事件监控机制)实现文件实时同步原文:http://www.summerspacestation.com/linux%E4%B8%8B%E9%80 ...
- Linux学习-利用inotify和rsync实现数据的实时同步
一.inotify简介 1.inotify介绍 异步的文件系统事件监控机制,利用事件驱动机制,而无须通过诸如cron等的 轮询机制来获取事件,linux内核从2.6.13起支持 inotify,通过i ...
随机推荐
- 设置应用栏(Setting Up the App Bar)
今天星期五,刚从体育场打完球回来,洗了洗脚.明天还要继续上班,也是非常艰难.近期我的小腰有点不舒服,就早点睡觉歇息. 所以今天就简单的翻译一篇Android官方站点上的文章,我会加一些补充. 原文地址 ...
- LintCode-丑数
设计一个算法.找出仅仅含素因子3,5,7 的第 k 大的数. 符合条件的数如:3.5.7,9,15...... 您在真实的面试中是否遇到过这个题? Yes 例子 假设k=4, 返回 9 挑战 要求时间 ...
- hdu 1233 还是畅通project(kruskal求最小生成树)
还是畅通project Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota ...
- MyBatis_关联关系查询
一.关联查询 当查询的内容涉及到具有关联关系的多个表时,就需要使用关联查询.根据表与表间的关联关系的不同.关联查询分为四种: 一对一关联查询: 一对多关联查询: 多对一关联查询: 多对多关联查询: 二 ...
- Python 项目实践一(外星人入侵小游戏)第三篇
今天是圣诞节,公司放假一天,趁着有空,学习了一下午,多写一篇博客吧! 接着上节的继续学习, 一 重构:模块game_functions 在大型项目中,经常需要在添加新代码前重构既有代码.重构旨在简化既 ...
- Intellij 解除(去除)SVN关联
方法一:通过Intellj 来处理 1. 安装插件 打开Intellij的setting(ctrl+alt+s),选择plugins,在右边搜索框输入"SVN",搜索.选择&quo ...
- jspsmart(保存文件)+poi(读取excel文件)操作excel文件
写在前面: 项目环境:jdk1.4+weblogic 需求:能上传excel2003+2007 由于项目不仅需要上传excel2003,还要上传excel2007,故我们抛弃了jxl(只能上传exce ...
- ES6之Set方法与Map方法
ES6提供了新的数据结构--Set与Map,Set本身是一个构造函数且成员的值是唯一的,没有重复的值!!!Set()是一个存储已排序的无重复元素的数据而Map()是一对数据Map()使用关键值Key来 ...
- boost::algorithm(字符串算法库)
没什么说的,需要 #include<boost/algorithm/string.hpp> 1.大小写转换 std::string s("test string"); ...
- Windows下RabbitMQ安装及入门
1.Windows下安装RabbitMQ需要以下几个步骤 (1):下载erlang,原因在于RabbitMQ服务端代码是使用并发式语言erlang编写的,下载地址:http://www.erlang. ...