fdbs avatar

lsyncd實時遠程及本地同步備份演示

🕙 by fdbs

  • Lysncd 實際上是lua語言封裝了inotify 和rsync 工具,採用了Linux 內核(2.6.13 及以後)裡的inotify 觸發機制,然後通過rsync去差異同步,達到實時的效果。
  • 通過時間延遲或累計觸發事件次數完美解決了inotify + rsync海量文件同步帶來的文件頻繁發送文件列表的問題。
  • 另外,它的配置方式很簡單,lua本身就是一種配置語言,可讀性非常強。lsyncd也有多種工作模式可以選擇,本地目錄cp,本地目錄rsync,遠程目錄rsyncssh。

Github:lsyncd 的 github

1
2
3
4
5
6
#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 遠程備份及本地備份

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
            }
}

1
2
ssh-keygen -t rsa
ssh-copy-id [email protected]

创建备份文件夹)

1
2
ssh 192.168.2.16
mkdir /root/remote

启动服务及开机启动

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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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 = "[email protected]::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 = "[email protected]:/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
        }
    }

写一条评论

Based on Golang + fastHTTP + sdb | go1.16.4 Processed in 1ms