日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時(shí)間:2025/3/17 编程问答 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 利用Inotify和Rsync将webproject文件自己主动同步到多台应用server 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.


背景:須要搭建一套跟線上一模一樣的環(huán)境,用來(lái)預(yù)公布,這是當(dāng)中的web分發(fā)的一個(gè)小模塊的實(shí)現(xiàn)過(guò)程。

?

1?工具以及環(huán)境簡(jiǎn)單介紹

1.1Inotify工具

Inotify,它是一個(gè)內(nèi)核用于通知用戶空間程序文件系統(tǒng)變化的機(jī)制。眾所周知,Linux?桌面系統(tǒng)與?MAC??Windows?相比有很多不如人意的地方,為了改善這樣的狀況,開(kāi)源社區(qū)提出用戶態(tài)須要內(nèi)核提供一些機(jī)制,以便用戶態(tài)能夠及時(shí)地得知內(nèi)核或底層硬件設(shè)備發(fā)生了什么,從而能夠更好地管理設(shè)備,給用戶提供更好的服務(wù),如hotplug、udev??inotify?就是這樣的需求催生的。Hotplug?是一種內(nèi)核向用戶態(tài)應(yīng)用通報(bào)關(guān)于熱插拔設(shè)備一些事件發(fā)生的機(jī)制,桌面系統(tǒng)能夠利用它對(duì)設(shè)備進(jìn)行有效的管理,udev?動(dòng)態(tài)地維護(hù)?/dev?下的設(shè)備文件,inotify?是一種文件系統(tǒng)的變化通知機(jī)制,如文件添加、刪除等事件能夠立馬讓用戶態(tài)得知,該機(jī)制是著名的桌面搜索引擎項(xiàng)目?beagle?引入的,并在?Gamin?等項(xiàng)目中被應(yīng)用。

?

1.2rsync工具

它是類unix系統(tǒng)下的數(shù)據(jù)鏡像備份工具,實(shí)現(xiàn)遠(yuǎn)程同步remote sync,它的特性例如以下:

(1),能夠鏡像保存整個(gè)文件夾樹(shù)和文件系統(tǒng)。

(2),能夠非常easy做到保持原來(lái)文件的權(quán)限、時(shí)間、軟硬鏈接等等。

(3),無(wú)須特殊權(quán)限就可以安裝。

(4),高速:第一次同步時(shí)?rsync?會(huì)復(fù)制全部?jī)?nèi)容,但在下一次僅僅傳輸改動(dòng)過(guò)的文件。rsync?在數(shù)據(jù)傳輸?shù)倪^(guò)程中能夠?qū)嵭袎嚎s及解壓縮操作,因此能夠使用更少的帶寬。

(5),安全:能夠使用scpssh等方式來(lái)傳輸文件,當(dāng)然也能夠通過(guò)直接的socket連接。

(6),支持匿名傳輸,以方便進(jìn)行站點(diǎn)鏡象。

?

1.3,簡(jiǎn)單環(huán)境介紹:

