50

lsyncd —— 多机器实时同步文件神器

 5 years ago
source link: https://shockerli.net/post/linux-tool-lsyncd/?amp%3Butm_medium=referral
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

lsyncd 是一个支持实时、双向、多机器的多模式文件同步工具。

使用 Lua 语言封装了 inotify 和 rsync 工具,采用了 Linux 内核(2.6.13 及以后)里的 inotify 触发机制,然后通过 rsync 去差异同步,达到实时的效果。

安装

在源文件服务器上安装:

yum -y install lsyncd

配置

lsyncd 主配置文件,假设放置在 /etc/lsyncd.conf :

settings {
    nodaemon = false,
    logfile = "/var/log/lsyncd.log",
    statusFile = "/var/log/lsyncd.status",
    inotifyMode = "CloseWrite",
    maxProcesses = 8
}

-- 可以有多个sync,各自的source,各自的target,各自的模式,互不影响。
sync {
    default.rsyncssh,
    source    = "/home/wwwroot/web1/",
    host      = "111.222.333.444",
    targetdir = "/home/wwwroot/web1/",
    -- 忽略文件路径规则,可用table也可用外部配置文件
    -- excludeFrom = "/etc/lsyncd_exclude.lst",
    exclude = {
        ".svn",
        "Runtime/**",
        "Uploads/**",
    },
    -- maxDelays = 5,
    delay = 0,
    -- init = false,
    rsync = {
        binary = "/usr/bin/rsync",
        archive = true,
        compress = true,
        verbose = true,
        _extra = {"--bwlimit=2000"},
    },
}

忽略规则

需要忽略同步的文件或文件夹,excludeFrom 选项才配置该文件,exclude 类型的配置不用该配置文件。假设配置文件放在 /etc/lsyncd_exclude.lst

.svn
Runtime/**
Uploads/**

免密登录

为避免每次都需要手动输入密码,可设置为 SSH 免密登录。

启动

lsyncd -log Exec /etc/lsyncd.conf

参考


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK