[root@backup ~]# ll /proc/sys/fs/inotify/ total 0 -rw-r--r-- 1 root root 0 Oct 17 10:12 max_queued_events -rw-r--r-- 1 root root 0 Oct 17 10:12 max_user_instances -rw-r--r-- 1 root root 0 Oct 17 10:12 max_user_watches
|
[b]文件[/b]
|
[b]默认值[/b]
|
[b]作用说明[/b]
|
|
max_user_watches
|
8192
|
设置inotifywait或inotifywatch命令可以监视的文件数量(单进程)
|
|
max_user_instances
|
128
|
设置每个用户可以运行的inotifywait或inotifywatch命令的进程数
|
|
max_queued_events
|
16384
|
设置inotify实例事件(event)队列可容纳的事件数量
|
[root@nfs01 ~]# man proc /proc/sys/fs/inotify (since Linux 2.6.13) This directory contains files max_queued_events, max_user_instances, and max_user_watches, that can be used to limit the amount of kernel memory consumed by the inotify interface. for further details, see inotify(7).
[root@nfs01 ~]# man 7 inotify /proc/sys/fs/inotify/max_queued_events The value in this file is used when an application calls inotify_init(2) to set an upper limit on the number of events that can be queued to the corresponding inotify instance. Events in excess of this limit are dropped, but an IN_Q_OVERFLOW event is always generated. /proc/sys/fs/inotify/max_user_instances This specifies an upper limit on the number of inotify instances that can be created per real user ID. /proc/sys/fs/inotify/max_user_watches This specifies an upper limit on the number of watches that can be created per real user ID.
|
[b]参数[/b]
|
[b]含义[/b]
|
|
-m, --monitor
[b](重要参数)[/b]
|
Keep listening for events forever. Without this option, inotifywait will exit after one event is received.
始终保持事件监听。
|
|
-d, --daemon
|
111
|
|
-r, --recursive
[b](重要参数)[/b]
|
Watch all subdirectories of any directories passed as arguments.
递归监控目录数据信息变化
|
|
-o, --outfile <file>
|
Print events to <file> rather than stdout.
打印事件到文件中,相当于标准正确输出
|
|
-s, --syslog
|
Output errors to syslog(3) system log module rather than stderr.
发送错误到syslog相当于标准错误输出
|
|
-q, --quiet
[b](重要参数)[/b]
|
If specified once, the program will be less verbose. Specifically, it will not state when it has completed establishing all inotify watches.
输出信息少(只打印事件信息)
|
|
--exclude <pattern>
|
Exclude all events on files matching the extended regular expression <pattern>.
排除文件或目录
|
|
--excludei <pattern>
|
Like --exclude but case insensitive.
排除文件或目录时,不区分大小写
|
|
--timefmt <fmt>
[b](重要参数)[/b]
|
Print using a specified printf-like format string; read the man page for more details.
指定时间输出格式
|
|
--format <fmt>
[b](重要参数)[/b]
|
Print using a specified printf-like formatstring; read the man page for more details.
打印使用指定的输出类似格式字符串;即实际监控输出内容
|
|
[b]-e[/b]
[b](重要参数)[/b]
|
Listen for specific event(s). If omitted, all events are listened for.
指定监听指定的事件,如果省略,表示所有事件都进行监听
|
|
以上的信息可以通过 inotifywait --help 获得
|
|
|
[b]事件名称[/b]
|
[b]事件说明[/b]
|
|
access
|
file or directory contents were read
文件或目录内容被读取
|
|
modify
|
file or directory contents were writterv
文件或目录内容被写入
|
|
attrib
|
file or directory attributes changed
文件或目录属性改变
|
|
close_write
[b](重要参数)[/b]
|
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
[b](重要参数)[/b]
|
file or directory moved to or from watched directory
文件或目录不管移动到或是移出监控目录都触发事件
|
|
create
[b](重要参数)[/b]
|
file or directory created within watched directory
文件或目录创建在监控目录中
|
|
delete
[b](重要参数)[/b]
|
file or directory deleted within watched directory
文件或目录被删除在监控目录中
|
|
delete_self
|
file or directory was deleted
文件或目录被删除,目录本身被删除
|
|
unmount
|
file system containing file or directory unmounted
|
|
以上的信息可以通过 inotifywait --help 获得
|
|
[root@nfs01 data]# touch test2.txt [root@nfs01 ~]# inotifywait -mrq /data --timefmt "%d-%m-%y %H:%M" --format "%T %w%f 事件信息: %e" -e create 17-10-17 11:19 /data/test2.txt 事件信息: CREATE
[root@nfs01 data]# \rm -f test1.txt [root@nfs01 ~]# inotifywait -mrq /data --timefmt "%d-%m-%y %H:%M" --format "%T %w%f 事件信息: %e" -e delete 17-10-17 11:28 /data/test1.txt 事件信息: DELETE
[root@nfs01 data]# echo "132" > test.txt [root@nfs01 ~]# inotifywait -mrq /data --timefmt "%d-%m-%y %H:%M" --format "%T %w%f 事件信息: %e" -e close_write 17-10-17 11:30 /data/test.txt 事件信息: CLOSE_WRITE,CLOSE
[root@nfs01 data]# mv /etc/hosts . [root@nfs01 ~]# inotifywait -mrq /data --timefmt "%d-%m-%y %H:%M" --format "%T %w%f 事件信息: %e" -e moved_to 17-10-17 11:33 /data/hosts 事件信息: MOVED_TO
[root@nfs01 data]# mv ./hosts /tmp/ [root@nfs01 ~]# inotifywait -mrq /data --timefmt "%d-%m-%y %H:%M" --format "%T %w%f 事件信息: %e" -e moved_from 17-10-17 11:34 /data/hosts 事件信息: MOVED_FROM
|
[b]命令参数[/b]
|
[b]参数说明[/b]
|
|
%w[b](重要参数)[/b]
|
事件出现时,监控文件或目录的名称信息
|
|
%f[b](重要参数)[/b]
|
事件出现时,将显示监控目录下触发事件的文件或目录信息,否则为空
|
|
%e[b](重要参数)[/b]
|
显示发生的事件信息,不同的事件信息用逗号进行分隔
|
|
%Xe
|
显示发生的事件信息,不同的事件信息有x进行分隔,可以修改X为指定分隔符
|
|
%T[b](重要参数)[/b]
|
输出时间格式中定义的时间格式信息,通过 --timefmt option 语法格式指定时间信息
这个格式是通过strftime函数进行匹配时间格式信息的
|
|
[b]以上的信息可以通过 inotifywait --help [/b][b]获得[/b]
|
|
|
[b]命令参数[/b]
|
[b]参数说明[/b]
|
|
%d[b](重要参数)[/b]
|
The day of the month as a decimal number(range 01 to 31)
每月的第几天,显示倍息为十进制数(范围是 01-31 )
|
|
%m[b](重要参数)[/b]
|
The month as a decimal number (range 01 to 12).
显示月份,显示信息为十进制(范围 01-12 )
|
|
%M
|
The minute as a decimal number (range 00 to 59).
显示分钟,显示信息为十进制(范围 00-59 )
|
|
%y[b](重要参数)[/b]
|
The year as a decimal number without a century (range 00 to 99).
年份信息,显示信息为十进制,并且没有世纪信息
|
|
%Y
|
The year as a decimal number including the century.
年份信息,显示信息为十进制,并且包含世纪信息
|
|
%H
|
The hour as a decimal number using a 24-hour clock (range 00 to 23).
小时信息,显示信息为十进制,使用 24小时制(范围 00-23 )
|
|
[b]说明:以上信息可以通过 man strftime[/b][b]信息获取[/b]
|
|
[root@nfs01 ~]# inotifywait -mrq /data --timefmt "%d/%m/%y %H:%M" --format "%T %w%f" 17/10/17 11:12 /data/test1.txt
|
[b]重要事件[/b]
|
[b]包含事件[/b]
|
[b]备注说明[/b]
|
|
[b]close[/b]
|
close_write
close_nowrite
|
文件或目录关闭,不管读或是写模式
即包含写关闭与读关闭
|
|
[b]close_write[/b]
|
create
|
包含文件创建事件,但不包含目录创建事件
|
|
[b]move[/b]
|
moved_to
moved_from
|
文件或目录不管移动到或是移动出监控目录都触发事件
即包含信息移入或移出监控目录事件
|
|
重要参数汇总:根据以上说明,在实际使用时,只要监控以下事件即可
create 创建、 delete 删除、 movedjto 移入、 close_write 修 改
inotifywait -mrq /data --format "%w%f" -e create,delete,moved_to,close_write
|
||
1窗口运行inotifywait 2窗口对文件夹进行操作,可在一窗口中查看出inotifywait的监控记录
[root@nfs01 ~]# inotifywait /data Setting up watches. Watches established. /data/ CREATE test1.txt /data/ OPEN test1.txt /data/ ATTRIB test1.txt /data/ CLOSE_WRITE,CLOSE test1.txt 创建文件,inotifywait显示创建文件的过程↑ [root@nfs01 data]# touch test1.txt
[root@nfs01 data]# mkdir testdir [root@nfs01 ~]# /data/ CREATE,ISDIR testdir
[root@nfs01 data]# touch testdir/test01.txt [root@nfs01 ~]# inotifywait -mrq /data /data/testdir/ OPEN test01.txt /data/testdir/ ATTRIB test01.txt /data/testdir/ CLOSE_WRITE,CLOSE test01.txt
[root@nfs01 data]# sed 's#132#123#g' test.txt -i [root@nfs01 ~]# inotifywait -mrq /data --timefmt "%d-%m-%y %H:%M" --format "%T %w%f 事件信息: %e" -e moved_from /data/test.txt 事件信息: OPEN /data/sedDh5R8v 事件信息: CREATE /data/sedDh5R8v 事件信息: OPEN /data/test.txt 事件信息: ACCESS /data/sedDh5R8v 事件信息: MODIFY /data/sedDh5R8v 事件信息: ATTRIB /data/sedDh5R8v 事件信息: ATTRIB /data/test.txt 事件信息: CLOSE_NOWRITE,CLOSE /data/sedDh5R8v 事件信息: CLOSE_WRITE,CLOSE /data/sedDh5R8v 事件信息: MOVED_FROM /data/test.txt 事件信息: MOVED_TO
inotifywait -mrq /data --timefmt "%d/%m/%y %H:%M" --format "%T %w%f 事件信息: %e" -e create
inotifywait -mrq /data --timefmt "%d/%m/%y %H:%M" --format "%T %w%f 事件信息: %e"
# inotifywait -mrq /data --timefmt "%F %H:%M" --format "%T %w%f 事件信息: %@e" -e delete 2017-10-17 11:28 /data/02.txt 事件信息: DELETE 2017-10-17 11:28 /data/03.txt 事件信息: DELETE 2017-10-17 11:28 /data/04.txt 事件信息: DELETE
# inotifywait -mrq /data --timefmt "%F %H:%M" --format "%T %w%f 事件信息: %@e" -e delete,close_write 2017-10-17 11:30 /data/oldgirl.txt 事件信息: CLOSE_WRITE@CLOSE 2017-10-17 11:30 /data/.oldgirl.txt.swx 事件信息: CLOSE_WRITE@CLOSE 2017-10-17 11:30 /data/.oldgirl.txt.swx 事件信息: DELETE 2017-10-17 11:30 /data/.oldgirl.txt.swp 事件信息: CLOSE_WRITE@CLOSE 2017-10-17 11:30 /data/.oldgirl.txt.swp 事件信息: DELETE 2017-10-17 11:30 /data/.oldgirl.txt.swp 事件信息: CLOSE_WRITE@CLOSE 2017-10-17 11:30 /data/.oldgirl.txt.swp 事件信息: DELETE
inotifywait -mrq /data --timefmt "%F %H:%M" --format "%T %w%f 事件信息: %@e" -e delete,close_write,moved_to 2017-10-17 11:34 /data/hosts 事件信息: MOVED_TO
[root@backup ~]# rpm -qa |grep rsync rsync-3.0.6-12.el6.x86_64
yum provides rysnc provides Find what package provides the given value
[root@backup ~]# vim /etc/rsyncd.conf uid = rsync gid = rsync use chroot = no max connections = 200 timeout = 300 pid file = /var/run/rsyncd.pid lock file = /var/run/rsync.lock log file = /var/log/rsyncd.log ignore errors read only = false list = false hosts allow = 172.16.1.0/24 auth users = rsync_backup secrets file = /etc/rsync.password [backup] comment = "backup dir by oldboy" path = /backup [nfsbackup] comment = "nfsbackup dir by hzs" path = /nfsbackup
[root@backup ~]# useradd -s /sbin/nologin -M rsync
[root@backup ~]# mkdir /nfsbackup/ [root@backup ~]# chown -R rsync.rsync /nfsbackup/
echo "rsync_backup:clsn123" >>/etc/rsync.password chmod 600 /etc/rsync.password
rsync --daemon
[root@backup ~]# ps -ef |grep rsync root 2076 1 0 17:05 ? 00:00:00 rsync --daemon root 2163 1817 0 17:38 pts/1 00:00:00 grep --color=auto rsync
[root@backup ~]# rpm -qa |grep rsync rsync-3.0.6-12.el6.x86_64
echo "clsn123" >>/etc/rsync.password chmod 600 /etc/rsync.password
[root@nfs01 sersync]# rsync -avz /data rsync_backup@172.16.1.41::nfsbackup --password-file=/etc/rsync.password sending incremental file list data/ data/.hzs data/.tar.gz data/.txt
[root@nfs01 ~]# yum repolist Loaded plugins: fastestmirror, security Loading mirror speeds from cached hostfile * base: mirrors.aliyun.com * epel: mirrors.aliyun.com * extras: mirrors.aliyun.com * updates: mirrors.aliyun.com repo id repo name status base CentOS-6 - Base - mirrors.aliyun.com 6,706 epel Extra Packages for Enterprise Linux 6 - x86_64 12,401 extras CentOS-6 - Extras - mirrors.aliyun.com 46 updates CentOS-6 - Updates - mirrors.aliyun.com 722 repolist: 19,875
[root@nfs01 ~]# rpm -ql inotify-tools /usr/bin/inotifywait #主要 /usr/bin/inotifywatch
#命令 man手册说明 # man inotifywait inotifywait - wait for changes to files using inotify 使用inotify进行监控,等待产生变化的文件信息 # man inotifywatch inotifywatch - gather filesystem access statistics using inotify 使用inotify进行监控,收集文件系统访问统计佶息
rsync -avz --delete /data/ rsync_backup@172.16.1.41::nfsbackup --password-file=/etc/rsync.password
inotifywait -mrq /data -format "%w%f" -e create,delete,move_to,close_write
[root@nfs01 sersync]# vim /server/scripts/inotify.sh #!/bin/bash inotifywait -mrq /data --format "%w%f" -e create,delete,moved_to,close_write|\ while read line do rsync -az --delete /data/ rsync_backup@172.16.1.41::nfsbackup --password- file=/etc/rsync.password done
#!/bin/bash Path=/data backup_Server=172.16.1.41 /usr/bin/inotifywait -mrq --format '%w%f' -e create,close_write,delete /data | while read line do if [ -f $line ];then rsync -az $line --delete rsync_backup@$backup_Server::nfsbackup --password-file=/etc/rsync.password else cd $Path &&\ rsync -az ./ --delete rsync_backup@$backup_Server::nfsbackup --password-file=/etc/rsync.password fi done
[root@nfs01 data]# sh /server/scripts/inotify.sh &
[root@nfs01 data]# touch {1..6}.txt
[root@backup ~]# ll /nfsbackup/ total 8 -rw-r--r-- 1 rsync rsync 0 Oct 17 12:06 1.txt -rw-r--r-- 1 rsync rsync 0 Oct 17 12:06 2.txt -rw-r--r-- 1 rsync rsync 0 Oct 17 12:06 3.txt -rw-r--r-- 1 rsync rsync 0 Oct 17 12:06 4.txt -rw-r--r-- 1 rsync rsync 0 Oct 17 12:06 5.txt -rw-r--r-- 1 rsync rsync 0 Oct 17 12:06 6.txt
[root@nfs01 data]# jobs [1]+ Running sh /server/scripts/inotify.sh &
[root@nfs01 data]# fg 1 sh /server/scripts/inotify.sh
01. sh inotify.sh &
02. nohup sh inotify.sh &
03. screen实现脚本程序后台运行
sh /server/scripts/inotify.sh & nohup nohup sh inotify.sh &
[root@test ~]# yum provides screen Loaded plugins: fastestmirror, security Loading mirror speeds from cached hostfile * base: mirrors.aliyun.com * epel: mirrors.aliyun.com * extras: mirrors.aliyun.com * updates: mirrors.aliyun.com base | 3.7 kB 00:00 epel | 4.3 kB 00:00 extras | 3.4 kB 00:00 updates | 3.4 kB 00:00 screen-4.0.3-19.el6.x86_64 : A screen manager that supports multiple logins on : one terminal Repo : base Matched from:
[root@test ~]# yum install -y screen
[root@test ~]# screen
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有