rsync inotify

rsync + inotify 触发式同步
【基本介绍】
rsync :
rsync  is  a program that behaves in much the same way that rcp does, but has many more options  and  uses  the  rsync  remote-update  protocol  to greatly  speed  up  file  transfers  when  the  destination file is being updated.

inotify :
The  inotify  API provides a mechanism for monitoring file system events.Inotify can be used to monitor individual files, or to  monitor  directo-ries.  When a directory is monitored, inotify will return events for the directory itself, and for files inside the directory.

rsync+inotify ,实现监控某个文件或目录,实现实时同步到远程地址。1->n的传输需求(1为客户端,n为服务端,客户端不需要配置rsync服务器,启动inotify监听脚本。服务端启动rsync daemon)

【rsync 安装配置】
rsync 安装---省略几百字

#touch /etc/rsyncd/rsyncd.conf   #创建rsync服务器的配置文件;

#touch /etc/rsyncd/rsyncd.secrets  创建用户密码文件;

#chmod 600 /etc/rsyncd/rsyncd.secrets  我们把权限设为600;

rsyncd.conf:
uid=root
gid=nobody
max connections=200
timeout=600
use chroot=no
use chroot=no
read only=false
log file=/var/log/rsyncd.log
pid file=/var/log/rsyncd.pid
lock file=/var/log/rsync.lock
host_allow=xxxx   (这个需要传输的服务器地址)
[app]
path=xxxx    (这个就是传输的地址)
comment=xxx
list=yes
auth users=web 
secrets file=/etc/rsyncd.secrets

rsyncd.secrets: (客户端与服务端一致,客户端不需要用户名)
web:@xxxxxxxxxxxxxxx@

【inotify】
inotify : 安装省略数百字


【rsync+inotify】
rsync.sh:
#!/bin/sh
src1=/xxxx  #(监听的路径)
/usr/local/inotify/bin/inotifywait --exclude "xxx" -mrq  -e create,move,delete,modify  $src1 | while read file
do
          rsync -zaq --delete --port=8730 --password-file=/etc/rsyncd.secrets  --exclude=log/ $src1 web@xxxx::app   #(每个客户端遍历一遍,app是定义的模块名)
          rsync -zaq --delete --port=8730 --password-file=/etc/rsyncd.secrets  --exclude=log/ $src1 web@xxxx::app
          rsync -zaq --delete --port=8730 --password-file=/etc/rsyncd.secrets  --exclude=log/ $src1 web@xxxx::app
done

在服务器端运行这个脚本即可实现实时同步。

【基本流程】
1.在服务器端/客户端安装rsync
2.在客户端安装inotify
3.配置服务端的rsync daemon或者rsync服务器监听
4.服务器端和客户端都设置rsyncd.secrets密码文件(客户端只需要密码,服务端用户名:密码)
4.在客户端端实现rsync+inotify脚本


【参考】
http://wenku.baidu.com/link?url=7xvTMZ2IvlMwrjt7-NQvwRNM4tyWcY5TmIvIKZxcCyrdck9x7tBEIgDN7cwM1V-hrC0Wlp8VDFK16pAwEyIanagEj7wnfM0ZYEAbNcnelly
rsync : http://www.tecmint.com/rsync-local-remote-file-synchronization-commands/
about exclude: http://www.infoq.com/articles/inotify-linux-file-system-event-monitoring
http://blog.coocla.org/199.html

猜你喜欢

转载自runpanda.iteye.com/blog/2075326