我们知道svn提交文件后可以自动更新代码到其web网站。 但是svn提交后容易出问题,而且机器多的话不好操作,文件一大就比较慢,容易出现问题。
这样就想着搭建一个高效的自动更新代码的架构。
rsync传输文件自是不用多说,非常的便捷高效。这里只贴下配置
代码是从192.168.0.127 同步更新到192.168.0.202的。
(192.168.0.202)
# vi /etc/rsyncd.conf uid = root gid = root usechroot = no max connections= 20 log file = /var/log/rsyncd.log pid file = /var/run/rsyncd.pid lock file = /var/run/rsyncd.lock secrets file = /etc/rsyncd.passwd hosts deny = 172.16.78.0/22 #本机上运行的数据库备份的模块配置 [back_sql] comment = 数据库备份 path=/data/backup read only = no exclude = test auth users = backuser [web] comment = web更新 path= /usr/local/nginx/html/ read only = no exclude = var auth users = backuser
vi /etc/rsyncd.passwd backuser:123456 chmod 600 /etc/rsyncd.passwd rsync --daemon 启动rsync服务
(注意权限需要是600)
(192.168.0.127):
vi /etc/rsyncd.passwd 123456 chmod 600 /etc/rsyncd.passwd
(注意权限需要是600)
这里介绍下inotify:
Inotify 是一种强大的、细粒度的、异步的文件系统事件监控机制,linux内核从2.6.13起,加入了Inotify支持,通过Inotify可以监控文件系统中添加、删除,修改、移动等各种细微事件,利用这个内核接口,第三方软件就可以监控文件系统下文件的各种变化情况,而inotify-tools就是这样的一个第三方软件。在上面章节中,我们讲到,rsync可以实现触发式的文件同步,但是通过crontab守护进程方式进行触发,同步的数据和实际数据会有差异,而inotify可以监控文件系统的各种变化,当文件有任何变动时,就触发rsync同步,这样刚好解决了同步数据的实时性问题。
安装inotify工具inotify-tools
由于inotify特性需要Linux内核的支持,在安装inotify-tools前要先确认Linux系统内核是否达到了2.6.13以上,如果Linux内核低于2.6.13版本,就需要重新编译内核加入inotify的支持,也可以用如下方法判断,内核是否支持inotify:
uname -r 看看内核是否大于2.6.13
inotifywait 用法: inotifywait 3.14 Wait for a particular event on a file or set of files. Usage: inotifywait [ options ] file1 [ file2 ] [ file3 ] [ ... ] Options: -h|--help Show this help text. @<file> Exclude the specified file from being watched. --exclude <pattern> Exclude all events on files matching the extended regular expression <pattern>. --excludei <pattern> Like --exclude but case insensitive. -m|--monitor Keep listening for events forever. Without this option, inotifywait will exit after one event is received. -d|--daemon Same as --monitor, except run in the background logging events to a file specified by --outfile. Implies --syslog. -r|--recursiveWatch directories recursively. --fromfile <file> Read files to watch from <file> or `-' for stdin. -o|--outfile <file> Print events to <file> rather than stdout. -s|--syslog Send errors to syslog rather than stderr. -q|--quiet Print less (only print events). -qq Print nothing (not even events). --format <fmt>Print using a specified printf-like format string; read the man page for more details. --timefmt <fmt>strftime-compatible format string for use with %T in --format string. -c|--csv Print events in CSV format. -t|--timeout <seconds> When listening for a single event, time out after waiting for an event for <seconds> seconds. If <seconds> is 0, inotifywait will never time out. -e|--event <event1> [ -e|--event <event2> ... ] Listen for specific event(s). If omitted, all events are listened for. Exit status: 0 - An event you asked to watch for was received. 1 - An event you did not ask to watch for was received (usually delete_self or unmount), or some error occurred. 2 - The --timeout option was given and no events occurred in the specified interval of time. Events: accessfile or directory contents were read modifyfile or directory contents were written attribfile or directory attributes changed close_writefile or directory closed, after being opened in writable mode close_nowritefile or directory closed, after being opened in read-only mode closefile or directory closed, regardless of read/write mode openfile or directory opened moved_tofile or directory moved to watched directory moved_fromfile or directory moved from watched directory movefile or directory moved to or from watched directory createfile or directory created within watched directory deletefile or directory deleted within watched directory delete_selffile or directory was deleted unmountfile system containing file or directory unmounted
解释下 Inotify 可以监视的文件系统事件包括:
IN_ACCESS,即文件被访问
IN_MODIFY,文件被 write
IN_ATTRIB,文件属性被修改,如 chmod、chown、touch 等
IN_CLOSE_WRITE,可写文件被 close
IN_CLOSE_NOWRITE,不可写文件被 close
IN_OPEN,文件被 open
IN_MOVED_FROM,文件被移走,如 mv
IN_MOVED_TO,文件被移来,如 mv、cp
IN_CREATE,创建新文件
IN_DELETE,文件被删除,如 rm
IN_DELETE_SELF,自删除,即一个可执行文件在执行时删除自己
IN_MOVE_SELF,自移动,即一个可执行文件在执行时移动自己
IN_UNMOUNT,宿主文件系统被 umount
IN_CLOSE,文件被关闭,等同于(IN_CLOSE_WRITE | IN_CLOSE_NOWRITE)
IN_MOVE,文件被移动,等同于(IN_MOVED_FROM | IN_MOVED_TO)
注:上面所说的文件也包括目录。
其他都是些比较简单的英文,就不做翻译。应该都能看懂。
(192.168.0.127)的监控文件脚本如下:
root@kali:/usr/local/nginx/html# cat inotify.sh #!/bin/bash #inotify监控次目录下文件,若有改变则同步/备份异地 src=/usr/local/nginx/html/ dest=web host="192.168.0.202" inotifywait -mrq --timefmt '%d/%m%y %H:%M' --format '%T%w%f' -e modify,delete,create,attrib $src | while read files do rsync -vzrtopg --delete --progress --password-file=/etc/rsyncd.passwd --exclude-from=exclude.list $src backuser@$host::$dest done cat exclude.list etc/game/local.xml code/core/Game/Snapshot/etc/config.xml 这是要过滤的文件。具体意思就是屏蔽 /usr/local/nginx/html/etc/game/local.xml /usr/local/nginx/html/code/core/Game/Snapshot/etc/config.xml 只能写相对路径,不然会报错。
经过测试如果你把192.168.0.202的一个文件误删了,192.168.0.202的代码是不会自动还原到跟192.168.0.127相同的。
为了解决这个问题,你可以crontab里添加一个每五分钟的命令
*/5 * * * * echo $[$RANDOM**3] > /usr/local/nginx/html/check_data
当然你觉得随机数不好,想看确切的时间的话也可以
echo `date +%s` > check_data。
这样一来,每过2台服务器五分钟就会自动校检数据啦。