lsyncd 文件实时同步

发布时间 2023-08-06 21:57:53作者: MacoPlus

配置文件


  1. 安装
    yum install -y lsyncd
  2. 配置 /etc/lsyncd.conf
    ----
    -- User configuration file for lsyncd.
    --
    -- Simple example for default rsync, but executing moves through on the target.
    --
    -- For more examples, see /usr/share/doc/lsyncd*/examples/
    --
    -- sync{default.rsyncssh, source="/var/www/html", host="localhost", targetdir="/tmp/htmlcopy/"}
    
    settings {
        logfile = "/var/log/lsyncd/lsyncd.log",         -- 日志
        statusFile = "/var/lib/lsyncd/lsyncd.status",   -- 记录同步的文件和目录
        pidfile = "/var/run/lsyncd.pid",                -- lsyncd 运行进程id号
        statusInterval = 3,                             -- 将lsyncd的状态写入上面的statusFile的间隔,默认10秒
        nodaemon = false,                               -- 是否以守护进程方式运行 lsyncd,设置为 true 表示以前台方式运行
        inotifyMode  = "CloseWrite",                    -- 指定inotify监控的事件,默认是CloseWrite,还可以是Modify或CloseWrite or Modify
        insist = true,                                  -- 设置为 true,强制在启动时进行一次完整同步
        maxProcesses = 1,                               -- 同步进程的最大个数
        maxDelays = 1,                                  -- 累计到多少所监控的事件激活一次同步,即使后面的delay延迟时间还未到
    }
    sync {
        default.rsync,
        source = "/static/",
        delete = true,                                  -- 如果源端不存在,则移除目标存在的文件,意思保持与源端一致
        target = "192.168.33.101:/static/",             -- 认证用户,目标机同步位置
        -- excludeFrom = "image*",                      -- 要同步的文件或目录列表,我测试lsyncd过滤并不好用,不清楚是哪里的问题。
        rsync = {
            binary = "/usr/bin/rsync",
            archive = true,                             -- 开启 rysnc 归档模式,等于 -rlptgoD
            compress = false,                           -- 关闭 rsync 压缩模式
            owner = true,                               -- 保留属主
            perms  = true,                              -- 保留文件权限
            group  = true,                              -- 保留属组
            times  = true,                              -- 保留文件修改时间信息
            links  = true,                              -- 同步链接文件
            timeout = 600,                              -- 超时时间,单位为秒
            sparse = false,                             -- 处理稀疏文件
            whole_file = false                          -- 如果设置为 true 则文件发生修改时同步整个文件,否则按rsync算法同步增量
        }
    }
  3. 启动lsyncd
    systemctl start lsyncd.service