Linux学习-inotify和rsync同步

10 篇文章 0 订阅

Inotify 是一个 Linux特性,它监控文件系统操作,比如读取、写入和创建。Inotify 反应灵敏,用法非常简单,并且比 cron 任务的繁忙轮询高效得多。
Inotify实际基于事件驱动机制,为应用程序监控文件系统事件提供了实时响应事件的机制,无须通过cron等的轮询机制来获取事件。cron等机制不仅无法做到实时性,而且消耗大量系统资源。相比之下,inotify基于事件驱动,可以做到对事件处理的实时响应,也没有轮询造成的系统资源消耗,是非常自然的事件通知接口,也与自然世界事件机制相符合。
inotify—对同步数据目录信息的监控

rsync —完成对数据信息的实时同步

[root@localhost ~]# ll /proc/sys/fs/inotify
total 0
#max_queued_events设置inotify实例事件(event)队列可容纳的事件数量
-rw-r--r-- 1 root root 0 Jul 18 16:47 max_queued_events
#max_user_instances设置每个用户可运行的inotifywait或inotifywatch命令的进程数。
-rw-r--r-- 1 root root 0 Jul 18 16:47 max_user_instances
#max_user_watches设置inotifywait或inotifywatch命令可以监视的文件数量(单进程).
-rw-r--r-- 1 root root 0 Jul 18 16:47 max_user_watches

编译安装inotify-tools

#编译安装时版本不能过高,过高会报错
[root@localhost inotify-tools-3.14]# ./autogen.sh 
... ...
[root@localhost inotify-tools-3.14]# ./configure
... ...
[root@localhost inotify-tools-3.14]# make && make install
... ...

inotify-tools安装完成后会生成两个工具(命令),inotifywait和inotifywatch
inotifywait在被监控的文件或目录上等待特定文件系统事件(open close delete等)发生,执行后处于阻塞状态,适合在shell脚本中使用

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

inotifywait使用
inotifywait [-hcmrq] [-e ] [-t ] [–format ] [–timefmt ] [ … ]
-t , --timeout :如果在设置的秒内没有发生事件,则退出。默认为0,无限期等待。

-r (–recursive): 递归查询目录。

-d(–daemon):自后台运行。

-q(–quiet):如果指定一次,程序不会说明什么时候完成了所有inotify watches的监听。

如果指定两次,程序将不输出任何内容,但致命错误除外。

-m(monitor):不是在接收单个事件之后退出,而是无限期地执行。默认行为是在第一个事件发生后退出。始终保持事件监听状态。

–exclude :对指定的文件不做任何处理,POSIX扩展正则表达式,区分大小写。

–excludei :对指定的文件不做任何处理,POSIX扩展正则表达式,不区分大小写。

–timefmt : 设置由StrfTimes(3)接受的时间格式字符串,转换格式"%T"。

