一 应用场景描述

在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=rootgid=rootusechroot=nomaxconnections=50#strictmodes=yespidfile=/var/run/rsyncd.pidlockfile=/var/run/rsyncd.locklogfile=/var/log/rsyncd.log[login_nginx]path=/etc/nginx/comment=loginnginxignoreerrorsreadonly=nowriteonly=nohostsallow=192.168.1.0/24hostsdeny=*list=false[game_center]path=/var/www/htmlcomment=game_centercodeignoreerrorsreadonly=nowriteonly=nohostsallow=192.168.1.0/24hostsdeny=*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 whichcaused 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-mrqapplication.conf.phpapplication.conf.phpOPENapplication.conf.phpCLOSE_NOWRITE,CLOSEapplication.conf.phpOPENapplication.conf.phpCLOSE_NOWRITE,CLOSEapplication.conf.phpOPENapplication.conf.phpCLOSE_NOWRITE,CLOSEapplication.conf.phpOPENapplication.conf.phpCLOSE_NOWRITE,CLOSEapplication.conf.phpOPENapplication.conf.phpCLOSE_NOWRITE,CLOSE


#!/bin/shwhileinotifywait-emodify/var/log/messages;doiftail-n1/var/log/messages|grephttpd;thenkdialog--msgbox"Apacheneedslove!"fidone



inotify_rsync_files.sh

#/usr/bin/bash#check/etc/nginx/and/var/www/htmlfileschangeshosts="192.168.1.183192.168.1.184192.168.1.185192.168.1.186"src1=/etc/nginx/dst1=game_nginxsrc2=/var/www/html/dst2=game_server#check/var/www/htmlfileschangesandrsyncthemtothedestinationhost/usr/bin/inotifywait-mrq--timefmt'%d/%m/%y%H:%M'--format'%T%w%f%e'-emove,close_write,modify,delete,create,attrib$src1$src2|whilereadfilesdoforhostin$(echo$hosts)doecho$files|grep/etc/nginxif[$?-eq0];then/usr/bin/rsync-avz--delete$src1/$host::$dst1ssh-t-p40022jidong@$host"sudoservicenginxreload"else/usr/bin/rsync-avz--delete$src2/$host::$dst2ssh-t-p40022jidong@$host"sudoservicephp-fpmreload"fidoneecho"$fileswasrsynced">>/tmp/rsyncd.log2>&1done


放入后台执行

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