一、环境:

节点名称     用途              IP               网页数据路径

 web1       服务节点    119.147.146.248          /data/www

 web2       服务节点    119.147.146.249          /data/www

 web      内容发布节点  119.147.146.246          /data/www

系统版本:CentOS5.6 64位

内核版本:2.6.18-238.el5

二、环境说明:

inotify-tools是用来监控文件系统变化的工具,所以必须安装在内容发布节点上,服务节点不需要安装,另外在web1、web2、web节点上都要安装rsync,在这个环境中,web充当了rsync的客户端角色,而两给服务节点充当了rsync的服务器端角色,整个同步的过程就是一个从客户端向服务器端发送数据的过程。

三、软件安装

1.web上安装inotify

下载:

wget http://sourceforge.net/projects/inotify-tools/files/inotify-tools/3.13/inotify-tools-3.13.tar.gz/download

# tar -zxvf inotify-tools-3.13.tar.gz

# cd inotify-tools-3.13

# ./configure

# make

# make install

2.web、web1和web2上安装rsync

# tar -zxvf rsync_2.6.9.orig.tar.gz

# cd rsync-2.6.9/

# ./configure --prefix=/usr/local/rsync

# make

# make install

三台安装是一样的

四、配置

1.在web1和web2上配置rsync

a. vi /etc/xinetd.d/rsync

将disable = yes 改为no,

b.新建rsyncd.conf

vi /etc/rsyncd.conf

web1的内容如下:

uid = nobody
gid = nobody
use chroot = no
max connections = 10
strict modes = yes
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log

[web1]
path = /data/www/
comment = web1 file
ignore errors
read only = no
write only = no
hosts allow = 119.147.146.246
hosts deny = *
list = false
uid = root
gid = root

auth users = web1
secrets file = /etc/web1.pass

web2的内容如下:

uid = nobody
gid = nobody
use chroot = no
max connections = 10
strict modes = yes
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log

[web2]
path = /data/www/
comment = web1 file
ignore errors
read only = no
write only = no
hosts allow = 119.147.146.246
hosts deny = *
list = false
uid = root
gid = root

auth users = web2
secrets file = /etc/web2.pass

c.新建web1、web2的密码文件

vi /etc/web1.pass

vi /etc/web2.pass

内容格式:

用户名:密码,如:

web1:123456

d.web1、web2上配置好rsync后,依次启动rsync守护进程,将rsync加入开机启动文件中

echo “/usr/local/rsync/bin/rsync --daemon” >> /etc/rc.loacl

2.配置web节点

a.编写shell脚本

vi inotifyrsync.sh

#!/bin/bash
host1=119.147.146.248
host2=119.147.146.249

src=/data/www/
dst1=web1
dst2=web2
user1=web1
user2=web2

/usr/local/bin/inotifywart -mrq --timefmt `%d/%m/%y %H:%M` --format `%T %w%f%e` -e modify,delte,create,attrib $src \
| while read files
        do
        /usr/bin/rsync -vzrtopg --delete --progress --password-file=/etc/web1.pass
            pass $src $user1@$host1::$dst1
        /usr/bin/rsync -vzrtopg --delete --progress --password-file=/etc/web2.pass
            pass $src $user2@$host2::$dst2
                echo "${files} was rsynced" >> /tmp/rsync.log 2 >&1
        done

b.在/etc目录下新建web1.pass和web2.pass密码文件,内容分别是web1和web2用户的密码,更改这两个文件的权限为600

c.把inotifyrsync.sh放到/data/www目录下,放到后台用行:

# chmod 755 /data/www/inotifyrsync.sh
# /data/www/inotifyrsync.sh &

d.最后将此脚本加入开机启动文件中

echo “/data/www/inotifyrsync.sh &” >> /etc/rc.local

五、测试:

在web节点/data/www目录下添加、删除或者修改文件,然后到web1、web2对应目录下查看文件是否随着web中/data/www目录下文件的变化而变化,如果是那边就配置成功了。

如果出现这个错误“/usr/local/bin/inotifywait: error while loading shared libraries: libinotifytools.so.0”可以采用以下办法解决:

ln -sv /usr/local/lib/libinotify* /usr/lib/
ln -s /usr/local/lib/libinotifytools.so.0 /usr/lib64/libinotifytools.so.0
cp /usr/lib/libinotifytools.so.0 /usr/local/lib/