rsync+lsync实现多服务器多文件夹双向同步

目的

在192.168.50.23与192.168.50.22之间同步/root/rsynctest和/root/rsynctest2文件夹

安装xinetd

yum install xinetd

也可以直接使用rsync的daemon模式并设置其开机启动,这样,就不需要安装和配置xinetd了

rsync --daemon --config=/etc/rsyncd.conf

安装rsync

yum -y install rsync

配置rsync

在两台服务器上都配置/etc/rsyncd.conf

log file = /var/log/rsyncd.log
lock file = /var/run/rsync.lock
motd file = /etc/rsync/rsyncd.Motd
port = 873
reverse lookup = no

[rsynctest]
path = /root/rsynctest/
uid = root
gid = root
use chroot = no
read only = no
list = no
max connections = 200
timeout = 600
auth users = Sync
secrets file = /etc/rsync/rsync.pas
hosts allow = *
# hosts deny = 192.168.21.254
[rsynctest2]
path = /root/rsynctest2/
uid = root
gid = root
use chroot = no
read only = no
list = no
max connections = 200
timeout = 600
auth users = Sync
secrets file = /etc/rsync/rsync.pas
hosts allow = *
# hosts deny = 192.168.21.254

/etc/rsync/rsync.pas

Sync:sync_passwd

/etc/rsync/rsyncpass.txt

sync_passwd

注意:/etc/rsync.pas和etc/rsync1.pas的权限必须为600,不然会出错,而且,如果在root下执行操作,这两个文件的owner必须为root

配置xinetd

我们使用xinetd来管理rsync服务,也可以设置xinetd开机启动,从而使rsync也开机启动

vi /etc/xinetd.d/rsync

找到disable = yes这一行,改为disable = no

重启xinetd

/etc/init.d/xinetd restart

配置xinetd开机自启请自行百度

安装lsyncd

yum install epel-release -y
yum install lua lsyncd -y

配置lsyncd

打开/etc/lsyncd.conf,编辑

settings {
    logfile = "/var/log/lsyncd/lsyncd.log", -- 日志路径
    status = "/var/log/lsyncd/lsyncd.status", -- 状态文件
    pidfile = "/var/run/lsyncd.pid", -- pid文件路径
    nodaemon = false,
    statusInterval = 1,  -- 状态文件写入最短时间
    maxProcesses = 4,    -- 最大进程
    maxDelays = 1        -- 最大延迟
}
--多host同步
servers = {
    "192.168.50.22", -- 除自己外其他host的ip
}
-- 多同步目录,rsync模块名和目录名相同
modules = {
    "rsynctest",
    "rsynctest2"
}
-- 源路径
source_path={
    "/root/rsynctest",
    "/root/rsynctest2"
}

username = "Sync"

i = 1

for _, server in ipairs(servers) do
    i = 1
    for _, module in ipairs(modules) do
        sync {
            default.rsync,
            source = source_path[i],
            target = username.."@"..server.."::"..module,
            delete = "running",
            exclude = {
            }, 
            rsync = {
                binary = "/usr/bin/rsync", -- rsync 版本要到3以上
                archive = true,
                compress = true,
                -- owner = false,
                -- group = false,
                -- perms = true,
                verbose = true,
                copy_links = true,
                password_file = "/etc/rsync/rsyncpass.txt",
                -- _extra = {"--bwlimit=200"}
            }
        }
        i = i + 1
    end
end

lsync的配置文件是Lua风格的,可以做很多更复杂的设置

启动lsyncd

/etc/init.d/lsyncd start 
posted @ 2018-07-22 23:29  EmrysChe  阅读(648)  评论(0编辑  收藏  举报