安装步骤
- wget http://packages.sw.be/rpmforge-release/rpmforge-release-0.3.6-1.el5.rf.i386.rpm
- rpm -ivh rpmforge-release-0.3.6-1.el5.rf.i386.rpm
- yum update rsync
一
两台服务器 系统均为centos5.3,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 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
完成后,注意查看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 &