背景:须要搭建一套跟线上一模一样的环境,用来预公布,这是当中的web分发的一个小模块的实现过程。

1 工具以及环境简单介绍

1.1,Inotify工具

Inotify,它是一个内核用于通知用户空间程序文件系统变化的机制。众所周知,Linux 桌面系统与 MAC 或 Windows 相比有很多不如人意的地方,为了改善这样的状况,开源社区提出用户态须要内核提供一些机制,以便用户态能够及时地得知内核或底层硬件设备发生了什么,从而能够更好地管理设备,给用户提供更好的服务,如hotplug、udev 和 inotify 就是这样的需求催生的。Hotplug 是一种内核向用户态应用通报关于热插拔设备一些事件发生的机制,桌面系统能够利用它对设备进行有效的管理,udev 动态地维护 /dev 下的设备文件,inotify 是一种文件系统的变化通知机制,如文件添加、删除等事件能够立马让用户态得知,该机制是著名的桌面搜索引擎项目 beagle 引入的,并在 Gamin 等项目中被应用。

1.2,rsync工具

它是类unix系统下的数据镜像备份工具,实现远程同步remote sync,它的特性例如以下:

(1),能够镜像保存整个文件夹树和文件系统。

(2),能够非常easy做到保持原来文件的权限、时间、软硬链接等等。

(3),无须特殊权限就可以安装。

(4),高速:第一次同步时 rsync 会复制全部内容,但在下一次仅仅传输改动过的文件。rsync 在数据传输的过程中能够实行压缩及解压缩操作,因此能够使用更少的带宽。

(5),安全:能够使用scp、ssh等方式来传输文件,当然也能够通过直接的socket连接。

(6),支持匿名传输,以方便进行站点镜象。

1.3,简单环境介绍:

(1),server端(代码公布server):192.168.0.51

(2),客户端(Webserver):192.168.0.50,192.168.0.53

(3),Web文件夹:/usr/local/nginx/web/

(4),基本原理:由192.168.0.51上inotify服务监測文件文件夹/usr/local/nginx/web是否有更新,假设有更新(改动,删除,新建)inotify就会通过rsync命令将更新的文件推向二台webserver(192.168.0.50和192.168.0.53)。

(5),架构图例如以下:

2.1,查看线上inotify版本号

通过rsync -h找到查看帮助,找到 --version參数。

[root@localhost bin]# inotifywait --help

inotifywait 3.14

Wait for a particular event on a file or set of files.

Usage: inotifywait [ options ] file1 [ file2 ] [ file3 ] [ ... ]

Options:

……

看到版本号号码是3.14

2.2,下载inotify版本号

下载地址:http://download.csdn.net/detail/mchdba/7564775

2.3,開始编译安装

[root@localhost root] tar -xvf inotify-tools-3.14.tar.gz

通过./configure --help查看编译參数,这里选取--prefix參数,開始编译:

[root@localhost inotify-tools-3.14]# ./configure --prefix=/usr/local/inotify-tools-3.14

...

config.status: creating Makefile

config.status: creating src/Makefile

config.status: creating man/Makefile

config.status: creating libinotifytools/Makefile

config.status: creating libinotifytools/src/Makefile

config.status: creating libinotifytools/src/inotifytools/Makefile

config.status: creating config.h

config.status: creating libinotifytools/src/inotifytools/inotify.h

config.status: executing depfiles commands

config.status: executing libtool commands

[root@localhost inotify-tools-3.14]# time make

...

fytools.so -Wl,-rpath -Wl,/usr/local/inotify-tools-3.14/lib

make[2]: Leaving directory `/root/inotify-tools-3.14/src'

Making all in man

make[2]: Entering directory `/root/inotify-tools-3.14/man'

make[3]: Entering directory `/root/inotify-tools-3.14'

make[3]: Leaving directory `/root/inotify-tools-3.14'

make[2]: Nothing to be done for `all'.

make[2]: Leaving directory `/root/inotify-tools-3.14/man'

