linux inotify+rsync+ssh数据时时同步
<font class="Apple-style-span" color="#0000FF"><span class="Apple-style-span" style="color: rgb(68, 68, 68); font-family: Arial, Helvetica, sans-serif; line-height: 22px; "><p style="line-height: 22px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; text-indent: 2em; ">一、介绍:</p><p style="line-height: 22px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; text-indent: 2em; "> inotify 是一种文件系统的变化通知机制,如文件增加、删除等事件可以立刻让用户态得知。从而达到文件实时同步的目的。</p><p style="line-height: 22px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; text-indent: 2em; ">二、环境需求:</p><p style="line-height: 22px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; text-indent: 2em; "> 软件:inotify-tools-3.13.tar.gz<br style="line-height: 22px; "> Os: centos5.3<br style="line-height: 22px; "> Server1:192.168.2.87<br style="line-height: 22px; "> Server2:192.168.2.88<br style="line-height: 22px; "> 需求 server1 目录 /opt/sites/mxfarm.rekoo.com 时时同步 server2 /tmp/test</p><p style="line-height: 22px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; text-indent: 2em; ">三、环境搭建:<br style="line-height: 22px; "> <br style="line-height: 22px; ">1、首先**内核支持inotify,如果不支持对内核打补丁,一般情况内核在 2.6.3以上的都支持的。<br style="line-height: 22px; "> <a href="mailto:ludy@server1:~$ls" style="line-height: 22px; text-decoration: none; color: rgb(255, 70, 102); ">ludy@server1:~$ls</a> -l /proc/sys/fs/inotify/<br style="line-height: 22px; "> 总用量 0<br style="line-height: 22px; "> -rw-r--r-- 1 ludy ludy 0 2008-12-16 14:40 max_queued_events<br style="line-height: 22px; "> -rw-r--r-- 1 ludy ludy 0 2008-12-16 14:40 max_user_instances<br style="line-height: 22px; "> -rw-r--r-- 1 root root 0 2008-12-16 09:07 max_user_watches<br style="line-height: 22px; "> 如果没有的话,呵呵安装inotify<br style="line-height: 22px; ">2、软件安装:<br style="line-height: 22px; "> wget <a href="http://sourceforge.net/projects/inotify-tools/files/inotify-tools/3.13/inotify-tools-3.13.tar.gz/download" style="line-height: 22px; text-decoration: none; color: rgb(255, 70, 102); ">http://sourceforge.net/projects/inotify-tools/files/inotify-tools/3.13/inotify-tools-3.13.tar.gz/download</a><br style="line-height: 22px; "> tar zxvf inotify-tools-3.13.tar.gz<br style="line-height: 22px; "> cd inotify-tools-3.13<br style="line-height: 22px; "> ./configure<br style="line-height: 22px; "> make && make install<br style="line-height: 22px; "> ldconfig</p><p style="line-height: 22px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; text-indent: 2em; ">3、生成SSH KEY 让 server1 ssh访问 server2不需要密码<br style="line-height: 22px; "> ssh-keygen<br style="line-height: 22px; "> 然后把你用户目录下的 .ssh/id_rsa.pub 拷贝到 server2 的root下<br style="line-height: 22px; "> scp id_rsa.pub <a href="mailto:root@192.168.2.88:~/.ssh" style="line-height: 22px; text-decoration: none; color: rgb(255, 70, 102); ">root@192.168.2.88:~/.ssh</a><br style="line-height: 22px; "> mv id_rsa.pub authorized_keys<br style="line-height: 22px; "> chown root:root authorized_keys<br style="line-height: 22px; "> chmod 600 authorized_keys<br style="line-height: 22px; ">4、编辑配置文件inosync.sh<br style="line-height: 22px; "> #!/bin/sh<br style="line-height: 22px; "> SRC=/opt/sites/mxfarm.rekoo.com/<br style="line-height: 22px; "> <a href="mailto:DST=root@192.168.2.88:/tmp/test" style="line-height: 22px; text-decoration: none; color: rgb(255, 70, 102); ">DST=root@192.168.2.88:/tmp/test</a><br style="line-height: 22px; "> INWT=/usr/local/bin/inotifywait<br style="line-height: 22px; "> RSYNC=/usr/bin/rsync</p><p style="line-height: 22px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; text-indent: 2em; "> $INWT -mrq -e create,move,delete,modify $SRC | while read D E F;do<br style="line-height: 22px; "> rsync -aHqzt --delete $SRC $DST<br style="line-height: 22px; "> done</p><p style="line-height: 22px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; text-indent: 2em; "> 主要参数介绍:<br style="line-height: 22px; "> $INWT -mrq -e create,move,delete,modify $SRC | while read D E F;do<br style="line-height: 22px; "> rsync -aHqzt --delete $SRC $DST<br style="line-height: 22px; "> -m 是保持一直监听<br style="line-height: 22px; "> -r 是递归查看目录<br style="line-height: 22px; "> -q 是打印出事件~<br style="line-height: 22px; "> -e create,move,delete,modify<br style="line-height: 22px; "> 监听 创建 移动 删除 写入 事件 <br style="line-height: 22px; "> rsync -aHqzt $SRC $DST <br style="line-height: 22px; "> -a 存档模式<br style="line-height: 22px; "> -H 保存硬连接<br style="line-height: 22px; "> -q 制止非错误信息<br style="line-height: 22px; "> -z 压缩文件数据在传输<br style="line-height: 22px; "> -t 维护修改时间<br style="line-height: 22px; "> -delete 删除于多余文件</p><p style="line-height: 22px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; text-indent: 2em; ">四、运行并测试: <br style="line-height: 22px; "> 在server1上运行./inosync.sh &<br style="line-height: 22px; "> <br style="line-height: 22px; "> cd /opt/sites/mxfarm.rekoo.com<br style="line-height: 22px; "> touch hello</p><p style="line-height: 22px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; text-indent: 2em; "> 在server2上/tmp/test 下看看:<br style="line-height: 22px; "> ls /tmp/test<br style="line-height: 22px; "> hello</p><p style="line-height: 22px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; text-indent: 2em; ">五、监控多目录,多文件,同步到多台服务器<br style="line-height: 22px; "> <br style="line-height: 22px; ">脚本分析:<br style="line-height: 22px; ">#!/bin/sh<br style="line-height: 22px; ">#set -x<br style="line-height: 22px; ">#var <br style="line-height: 22px; ">src="/opt/sites/mxfarm.rekoo.com /opt/work" #将两个目录<br style="line-height: 22px; ">des_ip="192.168.2.9 192.168.2.88" #同步到两台服务器</p><p style="line-height: 22px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; text-indent: 2em; ">#function<br style="line-height: 22px; ">inotify_fun ()<br style="line-height: 22px; ">{<br style="line-height: 22px; ">/usr/local/bin/inotifywait -mrq -e modify,delete,create,move $1 | while read time file<br style="line-height: 22px; ">do<br style="line-height: 22px; ">for ip in $des_ip<br style="line-height: 22px; ">do</p><p style="line-height: 22px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; text-indent: 2em; ">#echo "`date +%Y%m%d-%T`: rsync -avzq --delete --progress $1 $ip:`dirname $1`"<br style="line-height: 22px; ">echo "`date +%Y%m%d-%T`: rsync -avzq --delete --progress $1 $ip:/tmp/"</p><p style="line-height: 22px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; text-indent: 2em; ">#rsync -avzq --delete --progress $1 $ip:`dirname $1`<br style="line-height: 22px; ">rsync -avzq --delete --progress $1 $ip:/tmp/ 将mxfarm.rekoo.com和work目录同步到/tmp/目录下</p><p style="line-height: 22px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; text-indent: 2em; ">echo<br style="line-height: 22px; ">done<br style="line-height: 22px; ">done<br style="line-height: 22px; ">}</p><p style="line-height: 22px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; text-indent: 2em; ">#main<br style="line-height: 22px; ">for a in $src<br style="line-height: 22px; ">do<br style="line-height: 22px; ">inotify_fun $a & <br style="line-height: 22px; ">done</p><p style="line-height: 22px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; text-indent: 2em; "> <br style="line-height: 22px; ">六、可以对不同的ip 不同的目录创建多个脚本进行监控</p></span></font>
页:
[1]