您的位置:首页 > 其它

rsync+inotify实现多台web数据动态同步

2012-05-09 16:13 501 查看
背景:由于无存储共享设备,web集群中的代码均存放在本地,最终导致web节点之间的数据无法一致。

解决办法:采用rsync+inotify,实现多台web数据动态同步

解决思路:比如有a、b、c、d四台web,为解决哪台服务器为源数据服务器,我们在A服务器上安装rsync+inotify,然后将一个二级域名指向A服务器,这样以后网站编辑、开发人员之间访问二级域名进行日常网站更新,A服务器在检测到本地有数据更新时,便动态(触发式)向其它服务器发送更新数据。

注意:一定要使用rsync相同的版本,否则会出现未知错误。

选择rsync+inotify的理由:在常规的数据同步应用案例中,大多数人会选择使用rsync来完成数据同步,选择rsync+inotify的理由如下

1、服务器性能:rsync只能实现定时更新,无论网站有无文件更新,rsync都会按着定时任务去检查文件是否有更新,当数据文件较大时会使服务器性能下降;而rsync+inotify
为触发式更新,也就是说只有当某个文件发生改动时才会更新,这样一来对服务器性能影响较小。
2、数据实时性:如果选择rsync,每隔多长时间同步一次数据是个问题,时间越短,对性能影响就越大。时间太长,用户/编辑无法接受。采用rsync+inotify可实现实时更新,
当A服务器文件有更新时,其它服务器立即更新


环境拓扑

A:192.168.1.101
B:192.168.1.102
C:192.168.1.103
D:192.168.1.104
注:数据源服务器为A,目标服务器为B、C、D


一、目标服务器安装rsync (在B、C、D服务器上操作,安装配置均一样)

安装rsync 下载地址:http://rsync.samba.org/

cd /data/software
wget https://rsync.samba.org/ftp/rsync/rsync-3.0.9.tar.gz tar zxvf rsync-3.0.9.tar.gz
cd rsync-3.0.9
./configure
make
make install


安装完成后显示信息

mkdir -p /usr/local/bin
/usr/bin/install -c  -m 755 rsync /usr/local/bin
mkdir -p /usr/local/share/man/man1
mkdir -p /usr/local/share/man/man5
if test -f rsync.1; then /usr/bin/install -c -m 644 rsync.1 /usr/local/share/man/man1; fi
if test -f rsyncd.conf.5; then /usr/bin/install -c -m 644 rsyncd.conf.5 /usr/local/share/man/man5; fi


配置rsync

#vi /etc/rsync.conf 加入如下内容

uid = root
gid = root
use chroot = no
max connections = 20
strict modes = yes
log file = /data/logs/rsyncd/rsyncd.log
pid file = /data/logs/rsyncd/rsyncd.pid
lock file = /data/logs/rsyncd/rsync.lock
log format = %t %a %m %f %b
[web]
path = /data/vhosts/it121net/
auth users = username
read only = no
hosts allow = 192.168.1.0/24  #可以是IP段,也可以是IP地址
list = no
uid = root
gid = root
secrets file = /etc/rsync.passwd
ignore errors = yes


创建目录,用于存放日志。

mkdir /data/logs/rsyncd


创建认证

#vi /etc/rsync.passwd

username:passwd


#chmod 600 /etc/rsync.passwd

启动rsync,启动后使用netstat查看,会发现系统已启动873端口

# rsync --daemon --config=/etc/rsync.conf


加入开机启动

# echo "rsync --daemon --config=/etc/rsync.conf" >>/etc/rc.local


关闭

killall rsync


二、源服务器安装rsync+inotify (在a服务器上操作)

安装rsync(仅安装即可,不需配置)

cd /data/software
wget https://rsync.samba.org/ftp/rsync/rsync-3.0.9.tar.gz tar zxvf rsync-3.0.9.tar.gz
cd rsync-3.0.9
./configure
make
make install


echo passwd > /etc/rsync-client.passwd

chmod 600 /etc/rsync-client.passwd

安装inotify 下载地址:https://github.com/rvoicilas/inotify-tools/wiki/

cd /data/software
wget http://cloud.github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz tar zxvf inotify-tools-3.14.tar.gz
cd inotify-tools-3.14
./configure
make
make install


创建启动脚本

#vi /etc/rsync-web.sh 加入如下内容

#!/bin/sh
SRC=/data/vhosts/it121net/
DES=web
WEB2=192.168.1.102
WEB3=192.168.1.103
WEB4=192.168.1.104
USER=username
/usr/local/bin/inotifywait -mrq -e create,move,delete,modify $SRC | while read D E F
do
rsync -ahqzt --password-file=/etc/rsync-client.passwd  --delete $SRC $USER@$WEB2::$DES
rsync -ahqzt --password-file=/etc/rsync-client.passwd  --delete $SRC $USER@$WEB3::$DES
rsync -ahqzt --password-file=/etc/rsync-client.passwd  --delete $SRC $USER@$WEB4::$DES
done
#注意:网络上面大部分都是显示一个中杠,可能是编码的事情,实际是应该是两个杠。


增加权限

#chmod +x /etc/rsync-web.sh


启动脚本

#nohup /etc/rsync-web.sh &       //必须使用nohup放入后台执行,否则关闭终端后此脚本进程会自动结束
/etc/rsync-web.sh &


关闭脚本

sudo pkill rsync
sudo pkill inotifywait


@ERROR: chdir failed rsync error: error starting client-server protocol (code 5) at main.c(1503) [sender=3.0.6]解决办法

setsebool -P rsync_disable_trans on


rsync安装路径(注意查看)

/usr/bin/rsync
/usr/local/bin/rsync
/etc/xinetd.d/rsync

转载http://wiki.it121.net/linux/inotify
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: