lsyncd 的原理和作用
(1)开源软件lsyncd(http://code.google.com/p/lsyncd)采用inotify原理监听某一目录,如果目录内发生增、删、改、利用rsync协议自动同步到多个服务器
(2)inotify,从kernel 2.6.13开始正式并入内核,RHEL5支持,可以在.config 中search CONFIG_INOTIFY=y确认内核对inotify的支持
(3)可以本地多点目录的监控并实现到远程目录的同步
(4)在rsync client上通过lsyncd监控并推送数据给rsync server的rsync daemon,rsync server接收lsyncd推送过来的数据,并写入本地磁盘,完成数据同步
(5)lsyncd适合于目录(文件)内容非实时变化的数据同步,对于实时变化的数据同步可以考虑DRBD(http://www.drbd.org)
(6)lsyncd 暂时不支持用户认证参数(auth users),基于安全的考虑只用通过rsyncd的host allow(deny)来解决
rsync
rsync
rsync
lsyncd.conf.xml
rsyncd.conf
rsyncd.conf
server3(remote)
server1(local)
server2(remote)
<!--[if !vml]--><!--[endif]-->
安装和配置
wget http://www.samba.org/ftp/rsync/src/rsync-3.0.7.tar.gz
wget http://lsyncd.googlecode.com/files/lsyncd-1.37.tar.gz
Note:由于rsync的服务器和客户端存在版本匹配的问题,所以可以在所有服务器上面安装最新的rsync包,在服务器端运行daemon,客户端进行run rsync。
安装目标服务器server2(remote),server3相同
cd /usr/local
tar zxvf rsync-3.0.7.tar.gz
cd rsync-3.0.7
./configure && make && make install
配置/etc/rsyncd.conf
uid = web
gid = web
use chroot = no
max connections = 4
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
[testweb]
path = /usr/local/test/
read only = no
hosts allow = 192.168.21.0/24
Notes:uid,gid 说明同步过来文件的文件用户属性
安装xinetd
yum -y install xinetd
安装rsync daemon,由xinetd来启动
vi /etc/xinetd.d/rsync
service rsync
{
disable = no
only_from = 192.168.21.0/24
instances = 5
socket_type = stream
wait = no
user = root
server = /usr/local/bin/rsync
server_args = --daemon
log_on_failure += USERID
}
# chkconfig xinetd on
# chown web:web /usr/local/test
# service xinetd start
安装源服务器server1(local)
# cd /usr/local
# tar zxvf rsync-3.0.7.tar.gz
# cd rsync-3.0.7
# ./configure && make && make install
# cd /usr/local
# tar zxvf lsyncd-1.37.tar.gz
# cd lsyncd-1.37
# ./configure && make && make install
# cp lsyncd.conf.xml /etc/
配置/etc/lsyncd.conf.xml
33 <binary filename="/usr/local/bin/rsync"/>
51 <option text="-lt%r"/>
52 <option text="-vzrtopg"/>
53 <option text="--delete"/>
62 <source path="/usr/local/test/"/>
63 <target path="server2::testweb"/>
Note: lsyncd.conf.xml详细的配置请man lsyncd.conf.xml
配置lsyncd的启动脚本/etc/init.d/lsyncd
vi /etc/init.d/lsyncd
#!/bin/bash
#
# lsyncd: Starts the lsync Daemon
#
# chkconfig: 345 99 90
# description:Lsyncd uses rsync to synchronize local directories with a remote
# machine running rsyncd. Lsyncd watches multiple directories
# trees through inotify. The first step after adding the watches
# is to, rsync all directories with the remote host, and then sync
# single file buy collecting the inotify events.
# processname: lsyncd
. /etc/rc.d/init.d/functions
config="/etc/lsyncd.conf.xml"
lsyncd="/usr/local/bin/lsyncd"
lockfile="/var/lock/subsys/lsyncd"
prog="lsyncd"
RETVAL=0
start() {
if [ -f $lockfile ]; then
echo -n $"$prog is already running: "
echo
else
echo -n $"Starting $prog: "
daemon $lsyncd --conf=$config
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch $lockfile
return $RETVAL
fi
}
stop() {
echo -n $"Stopping $prog: "
killproc $lsyncd
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f $lockfile
return $RETVAL
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
status)
status $lsyncd
;;
*)
echo "Usage: lsyncd {start|stop|restart|status}"
exit 1
esac
exit $?
chmod 755 /etc/init.d/lsyncd
配置lsyncd的日志
vi /etc/logrotate.d/lsyncd
/var/log/lsyncd {
daily
copytruncate
compress
notifempty
missingok
postrotate
/etc/rc.d/init.d/lsyncd restart 2>&1 > /dev/null || true
endscript
}
chkconfig lsyncd on
service lsyncd start
测试和结论
在server1生成文件,5秒后在server2的同步目录发现文件被同步过来
(责任编辑:IT) |