(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.51inotify服務(wù)監(jiān)測(cè)文件文件夾/usr/local/nginx/web是否有更新,假設(shè)有更新(改動(dòng),刪除,新建)inotify就會(huì)通過(guò)rsync命令將更新的文件推向二臺(tái)webserver(192.168.0.50192.168.0.53)。

(5),架構(gòu)圖例如以下:

2.1,查看線上inotify版本號(hào)

通過(guò)rsync -h找到查看幫助,找到?--version參數(shù)。

[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:

……

?看到版本號(hào)號(hào)碼是3.14

?

2.2,下載inotify版本號(hào)

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

?

2.3,開(kāi)始編譯安裝

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

通過(guò)./configure --help查看編譯參數(shù),這里選取--prefix參數(shù),開(kāi)始編譯:

[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

環(huán)境變量設(shè)置:

[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,開(kāi)始安裝rsync軟件

192.168.0.51192.168.0.50192.168.0.53依照例如以下順序安裝rsync軟件


3.1,查看線上rsync版本號(hào)

通過(guò)rsync -h找到查看幫助,找到?--version參數(shù)。

[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

#?通過(guò)./configure --help查看編譯參數(shù),這里選取--prefix參數(shù)

[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.3check命令

[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/

看來(lái)已經(jīng)安裝好,命令能夠直接使用。

?

3.4,配置rsyncd.conf啟動(dòng)參數(shù)文件

[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?#本機(jī)地址,3臺(tái)IP地址都填寫(xiě)自己的IP地址

#ignore erros

read only=no

list=no

auth users=nginx

secrets file=/etc/rsync.pas??#密碼認(rèn)證文件地址

hosts allow=192.168.0.51,192.168.0.53?#同意rsync同步的ip地址,除了本機(jī)地址的其他2個(gè)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同步的參數(shù)名字,path就是同步的文件夾

comment=mirror for web

?

3.5,加入認(rèn)證文件

#?創(chuàng)建認(rèn)證文件

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

nginxpasspd #密碼

?nginx:nginxpasswd #username:密碼

PS】:不這樣設(shè)置兩行就會(huì)用戶驗(yàn)證失敗。

?#?賦予權(quán)限

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

?

3.6,創(chuàng)建文件夾并賦予權(quán)限

#?創(chuàng)建文件夾

mkdir -p /usr/local/nginx

mkdir -p /home/nginx/rsync

mkdir -p /usr/local/nginx/web

#?賦予權(quán)限

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

?

3.7,啟動(dòng)rsync服務(wù)

[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 ~]#

啟動(dòng)成功

?

3.8,測(cè)試rsync功能

一些調(diào)試報(bào)錯(cuò)經(jīng)歷:

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

說(shuō)明:這是由于/etc/rsync.pas的權(quán)限不正確,應(yīng)該設(shè)置為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上面改動(dòng)認(rèn)證文件

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

nginxpasswd

nginx: nginxpasswd

將原來(lái)僅僅有一行密碼的改成如此2行,就好使了,能使用rsync功能了。

測(cè)試驗(yàn)證,在192.168.0.50的空文件夾下,建立測(cè)試文件?1.txt2.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下目標(biāo)文件夾,為空文件夾,例如以下所看到的:

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

總用量?0

[root@localhost web]#

?

在192.168.0.50上運(yùn)行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下,看到文件已經(jīng)同步過(guò)來(lái),測(cè)試成功,例如以下所看到的:

[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結(jié)合rsync來(lái)進(jìn)行隨時(shí)隨地自己主動(dòng)公布webproject

編寫(xiě)一個(gè)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.50ip2是另外一臺(tái)serverip地址

host="192.168.0.50 192.168.0.53"

#?使用inotifywait隨時(shí)監(jiān)控$src文件夾的一切變更,假設(shè)有,就自己主動(dòng)調(diào)用后面的do…done里面的rsync代碼塊,將一切變更同步到兩臺(tái)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,設(shè)置后臺(tái)啟動(dòng)任務(wù)

[root@localhost inotify-tools-3.14]#

#?啟動(dòng)

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

#?能夠/tmp/inotify_rsync.log隨時(shí)查看運(yùn)行的日志信息

?

4.3,啟動(dòng)后,查看后臺(tái)運(yùn)行的進(jìn)程:

[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,測(cè)試,check結(jié)果:

??清空192.168.0.50192.168.0.53上面的/usr/local/nginx/web以下全部文件,然后在inotify監(jiān)聽(tīng)server上的/usr/local/nginx/web創(chuàng)建wb.txt。

依照原理,一旦在inotify創(chuàng)建了文件,那么就會(huì)把/usr/local/nginx/web以下的全部文件同步到192.168.0.50192.168.0.53的對(duì)應(yīng)/usr/local/nginx/web文件夾以下。

(1),去查看inotify任務(wù)日志信息

[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),去另外一臺(tái)192.168.0.53,查看下,文件已經(jīng)同步過(guò)去。

[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

?

至此,簡(jiǎn)單的通過(guò)inotifyrsync實(shí)現(xiàn)webproject自己主動(dòng)同步功能已經(jīng)完畢,另一點(diǎn)小細(xì)節(jié)留待興許解決。

?

參考文獻(xiàn):

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

?


總結(jié)

以上是生活随笔為你收集整理的利用Inotify和Rsync将webproject文件自己主动同步到多台应用server的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。