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