Linux之lsyncd同步工具使用

1.  编译安装lsyncd

    1.  下载lsyncd

        wget https://codeload.github.com/axkibe/lsyncd/zip/refs/tags/release-2.2.2

    2.  安装依赖包

        yum install -y lua lua-devel asciidoc cmake

    3.  编译安装       

unzip lsyncd-master.zip
cd lsyncd-master
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/lsyncd
报错:CMake Error: your CXX compiler: "CMAKE_CXX_COMPILER-NOTFOUND" was not found.   Please set CMAKE_CXX_COMPILER to a valid compiler path or name.
安装: yum install gcc-c++

make && make install 

2.  配置文件

settings {
    --定义日志文件
    logfile ="/var/log/lsyncd/lsyncd.log",
    --定义状态文件
    statusFile ="/var/log/lsyncd/lsyncd.status",
    --指定inotify监控的事件,默认是CloseWrite,还可以是Modify或CloseWrite or Modify
    --这个就是使用的inotify能监控的事件
    inotifyMode = "CloseWrite or Modify",
    --同步进程的最大个数。假如同时有20个文件需要同步,而maxProcesses = 8,则最大能看到有8个rysnc进程
    maxProcesses = 15,
    --(这个配置先森没有使用)累计到多少所监控的事件激活一次同步,即使后面的delay延迟时间还未到。
    --maxDelays=10,
    }
sync {
    --使用rsync通过daemon方式连接远程rsyncd进程;
    default.rsyncssh,
    --同步的源目录,使用绝对路径。
    source    = "/web/data/ftp",
    --定义目的地址,对应不同的模式有不同的写法。这里是远程rsync,使用“用户名@ip::模块名”写法。
    target    = "user@172.17.8.16::datahome",
    --是否同步删除,除了running选项,还有true、false和startup,这个配置先森不是很明白,大概是true是完全同步删除,false是不允许删除,startup和running要难理解一些\
    --先森的理解是,startup是仅在启动时将源目录和目的目录来一次完全同步,lsyncd运行时的源目录的删除文件在目的目录中不做删除操作\
    --running是启动时不对源、目的目录进行完全同步,lsyncd运行时源目录删除的文件,目的目录也会被删除。
    delete="running",
    --排除的文件,这里排除了一些隐藏文件,和文件打开是时的临时文件
    exclude = { ".*", ".tmp","*.swp","*.swx" },
    -- 累计事件,等待rsync同步延时时间,默认15秒。先森配置的是0,也就是实时同步。
    delay = 0,
    rsync     = {
        --本地rsync命令路径
        binary = "/usr/bin/rsync",
        archive = true,
        compress = true,
        verbose   = true,
        --远程rsyncd的密码
        password_file = "/etc/rsyncd/rsync.passwd",
        }
    }        

 3.  实战例子      

settings {
        logfile = "/var/log/lsyncd/lsyncd.log",
        statusFile = "/var/log/lsyncd/lsyncd.status"
}
sync {
    default.rsyncssh,
    source = "/home/java/", --源目录
    host = "192.168.1.165", --目的主机
    targetdir = "/home/java/", --远程目录
    delete = true,
    delay = 0,
    exclude={},
    rsync = {
           binary = "/usr/bin/rsync",
           archive = true, --归档
           compress = true, --压缩
           verbose = true,
           owner = true,   --属主
           perms = true,   --权限
           _extra = {"--bwlimit=2000"},
           },
        ssh = {
            port = 11984
            }
}           

4.  启动lsyncd服务

    /usr/local/lsyncd/bin/lsyncd /usr/local/lsyncd/conf/lsyncd.conf

5.  开机启动脚本

    1.  CentOS6

        1.  在/etc/init.d目录下,创建一个文件lsyncd,内容如下:           

#!/bin/bash
#
# chkconfig: - 85 15
# description: Lightweight inotify based sync daemon
#
# processname:  lsyncd
# config:       /usr/local/lsyncd/conf/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

OPTIONS="-pidfile /var/run/lsyncd.pid /usr/local/lsyncd/conf/lsyncd.conf"

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

RETVAL=0
prog="lsyncd"
thelock=/var/lock/subsys/lsyncd
start() {
        [ -f /usr/local/lsyncd/conf/lsyncd.conf ] || exit 6
        echo -n $"Starting $prog: "
        if [ $UID -ne 0 ]; then
                RETVAL=1
                failure
        else
                daemon /usr/local/lsyncd/bin/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 /usr/local/lsyncd/bin/lsyncd
                RETVAL=$?
                [ $RETVAL -eq 0 ] && rm -f $thelock
        fi;
        echo
        return $RETVAL
}

reload(){
        echo -n $"Reloading $prog: "
        killproc /usr/local/lsyncd/bin/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

        2.  添加到系统服务中

            chkconfig --add lsyncd

        3.  设置启动级别

            chkconfig --level 2345 lsyncd on         

    2.  CentOS7

        1.  在/lib/systemd/system目录下,创建一个文件lsyncd.service,内容如下:

[Unit]
Description=Live Syncing (Mirror) Daemon
After=network.target

[Service]
Type=simple
ExecStart=/usr/local/lsyncd/bin/lsyncd -nodaemon /usr/local/lsyncd/conf/lsyncd.conf

[Install]
WantedBy=multi-user.target

        2.  设置开机启动服务

            systemctl enable lsyncd.service

        3.  服务控制

            systemctl status lsyncd.service

            systemctl start lsyncd.service

            systemctl stop lsyncd.service                

6.  参数优化         

    1.  查看系统默认参数值

1
2
3
sysctl -a | grep max_queued_events
sysctl -a | grep max_user_watches
sysctl -a | grep max_user_instances

    2.  临时修改

1
2
3
sysctl -w fs.inotify.max_queued_events="99999999"
sysctl -w fs.inotify.max_user_watches="99999999"
sysctl -w fs.inotify.max_user_instances="65535"

    3.  固定修改:vim /etc/sysctl.conf #添加以下代码

1
2
3
fs.inotify.max_queued_events=99999999
fs.inotify.max_user_watches=99999999
fs.inotify.max_user_instances=65535

7.  排除子目录

    1.  在lsync配置文件,添加一行内容:

        excludeFrom

sync {
    default.rsyncssh,
    source = "/data/app/webshop/admin.zhenpin.com/activity/m/", --源目录
    host = "192.168.1.170", --目的主机
    targetdir = "/data/app/msale_zhen/activity/", --远程目录
    delete = true,
    delay = 0,
    exclude={},
    excludeFrom = "/usr/local/lsyncd/exclude.list",
    rsync = {
           binary = "/usr/bin/rsync",
           archive = true, --归档
           compress = true, --压缩
           verbose = true,
           owner = true,   --属主
           perms = true,   --权限
           _extra = {"--bwlimit=2000"},
           },
        ssh = {
            port = 11984
            }
}

    2.  在/usr/local/lsyncd/exclude.list文件中,添加要排除的目录或文件

        yangjianbo2/test1/

        yangjianbo2/test2/

        

  

              

posted @ 2021-10-13 14:00  奋斗史  阅读(1025)  评论(0)    收藏  举报