Chinaunix首页 | 论坛 | 博客
  • 博客访问: 278607
  • 博文数量: 81
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 35
  • 用 户 组: 普通用户
  • 注册时间: 2016-07-19 15:16
文章分类

全部博文(81)

文章存档

2021年(1)

2016年(3)

2015年(6)

2014年(6)

2013年(33)

2012年(32)

分类: LINUX

2013-06-22 01:09:11

lsyncd 的原理和作用

1)开源软件lsyncd)采用inotify原理监听某一目录,如果目录内发生增、删、改、利用rsync协议自动同步到多个服务器

2inotify,从kernel 2.6.13开始正式并入内核,RHEL5支持,可以在.config search CONFIG_INOTIFY=y确认内核对inotify的支持

3)可以本地多点目录的监控并实现到远程目录的同步

4)在rsync client上通过lsyncd监控并推送数据给rsync serverrsync daemonrsync server接收lsyncd推送过来的数据,并写入本地磁盘,完成数据同步

5lsyncd适合于目录(文件)内容非实时变化的数据同步,对于实时变化的数据同步可以考虑DRBD

6lsyncd 暂时不支持用户认证参数(auth users),基于安全的考虑只用通过rsyncdhost allowdeny)来解决

安装和配置

wget

wget

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

Notesuid,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                filename="/usr/local/bin/rsync"/>

51                

52                

53                

62        

63        

 

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的同步目录发现文件被同步过来

阅读(1312) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~