服务器维护,服务器代维,安全设置,漏洞扫描,入侵检测服务

dirtysea 发表于 2010-4-22 14:10:51

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; ">&nbsp;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; ">&nbsp;软件:inotify-tools-3.13.tar.gz<br style="line-height: 22px; ">&nbsp;Os: centos5.3<br style="line-height: 22px; ">&nbsp;Server1:192.168.2.87<br style="line-height: 22px; ">&nbsp;Server2:192.168.2.88<br style="line-height: 22px; ">&nbsp;需求 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; ">&nbsp;&nbsp;<br style="line-height: 22px; ">1、首先**内核支持inotify,如果不支持对内核打补丁,一般情况内核在 2.6.3以上的都支持的。<br style="line-height: 22px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="mailto:ludy@server1:~$ls" style="line-height: 22px; text-decoration: none; color: rgb(255, 70, 102); ">ludy@server1:~$ls</a>&nbsp;-l /proc/sys/fs/inotify/<br style="line-height: 22px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 总用量 0<br style="line-height: 22px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -rw-r--r-- 1 ludy ludy 0 2008-12-16 14:40 max_queued_events<br style="line-height: 22px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -rw-r--r-- 1 ludy ludy 0 2008-12-16 14:40 max_user_instances<br style="line-height: 22px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -rw-r--r-- 1 root root 0 2008-12-16 09:07 max_user_watches<br style="line-height: 22px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 如果没有的话,呵呵安装inotify<br style="line-height: 22px; ">2、软件安装:<br style="line-height: 22px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; wget&nbsp;<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; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; tar zxvf inotify-tools-3.13.tar.gz<br style="line-height: 22px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cd inotify-tools-3.13<br style="line-height: 22px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ./configure<br style="line-height: 22px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; make &amp;&amp; make install<br style="line-height: 22px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 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; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ssh-keygen<br style="line-height: 22px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 然后把你用户目录下的 .ssh/id_rsa.pub 拷贝到 server2 的root下<br style="line-height: 22px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; scp id_rsa.pub&nbsp;<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; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mv id_rsa.pub authorized_keys<br style="line-height: 22px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; chown root:root authorized_keys<br style="line-height: 22px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; chmod 600&nbsp; authorized_keys<br style="line-height: 22px; ">4、编辑配置文件inosync.sh<br style="line-height: 22px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #!/bin/sh<br style="line-height: 22px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SRC=/opt/sites/mxfarm.rekoo.com/<br style="line-height: 22px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<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; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; INWT=/usr/local/bin/inotifywait<br style="line-height: 22px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 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; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $INWT -mrq -e create,move,delete,modify $SRC | while read D E F;do<br style="line-height: 22px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; rsync -aHqzt --delete $SRC $DST<br style="line-height: 22px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 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; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 主要参数介绍:<br style="line-height: 22px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $INWT -mrq -e create,move,delete,modify $SRC | while read D E F;do<br style="line-height: 22px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; rsync -aHqzt --delete $SRC $DST<br style="line-height: 22px; ">&nbsp;&nbsp;&nbsp; -m 是保持一直监听<br style="line-height: 22px; ">&nbsp;&nbsp;&nbsp; -r 是递归查看目录<br style="line-height: 22px; ">&nbsp;&nbsp;&nbsp; -q 是打印出事件~<br style="line-height: 22px; ">&nbsp;&nbsp;&nbsp; -e create,move,delete,modify<br style="line-height: 22px; ">&nbsp;&nbsp;&nbsp; 监听 创建 移动 删除 写入 事件&nbsp;<br style="line-height: 22px; ">&nbsp;&nbsp;&nbsp; rsync -aHqzt $SRC $DST&nbsp;<br style="line-height: 22px; ">&nbsp;&nbsp;&nbsp; -a 存档模式<br style="line-height: 22px; ">&nbsp;&nbsp;&nbsp; -H 保存硬连接<br style="line-height: 22px; ">&nbsp;&nbsp;&nbsp; -q 制止非错误信息<br style="line-height: 22px; ">&nbsp;&nbsp;&nbsp; -z 压缩文件数据在传输<br style="line-height: 22px; ">&nbsp;&nbsp;&nbsp; -t 维护修改时间<br style="line-height: 22px; ">&nbsp;&nbsp;&nbsp; -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; ">四、运行并测试:&nbsp;&nbsp;<br style="line-height: 22px; ">&nbsp;&nbsp;在server1上运行./inosync.sh &amp;<br style="line-height: 22px; ">&nbsp;<br style="line-height: 22px; ">&nbsp;&nbsp;cd /opt/sites/mxfarm.rekoo.com<br style="line-height: 22px; ">&nbsp;&nbsp;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; ">&nbsp;&nbsp;在server2上/tmp/test 下看看:<br style="line-height: 22px; ">&nbsp;&nbsp;ls /tmp/test<br style="line-height: 22px; ">&nbsp;&nbsp;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; ">&nbsp;&nbsp;<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&nbsp;<br style="line-height: 22px; ">src="/opt/sites/mxfarm.rekoo.com /opt/work"&nbsp; #将两个目录<br style="line-height: 22px; ">des_ip="192.168.2.9 192.168.2.88"&nbsp;&nbsp; #同步到两台服务器</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 &amp;&nbsp;<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; ">&nbsp;&nbsp;<br style="line-height: 22px; ">六、可以对不同的ip 不同的目录创建多个脚本进行监控</p></span></font>
页: [1]
查看完整版本: linux inotify+rsync+ssh数据时时同步