make[2]: Entering directory `/root/inotify-tools-3.14'

cd . && /bin/sh ./config.status config.h

config.status: creating config.h

config.status: config.h is unchanged

make[2]: Leaving directory `/root/inotify-tools-3.14'

make[1]: Leaving directory `/root/inotify-tools-3.14'

real  0m2.889s

user 0m1.768s

sys   0m0.589s

[root@localhost inotify-tools-3.14]# time make install

...

make[2]: Nothing to be done for `install-exec-am'.

make[2]: Nothing to be done for `install-data-am'.

make[2]: Leaving directory `/root/inotify-tools-3.14'

make[1]: Leaving directory `/root/inotify-tools-3.14'

real  0m0.854s

user 0m0.454s

sys   0m0.254s

2.4,做成软连接到/usr/lib下

ln -sv /usr/local/inotify-tools-3.14/lib/libinotify* /usr/lib/

ln -s /usr/local/inotify-tools-3.14/lib/libinotifytools.so.0 /usr/lib64/libinotifytools.so.0

环境变量设置:

[root@localhost ~]# echo "export PATH=$PATH:/usr/local/inotify-tools-3.14/bin">>/etc/profile

[root@localhost ~]# source /etc/profile

[root@localhost ~]# inotifywait --help

inotifywait 3.14

Wait for a particular event on a file or set of files.

Usage: inotifywait [ options ] file1 [ file2 ] [ file3 ] [ ... ]

...

如今能够直接用inotify命令而不用附带加上全路径

3,開始安装rsync软件

在192.168.0.51,192.168.0.50,192.168.0.53依照例如以下顺序安装rsync软件

3.1,查看线上rsync版本号

通过rsync -h找到查看帮助,找到 --version參数。

[root@localhost ~]# rsync --version

rsync  version 3.0.6  protocol version 30

Copyright (C) 1996-2009 by Andrew Tridgell, Wayne Davison, and others.

Web site: http://rsync.samba.org/

[root@localhost ~]#

3.2,下载

wget http://rsync.samba.org/ftp/rsync/src/rsync-3.0.6.tar.gz

3.3,编译安装

# 解压缩

[root@localhost root]# tar -xvf rsync-3.0.6.tar.gz

[root@localhost rsync-3.0.6]# cd rsync-3.0.6

# 通过./configure --help查看编译參数,这里选取--prefix參数

[root@localhost rsync-3.0.6]# ./configure --prefix=/usr/local/rsync-3.0.6/

......

config.status: creating lib/dummy

config.status: creating zlib/dummy

config.status: creating popt/dummy

config.status: creating shconfig

config.status: creating config.h

rsync 3.0.6 configuration successful

[root@localhost rsync-3.0.6]# make

......

gcc -std=gnu99 -I. -I. -g -O2 -DHAVE_CONFIG_H -Wall -W -I./popt  -c popt/poptparse.c -o popt/poptparse.o

gcc -std=gnu99 -g -O2 -DHAVE_CONFIG_H -Wall -W -I./popt  -o rsync flist.o rsync.o generator.o receiver.o cleanup.o sender.o exclude.o util.o main.o checksum.o match.o syscall.o log.o backup.o options.o io.o compat.o hlink.o token.o uidlist.o socket.o hashtable.o fileio.o batch.o clientname.o chmod.o acls.o xattrs.o progress.o pipe.o params.o loadparm.o clientserver.o access.o connection.o authenticate.o lib/wildmatch.o lib/compat.o lib/snprintf.o lib/mdfour.o lib/md5.o lib/permstring.o lib/pool_alloc.o lib/sysacls.o lib/sysxattrs.o  zlib/deflate.o zlib/inffast.o zlib/inflate.o zlib/inftrees.o zlib/trees.o zlib/zutil.o zlib/adler32.o zlib/compress.o zlib/crc32.o popt/findme.o  popt/popt.o  popt/poptconfig.o popt/popthelp.o popt/poptparse.o

[root@localhost rsync-3.0.6]#

[root@localhost rsync-3.0.6]# make install

mkdir -p /usr/local/rsync-3.0.6/bin

/usr/bin/install -c  -m 755 rsync /usr/local/rsync-3.0.6/bin

mkdir -p /usr/local/rsync-3.0.6/share/man/man1

mkdir -p /usr/local/rsync-3.0.6/share/man/man5

if test -f rsync.1; then /usr/bin/install -c -m 644 rsync.1 /usr/local/rsync-3.0.6/share/man/man1; fi

if test -f rsyncd.conf.5; then /usr/bin/install -c -m 644 rsyncd.conf.5 /usr/local/rsync-3.0.6/share/man/man5; fi

[root@localhost rsync-3.0.6]#

3.3,check命令

[root@localhost ~]# rsync -h

rsync  version 3.0.6  protocol version 30

Copyright (C) 1996-2009 by Andrew Tridgell, Wayne Davison, and others.

Web site: http://rsync.samba.org/

看来已经安装好,命令能够直接使用。

3.4,配置rsyncd.conf启动參数文件

[root@localhost ~]# vim /etc/rsyncd.conf

uid=nginx  #用户id名称

gid=nginx  #用户所属组ID

use chroot=no

max connections=10

strict modes=yes

port=873

address=192.168.0.50 #本机地址,3台IP地址都填写自己的IP地址

#ignore erros

read only=no

list=no

auth users=nginx

secrets file=/etc/rsync.pas  #密码认证文件地址

hosts allow=192.168.0.51,192.168.0.53 #同意rsync同步的ip地址,除了本机地址的其他2个ip地址。

pid file=/home/nginx/rsync/rsyncd.pid

lock file=/home/nginx/rsync/rsync.lock

log file=/home/nginx/rsync/rsyncd.log

[web]

path=/usr/local/nginx/ # 这里的web就是rsync同步的參数名字,path就是同步的文件夹

comment=mirror for web

3.5,加入认证文件

# 创建认证文件

[root@localhost ~]# vim /etc/rsync.pas

nginxpasspd #密码

nginx:nginxpasswd #username:密码

【PS】:不这样设置两行就会用户验证失败。

# 赋予权限

[root@localhost ~]# chmod 600 /etc/rsync.pas

3.6,创建文件夹并赋予权限

# 创建文件夹

mkdir -p /usr/local/nginx

mkdir -p /home/nginx/rsync

mkdir -p /usr/local/nginx/web

# 赋予权限

chown -R nginx.nginx  /usr/local/nginx /home/nginx/rsync/ /usr/local/nginx/web

3.7,启动rsync服务

[root@localhost ~]# rsync --daemon --config=/etc/rsyncd.conf

[root@localhost ~]# ps -eaf|grep rsync

root      1387     1  0 Jun28 ?        00:00:00 rsync --daemon --config=/etc/rsyncd.conf

root      3201  3136  0 00:50 pts/0    00:00:00 grep rsync

[root@localhost ~]#

启动成功

3.8,測试rsync功能

一些调试报错经历:

[root@localhost ]#

rsync -vzrt --delete --progress --itemize-changes --exclude-from=/home/nginx/exclude_fastdfs.txt /data/fastdfs/data nginx@192.168.0.53::web --password-file=/etc/rsync.pas

password file must not be other-accessible

continuing without password file

Password:

@ERROR: auth failed on module web

rsync error: error starting client-server protocol (code 5) at main.c(1503) [sender=3.0.6]

[root@localhost ]#

说明:这是由于/etc/rsync.pas的权限不正确,应该设置为600。如:chmod 600 /etc/rsync.pas

[root@localhost inotify-tools-3.14]#

rsync -vzrt --delete --progress --itemize-changes --exclude-from=/home/nginx/exclude_fastdfs.txt /data/fastdfs/data nginx@192.168.0.53::fastdfs --password-file=/home/nginx/rsync.pas

@ERROR: auth failed on module fastdfs

rsync error: error starting client-server protocol (code 5) at main.c(1503) [sender=3.0.6]

[root@localhost inotify-tools-3.14]# ll

去查看192.168.0.53的log信息

[root@localhost inotify-tools-3.14]# tail -f /home/nginx/rsync/rsyncd.log

2014/06/28 17:24:14 [19031] auth failed on module web from unknown (192.168.0.50): missing secret for user "nginx"

2014/06/28 17:28:21 [19198] name lookup failed for 192.168.0.50: Name or service not known

2014/06/28 17:28:21 [19198] connect from UNKNOWN (192.168.0.50)

2014/06/28 17:28:21 [19198] auth failed on module web from unknown (192.168.0.50): missing secret for user "nginx"

2014/06/28 17:28:48 [19488] name lookup failed for 192.168.0.50: Name or service not known

去192.168.0.53上面改动认证文件

[root@localhost data]# vim /etc/rsync.pas

nginxpasswd

nginx: nginxpasswd

将原来仅仅有一行密码的改成如此2行,就好使了,能使用rsync功能了。

測试验证,在192.168.0.50的空文件夹下,建立測试文件 1.txt,2.txt

[root@localhost data]# cd /usr/local/nginx/web

[root@localhost web]# ll

总用量 0

[root@localhost web]# vim 1.txt

[root@localhost web]# vim 2.txt

[root@localhost web]# mkdir test

[root@localhost web]# vim ./test/3.txt

[root@localhost web]# ll /usr/local/nginx/web

总用量 12

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

-rw-r--r--. 1 nginx nginx    6 6月  28 19:18 2.txt

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

[root@localhost web]#

在rsync同步之前,先去53上面check下目标文件夹,为空文件夹,例如以下所看到的:

[root@localhost web]# ll /usr/local/nginx/web

总用量 0

[root@localhost web]#

在192.168.0.50上运行rsync命令,同步文件夹

[root@localhost web]# /usr/bin/rsync -auzv --progress --delete /usr/local/nginx/web nginx@192.168.0.53::webroot   --password-file=/etc/rsync.pas

sending incremental file list

web/

web/1.txt

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

web/2.txt

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

web/test/

web/test/3.txt

3 100%    2.93kB/s    0:00:00 (xfer#3, to-check=0/5)

sent 264 bytes  received 73 bytes  224.67 bytes/sec

total size is 15  speedup is 0.04

[root@localhost web]#

再去192.168.0.53上check下,看到文件已经同步过来,測试成功,例如以下所看到的:

[root@localhost web]# ll

总用量 12

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

-rw-r--r--. 1 nginx nginx    6 6月  28 19:18 2.txt

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

[root@localhost web]#

4,使用Inotify结合rsync来进行随时随地自己主动公布webproject

编写一个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是另外一台serverip地址

host="192.168.0.50 192.168.0.53"

# 使用inotifywait随时监控$src文件夹的一切变更,假设有,就自己主动调用后面的do…done里面的rsync代码块,将一切变更同步到两台webserver上同样的文件夹里面。

/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监听server上的/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实现webproject自己主动同步功能已经完毕,另一点小细节留待兴许解决。

參考文献:

http://zhumeng8337797.blog.163.com/blog/static/100768914201172952619883/

利用Inotify和Rsync将webproject文件自己主动同步到多台应用server的更多相关文章

  1. Linux学习-利用inotify和rsync实现数据的实时同步

    一.inotify简介 1.inotify介绍 异步的文件系统事件监控机制,利用事件驱动机制,而无须通过诸如cron等的 轮询机制来获取事件,linux内核从2.6.13起支持 inotify,通过i ...

  2. 利用inotify和rsync服务实现数据实时同步

    文件定时同步的实现: 利用rsync结合cron计划任务实现: rsync -av --delete /data/ 10.0.0.12:/back -a:保留文件属性 -v:显示过程 -delete: ...

  3. rsync + inotify-tools实现文件的实时同步

    文章摘自:http://lxw66.blog.51cto.com/5547576/1331048 rsync 帮助文档:http://man.linuxde.net/rsync 最近有个想法就是部署一 ...

  4. rsync 通过密码文件实现远程同步

    https://my.oschina.net/yyping/blog/91964 1.源文件服务器:192.168.10.203 2.备份服务器:192.168.10.88 配置备份服务器(192.1 ...

  5. rsync+inotify 实现服务器之间目录文件实时同步(转)

    软件简介: 1.rsync 与传统的 cp. tar 备份方式相比,rsync 具有安全性高.备份迅速.支持增量备份等优点,通过 rsync 可 以解决对实时性要求不高的数据备份需求,例如定期的备份文 ...

  6. inotify配合rsync实现文件同步

    一.slave端rsync设置在此部署rsync服务和rsync daemon 1.安装rsync   2.配置rsyncd.conf文件#vi /etc/rsyncd.conf配置文件uid = r ...

  7. Sersync实现触发式文件同步 替代inotify和rsync

    Sersync实现触发式文件同步 替代inotify和rsync Pyinotify是一个Python模块,用来监测文件系统的变化. Pyinotify依赖于Linux内核的功能—inotify(内核 ...

  8. [转]CENTOS 使用RSYNC+INOTIFY实现文件实时自动同步

    FROM : http://www.qiansw.com/centos-rsync-inotify-file-sync.html 生产环境中的两台web服务器,有个目录需要完全一样.使用rsync和i ...

  9. inotify 与 rsync文件同步实现与问题

    首先分别介绍inotify 与 rsync的使用,然后用两者实现实时文件同步,最后说一下这样的系统存在什么样的问题. 1. inotify 这个具体使用网上很多,参考 inotify-tools 命令 ...

随机推荐

  1. ci 笔记

    一.CI的HelloWorld! 注意:CI禁止直接通过文件目录来访问控制器. ./application/controllers/hello.php 1 <?php 2 //放止用户直接通过路 ...

  2. System.AccessViolationException: 尝试读取或写入受保护的内存 解决办法

    netsh winsock reset   --运行此命令解决 错误描述: 之前装的vs2010后 再又安装了vs2013 ,运行之前的vs2010项目 就出现以上错误 错误应用程序名称: w3wp. ...

  3. asp.net 页面实践执行顺序

    protected void Page_Load(object sender, EventArgs e) { } #region OnPreInit 第一步 protected override vo ...

  4. ADO.NET程序访问数据的组件

    组成--数据集(内存中的数据库) --DataSet数据集 --DataTable数据表 --DataColumn数据列 --DataRow数据行 --DataView数据视图--NET数据提供程序 ...

  5. studio中集成.so文件的两种方式

    .so文件作为NDK的开发包,如果不进行依赖使用将会报错所以,需要.so的一定要配置 注:如果没有引用so文件,可能会在程序执行的时候加载类库失败,有类似如下的DEBUG提示:    java.lan ...

  6. -canOpenURL: failed for URL

    这在 Xcode 6.4 + iOS 8 时,是不会有的情况,原因是[为了强制增强数据访问安全, iOS9 默认会把所有从NSURLConnection . CFURL . NSURLSession发 ...

  7. CSS 设计彻底研究(一)(X)HTML与CSS核心基础

    第1章 (X)HTML与CSS核心基础 这一章重点介绍了4个方面的问题.先介绍了 HTML和XHTML的发展历程以及需要注意的问题,然后介绍了如何将CSS引入HTML,接着讲解了CSS的各种选择器,及 ...

  8. Ubutu命令 笔记积累

    1 man command 查询帮助 查询结果会有 name    synopsis(概要)  description 2 terminal 中快捷键: Ctrl +u 撤销 Ctrl +l  清屏 ...

  9. size_t和size_type类型

    size_t一般用来表示一种计数,比如有多少东西被拷贝等.例如:sizeof操作符的结果类型是size_t,该类型保证能容纳实现所建立的最大对象的字节大小. 它的意义大致是“适于计量内存中可容纳的数据 ...

  10. 傻瓜式硬盘重装win7系统图文加视频教程

    标准适用环境: win7系统还能用,但是想重装win7系统.[当然win7进不去也可以在PE进行] 其它 需要工具: win7 ISO镜像                          | [ w ...