Lsyncd搭建同步镜像-远程实时同步备份

2020-03-26 10:30:09 浏览数 (1)

背景:因自研日志系统改造,为容灾需要,需将主日志机的日志文件完整备份到备机,而公司内部并没有相应的系统。经过调研,我们的应用场景很简单,logstash也是可以满足我们的需求,但太重了点,后运维推荐使用lsyncd,故将使用中遇到的坑整理处理

1. Lsyncd是啥

    lsyncd会密切监测本地服务器上的参照目录,当发现目录下有文件或目录变更后,立刻通知远程服务器,并通过rsync 或rsync ssh方式实现文件同步。这样做的好处就是,你可以利用Lsyncd搭建一个VPS同步镜像,应用场景例如CDN镜像、网站数据备份、网站搬家等等

2. 安装Lsyncd

    我们的服务器比较简单,基于centos的yum安装即可

代码语言:javascript复制
yum install lsyncd

 之后修改配置文件/etc/lsyncd.conf,对应的参数说明可以参考官方文档 具体如下:

代码语言:javascript复制
settings {
    logfile      = "/var/log/lsyncd/lsyncd.log",
    statusFile   = "/var/log/lsyncd/lsyncd.status",
    inotifyMode  = "CloseWrite",
    maxProcesses = 1000,
    maxDelays    = 200,
}

sync {
    default.rsync,
    source = "/tmp/logs/",
    target = "user00@xxx.xxx.xxx.xxx::backup",
    delay  = 100,
    rsync  = {
        binary    = "/usr/bin/rsync",
        password_file = "/etc/rsyncd.passwd",
        archive   = true,
        compress = false,
        verbose   = true
    }
}

 之后运行测试

代码语言:javascript复制
lsyncd -log Exec /etc/lsyncd.conf

但是在使用的过程中遇到个问题

代码语言:javascript复制
Error: error preparing /etc/lsyncd.conf: Parameter "rsync.password_file" unknown. (if this is not a typo add it to checkgauge)

 后来google告诉我这是一个bug,开发者说2.1.2已经修复,但是github上有人反馈2.1.4版本还是有问题,而我们的TLinux1.2默认用yum安装的话就是2.1.4的,于是自己卸载升级为2.1.5,后面附上编译安装流程,问题解决。

之后新建密码文件/etc/rsyncd.passwd

代码语言:javascript复制
test1234

 执行如下命令进行测试

代码语言:javascript复制
/usr/bin/rsync --delete -gvsolptD --password-file=/etc/rsyncd.passwd-r /tmp/logs/user00@::backup/

设置文件的owner为root,并将权限设置为600,否则会提示

代码语言:javascript复制
@ERROR: chroot failed
rsync error: error starting client-server protocol (code 5) at main.c(1522) [receiver=3.0.3]

3. Rsync client备机启动

    拿我们正常的机器应该都默认安装了rsync,只需要启动一个监听服务即可。

修改/etc/rsyncd.conf文件

代码语言:javascript复制
uid=root
gid=root
use chroot=yes
max connections=100
pid file=/var/run/rsyncd.pid
lock file=/var/run/rsync.lock
log file=/var/log/rsyncd.log
timeout=600
port=873
secrets file=/etc/.rsyncd.secrets

[app]
path=//app
comment=rsync files
read only=no
list=yes
auth users=user00

[backup]
path=/tmp/backup
read only=no
list=yes
auth users=user00

 添加密码文件/etc/.rsyncd.secrets

代码语言:javascript复制
user00:test1234
user01:test12345

设置文件的owner为root,并将权限设置为600

执行启动,启动address为备机的ip,即执行该命令的机器ip

代码语言:javascript复制
/usr/bin/rsync --address= --config=/etc/rsyncd.conf --daemon

最后启动

代码语言:javascript复制
service lsyncd start

修改文件可以看到日志文件/var/log/lsyncd/lsyncd.log

日志详情日志详情

附录:Lsyncd 2.1.5编译安装

代码语言:javascript复制
yum install lua-devel
tar -xzf lsyncd-2.1.5.tar.gz 
cd lsyncd-2.1.5/
./configure --prefix=/usr/bin
make && make install

lsyncd依赖lua,如果没有lua-devel,会有如下提示

代码语言:javascript复制
configure: error: Need a Lua toolchain with matching versions ('lua' library and 'lua' and 'luac' programs)

 添加/etc/lsyncd.conf文件

代码语言:javascript复制
settings {
    logfile      = "/var/log/lsyncd/lsyncd.log",
    statusFile   = "/var/log/lsyncd/lsyncd.status",
    inotifyMode  = "CloseWrite",
    maxProcesses = 1000,
    maxDelays    = 200,
}

sync {
    default.rsync,
    source = "/tmp/logs/",
    target = "user00@xxx.xxx.xxx.xxx::backup",
    delay  = 100,
    rsync  = {
        binary    = "/usr/bin/rsync",
        password_file = "/etc/rsyncd.passwd",
        archive   = true,
        compress = false,
        verbose   = true
    }
}

 添加启动service文件

代码语言:javascript复制
vi /etc/init.d/lsyncd
代码语言:javascript复制
#!/bin/bash
#
# chkconfig: - 85 15
# description: Lightweight inotify based sync daemon
#
# processname:  lsyncd
# config:       /etc/lsyncd.conf
# config:       /etc/sysconfig/lsyncd
# pidfile:      /var/run/lsyncd.pid

# Source function library
. /etc/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0

LSYNCD_OPTIONS="-pidfile /var/run/lsyncd.pid /etc/lsyncd.conf"

if [ -e /etc/sysconfig/lsyncd ]; then
  . /etc/sysconfig/lsyncd
fi

RETVAL=0

prog="lsyncd"
thelock=/var/lock/subsys/lsyncd

start() {
	[ -f /etc/lsyncd.conf ] || exit 6
        echo -n $"Starting $prog: "
        if [ $UID -ne 0 ]; then
                RETVAL=1
                failure
        else
                daemon /usr/bin/lsyncd $LSYNCD_OPTIONS
                RETVAL=$?
                [ $RETVAL -eq 0 ] && touch $thelock
        fi;
        echo
        return $RETVAL
}

stop() {
        echo -n $"Stopping $prog: "
        if [ $UID -ne 0 ]; then
                RETVAL=1
                failure
        else
                killproc lsyncd
                RETVAL=$?
                [ $RETVAL -eq 0 ] && rm -f $thelock
        fi;
        echo
        return $RETVAL
}

reload(){
        echo -n $"Reloading $prog: "
        killproc lsyncd -HUP
        RETVAL=$?
        echo
        return $RETVAL
}

restart(){
        stop
        start
}

condrestart(){
    [ -e $thelock ] && restart
    return 0
}

case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  restart)
        restart
        ;;
  reload)
        reload
        ;;
  condrestart)
        condrestart
        ;;
  status)
        status lsyncd
        RETVAL=$?
        ;;
  *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|reload}"
        RETVAL=1
esac

exit $RETVAL

0 人点赞