文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>用lsyncd来做数据实时同步

用lsyncd来做数据实时同步

时间:2010-04-18  来源:l_penny

    机缘巧合,听群里的兄弟提到了lsyncd这个软件,于是google了一把,发现竟然是google开源项目组的一个开源软件(相关链接http://code.google.com/p/lsyncd/)。     眼神大致一扫,立马下载了一个进行体验:     搭建lsyncd需要内核版本在2.6.13以上,因为它需要一个inotify的内核,这个内核的作用就是可以实时提供系统文件变化的信息,供lsyncd来进行同步文件操作。关于inotify这个东东,我下个文件会详细介绍。     我的环境是centos 5.4,还需要两个软件包,rsync和lsyncd。 下载地址: http://www.samba.org/ftp/rsync/src/rsync-3.0.6.tar.gz http://lsyncd.googlecode.com/files/lsyncd-1.26.tar.gz   一切准备就绪后,我们就开始啦: 1. 在两台服务器上安装rsync     因为centos一般都自带rsync软件,不过版本都比较老(一般是2.6.3),我们先卸载掉老版本,然后安装新版本。 [root@server ~]# rpm -e rsync [root@server ~]# tar zvxf rsync-3.0.6.tar.gz [root@server ~]# cd rsync-3.0.6 [root@server ~]# ./configure --prefix=/usr/local/rsync [root@server ~]# make && make install   2. 在主服务器上安装lsyncd     主服务器为同步数据的源服务器,从服务器不需要安装lsyncd,只启动rsyncd进程即可。 [root@server ~]#tar zxvf lsyncd-1.26.tar.gz [root@server ~]#cd lsyncd-1.26 [root@server ~]#./configure [root@server ~]#make && make install   3. 配置从服务器的rsync守护进程 vi /etc/rsyncd.conf
内容如下:
uid = root
gid = root
use chroot = no
max connections = 5
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
  [data] path = /data hosts allow = 172.16.103.11  #这是主服务器的ip地址 ignore errors
read only = no
write only = no
  保存退出后启动daemon进程。 [root@client ~]# rsync --daemon PS: 我们一般使用rsync都是把启动守护进程的作为主服务器,然后从服务器来同步主服务器的数据,也就是同步rsyncd.conf中tag路径下的数据,而这次刚好相反,我们需要把lsyncd服务器的数据同步至rsync服务器的tag目录。   4. 配置主服务器lsyncd服务 首先编辑配置文件
[root@server lsyncd-1.26]# cp lsyncd.conf.xml /etc/
[root@server ~]# vi  /etc/lsyncd.conf.xml
<lsyncd version="1">
<settings>
<logfile      filename="/var/log/lsyncd.log"/>
<binary     filename="/usr/local/bin/rsync"/>
<pidfile      filename="/var/run/lsyncd.pid"/>
<callopts>
<option text="-avz"/>                 \\这里填写rsync的参数
<option text="--delete"/>
<option text="--force"/>
<exclude-file/>
<source/>
<destination/>
</callopts>
</settings>
<directory>
<source path="/data/"/>                \\源路径
<target path="172.16.100.98::data"/>   \\目的路径
</directory>
</lsyncd>
保存退出。 启动服务: [root@server ~]#  /usr/local/lsyncd/bin/lsyncd --conf /etc/lsyncd.conf.xml   也可以写入系统服务 [root@server ~]# 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
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 /etc/lsyncd.conf.xml"  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 $?
  5. 进行一系列的测试,新建、删除等操作,在我搭建的环境中,两台都是虚拟机,百兆网卡,同步的速度还是蛮快的,系统负载也不高,对于目录文件变动较少的环境比较适用。   后记:     怪我看lsyncd介绍太马虎了,本以为这个软件同步效率可以赶超drbd,结果发现是看差几个字,罪过罪过~~~     摘一段官方介绍:

When not to use:

File with active file handles (e.g. database files) Directories where many changes occur (like mail or news servers)

In these cases e.g. DRBD (see http://www.linux-ha.org/DRBD) might be better for you.

    哈哈,就这段字看差了,我是做邮件的,看来这个软件只能供我自己消遣了。
相关阅读 更多 +
排行榜 更多 +
百炼英雄抽卡技巧指南

百炼英雄抽卡技巧指南

休闲益智 下载
英雄没有闪滚雷旋风技能如何搭配

英雄没有闪滚雷旋风技能如何搭配

休闲益智 下载
英雄没有闪雷旋风BD构筑推荐

英雄没有闪雷旋风BD构筑推荐

休闲益智 下载