最近在测试rsync+inotify的方式同步PHP代码到一个集群下的WEB服务器。如被监控的目录是/var/www/html下有三个目录

dream_android dream_ios game_router

当我把dream_android这个目录更名为android后,发现其他服务器上没有出现android并且原有的dream_android并没有被删除。测试游戏时发现大量的404错误,最大的问题就是代码同步出现了问题。

检查同步脚本中inotify和rsync相关的信息

/usr/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e close_write,modify,delete,create,attrib

这里监控/var/www/html 目录下的文件修改,删除,创建和属性更改。查看inotifywait的man手册发现inotifywait有个move的event


/usr/bin/rsync -avz --delete $src2/ $host::$dst2 这里将目标服多余的文件删除。

rsync的--delete参数奇怪的一点是如果一个目录被重新更名了,但是目录内的文件没有发生改变,将不会被同步出去并删除目标服上其他多余的文件


moved_to

A file or directory was moved into a watched directory. This event occurs even if the file issimply moved from and to the same directory.


moved_from

A file or directory was moved from a watched directory. This event occurs even if the file issimply moved from and to the same directory.


move A file or directory was moved from or to a watched directory. Note that this is actuallyimplemented simply by listening for both moved_to and moved_from, hence all close eventsreceived will be output as one or both of these, not MOVE.


增加一个move事件就可以监控文件的更名了。

/usr/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e move,close_write,modify,delete,create,attrib