inotify

安装inotify

  1. [[email protected] ~]# mkdir -p /home/oldboy/tools

安装inotify-tools-3.14.tar.gz

  1. [[email protected] tools]# ls -l /proc/sys/fs/inotify/ #出现下面三个表示支持inotify
  2. total 0
  3. -rw-r--r-- 1 root root 0 Feb 6 15:36 max_queued_events
  4. -rw-r--r-- 1 root root 0 Feb 6 15:36 max_user_instances
  5. -rw-r--r-- 1 root root 0 Feb 6 15:36 max_user_watches

解压:

  1. [[email protected] tools]# tar zxf inotify-tools-3.14.tar.gz

编译安装:

  1. [[email protected] tools]# cd inotify-tools-3.14
  2. [[email protected] inotify-tools-3.14]# ./configure --prefix=/usr/local/inotify-tools-3.14
  3. [[email protected] inotify-tools-3.14]# echo $?
  4. 0
  5. [[email protected] inotify-tools-3.14]# make && make install
  6. [[email protected] inotify-tools-3.14]# echo $? #0表示安装正确
  7. 0
  8. [[email protected] tools]# ln -s /usr/local/inotify-tools-3.14/ /usr/local/inotify-tools #创建软连接
  9. [[email protected] tools]# ls -l /usr/local/inotify-tools/
  10. total 16
  11. drwxr-xr-x. 2 root root 4096 Feb 6 15:43 bin #inotify执行命令(二进制)
  12. drwxr-xr-x. 3 root root 4096 Feb 6 15:42 include #inotify程序所需要的头文件
  13. drwxr-xr-x. 2 root root 4096 Feb 6 15:43 lib #动态链接的库文件
  14. drwxr-xr-x. 4 root root 4096 Feb 6 15:42 share #帮助文档
  1. [[email protected] inotify-tools]# ll bin/
  2. total 88
  3. -rwxr-xr-x. 1 root root 44287 Feb 6 15:43 inotifywait
  4. -rwxr-xr-x. 1 root root 41409 Feb 6 15:43 inotifywatch

inotifywait:在被监控的文件或目录上等待特定文件系统事件(open、close、delete等)发生,执行后处于阻塞转态,适合在shell脚本中使用。

inotifywatch:收集被监控的文件系统使用度统计数据,指文件系统事件发生的次数统计。

