我们知道svn提交文件后可以自动更新代码到其web网站。 但是svn提交后容易出问题,而且机器多的话不好操作,文件一大就比较慢,容易出现问题。
这样就想着搭建一个高效的自动更新代码的架构。
rsync传输文件自是不用多说,非常的便捷高效。这里只贴下配置
服务端(192.168.0.202)
# vi /etc/rsyncd.conf uid=root gid=root max connections=400 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=root [web] comment = web更新 path= /usr/local/nginx/html/ read only = no exclude = var auth users=root
vi /etc/rsyncd.secrets root:123456 chmod 600 /etc/rsyncd.secrets
(注意权限需要是600)
客户端(192.168.0.127):
vi /etc/rsyncd.secrets 123456 chmod 600 /etc/rsyncd.secrets
(注意权限需要是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|--recursive Watch 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:
access file or directory contents were read
modify file or directory contents were written
attrib file or directory attributes changed
close_write file or directory closed, after being opened in
writable mode
close_nowrite file or directory closed, after being opened in
read-only mode
close file or directory closed, regardless of read/write mode
open file or directory opened
moved_to file or directory moved to watched directory
moved_from file or directory moved from watched directory
move file or directory moved to or from watched directory
create file or directory created within watched directory
delete file or directory deleted within watched directory
delete_self file or directory was deleted
unmount file 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.secrets --exclude=log $src root@$host::$dest echo "${files} was rsynced." >> /var/log/rsyncd.log 2>&1#其实此处$files 不妥,过滤掉的文件也会显示已经传输。 done
经过测试如果你把服务端的一个文件误删了,他是不会自动同步的。只有客户端文件有所改变才会自动更新。
为了解决这个问题,你可以crontab里添加一个每五分钟的命令
*/5 * * * * echo $[$RANDOM**3] > /usr/local/nginx/html/check_data
当然你觉得随机数不好,想看确切的时间的话也可以
echo `date +%s` > check_data。
如此一来,就无时无刻在校检服务器与我们客户端的数据啦。这样的话就算完成了。
本文出自 “往学习的路上前景” 博客,请务必保留此出处http://jonyisme.blog.51cto.com/3690784/1617390
原文地址:http://jonyisme.blog.51cto.com/3690784/1617390