您的位置:首页 > 其它

基于Lsync+sync实现实时文件同步

2009-10-27 18:54 579 查看
lsyncd 全称是Live Syncing (Mirror) Daemon,它是一款能实时同步本地文件或目录到远端的机器,

它的工作原理:监视本地的目录,当源数据有文件或目录更新时,更新本地文件或目录到远端机器,
保持实时文件同步。它更新数据时需要远端机器运行rsync demon。

下面我将通过一个示例来通过lsyncd与rsync实现数据的实时同步
本示例所需要的软件是:lsyncd与rsync
在它们的官方主页上可以下载最新的源代码
lsyncd的官方主页为:http://code.google.com/p/lsyncd/
rsync的官方主页为:http://samba.anu.edu.au/rsync/
系统资源要求,对客户端要求是linux系统,对服务器端要求是linux rhel5或者是CentOS,笔者在linux rhel4上安装的过程中就遇到:
make all-am
make[1]: Entering directory `/root/lsyncd-1.25'
gcc -DHAVE_CONFIG_H -DXML_CONFIG -I. -Wall `xml2-config --cflags` -g -O2 -MT lsyncd.o -MD -MP -MF .deps/lsyncd.Tpo -c -o lsyncd.o lsyncd.c
In file included from lsyncd.c:19:
inotify-nosys.h:155:3: #error "Unsupported architecture!"
inotify-nosys.h:158:3: #error "Unsupported architecture!"
inotify-nosys.h:161:3: #error "Unsupported architecture!"
In file included from lsyncd.c:19:
inotify-nosys.h: In function `inotify_init':
inotify-nosys.h:167: error: `__NR_inotify_init' undeclared (first use in this function)
inotify-nosys.h:167: error: (Each undeclared identifier is reported only once
inotify-nosys.h:167: error: for each function it appears in.)
inotify-nosys.h: In function `inotify_add_watch':
inotify-nosys.h:172: error: `__NR_inotify_add_watch' undeclared (first use in this function)
inotify-nosys.h: In function `inotify_rm_watch':
inotify-nosys.h:177: error: `__NR_inotify_rm_watch' undeclared (first use in this function)
make[1]: *** [lsyncd.o] Error 1
make[1]: Leaving directory `/root/lsyncd-1.25'
make: *** [all] Error 2
不过最后请教张宴,发现问题是因为:
文件系统变化通知机制inotify是在Linux 2.6.13内核中新引入的,而RHEL4的内核应该在2.6.9以下,从报错信息来看,也是因为内核不支持inotify。RedHat AS5、CentOS 5等Linux内核为2.6.18版本,支持inotify。
详见网页:http://blog.s135.com/sd2c_nginx/1/1/

第一步:在服务器上安装rsync
#wget http://samba.anu.edu.au/ftp/rsync/src/rsync-3.0.5.tar.gz #tar xf rsync-3.0.5.tar.gz
#cd rsync-3.0.5
#./configure --prefix=/usr/local/rsync --disable-ipv6 --disable-iconv
#make && make install

第二步:在服务器上安装lsyncd
#wget http://lsyncd.googlecode.com/files/lsyncd-1.26.tar.gz #tar zxvf lsyncd-1.26.tar.gz
#cd lsyncd-1.26
#./configure
#make && make install
第三步:客户端的配置
#vi /etc/rsyncd.conf
[test]
path = /usr/xxtsrc/photo/ #保持数据的目录
hosts allow = 192.168.5.186 #允许访问的主机IP
read only = false #是否只读

新建目录
#mkdit /usr/xxtsrc/photo/
#chown nobody:nobody /usr/xxtsrc/photo/
在服务器上启动rsync
#/usr/local/rsync/bin/rsync --daemon
使rsync随机器启动
echo "/usr/local/rsync/bin/rsync --daemon" >> /etc/rc.local

在服务器端配置lsyncd
建立配置文件
#vi /etc/lsyncd.conf
/usr/xxtsrc/photo/ 192.168.5.186::test
说明:/usr/xxtsrc/photo/ 要实时同步的目录
192.168.5.186 要实时同步到的远端机器
test 对应远端机器的rsync的tag。
如果要同步多台就在这个文件中每同步一台加一行

对lsyncd建立日志文件
#vi /etc/logrotate.d/lsyncd
/var/log/lsyncd {
missingok
notifempty
sharedscripts
postrotate
/etc/rc.d/init.d/lsyncd restart 2>&1 > /dev/null || true
endscript
}
说明:它的日志保存的文件为/var/log/lsyncd,
建立lsyncd的启动文件
#vi /etc/rc.d/init.d/lsyncd
如下:
#! /bin/sh
. /etc/rc.d/init.d/functions
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: "
IFS=
for i in `cat /etc/lsyncd.conf`
do
source=`echo $i | awk '{print $1}'`
target=`echo $i | awk '{print $2}'`
daemon "$lsyncd $source $target/"
done
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/rc.d/init.d/lsyncd
启动lsyncd
#/etc/rc.d/init.d/lsyncd start
确认lsyncd是否启动
#/etc/rc.d/init.d/lsyncd status
lsyncd (pid 22863) is running...
把lsyncd加入到随机器启动当中
#chkconfig lsyncd on

测试:
在服务器端的/usr/xxtsrc/photo下建立一个文件。客户端的/usr/xxtsrc/photo/发现新建的文件已经同步过去了。但是如没有同步成功请检查日志文件/var/log/lsyncd,确定错误。
在测试额过程中把服务器端得lsyncd杀掉之后,在服务器端得/usr/xxtsrc/photo上新建和删除文件,然后重启lsyncd的服务器,在客户端查看的时候已经发现对应的文件已经删除或者是同步过去了。
不过在测试大文件的过程中,还是会出现一些滞后的情况,因此Lsync+sync比较适合少量小文件的同步。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: