一.
实验环境
两台服务器,系统均为
RHEL5-Update2
,内核为
2.6.18-92.el5
,
IP
为:
192.168.4.167-server1
192.168.4.168-server2
把
192.168.4.167
的
/disk
目录实时同步到
192.168.4.168
的
/disk
目录下
查看是否支持
inotify
,从
kernel 2.6.13
开始正式并入内核,
RHEL5
已经支持。
看看是否有
/proc/sys/fs/inotify/
目录,以确定内核是否支持
inotify
[root@RHEL5 Rsync]# ll /proc/sys/fs/inotify
total 0
-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
二.
具体操作
1.
生成
SSH KEY
让
server1
SSH server2
不需要密码
(1)
Server1
#
ssh-keygen -t rsa
这个命令生成一个密钥对:
id_rsa
(私钥文件)和
id_rsa.pub
(公钥文件)。默认被保存在
~/.ssh/
目录下。
(2)
公钥添加到远程主机的
authorized_keys
文件中
将文件上传到远程主机中
server1#scp ~/.ssh/id_rsa.pub
[email protected]:/root/
SSH
到登陆到远程主机,将公钥追加到
authorized_keys
文件中
server2#cat /root/id_rsa.pub >> /root/.ssh/authorized_keys
(3)server2#service sshd restart
2.
在
server1
上安装
rsync
Server1# tar �Czxvf rsync-3.0.2.tar.gz
Server1# cd rsync-3.0.2
Server1# ./configure
Server1#make
Server1# make install
3.
安装
inotify
Server1# tar �Czxvf inotify-tools-3.13.tar.gz
Server1# cd inotify-tools-3.13
Server1# ./configure
Server1# make
Server1# make install
完成后,注意查看
manpage
,
man inotify
、
man inotifywait
・
inotifywait
仅执行阻塞,等待
inotify
事件。您可以监控任何一组文件和目录,或监控整个目录树(目录、子目录、子目录的子目录等等)。在
shell
脚本中使用
inotifywait
。
・
inotifywatch
收集关于被监视的文件系统的统计数据,包括每个
inotify
事件发生多少次。
4.
写个
inotif_rsync.sh
脚本
/bin/sh
src=/disk
des=/
ip=192.168.4.168
/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' \
-e modify,delete,create,attrib \
${src} \
| while read file
do
rsync -avz --delete --progress ${src} root@${ip}:${des} &&
echo "${file} was rsynced"
echo "---------------------------------------------------------------------------"
done
将其赋予可执行权限:
chmod 755 inotif_rsync.sh
执行此脚本:
./ inotif_rsync.sh &
5.
测试
在
server1
服务器的
/disk
目录下创建文件或目录,到
server2
的
/disk
目录下可以看到即OK
Server1# cd /disk
Server1# touch aaa
Server2# cd /disk
Server2# ls
看到
aaa
文件即OK!!!!
三.
关于
inofity
的具体说明如下:
1.
otify
的系统相关参数:
/proc interfaces
The following interfaces can be used to limit the amount of kernel memory consumed by 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 a limit on the number of watches that can be associated with each inotify instance.
2.
otifywait
相关的参数(更多,查看
manpage
):
inotifywait
This command simply blocks for inotify events, making it appropriate for use in shell scripts. It can watch any set of files and directories, and
can recursively watch entire directory trees.
-m, --monitor
Instead of exiting after receiving a single event, execute indefinitely. The default behaviour is to exit after the first event occurs.
-r, --recursive
Watch all subdirectories of any directories passed as arguments. Watches will be set up recursively to an unlimited depth. Symbolic links are not
traversed. Newly created subdirectories will also be watched.
-q, --quiet
If specified once, the program will be less verbose. Specifically, it will not state when it has completed establishing all inotify watches.
-e <event>, --event <event>
Listen for specific event(s) only. The events which can be listened for are listed in the EVENTS section. This option can be specified more than once. If omitted, all events are listened for. use“
,
”separate multi events