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

来源:LinuxIDC.com 作者:LinuxIDC.com
  
CentOS 5.3的rsync升级3。X

安装步骤

  1. wget http://packages.sw.be/rpmforge-release/rpmforge-release-0.3.6-1.el5.rf.i386.rpm   
  2. rpm -ivh rpmforge-release-0.3.6-1.el5.rf.i386.rpm  
  3. yum update rsync  

两台服务器 系统均为centos5.3IP为:

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   Server1ssh-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

完成后,注意查看manpageman inotify man inotifywait

·  inotifywait 仅执行阻塞,等待 inotify 事件。您可以监控任何一组文件和目录,或监控整个目录树(目录、子目录、子目录的子目录等等)。在 shell 脚本中使用 inotifywait

·  inotifywatch 收集关于被监视的文件系统的统计数据,包括每个 inotify 事件发生多少次。

 

4.写个inotif_rsync.sh脚本

  1. /bin/sh  
  2. src=/disk  
  3. des=/  
  4. ip=192.168.4.168  
  5. /usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format  '%T %w%f' /  
  6.  -e modify,delete,create,attrib /  
  7. ${src} /  
  8. while read  file  
  9.         do  
  10.                 rsync -avz --delete --progress ${src} root@${ip}:${des} &&  
  11.                 echo "${file} was rsynced"  
  12.                 echo "---------------------------------------------------------------------------"  
  13.         done  

将其赋予可执行权限:chmod 755 inotif_rsync.sh

执行此脚本:./ inotif_rsync.sh &


时间:2011-11-28 23:00 来源:LinuxIDC.com 作者:LinuxIDC.com 原文链接

好文,顶一下
(2)
100%
文章真差,踩一下
(0)
0%
------分隔线----------------------------


把开源带在你的身边-精美linux小纪念品
无觅相关文章插件,快速提升流量