http://blog.johntechinfo.com/category/technology/sersync
http://www.ibm.com/developerworks/cn/linux/l-inotifynew/
可利用inotify+rsync构建的数据实时同步来实现
1.发布web站点
客户端:192.168.0.35 (发布站点,被备份的服务器)
服务端:192.168.0.36 (同步站点,备份服务器)
192.168.0.36配置:
安装软件:
yum install rsync -y
添加用户
useradd test -p westos
修改配置文件:
vi /etc/rsyncd.conf
****************************************************
uid=root
gid=root
max connections=36000
use chroot=no
log file=/var/log/rsyncd.log
pid file=/var/run/rsyncd.pid
lock file=/var/run/rsyncd.lock
[web]
path=/var/www/html
comment = web backup
ignore errors = yes
read only = no
auth users=test
secrets file=/etc/rsyncd.pass
hosts allow = 192.168.0.0/24
hosts deny = *
****************************************************
添加密码文件:
vi /etc/rsyncd.pass
test:westos
chmod 600 /etc/rsyncd.pass
启动服务:
rsync --daemon
192.168.0.35配置:
安装软件:
yum install rsync -y
添加密码文件:
echo "westos" > /etc/rsyncd.pass
chmod 600 /etc/rsyncd.pass
测试:在192.168.0.35下的/var/www/html下添加文件,然后
rsync -avzP /var/www/html/ test@192.168.0.36::web/ --password-file=/etc/rsyncd.pass
观察是否在192.168.0.36的/var/www/html下出现!
rsync -avzP test@192.168.0.36::web/ --password-file=/etc/rsyncd.pass /var/www/html/ (反方向同步)
通过inotify实现实时同步:
192.168.0.35配置:
源码安装inotify:
tar zxf inotify-tools-3.14.tar.gz
cd inotify-tools-3.14
./configure --prefix=/usr/local/inotify
make && make install
编写脚本:
vi inotify.sh
****************************************************
#!/bin/bash
src=/var/www/html
dsthost=192.168.0.36
user=test
dstmodule=web
passwdfile=/etc/rsyncd.pass
/usr/local/inotify/bin/inotifywait -mrq --timefmt '%d%m%y %H:%M' --format '%T %w%f%e' -e create,delete,modify,attrib $src \
| while read files
do
cd $src && rsync -avrz -R --delete ./ --timeout=100 test@192.168.0.36::web/ --password-file=/etc/rsyncd.pass > /dev/null 2>&1
done
****************************************************
sh inotify.sh &
测试:在192.168.0.35端添加,删除文件看192.168.0.36的web模块下的目录文件变化!