inotifywait命令常用参数

  1. [[email protected] inotify-tools]# ./bin/inotifywait --help
  2. inotifywait 3.14
  3. Wait for a particular event on a file or set of files.
  4. Usage: inotifywait [ options ] file1 [ file2 ] [ file3 ] [ ... ]
  5. Options:
  6.         -h|--help Show this help text.
  7.         @<file> Exclude the specified file from being watched.
  8.         --exclude <pattern>
  9.                         Exclude all events on files matching the
  10.                         extended regular expression <pattern>.
  11.         --excludei <pattern>
  12.                         Like --exclude but case insensitive. #排除文件或目录时,不区分大小写
  13.         -m|--monitor Keep listening for events forever. Without
  14.                         this option, inotifywait will exit after one
  15.                         event
    is received. #始终保持事件监听状态
  16.         -d|--daemon Same as --monitor, except run in the background
  17.                         logging events to a file specified by --outfile.
  18.                         Implies --syslog.
  19.         -r|--recursive Watch directories recursively. #递归查询目录
  20.         --fromfile <file>
  21.                         Read files to watch from <file> or `-‘ for stdin.
  22.         -o|--outfile <file>
  23.                         Print events to <file> rather than stdout.
  24.         -s|--syslog Send errors to syslog rather than stderr.
  25.         -q|--quiet Print less (only print events). #打印很少的信息,仅仅打印监控事件的信息
  26.         -qq Print nothing (not even events).
  27.         --format <fmt> Print using a specified printf-like format
  28.                         string; read the man page for more details.
  29.         --timefmt <fmt> strftime-compatible format string
    for use with
  30.                         %T in --format string. #指定事件输出的格式
  31.         -c|--csv Print events in CSV format.
  32.         -t|--timeout <seconds>
  33.                         When listening for a single event, time out after
  34.                         waiting for an event
    for <seconds> seconds.
  35.                         If <seconds> is 0, inotifywait will never time out.
  36.         -e|--event <event1> [ -e|--event <event2> ... ]
  37.                 Listen for specific event(s). If omitted, all events are
  38.                 listened for. #监控事件
  39.  
  40. Exit status:
  41.         0 - An event you asked to watch for was received.
  42.         1 - An event you did not ask to watch for was received
  43.               (usually delete_self or unmount), or some error occurred.
  44.         2 - The --timeout option was given and no events occurred
  45.               in the specified interval of time.
  46.  
  47. Events:
  48.         access file or directory contents were read
  49.         modify file or directory contents were written
  50.         attrib file or directory attributes changed
  51.         close_write file or directory closed, after being opened in
  52.                         writeable mode
  53.         close_nowrite file or directory closed, after being opened in
  54.                         read-only mode
  55.         close file or directory closed, regardless of read/write mode
  56.         open file or directory opened
  57.         moved_to file or directory moved to watched directory
  58.         moved_from file or directory moved from watched directory
  59.         move file or directory moved to or from watched directory
  60.         create file or directory created within watched directory
  61.         delete file or directory deleted within watched directory
  62.         delete_self file or directory was deleted
  63.         unmount file system containing file or directory unmounted

测试一:

窗口一:

  1. [[email protected] inotify-tools]# /usr/local/inotify-tools/bin/inotifywait -mrq --timefmt ‘%d/%m/%y %H:%M‘ --format ‘%T %w%f‘ -e create /data #create创建(监控创建事件)
  2. 06/02/17 16:13 /data/test.txt
  3. 06/02/17 16:13 /data/test2.txt
  4. 06/02/17 16:14 /data/1.txt
  5. 06/02/17 16:14 /data/2.txt
  6. 06/02/17 16:14 /data/3.txt
  7. 06/02/17 16:14 /data/4.txt

窗口二:

  1. [[email protected] ~]# cd /data/
  2. [[email protected] data]# ls
  3. [[email protected] data]# touch test.txt
  4. [[email protected] data]# touch test2.txt
  5. [[email protected] data]# touch {1..4}.txt

测试二:

窗口一:

  1. [[email protected] inotify-tools]# /usr/local/inotify-tools/bin/inotifywait -mrq --timefmt ‘%d/%m/%y %H:%M‘ --format ‘%T %w%f‘ -e delete /data #delete删除(监控删除事件)
  2. 06/02/17 16:17 /data/test.txt
  3. 06/02/17 16:17 /data/test2.txt

窗口二:

  1. [[email protected] data]# rm -f test.txt
  2. [[email protected] data]# rm -f test2.txt

同时监控多个事件,事件之间用逗号分隔。

  1. /usr/local/inotify-tools/bin/inotifywait -mrq --timefmt ‘%d/%m/%y %H:%M‘ --format ‘%T %w%f‘ -e delete,create,close_write /data

测试:

窗口一:

  1. [[email protected] scripts]# cat inotify.sh
  2. #!/bin/bash
  3. inotify=/usr/local/inotify-tools/bin/inotifywait
  4. $inotify -mrq --format ‘%w%f‘ -e create,close_write,delete /data
  5. |while read file
  6. do
  7.   cd / &&
  8.   rsync -az ./data --delete [email protected]::backup/
  9.   --password-file=/etc/rsync.password
  10. done
  11. [[email protected] scripts]# sh -x inotify.sh
  12. + inotify=/usr/local/inotify-tools/bin/inotifywait
  13. + read file
  14. + /usr/local/inotify-tools/bin/inotifywait -mrq --format %w%f -e create,close_write,delete /data
  15. + cd /
  16. + rsync -az ./data --delete [email protected]::backup/ --password-file=/etc/rsync.password
  17. + read file
  18. + cd /
  19. + rsync -az ./data --delete [email protected]::backup/ --password-file=/etc/rsync.password
  20. + read file

窗口二:

  1. [[email protected] data]# touch oldboy.txt
  2. [[email protected] data]# touch test.log

备份服务器:

  1. [[email protected] backup]# ls
  2. data
  3. [[email protected] backup]# tree
  4. .
  5. └── data
  6.     ├── 2.txt
  7.     ├── 3.txt
  8.     ├── 4.txt
  9.     ├── oldboy.txt
  10.     ├── test.log
  11.     └── test.txt
  1. [[email protected] scripts]# /bin/sh /server/scripts/inotify.sh & #放入后台执行

写入rc.local

应用场景:10—300k小文件并发200—300,不会有延迟。

关键参数说明:

在/proc/sys/fs/inotify目录下有三个文件,对inotify机制有一定的限制。

max_user_watches:设置inotifywait或inotifywatch命令可以监视的文件数量(单进程)。

max_user_instances:设置每个用户可以运行的inotifywait或inotifywatch命令的进程数。

max_queued_events:设置inotify实例事件(event)队列可容纳的事件数量。

  1. [[email protected] scripts]# cat /proc/sys/fs/inotify/max_user_watches # 修改为50000000
  2. 8192
  3. [[email protected] scripts]# cat /proc/sys/fs/inotify/max_user_instances #
  4. 128
  5. [[email protected] scripts]# cat /proc/sys/fs/inotify/max_queued_events #修改为50000000
  6. 16384

每秒200个文件并发,数据同步几乎无延迟。

inotify优点:

实时数据同步。

inotify缺点:

1、并发如果大于200个文件(10—100K),同步会有延迟。

2、监控到事件后,调用rsync同步是单进程的(加&并发),sersync多进程同步。

sersync的功能:

1、配置文件。

2、真正的守护进程socker。

3、可以对失败文件定时重传(定时任务)。

4、第三方的http接口。

5、默认多线程同步。

高并发数据实时同步方案:

1、文件级别:inotify(sersync)+rsync。

2、文件系统级别:drbd。

3、第三方软件的同步功能:mysql同步、oracle、mongodb。

4、程序双写。

5、业务逻辑解决。

时间: 2024-08-01 21:08:10

inotify的相关文章

rsync+inotify实时同步

一.Rsync简介: rsync是一个远程数据同步工具,可通过lan/wan快速同步多台主机间的文件.它使用所谓的"rsync演算法"来使本地和远程两个主机之间的文件达到同步,这个算法只传送两个文件的不同部分,而不是每次都整份传送,因此速度相当快.所以通常可以作为备份工具来使用. 运行rsync server的机器也叫backup server,一个rsync server可同时备份多个client的数据:也可以多个rsync server备份一个client的数据.rsync可以搭配

nfs 深度讲解及inotify

目  录 第1章共享目录的挂载及参数mount1 1.1挂载nfs下共享的data目录... 1 1.2 查看挂载的目录... 2 1.3 mount 挂载的参数... 2 1.3.1 mount –o 参数对用的选项... 3 1.3.2 man mount后的-o参数中英文翻译对比... 3 1.4 Mount挂载性能优化参数选项... 4 第2章 NFS深度讲解... 5 2.1 NFS内核优化建议... 5 2.2 服务端nfs内核优化... 5 2.3 企业生产场景NFS共享存储优化小

Linux inotify

是一种异步的文件系统事件监控机制 inotify进程                                                                    rsync  daemon  服务 /data/                                                                           /data/ 写入一个文件 通知rsync,执行命令同步 rsync  -avz   /data/xx      

inotify+rsync实现实时同步部署

1.1.架构规划 1.1.1架构规划准备 服务器系统 角色 IP Centos6.7 x86_64 NFS服务器端(NFS-server-inotify-tools) 192.168.1.14 Centos6.7 x86_64 rsync服务器端(rsync热备服务器) 192.168.1.17 1.1.2架构图 2.1 部署前检查 2.1.1 检查rsync热备服务器daemon是否起来 1 [[email protected] data]# lsof -i:873 2 COMMAND PID

inotify的原理

简单分享下inotify的原理 形象的说就是如果要监视哪个文件或者目录就在内核中某一个数据结构中添加一个记录,说明我要监视哪个目录或文件,并且标明监视的行为.在linux文件系统进行操作的时候,比如文件创建,修改,等等都会在底层先去查询下正在操作的目录有没有被监视,如果被监视了,并且监视的行为就是现在正在执行的行为,则会在哪个数据结构中留下记录,以此表明事件已经发生. 里面涉及的那些数据结构可以参考这篇博文http://blog.csdn.net/myarrow/article/details/

Inotify数达到限制或文件空间不足的不同表现同一本质原因分析

操作系统环境: LSB Version:    :base-4.0-amd64:base-4.0-noarch:core-4.0-amd64:core-4.0-noarch:graphics-4.0-amd64:graphics-4.0-noarch:printing-4.0-amd64:printing-4.0-noarch Distributor ID: RedHatEnterpriseServer Description:    Red Hat Enterprise Linux Serve

rsync、inotify实现web实时同步

. rsync.inotify实现web实时同步,布布扣,bubuko.com

windwos与linux基于inotify实现文件实时同步实战记录

cwRsync是基于cygwin平台的rsync软件包,支持windows对windows.windows对Linux.Linux对windows高效文件同步.由于CwRsync已经集成了cygwin类库,因此安装的时候可以省去cygwin包.Cwrsync还集成了OpenSSH for windows,可以实现Linux 下Rsync一模一样的操作.使用 cwRsync 来同步文件后,只需要对一台主服务器进行文件修改,其他镜像服务器可以自动同步,包括文件的更新.删除.重命名等. 正好符合我现在

rsync+inotify的使用注意事项

rsync+inotify是一个比较常用的实时同步解决方案,但是它并不是在所有的场景中都适用的,rsync+inotify比较适用的是在10台以下的一些小规模web集群中实时同步,不过在使用rsync+inotify解决方案和日常使用rsync不同,在rsync+inotify中rsync服务端需要部署多个而客户端只有一个,在rsync的客户端上做为日常内容发布的服务器从而由它把数据推送至各个各个rsync的服务端上.其中用来监控文件系统变化的工具是inotify-tools,在rsync的客户

sersync基于rsync+inotify实现数据实时同步

一.环境描述 需求:服务器A与服务器B为主备服务模式,需要保持文件一致性,现采用sersync基于rsync+inotify实现数据实时同步 主服务器A:192.168.1.23 从服务器B:192.168.1.243 实时同步/var/atlassian目录到从服务器. 二.实施 1.从服务器192.168.1.243 rsync服务搭建 1.1安装软件包 wget http://rsync.samba.org/ftp/rsync/src/rsync-3.1.1.tar.gz tar xf r