手机版
你好,游客 登录 注册
背景:
阅读新闻

利用Inotify和Rsync将Web工程文件自动同步到多台应用服务器

[日期:2014-06-30] 来源:Linux社区  作者:黄杉 [字体:大 中 小]

4,使用Inotify结合rsync来进行随时随地自动发布web工程

编写一个inotify使用案例脚本inotify_web.sh:


4.1 inotify_web脚本

[root@localhost inotify-tools-3.14]# vim /usr/local/inotify-tools-3.14/inotify_web.sh

#!/bin/bash

src=/usr/local/nginx/web

des=web

#ip1=192.168.0.50,ip2是另外一台服务器ip地址

host="192.168.0.50 192.168.0.53"

# 使用inotifywait随时监控$src目录的一切变更,如果有,就自动调用后面的do…done里面的rsync代码块,将一切变更同步到两台web服务器上相同的目录里面。

/usr/local/inotify-tools-3.14/bin/inotifywait -mrq --timefmt '%d/%m/%y/%H:%M' --format '%T%w%f' -e close_write,move,delete,create $src | while read files

do

  for hostip in $host

        do

                      echo "`date '+%F %T'` start to resync $src to $hostip"

                      /usr/bin/rsync -auzv --progress --delete $src nginx@$hostip::$des --password-file=/etc/rsync.pas

                  echo "$src has been resynced to $hostip `date '+%F %T'`"

        done

    echo "${files} was rsynced" >>/tmp/rsync.log 2>&1

done

 

4.2,设置后台启动任务

[root@localhost inotify-tools-3.14]#

# 启动

nohup sh /usr/local/inotify-tools-3.14/inotify_web.sh >/tmp/inotify_rsync.log 2>&1 &

# 可以/tmp/inotify_rsync.log随时查看执行的日志信息

 

4.3,启动后,查看后台运行的进程:

[root@localhost inotify-tools-3.14]# ps -eaf|grep ino

root    17842 17594  0 20:15 pts/1    00:00:00 sh /usr/local/inotify-tools-3.14/inotify_web.sh

root    17843 17842  0 20:15 pts/1    00:00:00 /usr/local/inotify-tools-3.14/bin/inotifywait -mrq --timefmt %d/%m/%y/%H:%M --format %T%w%f -e close_write,move,delete,create /usr/local/nginx/web

root    17844 17842  0 20:15 pts/1    00:00:00 sh /usr/local/inotify-tools-3.14/inotify_web.sh

root    17872 17594  0 20:16 pts/1    00:00:00 tail -f /tmp/inotify_rsync.log

nginx    17882 17848  0 20:18 pts/0    00:00:00 grep ino

 

4.4,测试,check结果:

  清空192.168.0.50和192.168.0.53上面的/usr/local/nginx/web下面所有文件,然后在inotify监听服务器上的/usr/local/nginx/web创建wb.txt。

按照原理,一旦在inotify创建了文件,那么就会把/usr/local/nginx/web下面的所有文件同步到192.168.0.50和192.168.0.53的相应/usr/local/nginx/web目录下面。

(1),去查看inotify任务日志信息

[root@localhost web]# tail -f /tmp/inotify_rsync.log

nohup: 忽略输入

2014-06-28 20:16:20 start to resync /usr/local/nginx/web to 192.168.0.50

sending incremental file list

web/

web/dd.txt

          0 100%    0.00kB/s    0:00:00 (xfer#1, to-check=4/6)

web/i3.txt/

web/t.txt/

 

sent 193 bytes  received 40 bytes  466.00 bytes/sec

total size is 6  speedup is 0.03

/usr/local/nginx/web has been resynced to 192.168.0.50 2014-06-28 20:16:20

2014-06-28 20:16:20 start to resync /usr/local/nginx/web to 192.168.0.53

sending incremental file list

web/

web/dd.txt

          0 100%    0.00kB/s    0:00:00 (xfer#1, to-check=4/6)

web/wb.txt

          6 100%    0.00kB/s    0:00:00 (xfer#2, to-check=3/6)

web/i3.txt/

web/t.txt/

web/test/

 

sent 242 bytes  received 62 bytes  608.00 bytes/sec

total size is 6  speedup is 0.02

/usr/local/nginx/web has been resynced to 192.168.0.53 2014-06-28 20:16:20

 

(2),去另外一台192.168.0.53,查看下,文件已经同步过去。

[root@localhost web]# pwd

/usr/local/nginx/web

[root@localhost web]# ll

总用量 16

-rw-r--r--. 1 nginx nginx    0 6月  28 20:04 dd.txt

drwxrwxr-x. 2 nginx nginx 4096 6月  28 20:16 i3.txt

drwxr-xr-x. 2 nginx nginx 4096 6月  28 19:53 test

drwxr-xr-x. 2 nginx nginx 4096 6月  28 20:10 t.txt

-rw-r--r--. 1 nginx nginx    6 6月  28 19:51 wb.txt

 

至此,简单的通过inotify和rsync实现web工程自动同步功能已经完成,还有一点小细节留待后续解决。

 

本文永久更新链接地址:http://www.linuxidc.com/Linux/2014-06/103894.htm

linux
相关资讯       rsync  inotify 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款