使用rsync+inotify實(shí)現(xiàn)多臺(tái)游戲服代碼自動(dòng)實(shí)時(shí)同步

一 應(yīng)用場景描述

梁子湖網(wǎng)站建設(shè)公司成都創(chuàng)新互聯(lián)公司,梁子湖網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為梁子湖數(shù)千家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)網(wǎng)站制作要多少錢,請找那個(gè)售后服務(wù)好的梁子湖做網(wǎng)站的公司定做!

 在LB后端有多臺(tái)游戲服運(yùn)行PHP代碼,每臺(tái)服務(wù)器的環(huán)境部署一樣,PHP代碼一樣?,F(xiàn)在有時(shí)需要更改游戲配置文件,如更改各個(gè)區(qū)服的開服狀態(tài)等。登陸到每臺(tái)服務(wù)器去更改相應(yīng)的文件會(huì)比較繁瑣,需要當(dāng)在第一臺(tái)服務(wù)器上的配置文件作更改時(shí),其他服務(wù)器上的配置文件自動(dòng)更改。于是考慮可以使用rsync + inotify的方式來同步代碼。

二 rsync和inotify配置

使用yum -y install rsync安裝rsync服務(wù)

rsync有兩種工作模式,命令行模式和C/S模式

使用man rsync查看rsync命令的詳細(xì)使用

使用man rsyncd.conf查看rsync C/S模式運(yùn)行時(shí)的配置文件的詳細(xì)配置

啟動(dòng)rsync C/S模式

/usr/bin/rsync --daemon

/etc/rsyncd.conf 這個(gè)文件默認(rèn)不存在,需要手動(dòng)創(chuàng)建一個(gè).

  uid = root
  gid = root
  use chroot = no
  max connections = 50
  #strict modes = yes
  pid file = /var/run/rsyncd.pid
  lock file = /var/run/rsyncd.lock
  log file = /var/log/rsyncd.log

  [login_nginx]
  path = /etc/nginx/
  comment = login nginx
  ignore errors
  read only = no
  write only = no
  hosts allow = 192.168.1.0/24
  hosts deny = *
  list = false


  [game_center]
  path = /var/www/html
  comment = game_center code
  ignore errors
  read only = no
  write only = no
  hosts allow = 192.168.1.0/24
  hosts deny = *
  list = false

這里uid和gid特別重要,表示當(dāng)傳輸文件時(shí),rsyncd守護(hù)進(jìn)程應(yīng)該具有的對指定目錄的權(quán)限。如果權(quán)限不對,傳輸將失敗。

use chroot         如果設(shè)置為true,那么rsync 在傳輸文件之前會(huì)chroot到指定的目錄下。

list               這個(gè)參數(shù)設(shè)置當(dāng)客戶端請求列出可用模塊時(shí),這個(gè)模塊是否可以列出

max connections    設(shè)置最大并發(fā)連接數(shù),默認(rèn)是0,表示無限制。

game_center        是這個(gè)模塊的名稱

path               設(shè)置同步目錄

ignore errors      忽略一些I/O錯(cuò)誤

read only          設(shè)置是否允許只讀      

write only         設(shè)置是否允許只寫

hosts allow        設(shè)置允許訪問的IP范圍,可以是單個(gè)IP,也可以是IP段

hosts deny         設(shè)置拒絕訪問的IP范圍

還可以設(shè)置賬號和密碼認(rèn)賬模式

auth users = applecode

secrets file = /etc/rsyncd.secrets.apple

applecode這個(gè)賬號不需要再本地系統(tǒng)創(chuàng)建,最好將/etc/rsyncd.secrets.apple 的權(quán)限設(shè)置為600

/etc/rsyncd.secrets.apple的內(nèi)容如下:

applecode:applecodexxx

在客戶端主機(jī)上也要存在/etc/rsyncd.secrets.apple這個(gè)文件,內(nèi)容是賬號密碼

applecodexxx

開啟了認(rèn)賬模式使用rsync命令同步時(shí)需要添加參數(shù)   

--password-file=/etc/rsyncd.secrets.apple

使用rsync不能實(shí)時(shí)監(jiān)測數(shù)據(jù)的變化并觸發(fā)同步,這樣可以使用inotify配合rsync。

可以使用man 7 inotify查看inotify的相關(guān)說明

inotify是一個(gè)強(qiáng)大的,細(xì)粒度的,異步的文件系統(tǒng)系統(tǒng)監(jiān)測機(jī)制。從內(nèi)核2.6.13,Linux內(nèi)核提供inotify接口,第三方軟件可以通過inotify接口監(jiān)控文件系統(tǒng)中文件的修改,刪除,移動(dòng),增加等各種事件。

使用uname -a 查看Linux內(nèi)核是否低于2.6.13

查看是否存在/proc/sys/fs/inotify/目錄

ls -lh /proc/sys/fs/inotify/

Inotify可以監(jiān)測的文件系統(tǒng)事件

         IN_ACCESS          File was accessed (read) (*)

         IN_ATTRIB          Metadata changed (permissions, timestamps,

                            extended attributes, etc.) (*)

         IN_CLOSE_WRITE     File opened for writing was closed (*)

         IN_CLOSE_NOWRITE   File not opened for writing was closed (*)

         IN_CREATE          File/directory created in watched directory (*)

         IN_DELETE          File/directory deleted from watched directory (*)

         IN_DELETE_SELF     Watched file/directory was itself deleted

         IN_MODIFY          File was modified (*)

         IN_MOVE_SELF       Watched file/directory was itself moved

         IN_MOVED_FROM      File moved out of watched directory (*)

         IN_MOVED_TO        File moved into watched directory (*)

         IN_OPEN            File was opened (*)

