zoukankan      html  css  js  c++  java
  • lsync目录文件实时同步工具

    参考文档:https://vastxiao.github.io/article/2017/09/02/lsync-201709021425/

    官网:https://axkibe.github.io/lsyncd/

    1、使用环境

    服务器角色 操作系统类型 IP地址 目录 目标
    源服务器 Centos7.4 11.11.11.2 /share 实时同步到目标服务器
    目标服务器 Centos7.4 11.11.11.3 /opt/share  

     

     

    2、安装

    2.1、目标服务器需要安装的依赖包

    yum -y install lua lua-devel rsync

    2.2、源服务器配置

    1、配置源服务器免密码ssh登录目标服务器

    如果要通过ssh的方式,从源服务器同步到目标服务器的话,需要在源服务器上配置ssh免密码登录,如果是同一台服务器的本地目录同步到另一个目录的话,不需要配置ssh免密码登录
    su - root #用root用户进行远程同步
    ssh-keygen -t dsa #一路回车
    ssh-copy-id -i /root/.ssh/id_dsa.pub root@11.11.11.3 #将源服务器的公钥发送到目标服务器,这里需要输入目标服务器的root用户密码
    ssh root@11.11.11.3 ip address #查看结果是否为目标服务器的ip地址,如果是的话,说明ssh免密钥登录配置成功

    2、安装依赖包

    yum -y install lua lua-devel rsyn
    3、安装lsync
    yum -y install lsyncd       #这条命令只适用于Centos 6.*版本的系统,7以上的系统会报找不到lsyncd包
    #Cnetos
    7.*版本如果要装lsyncd的话,需要先安装扩展源eple如下,然后在执行yum -y install lsyncd # wget http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm # rpm -ivh epel-release-latest-7.noarch.rpm

    3、yum安装文件结构

            路径              说明
    /etc/lsyncd.conf    主配置文件
    /etc/sysconfig/lsyncd       init环境变量和启动选项配置文件
    /etc/logrotate.d/lsyncd    日志滚动配置文件
    /usr/share/doc/lsyncd-*/examples/    目录下有lsyncd.conf配置例子
    /etc/init.d/lsyncd         lsyncd的init启动脚本
    /usr/bin/lsyncd     lsyncd命令路径
    /var/run/lsyncd/    可放lsyncd.pid的目录
    /var/log/lsyncd/    默认的日志目录

    4、文件配置

    4.1、使用远程同步模式同步数据(正式用)

    这里ssh端口改为22022

    实现结果:对源目录下的文件进行增删改都会同步到目标服务器,但需要注意的是在启动服务后会删除目标服务指定的目录下的文件(类似于镜像文件

    [root@localhost share]# cat /etc/lsyncd.conf
    settings {
        inotifyMode = "CloseWrite",
        statusFile = "/var/log/lsyncd.status",
        statusInterval = 3,
        logfile = "/var/log/lsyncd.log"
        }
    
    sync {
        default.rsyncssh,
        source = "/share",
        host = "11.11.11.3",
        targetdir = "/opt/share/",
        maxDelays = 1,
        delay = 3,
        -- init = fales,
        rsync = {
            binary = "/usr/bin/rsync",
            archive = true,
            compress = true,
            verbose = true,
            _extra = {"--bwlimit=2000"},
            },
            ssh = {
               port = 22022
            }
        }
    [root@localhost share]#
    启动服务
    centos6
    # /etc/init.d/lsyncd start
    设置开机启动
    # chkconfig lsyncd on
    centos7
    #systemctl start lsyncd.service
    设置开机启动
    systemctl enable lsyncd.service 

    参数详解:

    setting配置参数详解
    inotifyMode: 默认是CloseWrite,还可以是Modify或者 CloseWrite or Modify
    statusFile: 定义状态文件位置
    statusInterval:将lsyncd的状态写入statusFile文件的建个时间,默认是10s ,对实时同步要求比较高的话,可以调小,我设置为3s。
    logfile:定义日志文件的位置,同步内容也可以在此文件看到
    
    sync配置参数详解
     default.rsyncssh:同步到远程主机目录,rsync的ssh模式,需要使用key来认证
     source:源目录,需要被同步的目录
     host:远程备份主机IP
     targetdir:远程备份目录
     delay:等待rsync同步延时时间,默认15s,如果对实时同步要求比较高,可以设置相对低点,我设置的为3s
     init: 默认为true,启动lsyncd服务后,同步源目录里边的所有数据,如果值为false时,只同步源目录lsyncd服务启动后改变的文件
       rsync选项中参数:
         binary:定义rsync的执行位置
         archive:定义传输是否归档
         compress:定义远程创术是否压缩,
         verbose:定义同步时显示的详细信息,列出同步的文件
         _extra:限制传输速度,为了不影响正常业务运行,可以做此设置,单位kb/s,与rsync相同(计算方法:10MB/s 等于 10 x 1024/8 = 1280kb/s; 2000Kb/s 等于2000 x 8/1024 = 15.6MB/s)
       ssh选项参数
         port:定义远程备份主机的远程端口,定义非22端口,如果是22端口,可以不写。

    4.2、使用本地同步模式同步数据(本地备份

    [root@tomcat2 ~]# cat /etc/lsyncd.conf 
    settings {
       --pidfile = "/var/run/lsyncd/lsyncd.pid",
       --nodaemon  = false,
       inotifyMode = "CloseWrite",
       maxProcesses = 8,
       statusFile = "/tmp/lsyncd.status",
       statusInterval = 10,
       logfile = "/var/log/lsyncd/lsyncd.log"
    }
    
    sync {
       default.rsync,
       source = "/opt/webapplication",
       target = "/back_up/webapplication",
       delete = "false",
       --exclude = { "logs" },
       delay = 5,
       --init = true,
       rsync    = {
        binary = "/usr/bin/rsync",
        archive = true,
        compress = true,
        verbose = true,
            bwlimit = 2000
        }
    }
    
    sync {
       default.rsync,
       source = "/data",
       target = "/back_up/data",
       delete = "false",
       --exclude = { "logs" },
       delay = 5,
       --init = true,
       rsync    = {
        binary = "/usr/bin/rsync",
        archive = true,
        compress = true,
        verbose = true,
            bwlimit = 2000
        }
    }
    [root@tomcat2 ~]# 

    4.3、其他同步方式

    settings {
        logfile ="/usr/local/lsyncd-2.1.5/var/lsyncd.log",
        statusFile ="/usr/local/lsyncd-2.1.5/var/lsyncd.status",
        inotifyMode = "CloseWrite",
        maxProcesses = 8,
        }
    -- I. 本地目录同步,direct:cp/rm/mv。 适用:500+万文件,变动不大
    sync {
        default.direct,
        source    = "/tmp/src",
        target    = "/tmp/dest",
        delay = 1
        maxProcesses = 1
        }
    -- II. 本地目录同步,rsync模式:rsync
    sync {
        default.rsync,
        source    = "/tmp/src",
        target    = "/tmp/dest1",
        excludeFrom = "/etc/rsyncd.d/rsync_exclude.lst",
        rsync     = {
            binary = "/usr/bin/rsync",
            archive = true,
            compress = true,
            bwlimit   = 2000
            }
        }
    -- III. 远程目录同步,rsync模式 + rsyncd daemon
    sync {
        default.rsync,
        source    = "/tmp/src",
        target    = "syncuser@172.29.88.223::module1",
        delete="running",
        exclude = { ".*", ".tmp" },
        delay = 30,
        init = false,
        rsync     = {
            binary = "/usr/bin/rsync",
            archive = true,
            compress = true,
            verbose   = true,
            password_file = "/etc/rsyncd.d/rsync.pwd",
            _extra    = {"--bwlimit=200"}
            }
        }
    -- IV. 远程目录同步,rsync模式 + ssh shell
    sync {
        default.rsync,
        source    = "/tmp/src",
        target    = "172.29.88.223:/tmp/dest",
        -- target    = "root@172.29.88.223:/remote/dest",
        -- 上面target,注意如果是普通用户,必须拥有写权限
        maxDelays = 5,
        delay = 30,
        -- init = true,    进程启动时是否全部同步一次,默认是true
        rsync     = {
            binary = "/usr/bin/rsync",
            archive = true,
            compress = true,
            bwlimit   = 2000
            -- rsh = "/usr/bin/ssh -p 22 -o StrictHostKeyChecking=no"
            -- 如果要指定其它端口,请用上面的rsh
            }
        }
    -- V. 远程目录同步,rsync模式 + rsyncssh,效果与上面相同
    sync {
        default.rsyncssh,
        source    = "/tmp/src2",
        host      = "172.29.88.223",
        targetdir = "/remote/dir",
        excludeFrom = "/etc/rsyncd.d/rsync_exclude.lst",
        -- maxDelays = 5,
        delay = 0,
        -- init = false,
        rsync    = {
            binary = "/usr/bin/rsync",
            archive = true,
            compress = true,
            verbose   = true,
            _extra = {"--bwlimit=2000"},
            },
        ssh      = {
            port  =  1234
            }
        }

    5、使用脚本监测lsyncd服务并设置定时任务

    脚本每25秒监测一次lsyncd服务的进程是否正常,若进程不在了,则自动启动进程,正常与否都会写入日志文件
    ```bash
    cat /opt/lsync_monitor.sh
    #!/bin/bash
    source /etc/profile
    
    START()
    {
    lsyncd -log Exec /etc/lsyncd.conf
    sleep 5
    }
    
    CHECK()
    {
    num=`ps -ef | grep lsyncd.conf | grep -v grep | wc -l`
    if [ $num -eq 0 ];then
      {
      dat=`date`
      echo "$dat lsyncd stoped!!!"
      return 1
      }
    elif [ $num -eq 1 ];then
      {
      dat=`date`
      echo "$dat lsyncd running..."
      return 0
      }
    fi
    }
    
    TOT()
    {
    CHECK
    res=$?
    
    if [ $res -eq 1 ];then
      START
    fi
    }
    
    TOT
    sleep 25
    TOT

    6、其他设置

    ##### 5.2 lsync_monitor.sh产生日志只保留1个月的记录
    由于lsync_monitor.sh每分钟写入6行内容,所以一天下来日志量也是比较大的
    ```bash
    cat /opt/month.sh
    #!/bin/bash
    source /etc/profile
    mv -f /var/log/lsync_monitor.log /var/log/last_month.log
    > /var/log/lsync_monitor.log
    ```
    ##### 5.3 加入crontab定时任务
    ```bash
    crontab -e
    */5 * * * * /usr/sbin/ntpdate -s 192.168.8.18
    #####lsync to 10.66.84.16:/opt/share======
    */1 * * * * /opt/lsync_monitor.sh >> /var/log/lsync_monitor.log
    0 0 1 * * /opt/month.sh

    FAQ:

    当需要同步的数据量上百G时,进程起来一会后,自动停了。查看日志:
    因为同步的数据量较大,需要lsyncd监测的inode数量超过了自身默认的最大监控数量,所以报错
    ```bash
    tail -f /var/log/lsyncd/lsyncd.log
    遇到:Error: Terminating since out of inotify watches.
    Consider increasing /proc/sys/fs/inotify/max_user_watches
    
    解决办法:
    echo 65535000 >  /proc/sys/fs/inotify/max_user_watches
    这个值改成多少需要根据源目录所在分区的的inode多少来定(使用df -i查看该分区总的inode数量)
    sysctl -p
    重启lsyncd
  • 相关阅读:
    OpenJudge 3765(最大权闭合图,最小割
    多校8-1010 HDU5389 (dp
    570D Codeforces Round #316 (Div. 2) D(dfs序,时间戳,二分
    CodeForces
    hiho一下!
    HDU 4123(树上任意点到其他点的最远距离,rmq
    Oracle创建索引;查询索引
    HBase启动和停止命令
    flink dom4j冲突异常
    flink checkpoint状态储存三种方式选择
  • 原文地址:https://www.cnblogs.com/huangyanqi/p/9753508.html
Copyright © 2011-2022 走看看