–format : 以用户指定的格式输出,使用类似printf的语法。事件字符串的输出限制在4000个字符左右,超出的部分将会被截断。格式化有:%w,%f,%e,%T。(%w:将会替换为事件发生时被监听的文件,%f: 当一个事件发生在一个目录中时,它将被替换为导致该事件发生的文件名。否则,将替换为空字符串。 %e:替换为发生的事件,以逗号分隔。%T:以–timefmt选项指定的格式替换成当前时间,该格式应该是适合于传递到STFFET(3)的格式字符串。
事件
access从被监视的目录中读取被监视的文件。
modify从被监视的目录中读取被写的文件。
attrib监视目录中元数据被修改的文件,这包括时间戳、文件权限、扩展属性等。
close_write监视目录中的文件以可写模式打开后被关闭。文件不一定写入内容。
close_nowrite监视目录中的文件以只读模式打开后被关闭。
close监视目录中的文件已关闭,不管它是如何打开的。实际上是通过监视close_write和close_nowrite来实现的,因此接收到的所有close事件都将作为其中之一输出,而不是close。
open已打开监视目录中的文件。
moved_to文件或目录已移动到监视目录中。即使文件从同一目录移动,也会发生此事件。
moved_from文件或目录从监视目录中移动。即使文件从同一目录移动,也会发生此事件。
move一个文件或目录被从一个被监视的目录移动到另一个被监视的目录。实际上是通过监听moved和moved两个目录来实现的,因此所有接收到的关闭事件都将作为其中的一个或两个输出
move_self监视的文件或目录已被移动。在此事件之后,不再监视文件或目录。
create文件或目录在监听目录中被创建。
delete文件或目录在监听目录中被删除。
delete_self监视的文件或目录已被删除。在此事件之后,不再监视该文件或目录。即使没有显式地监听此事件,也可能发生此事件。
unmount 已卸载监视文件或目录所在的文件系统。在此事件之后,不再监视文件或目录。即使没有显式地监听此事件,也可能发生此事件。

#-h查看帮助信息
[root@localhost ~]# inotifywait -h
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.
	-d|--daemon   	Same as --monitor, except run in the background
	              	logging events to a file specified by --outfile.
	              	Implies --syslog.
	-r|--recursive	Watch directories recursively.
	--fromfile <file>
	              	Read files to watch from <file> or `-' for stdin.
	-o|--outfile <file>
	              	Print events to <file> rather than stdout.
	-s|--syslog   	Send errors to syslog rather than stderr.
	-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

rsync 是一个常用的 Linux 应用程序,用于文件同步,它可以在本地计算机与远程计算机之间,或者两个本地目录之间同步文件(但不支持两台远程计算机之间的同步)。它也可以当作文件复制工具,替代cp和mv命令。
使用方法

#拷贝当前机器上的test.txt至192.168.0.109服务器的root目录下
[root@localhost ~]# touch test.txt
[root@localhost ~]# ls
anaconda-ks.cfg     inotify-tools-3.14.tar.gz  install.log.syslog
inotify-tools-3.14  install.log                test.txt
[root@localhost ~]# rsync -r test.txt 192.168.0.109:/root
The authenticity of host '192.168.0.109 (192.168.0.109)' can't be established.
RSA key fingerprint is 3c:42:8a:c5:84:28:0f:22:ef:1a:39:7f:c4:33:48:2c.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.0.109' (RSA) to the list of known hosts.
root@192.168.0.109's password: 
#同时拷贝两个文件至远程服务器的目录,拷贝的时候文件的修改时间会发生变化
[root@localhost ~]# rsync test1.txt test2.txt 192.168.0.109:/root
#使用-a(--archive)参数拷贝数据时,除了可以递归同步以外,还可以同步元信息(比如修改时间、权限等)。由于 rsync 默认使用文件大小和修改时间决定文件是否需要更新,所以-a比-r更有用
[root@localhost ~]# rsync -a test3.txt 192.168.0.109:/root
#使用-n参数模拟命令执行的结果,并不真的执行命令。-v参数则是将结果输出到终端,这样就可以看到哪些内容会被同步。
[root@localhost ~]# rsync -anv *.txt 192.168.0.109:/root
root@192.168.0.109's password: 
sending incremental file list
test.txt
test1.txt
test2.txt
test3.txt

sent 90 bytes  received 24 bytes  32.57 bytes/sec
total size is 0  speedup is 0.00 (DRY RUN)

#--delete用于同步两个目录,保持两个目录下内容同步
[root@localhost ~]# rsync -av --delete txtdirectory/ 192.168.0.109:/root/txtDirectory
root@192.168.0.109's password: 
sending incremental file list
./
deleting a.txt
test.txt
test2.txt
test3.txt

sent 201 bytes  received 72 bytes  78.00 bytes/sec
total size is 0  speedup is 0.00
#--version查看版本号
[root@localhost ~]# rsync --version
rsync  version 3.0.6  protocol version 30
Copyright (C) 1996-2009 by Andrew Tridgell, Wayne Davison, and others.
Web site: http://rsync.samba.org/
Capabilities:
    64-bit files, 64-bit inums, 64-bit timestamps, 64-bit long ints,
    socketpairs, hardlinks, symlinks, IPv6, batchfiles, inplace,
    append, ACLs, xattrs, iconv, symtimes

rsync comes with ABSOLUTELY NO WARRANTY.  This is free software, and you
are welcome to redistribute it under certain conditions.  See the GNU
General Public Licence for details.

  • 12
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值