Asins Blog

简单其实不简单

This project is maintained by asins

一、lsyncd介绍

二、安装

使用brew安装简单方便

brew install lsyncd

三、配置


-- install :
-- lsyncd for osx
-- local dir -> ssh 
-- brew install lsyncd
-- brew install rsync ; /usr/local/bin/rsync instead of local rsync
-- start up : sudo lsyncd ~/a.config


-- file ~/a.config
settings {
    logfile      = "/var/log/lysncd/lsyncd.log",
    statusFile   = "/var/log/lysncd/lsyncd.status",
    inotifyMode  = "CloseWrite", -- 系统inotify指定监听的变化,可以是"Modify"、"CloseWrite" (默认) 或"CloseWrite or Modify".
    maxProcesses = 7,
    -- nodaemon =true,
}

sync {
    default.rsync,
    source    = "/data1/htdocs/fanxing.kugou.git/",
    target    = "www@10.16.6.91:/data1/htdocs/kfwlf.kugou.com/",
	exclude = {"/data1/htdocs/fanxing.kugou.git/.git"},
    -- excludeFrom = "/etc/rsyncd.d/rsync_exclude.lst",
    maxDelays = 1,
    delay = 3,
    delete = true,
    -- init = true,
    rsync     = {
        binary = "/usr/local/bin/rsync",
        archive = true,
        compress = true,
        bwlimit   = 2000,
        rsh = "/usr/bin/ssh -l www -i /Users/mmfei/.ssh/id_rsa -p 32200 -o StrictHostKeyChecking=no"
        -- 如果要指定其它端口,请用上面的rsh
    }
}

3.1 配置说明

settings

里面是全局设置,--开头表示注释,下面是几个常用选项说明:

sync

里面是定义同步参数,可以继续使用maxDelays来重写settings的全局变量。一般第一个参数指定lsyncd以什么模式运行:rsyncrsyncsshdirect三种模式:

rsync

(提示一下,deleteexclude本来都是rsync的选项,上面是配置在sync中的,我想这样做的原因是为了减少rsync的开销)

其它还有rsyncssh模式独有的配置项,如hosttargetdirrsync_pathpassword_file,见后文示例。rsyncOps={"-avz","--delete"}这样的写法在2.1.*版本已经不支持。

lsyncd.conf可以有多个sync,各自的source,各自的target,各自的模式,互不影响。

3.2 其它模式示例

以下配置本人都已经过验证可行,必须根据实际需要裁剪配置:

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,
    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
        }
    }

上面的内容几乎涵盖了所有同步的模式,其中第III个要求像rsync一样配置rsyncd服务端,见本文开头。第IVV配置ssh方式同步,达到的效果相同,但实际同步时你会发现每次同步都会提示输入ssh的密码,可以通过以下方法解决:

在远端被同步的服务器上开启ssh无密码登录,请注意用户身份:

user$ ssh-keygen -t rsa
一路回车...
user$ cd ~/.ssh
user$ cat id_rsa.pub >> authorized_keys

id_rsa私钥拷贝到执行lsyncd的机器上

user$ chmod 600 ~/.ssh/id_rsa
测试能否无密码登录
user$ ssh user@172.29.88.223