https://github.com/rvoicilas/inotify-tools/wiki/
远程执行脚本
ssh -l root 10.10.10.10 "/opt/test/stop.sh";
ssh -l root 10.10.10.10 "scp /opt/data/database.tcb 10.10.10.10:/opt/ttserver/test/test.tcb"
rsync+inotifywait
- #!/bin/sh
- /usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f %e' \
- -e modify,create,move \
- /opt/cms5/share/template/cms_page/template/ \
- | while read date time file event
- do
- rsync -av --progress --include=kk --exclude='*' --password-file=/etc/rsyncd.secrets /opt/backuptest/ haha@10.1.224.171::test >>/opt/log/templatenotify.log
- #echo "${date} ${time} ${file} ${event}"
- done
- #!/bin/sh
- #/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w %f %e' \
- /usr/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w %f %e' \
- -e modify,create,move \
- /opt/backuptest/ \
- | while read date time path file event
- do
- # rsync -av --password-file=/etc/rsyncd.secrets /opt/rsynctest/ test@10.10.10.10::rsynctest >>/tmp/rsync.log
- rsync --include='${file}' --exclude='*' -av ${path} root@10.1.224.171:${path}
- echo "${date} ${time} ${path} ${file} ${event}"
- done
--------templatenotify.sh----------
- #!/bin/sh
- #this is a test not used for production ,please look 154
- logfile="/opt/log/templatenotify-`date +%Y%m%d`.log"
- toip=10.11.10.10
- /usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f %e' \
- -e CLOSE_WRITE,delete,create,move \
- /opt/template/ \
- | while read date time file event
- do
- case "$event" in
- DELETE)
- echo "delete ${file} but not rsync ----delete--- over" >>${logfile};
- ;;
- *)
- if [ "${file: -4}" != '4913' ] && [ "${file: -1}" != '~' ]; then
- echo "rsync -av $file root@${toip}:`dirname $file`" >>${logfile}
- rsync -av $file root@${toip}:`dirname $file` >>${logfile} 2>&1
- echo "${date} ${time} ${file} ${event} --------over" >>${logfile}
- fi
- ;;
- esac
- done
定时删除无用文件比如log
crontab -l
0 3 * * * (find /opt/modules/ -type f -mtime +2 -exec rm -f {} \;)
- [@tc_157_49 share]# cat /usr/local/src/switch/templatenotify.sh
- #!/bin/sh
- #this is a test not used for production ,please look 154
- logfile="/opt/log/templatenotify-`date +%Y%m%d`.log"
- toip=10.11.157.53
- /usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f %e' \
- -e CLOSE_WRITE,delete,create,move \
- /opt/cms5/share/template/cms_page/template/ \
- | while read date time file event
- do
- case "$event" in
- DELETE)
- echo "delete ${file} but not rsync ----delete--- over" >>${logfile};
- ;;
- *)
- if [ "${file: -4}" != '4913' ] && [ "${file: -1}" != '~' ]; then
- echo "rsync -av $file root@${toip}:`dirname $file`" >>${logfile}
- rsync -av $file root@${toip}:`dirname $file` >>${logfile} 2>&1
- echo "${date} ${time} ${file} ${event} --------over" >>${logfile}
- fi
- ;;
- esac
- done
概要 - 为什么需要监控文件系统?
在日常工作中,人们往往需要知道在某些文件(夹)上都有那些变化,比如:- 通知配置文件的改变
- 跟踪某些关键的系统文件的变化
- 监控某个分区磁盘的整体使用情况
- 系统崩溃时进行自动清理
- 自动触发备份进程
- 向服务器上传文件结束时发出通知
Inotify到底是什么?
% grep INOTIFY_USER /boot/config-$(uname -r)
CONFIG_INOTIFY_USER=y
简单的文件变化通知样例:
% inotifywait -rme modify,attrib,move,close_write,create,delete,delete_self /srv/test
Setting up watches. Beware: since -r was given, this may take a while!
Watches established.
% mkdir /srv/test/infoq
% echo TODO > /srv/test/infoq/article.txt
% rm /srv/test/infoq/article.txt
/srv/test/ CREATE,ISDIR infoq
/srv/test/infoq/ CREATE article.txt
/srv/test/infoq/ MODIFY article.txt
/srv/test/infoq/ CLOSE_WRITE,CLOSE article.txt
/srv/test/infoq/ DELETE article.txt
% inotifywait --exclude '^/srv/test/(large|ignore)/' -rme modify,attrib,move,close_write,create,delete,delete_self /srv/test
Setting up watches. Beware: since -r was given, this may take a while!
Watches established.
% echo test > /srv/test/action.txt
% echo test > /srv/test/large/no_action.txt
% echo test > /srv/test/ignore/no_action.txt
% echo test > /srv/test/large-name-but-action.txt
/srv/test/ CREATE action.txt
/srv/test/ MODIFY action.txt
/srv/test/ CLOSE_WRITE,CLOSE action.txt
/srv/test/ CREATE large-name-but-action.txt
/srv/test/ MODIFY large-name-but-action.txt
/srv/test/ CLOSE_WRITE,CLOSE large-name-but-action.txt
Inotifywatch - 使用inotify来统计文件系统访问信息
% inotifywatch -v -e access -e modify -t 120 -r ~/InfoQ
Establishing watches...
Setting up watch(es) on /home/mika/InfoQ
OK, /home/mika/InfoQ is now being watched.
Total of 58 watches.
Finished establishing watches, now collecting statistics.
Will listen for events for 120 seconds.
total modify filename
2 2 /home/mika/InfoQ/inotify/
Inotify的配置选项
% inotifywait -r /
Setting up watches. Beware: since -r was given, this may take a while!
Failed to watch /; upper limit on inotify watches reached!
Please increase the amount of inotify watches allowed per user via `/proc/sys/fs/inotify/max_user_watches'.
# cat /proc/sys/fs/inotify/max_user_watches
8192
# echo 16000 > /proc/sys/fs/inotify/max_user_watches
# cat /proc/sys/fs/inotify/max_user_watches
16000
使用Inotify的一些工具
# echo username > /etc/incron.allow
/srv/test/ IN_CLOSE_WRITE mail -s "$@/$#\n" root
# strace -e inotify_init,inotify_add_watch tail -f ~log/syslog
[...]
inotify_init() = 4
inotify_add_watch(4, "/var/log/syslog", IN_MODIFY|IN_ATTRIB|IN_DELETE_SELF|IN_MOVE_SELF) = 1