温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用rsync + inotify 实现多台游戏服代码自动实时同步

发布时间:2020-07-08 12:09:06 来源:网络 阅读:907 作者:自由linux 栏目:web开发

一 应用场景描述

 在LB后端有多台游戏服运行PHP代码,每台服务器的环境部署一样,PHP代码一样。现在有时需要更改游戏配置文件,如更改各个区服的开服状态等。登陆到每台服务器去更改相应的文件会比较繁琐,需要当在第一台服务器上的配置文件作更改时,其他服务器上的配置文件自动更改。于是考虑可以使用rsync + inotify的方式来同步代码。


二 rsync和inotify配置

使用yum -y install rsync安装rsync服务

rsync有两种工作模式,命令行模式和C/S模式

使用man rsync查看rsync命令的详细使用

使用man rsyncd.conf查看rsync C/S模式运行时的配置文件的详细配置

启动rsync C/S模式

/usr/bin/rsync --daemon

/etc/rsyncd.conf 这个文件默认不存在,需要手动创建一个.


  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特别重要,表示当传输文件时,rsyncd守护进程应该具有的对指定目录的权限。如果权限不对,传输将失败。


use chroot         如果设置为true,那么rsync 在传输文件之前会chroot到指定的目录下。

list               这个参数设置当客户端请求列出可用模块时,这个模块是否可以列出

max connections    设置最大并发连接数,默认是0,表示无限制。

game_center        是这个模块的名称

path               设置同步目录

ignore errors      忽略一些I/O错误

read only          设置是否允许只读      

write only         设置是否允许只写

hosts allow        设置允许访问的IP范围,可以是单个IP,也可以是IP段

hosts deny         设置拒绝访问的IP范围


还可以设置账号和密码认账模式

auth users = applecode

secrets file = /etc/rsyncd.secrets.apple

applecode这个账号不需要再本地系统创建,最好将/etc/rsyncd.secrets.apple 的权限设置为600


/etc/rsyncd.secrets.apple的内容如下:

applecode:applecodexxx


在客户端主机上也要存在/etc/rsyncd.secrets.apple这个文件,内容是账号密码

applecodexxx


开启了认账模式使用rsync命令同步时需要添加参数   

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



使用rsync不能实时监测数据的变化并触发同步,这样可以使用inotify配合rsync。

可以使用man 7 inotify查看inotify的相关说明

inotify是一个强大的,细粒度的,异步的文件系统系统监测机制。从内核2.6.13,Linux内核提供inotify接口,第三方软件可以通过inotify接口监控文件系统中文件的修改,删除,移动,增加等各种事件。


使用uname -a 查看Linux内核是否低于2.6.13

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

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


Inotify可以监测的文件系统事件

         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

安装后包含两个工具

/usr/bin/inotifywait

/usr/bin/inotifywatch


inotifywait   等待要监控的文件发生变化

inotifywatch  收集变化信息


这里主要使用inotifywait命令

通过man inotifywait 查看inotify命令的详细使用信息


  -m, --monitor

 

              默认是当第一个事件出现时,执行操作后就退出。这个参数可以让inotifywait一直执行


 -r, --recursive

              监测所有子目录的变化情况。如果监测的是一个很大的目录,这里要注意设置/proc/sys/fs/inotify/max_user_watches的值。


  -q, --quiet

              不显示详细输出

--timefmt 

              设置时间格式,例如  --timefmt '%d/%m/%y %H:%M'

--format 

              设置输出格式。例如  --format '%T %w%f%e'

              %T     以 --timefmt 指定的格式显示时间

              %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           列出需要监听的事件


以下是可以监听的事件

access       被监测的文件或被监测目录里的文件被访问

modify       被监测的文件或被监测目录里的文件被写入

attrib       被监测的文件或被监测目录里的文件的属性被修改,包括时间戳,文件权限等

close_write  被监测的文件或被监测目录里的文件以写入模式打开后被关闭

close_nowrite被监测的文件或被监测目录里的文件以只读模式打开后被关闭

close        不关心被监测的文件或被监测目录里的文件以何种方式打开.

open         文件打开

moved_to     移入文件到被监测的目录,同一个目录内的文件被移入移出也会产生事件

moved_from   从被监测的目录移出文件

move         移入移出都监测

move_self    被监测的文件或目录被移走.产生这个事件后,这个文件或目录不再被监测.

create       一个文件或目录在被监测的目录内被创建

delete       一个文件或目录在被监测的目录内被移除

delete_self  被监测的文件或目录被删除,产生这个事件后,这个文件或目录被再被监测.

unmount      被监测的文件或目录所在的文件系统被卸载.产生这个事件后,被监测的文件或目录将不              被监测.



$ 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


放入后台执行

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


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI