使用lsyncd实时远程及本地同步备份

# lsyncd

  • Lysncd 实际上是lua语言封装了 inotify 和 rsync 工具,采用了 Linux 内核(2.6.13 及以后)里的 inotify 触发机制,然后通过rsync去差异同步,达到实时的效果。
  • 通过时间延迟或累计触发事件次数完美解决了 inotify + rsync海量文件同步带来的文件频繁发送文件列表的问题 。
  • 另外,它的配置方式很简单,lua本身就是一种配置语言,可读性非常强。lsyncd也有多种工作模式可以选择,本地目录cp,本地目录rsync,远程目录rsyncssh。

Github:lsyncd's github

# 安装

#Ubuntu
apt-get install lsyncd 
# yum install lua lua-devel
CentOS
rpm -ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
yum install lsyncd -y

# 配置

sudo vi /etc/lsyncd.conf 远程备份及本地备份

settings {
        logfile = "/var/log/lsyncd/lsyncd.log",
        statusFile = "/var/log/lsyncd/lsyncd.status"
}
sync {
    default.rsyncssh,
    source = "/root/linsir", --源目录
    host = "192.168.2.16", --目的主机
    targetdir = "/root/remote", --远程目录
    delete = true,
    delay = 0,
    exclude={                 
        ".txt"             --排除
    },
    rsync = {
            binary = "/usr/bin/rsync",
            archive = true, --归档
            compress = true, --压缩
            verbose = true, 
            owner = true,   --属主
            perms = true,   --权限
            _extra = {"--bwlimit=2000"},
            },
        ssh = {
            port = 3322
            }
}

# 配置 ssh密钥登录

ssh-keygen -t rsa
ssh-copy-id root@192.168.2.16

# 创建备份文件夹

ssh 192.168.2.16
mkdir /root/remote

# 启动服务及开机启动

/etc/init.d/lsyncd start
chkconfig lsyncd on

# Examples

settings {
    logfile = "/var/log/lsyncd/lsyncd.log",
    statusFile = "/var/log/lsyncd/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
        }
    }

# 参考地址

--EOF--


>看不到评论?GFW!!!