#实时同步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. Material使用05 自定义主题、黑夜模式\白天模式切换

    需求: 1 不使用materil依赖内建的主题,使用自己创建的主题 2 利用自己创建的主题实现白天模式和黑夜模式 1 自定义主题 1.1 创建自定义主题文件 them.scss // 引入materi ...

  2. redis的pipeline操作

    1.简单描述 redis是一个CS模式的tcp的server,一个client发起了命令操作的请求,然后会阻塞等待服务端的处理和数据的返回.基本上一个命令请求就是2个报文,一去一回.如果多个命令,每次 ...

  3. 关于Idea中右边的maven projects窗口找不到了如何调出来

    关于Idea中右边的maven  projects窗口找不到了如何调出来? 具体的idea版本我不太清楚,我用的是2016版,其他版本应该也是一样的. 首先idea自带了maven控件,不像Eclip ...

  4. Can't update: no tracked branch No tracked branch configured for branch dev.

    1.git pull 命令出现以下错误 $ git pull There is no tracking information for the current branch. Please speci ...

  5. GitLab配置ssh key

    一.背景 当前很多公司都选择git作为代码版本控制工具,然后自己公司搭建私有的gitlab来管理代码,我们在clone代码的时候可以选择http协议,当然我们亦可以选择ssh协议来拉取代码.但是网上很 ...

  6. MySQL 使用经验

    本文同时发表在https://github.com/zhangyachen/zhangyachen.github.io/issues/10 在索引中完成排序 SELECT thread_id FROM ...

  7. Foreign websites

    [社交] 1.Twitter. It's what's happening. 2.Telegram Messenger 3.Facebook - Log In or Sign Up 4.Instagr ...

  8. ES6 Proxy和Reflect(下)

    construct() construct方法用于拦截new命令. var handler = { construct (target, args) { return new target(...ar ...

  9. [知了堂学习笔记]_用JS制作《飞机大作战》游戏_第4讲(创建敌方飞机、敌方飞机发射子弹、玩家子弹击中敌方小飞机,小飞机死亡)

    一.创建敌方飞机 1.思考创建思路: 创建敌方飞机思路与创建玩家飞机思路一样: (1)思考敌方飞机具备什么属性: 敌方飞机的图片.坐标.飞行速度.状态(是否被击中) 设置小飞机被击中时消失时间.飞机可 ...

  10. vmstat 命令详解

    作用:vmstat 的含义为显示虚拟内存状态(virtual memor statics),但是它可以报告关于进程,内存,I/O 等系统整体运行状态 选项: -a 显示活动内页 -f 显示启动后创建的 ...