Linux命令之文件实时备份incron,rsync,inotify

本文发布时间: 2019-Mar-22
一、rsync、incron简介关于rsync工具的介绍和使用,以及其结合cron工具实现定时备份的功能,可参考博文:http://blog.csdn.net/wangjunjun2008/article/details/38658539;inotify 是一种文件变化通知机制,Linux内核2.6.13(2005/06/18)版本开始引入;inotify仅仅是一个API,需要通过开发应用程序进行调用;inotify-tools则是inotify的一种实现,它是一套组件,包括一个C库和几个命令行工具,这些命令行工具可用于通过命令行或脚本对某文件系统的事件进行监控;incron这个名称就是inotify cron system的合体,意思就是基于inotify的cron系统;包含一个后台守护进程(incrond)和一个事件编辑器(incron);详细介绍可参考:http://www.howtoforge.com/triggering-commands-on-file-or-directory-changes-with-incron二、验证Linux内核是否支持inotify支持(可略过)#检查机器内核是否支持inotify(多种方法)#方法一:查看linux内核版本,若版本为2.6.13+,则说明内核支持inotify$ uname -r#方法二:查看 /usr/include/sys/inotify.h 文件是否存在,若存在,则说明内核支持inotify$ ls -l /usr/include/sys | grep inotify.h#方法三:查看 /proc/sys/fs/inotify 目录是否存在,且该目录是否存在以下文件;若有,则说明内核支持inotify$ ls -l /proc/sys/fs/inotify-rw-r--r-- 1 root root 0 Oct 9 09:36 max_queued_events-rw-r--r-- 1 root root 0 Oct 9 09:36 max_user_instances-rw-r--r-- 1 root root 0 Oct 9 09:36 max_user_watches#注: /proc/sys/fs/inotify 目录下的文件为inotify的默认内核参数max_queued_events 默认值:16384;调用inotify_init时分配给inotify instance中可排队的event的数目的最大值,超出这个值得事件被丢弃,但会触发IN_Q_OVERFLOW事件;注意:max_queued_events 是 Inotify 管理的队列的最大长度,文件系统变化越频繁,这个值就应该越大;如果在日志中看到Event Queue Overflow,说明max_queued_events太小,需要调整参数后再次使用;max_user_instances 默认值:128;指定了每一个user ID可创建的inotify instatnces的数量上限;max_user_watches 默认值:8192;指定了每个inotify instance相关联的watches的上限(可以监控的文件或目录数目上限);三、incron、rsync下载、安装rsync下载、安装可参考:http://blog.csdn.net/wangjunjun2008/article/details/38658539;incron安装环境要求:Linux内核2.6.13+,通过uname -a 查看;incron下载地址:http://inotify.aiken.cz/?section=incron&page=download&lang=en;Incron安装步骤:$ 下载 incron-0.5.10.tar.gz 包;$ tar zxvf incron-0.5.10.tar.gz$ cd incron-0.5.10$ make$ make install#安装完成后会得到二进制文件incrond和incrontab,且存放到系统PATH中,可以直接使用;#使用yum安装: yum install incron#使用sudo安装: sudo apt-get install incron四、incrond、incrontab配置、使用及参数说明#启动后台守护线程incrond [start]#查看incrond帮助incrond -husage: incrond [<options>]<operation> may be one of the following:These options may be used:-?, --about gives short information about program-h, --help prints this help text-n, --foreground runs on foreground (no daemonizing)-k, --kill terminates running instance of incrond-f <FILE>, --config=<FILE> overrides default configuration file (requires root privileges)-V, --version prints program version#查看incron帮助incrontab -husage: incrontab [<options>] <operation>incrontab [<options>] <FILE-TO-IMPORT><operation> may be one of the following:-?, --about gives short information about program-h, --help prints this help text-l, --list 查看用户的incron监控-r, --remove removes user table-e, --edit 新增/编辑用户incron监控-t, --types 查看incron支持的监控事件类型-d, --reload request incrond to reload user table-V, --version prints program versionThese options may be used:-u <USER>, --user=<USER> overrides current user (需要root权限)-f <FILE>, --config=<FILE> overrides default configuration file (需要root权限)其中比较重要的选项为: -l, -t, -e; -e为配置incron的事件监控,格式如下:<path> <event> <command>每个监控占一行,默认配置在/var/spool/incron/目录下;【选项说明】<path> 欲监控的文件或者目录;<event> 对象事件,多个事件用逗号”,”隔开;可使用的监控事件如下:IN_ACCESS: 文件被访问;IN_MODIFY: 文件被修改;IN_ATTRIB: 文件属性被修改,如 chmod、chown、touch 等;IN_CLOSE_WRITE: 可写文件被关闭;IN_CLOSE_NOWRITE: 不可写文件被关闭;IN_CLOSE: 文件被关闭,等同于(IN_CLOSE_WRITE | IN_CLOSE_NOWRITE) ;IN_OPEN: 文件被打开;IN_MOVED_FROM: 文件被移走,如 mv ;IN_MOVED_TO: 文件被移来,如 mv、cp ;IN_MOVE: 文件被移动,等同于(IN_MOVED_FROM | IN_MOVED_TO);IN_CREATE: 创建新文件;IN_DELETE: 文件被删除,如 rm ;IN_DELETE_SELF: 自删除,即一个可执行文件在执行时删除自己;IN_MOVE_SELF: 自移动,即一个可执行文件在执行时移动自己;IN_ONESHOT: 仅监控一次事件;IN_ONLYDIR: 只监控目录;IN_UNMOUNT: 宿主文件系统被 umount;IN_ALL_EVENTS: all of the above events;IN_DONT_FOLLOW: Don't dereference pathname if it is a symbolic link;<command> 系统命令或脚本(测试发现,在命令中使用’重定向’无效,只能在脚本中使用);还可以使用下面的这些变量:$@:代表<path>,即监控对象;$#:发生系统事件的文件/目录(例如监控某文件夹,其下的某文件发生了变化,那么$#就代表了该文件名);$%:代表<event>,即发生的事件(事件代码);$&: 代表<event>,即发生的事件(事件数字编号);【举例如下】启动incrond: incrond start;配置incrontab: incrontab -e; 内容为: /home/test/mon IN_ALL_EVENTS echo “$@ $# $%”进入/home/test/mon/目录,在其中进行操作,则会打印对应的事件信息;【该部分内容转载自:http://hi.baidu.com/qu3999352/item/ad1aae6381e2329cc4d2496f】五、incron结合rsync实现数据实时备份(略)六、[扩展]关于inotifywait、inotifywatch命令的介绍及使用(一)inotify-tools下载、安装下载inotify-tools地址:https://github.com/rvoicilas/inotify-tools/wiki$ tar zxvf inotify-tools-3.14.tar.gz$ cd inotify-tools-3.14$ ./configure$ make$ make install#查看inotify命令$ ls -l /usr/local/bin | grep inotify-rwxr-xr-x 1 root root 37264 04-14 13:42 /usr/local/bin/inotifywait-rwxr-xr-x 1 root root 35438 04-14 13:42 /usr/local/bin/inotifywatch(二)inotifywait、inotifywatch命令的使用及参数说明按照上述方法安装完inotify-tools后,会在 /usr/local/bin 目录下新生成inotifywait和inotifywatch两个指令;inotifywait:用于等待一个特定事件;inotifywatch:用于收集被监控的文件系统统计数据,包括每个inotify事件发生多少次等信息;inotifywait命令常用选项如下:-m, --monitor 持续监控,inotifywait默认在监控的指定文件事件发生一次后退出;-r, --recursive 递归监控指定目录下的所有子目录及其文件;如果要监控的目录中文件数量巨大,则通常需要修改max_users_watchs内核参数,因为其默认值为8192;-e <event>, --event <event> 指定需要监控的特定事件,默认监控所有文件事件;--timefmt <fmt> 当在--format选项中使用%T时,--timefrt选项则可以用来指定自定义的符合strftime规范的时间格式,此时间格式可用的格式符可以通过strftime的手册页获取;--timefrt后常用的参数是'%d/%m/%y %H:%M';--format <fmt> 自定义inotifywait的输出格式;如 --format '%T %w %f';常用的格式符如下:%w:显示被监控文件的文件名;%f:如果发生某事件的对象是目录,则显示被监控目录的名字;默认显示为空串;%T:使用--timefmt选项中自定义的时间格式;-fromfile 从文件中读取需要监控的文件或排除的文件;一个文件一行,排除的文件以"@"开头;-z: 即“-zero” 输出表格的行和列,即使元素为空-r: 即“-recursive” 监视一个目录下的所有子目录-t: 即“-timeout” 设置超时时间-e: 即“-event” 只监听指定的事件-q, --quiet 不输出监控结果;当要排除同步某个目录时,为rsync添加--exculde=PATTERN参数(路径是相对路径);当要排除都某个目录的事件监控的处理时,为inotifywait添加--exclude或--excludei参数;例如,要监控/tmp/test目录及其内部所有文件上发生的create,delete,modify,close_write事件,则使用如下命令:# inotify -r --timefmt '%d/%m/%y %H:%M' --format '%T %w %f' -e create,delete,modify,close_write /tmp/test转载自:http://blog.chinaunix.net/uid-233544-id-3129307.html


(以上内容不代表本站观点。)
---------------------------------
本网站以及域名有仲裁协议。
本網站以及域名有仲裁協議。

2024-Mar-04 02:10pm
栏目列表