使用yum install -y  inotify-tools inotify-tools-devel 安裝inotify-tools

安裝后包含兩個(gè)工具

/usr/bin/inotifywait

/usr/bin/inotifywatch

inotifywait   等待要監(jiān)控的文件發(fā)生變化

inotifywatch  收集變化信息

這里主要使用inotifywait命令

通過man inotifywait 查看inotify命令的詳細(xì)使用信息

  -m, --monitor

 

              默認(rèn)是當(dāng)?shù)谝粋€(gè)事件出現(xiàn)時(shí),執(zhí)行操作后就退出。這個(gè)參數(shù)可以讓inotifywait一直執(zhí)行

 -r, --recursive

              監(jiān)測所有子目錄的變化情況。如果監(jiān)測的是一個(gè)很大的目錄,這里要注意設(shè)置/proc/sys/fs/inotify/max_user_watches的值。

  -q, --quiet

              不顯示詳細(xì)輸出

--timefmt 

              設(shè)置時(shí)間格式,例如  --timefmt '%d/%m/%y %H:%M'

--format 

              設(shè)置輸出格式。例如  --format '%T %w%f%e'

              %T     以 --timefmt 指定的格式顯示時(shí)間

              %e     Replaced with the Event(s) which occurred, comma-separated.

              %w     This will be replaced with the name of the Watched file on which an event occurred.

              

              %f     When an event occurs within a directory, this will be replaced with the name of the File which caused the event to occur.  Otherwise, this will be replaced with an empty string.


-e           列出需要監(jiān)聽的事件

以下是可以監(jiān)聽的事件

access       被監(jiān)測的文件或被監(jiān)測目錄里的文件被訪問

modify       被監(jiān)測的文件或被監(jiān)測目錄里的文件被寫入

attrib       被監(jiān)測的文件或被監(jiān)測目錄里的文件的屬性被修改,包括時(shí)間戳,文件權(quán)限等

close_write  被監(jiān)測的文件或被監(jiān)測目錄里的文件以寫入模式打開后被關(guān)閉

close_nowrite被監(jiān)測的文件或被監(jiān)測目錄里的文件以只讀模式打開后被關(guān)閉

close        不關(guān)心被監(jiān)測的文件或被監(jiān)測目錄里的文件以何種方式打開.

open         文件打開

moved_to     移入文件到被監(jiān)測的目錄,同一個(gè)目錄內(nèi)的文件被移入移出也會(huì)產(chǎn)生事件

moved_from   從被監(jiān)測的目錄移出文件

move         移入移出都監(jiān)測

move_self    被監(jiān)測的文件或目錄被移走.產(chǎn)生這個(gè)事件后,這個(gè)文件或目錄不再被監(jiān)測.

create       一個(gè)文件或目錄在被監(jiān)測的目錄內(nèi)被創(chuàng)建

delete       一個(gè)文件或目錄在被監(jiān)測的目錄內(nèi)被移除

delete_self  被監(jiān)測的文件或目錄被刪除,產(chǎn)生這個(gè)事件后,這個(gè)文件或目錄被再被監(jiān)測.

unmount      被監(jiān)測的文件或目錄所在的文件系統(tǒng)被卸載.產(chǎn)生這個(gè)事件后,被監(jiān)測的文件或目錄將不              被監(jiān)測.

$ inotifywait -mrq application.conf.php
application.conf.php OPEN 
application.conf.php CLOSE_NOWRITE,CLOSE 
application.conf.php OPEN 
application.conf.php CLOSE_NOWRITE,CLOSE 
application.conf.php OPEN 
application.conf.php CLOSE_NOWRITE,CLOSE 
application.conf.php OPEN 
application.conf.php CLOSE_NOWRITE,CLOSE 
application.conf.php OPEN 
application.conf.php CLOSE_NOWRITE,CLOSE

        

 #!/bin/sh
       while inotifywait -e modify /var/log/messages; do
         if tail -n1 /var/log/messages | grep httpd; then
           kdialog --msgbox "Apache needs love!"
         fi
       done

inotify_rsync_files.sh

#/usr/bin/bash

#check /etc/nginx/  and  /var/www/html files changes
hosts="192.168.1.183  192.168.1.184  192.168.1.185  192.168.1.186"
src1=/etc/nginx/
dst1=game_nginx
src2=/var/www/html/
dst2=game_server

#check /var/www/html files changes and rsync them to the destination host
/usr/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e move,close_write,modify,delete,create,attrib
 $src1  $src2|while read files
                         do 
                             for host in $(echo $hosts)
                             do 
                               echo $files|grep /etc/nginx
                               if [ $? -eq 0 ];then 
                                   /usr/bin/rsync -avz --delete  $src1/    $host::$dst1
                                   ssh -t -p 40022 jidong@$host "sudo service nginx reload"
                               else
                                   /usr/bin/rsync -avz --delete  $src2/    $host::$dst2
                                   ssh -t -p 40022 jidong@$host "sudo service php-fpm reload"
                               fi
                             done
                             echo "$files was rsynced" >> /tmp/rsyncd.log 2>&1
                         done

放入后臺(tái)執(zhí)行

nohup sh /data/tools/inotify_rsync_files.sh >> ~/inotify_rsync.txt &  

標(biāo)題名稱:使用rsync+inotify實(shí)現(xiàn)多臺(tái)游戲服代碼自動(dòng)實(shí)時(shí)同步
網(wǎng)站路徑:http://www.muchs.cn/article28/pgdpcp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App開發(fā)、域名注冊、微信公眾號網(wǎng)站營銷、自適應(yīng)網(wǎng)站、手機(jī)網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都seo排名網(wǎng)站優(yōu)化