lsync+rsync 实时同步(ubuntu16.04系统)

1.同步端需要安装 lsync/rsync
apt-get install lsyncd rsync
2.生成ssh公钥,粘贴到目标机器里面
3.创建配置文件
mkdir /etc/lsyncd
cat /etc/lsyncd/lsyncd.conf.lua
sync {
default.rsyncssh,
source = "/var/www/html",
host = "172.16.80.26",
targetdir = "/var/www/html",
-- 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 = 22
}
}


4.启动lsyncd

/etc/init.d/lsyncd start

 

参考https://vastxiao.github.io/article/2017/09/02/Linux/lsyncd_usage/

 

-- # 监测本地目录发生变化就用touch更新一下mtime时间。
flushModifyTime ={
    delay = 10,
    maxProcesses = 10,
    onCreate  = "touch ^sourcePathname",
    onModify  = "touch ^sourcePathname",
}
sync {
    flushModifyTime,
    source    = "/videos_store/video/",
    --delete    = false
}
-- # 本地目录同步,direct:cp/rm/mv。 适用:500+万文件,变动不大
sync {
    default.direct,
    source    = "/tmp/src",
    target    = "/tmp/dest",
    delay = 1
    maxProcesses = 1,
} 
-- # 本地目录同步,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
        } 
    }
-- # 远程目录同步,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"}
        }
    }
-- # 远程目录同步,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
        }
    }
-- # 远程目录同步,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
        }
    }

  

 

posted @ 2018-08-30 16:36  ddif  阅读(1938)  评论(0编辑  收藏  举报