您的位置:首页 > 其它

使用rsync+inotify配置触发式(实时)远程同步

2009-06-10 17:19 816 查看
[align=center] [/align]

一.实验环境
两台服务器,系统均为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目录下
[align=left]查看是否支持inotify,从kernel 2.6.13开始正式并入内核,RHEL5已经支持。[/align]
[align=left]看看是否有 /proc/sys/fs/inotify/目录,以确定内核是否支持inotify[/align]
[align=left][root@RHEL5 Rsync]# ll /proc/sys/fs/inotify[/align]
[align=left]total 0[/align]
[align=left]-rw-r--r-- 1 root root 0 Oct 9 09:36 max_queued_events[/align]
[align=left]-rw-r--r-- 1 root root 0 Oct 9 09:36 max_user_instances[/align]
[align=left]-rw-r--r-- 1 root root 0 Oct 9 09:36 max_user_watches[/align]

二.具体操作
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 root@192.168.4.168:/root/
SSH到登陆到远程主机,将公钥追加到 authorized_keys 文件中
server2#cat /root/id_rsa.pub >> /root/.ssh/authorized_keys
(3)server2#service sshd restart

2.在server1上安装rsync
Server1# tar –zxvf rsync-3.0.2.tar.gz
Server1# cd rsync-3.0.2
Server1# ./configure
Server1#make
Server1# make install

3.安装inotify
Server1# tar –zxvf inotify-tools-3.13.tar.gz
Server1# cd inotify-tools-3.13
Server1# ./configure
Server1# make
Server1# make install
[align=left]完成后,注意查看manpage,man inotify 、 man inotifywait[/align]
[align=left]· inotifywait 仅执行阻塞,等待 inotify 事件。您可以监控任何一组文件和目录,或监控整个目录树(目录、子目录、子目录的子目录等等)。在 shell 脚本中使用 inotifywait。 [/align]
· 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的具体说明如下:
[align=left]1.otify的系统相关参数:[/align]
[align=left] /proc interfaces[/align]
[align=left] The following interfaces can be used to limit the amount of kernel memory consumed by inotify:[/align]
[align=left] [/align]
[align=left] /proc/sys/fs/inotify/max_queued_events[/align]
[align=left] 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.[/align]
[align=left] [/align]
[align=left] /proc/sys/fs/inotify/max_user_instances[/align]
[align=left] This specifies an upper limit on the number of inotify instances that can be created per real user ID.[/align]
[align=left] [/align]
[align=left] /proc/sys/fs/inotify/max_user_watches[/align]
[align=left] This specifies a limit on the number of watches that can be associated with each inotify instance.[/align]
[align=left] [/align]
[align=left] [/align]
[align=left]2.otifywait 相关的参数(更多,查看manpage):[/align]
[align=left]inotifywait[/align]
[align=left]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.[/align]
[align=left]-m, --monitor[/align]
[align=left] Instead of exiting after receiving a single event, execute indefinitely. The default behaviour is to exit after the first event occurs.[/align]
[align=left]-r, --recursive[/align]
[align=left] Watch all subdirectories of any directories passed as arguments. Watches will be set up recursively to an unlimited depth. Symbolic links are not [/align]
[align=left] [/align]
[align=left]traversed. Newly created subdirectories will also be watched.[/align]
[align=left]-q, --quiet[/align]
[align=left] If specified once, the program will be less verbose. Specifically, it will not state when it has completed establishing all inotify watches.[/align]
[align=left] -e <event>, --event <event>[/align]
[align=left] 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[/align]

本文出自 “才刚上路” 博客,请务必保留此出处http://jlsfwq.blog.51cto.com/818700/165603
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: