阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

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

141次阅读
没有评论

共计 16727 个字符,预计需要花费 42 分钟才能阅读完成。

背景:需要搭建一套跟线上一模一样的环境,用来预发布,这是其中的 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),可以很容易做到保持原来文件的权限、时间、软硬链接等等。

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

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

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

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

1.3,简单环境介绍:

(1),服务器端(代码发布服务器):192.168.0.51

(2),客户端(Web 服务器):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 命令将更新的文件推向二台 web 服务器 (192.168.0.50 和 192.168.0.53)。

(5),架构图如下:

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

利用 inotifywait 监控主机文件和目录 http://www.linuxidc.com/Linux/2013-03/81075.htm

利用 inotify+rsync 实现 Linux 文件批量更新 http://www.linuxidc.com/Linux/2012-01/52132.htm

inotify-tools+rsync 实时同步文件安装和配置 http://www.linuxidc.com/Linux/2012-06/63624.htm

rsync 同步完整配置 http://www.linuxidc.com/Linux/2013-06/85781.htm

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://linux.linuxidc.com/

用户名与密码都是 www.linuxidc.com

具体下载目录在 /2014 年资料 / 6 月 /30 日 / 利用 Inotify 和 Rsync 将 Web 工程文件自动同步到多台应用服务器

下载方法见 http://www.linuxidc.com/Linux/2013-07/87684.htm

—————————————— 分割线 —————————————— 

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 命令而不用附带加上全路径

更多详情见请继续阅读下一页的精彩内容 :http://www.linuxidc.com/Linux/2014-06/103894p2.htm

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 #用户名:密码

【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]#

背景:需要搭建一套跟线上一模一样的环境,用来预发布,这是其中的 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),可以很容易做到保持原来文件的权限、时间、软硬链接等等。

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

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

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

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

1.3,简单环境介绍:

(1),服务器端(代码发布服务器):192.168.0.51

(2),客户端(Web 服务器):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 命令将更新的文件推向二台 web 服务器 (192.168.0.50 和 192.168.0.53)。

(5),架构图如下:

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

利用 inotifywait 监控主机文件和目录 http://www.linuxidc.com/Linux/2013-03/81075.htm

利用 inotify+rsync 实现 Linux 文件批量更新 http://www.linuxidc.com/Linux/2012-01/52132.htm

inotify-tools+rsync 实时同步文件安装和配置 http://www.linuxidc.com/Linux/2012-06/63624.htm

rsync 同步完整配置 http://www.linuxidc.com/Linux/2013-06/85781.htm

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://linux.linuxidc.com/

用户名与密码都是 www.linuxidc.com

具体下载目录在 /2014 年资料 / 6 月 /30 日 / 利用 Inotify 和 Rsync 将 Web 工程文件自动同步到多台应用服务器

下载方法见 http://www.linuxidc.com/Linux/2013-07/87684.htm

—————————————— 分割线 —————————————— 

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 命令而不用附带加上全路径

更多详情见请继续阅读下一页的精彩内容 :http://www.linuxidc.com/Linux/2014-06/103894p2.htm

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 工程自动同步功能已经完成,还有一点小细节留待后续解决。

 

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-01-20发表,共计16727字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中