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 ...
随机推荐
- Ration Rose2003安装及破解
曾经学习UML的时候,用的是EA. 近期在看Head First想着画绘图装一下Ration Rose吧.于是就着手開始装.本来网上关于Ration Rose的安装及破解教程非常多,可是我在安装的过程 ...
- AbstractFactory抽象工厂模式
#include <iostream> using namespace std; class ProductAbase { public: ProductAbase(){} virtual ...
- Sql去重
distinct:用于返回唯一不同的值. 注意:1.语句中select显示的字段只能是distinct指定的字段,其他字段是不可能出现的. 2.distinct必须放在开头 如select disti ...
- CentOS 7 安装.NET Core 2.0
一.添加dotnet产品Feed 在安装.NET Core之前,您需要注册Microsoft产品Feed. 这只需要做一次. 首先,注册Microsoft签名密钥,然后添加Microsoft产品Fee ...
- 解决 ASP.NET Core Hangfire 未授权(401 Unauthorized)
相关文章:ASP.NET Core 使用 Hangfire 定时任务 ASP.NET Core Hangfire 在正式环境发布之后,如果访问 http://10.1.2.31:5000/hangfi ...
- jsp中EL表达式不起作用的问题1
问题:在jsp页面中使用el表达式取值,取不到值,但是使用jsp中嵌套java代码可以取到值,对应代码如下: 解决: 只要在 jsp中 头文件中写上 : <%@page isELIgnored= ...
- 图片验证码(Struts2中使用)
写在前面: 最近在项目中做了一个登录页面,用到了图片验证码的功能,所以记录一下.方便之后再有用到,直接拿来用即可.其实图片验证码的生成都是有固定步骤的,网上也有很多的例子,有的时候,如果不想深究,都是 ...
- LINUX:alias命令详解
发现目前安装的g++并没有开启选项 -std=c++11,无法使用c++11的新标准及其中的列表初始化.搜索后得到解决方法:键入:alias g++="g++ -std=c++11&quo ...
- iOS 轻松实现自定义TabBar
自定义TabBar的案例网上不少,昨天受到开发小伙伴的影响,尝试了一下非大神的取巧思路:Demo 1.创建RootViewController,后面创建几个继承的VC,将这几个VC添加到TabBarC ...
- ubuntu16.04 查询ip,网关,dns信息
用ifconfig命令只能查询ip,子网掩码信息,不能获取dns和网关信息 用下面命令即可查询 nmcli dev show