搭建linux服务器间实时同步,采用Lsyncd — Live Syncing (Mirror) Daemon方案

项目介绍:
Lsyncd (Live Syncing Daemon) synchronizes local directories with remote targets

Description

Lsyncd watches a local directory trees event monitor interface (inotify or fsevents). It aggregates and combines events for a few seconds and then spawns one (or more) process(es) to synchronize the changes. By default this is rsync. Lsyncd is thus a light-weight live mirror solution that is comparatively easy to install not requiring new filesystems or block devices and does not hamper local filesystem performance.

Rsync+ssh is an advanced action configuration that uses a SSH to act file and directory moves directly on the target instead of re-transmitting the move destination over the wire.

Fine-grained customization can be achieved through the config file. Custom action configs can even be written from scratch in cascading layers ranging from shell scripts to code written in the Lua language. This way simple, powerful and flexible configurations can be acheived. See the manual for details.

Lsyncd 2.2.1 requires rsync >= 3.1 on all source and target machines.

License: GPLv2 or any later GPL version.

When to use

Lsyncd is designed to synchronize a local directory tree with low profile of expected changes to a remote mirror. Lsyncd is especially useful to sync data from a secure area to a not-so-secure area.

Other synchronization tools

DRBD operates on block device level. This makes it useful for synchronizing systems that are under heavy load. Lsyncd on the other hand does not require you to change block devices and/or mount points, allows you to change uid/gid of the transferred files, separates the receiver through the one-way nature of rsync. DRBD is likely the better option if you are syncing databases.

GlusterFS and BindFS use a FUSE-Filesystem to interject kernel/userspace filesystem events.

Mirror is an asynchronous synchronisation tool that takes use of the inotify notifications much like Lsyncd. The main differences are: it is developed specifically for master-master use, thus running on a daemon on both systems, uses its own transportation layer instead of rsync and is Java instead of Lsyncd’s C core with Lua scripting.

Lsyncd usage examples

1
lsyncd -rsync /home remotehost.org::share/

This watches and rsyncs the local directory /home with all sub-directories and transfers them to ‘remotehost’ using the rsync-share ‘share’.

1
lsyncd -rsyncssh /home remotehost.org backup-home/

This will also rsync/watch ‘/home’, but it uses a ssh connection to make moves local on the remotehost instead of re-transmitting the moved file over the wire.

Some more complicated examples, tips and tricks you can find in the manual.

Disclaimer

Besides the usual disclaimer in the license, we want to specifically emphasize that the authors, and any organizations the authors are associated with, can not be held responsible for data-loss caused by possible malfunctions of Lsyncd.

项目地址:https://github.com/axkibe/lsyncd

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

实现简单高效的本地目录同步备份(网络存储挂载也当作本地目录),一个命令搞定。

为什么不用inotify + rsync 和sersync + rsync
inotify + rsync文件目录很大或文件很大时效率低、sersync停止开发了,并未解决遍历目录列表的问题,同样存在效率问题,但功能上较inotify有一定改进

相关配置教程:
https://www.cnblogs.com/betx/p/6523953.html
http://www.cnblogs.com/betx/p/6524760.html
http://www.cnblogs.com/betx/p/6524916.html
https://segmentfault.com/a/1190000002737213

———————————————————————–
———————————————————————–
Part 1 发送端配置教程
———————————————————————–

一、配置密钥,服务器之间免密码登录(第一次连接需要手动连接)
1. 主/从服务器之间启用基于密钥的身份验证。登录发送端服务器并用

1
ssh-keygen

命令生成公共或私有的密钥,注意密匙路径及名称修改为/root/.ssh/Lysncd_rsa。

2. 使用ssh-copy-id命令复制密钥文件到接收端服务器并设置权限,该命令详细参考https://xiaohost.com/2367.html

1
ssh-copy-id -p xxxx -i /root/.ssh/Lysncd_rsa.pub root@xxx.xxx.xx.xxx

命令说明:
注意:如果接收端使用的密匙登录,而不是密码登录,则需要有密匙,或将Lysncd_rsa.pub下载后上传到接收端/root/.ssh/目录下
xxxx为接收服务器ssh端口(默认22,但一般服务器为了不被暴力破解,都更换了自定义的ssh端口,根据自己的端口设置来,如果不知道端口,使用命令cat /etc/ssh/sshd_config查看prot配置项后面的数字)
xxx.xxx.xx.xxx为接收端服务器的IP

3.若要双向连接,在接收端生成密匙,再使用上面命令复制到发送端即可

二、安装rsync + lsyncd
1. 安装rsync

1
yum install rsync -y

2. 安装Lsyncd
3. 安装lsyncd依赖包

1
yum install lua lua-devel pkgconfig gcc asciidoc -y

4. 安装lsyncd

1
yum install lsyncd -y

3.注意,如果安装报错找不到软件lsyncd,需要安装扩展源(如果之前安装其他软件的时候已经安装过了,则不需要),CentOS7/RHEL7 安装 EPEL 步骤:
(1)yum 自动安装方法

1
yum -y install epel-release

(2)手动安装方法:

1
rpm -iUvh http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-9.noarch.rpm

或者先下载再安装

1
2
wget http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-9.noarch.rpm
rpm -vih epel-release-7-2.noarch.rpm

以上两种方法安装完新的源后,都需要更新系统源的缓存

1
yum clean all && yum makecache

扩展阅读:EPEL 是 yum 的一个软件源, 里面包含了许多基本源里没有的软件, EPEL, 即 Extra Packages for Enterprise Linux 的简称, 是为企业级 Linux 提供的一组高质量的额外软件包, 包括但不限于 Red Hat Enterprise Linux (RHEL), CentOS and Scientific Linux (SL), Oracle Enterprise Linux (OEL).

三、配置lsyncd
1.复制配置示例覆盖安装配置文件

1
cp /usr/share/doc/lsyncd-2.2.2/examples/lrsync.lua /etc/lsyncd.conf

注意lsyncd-2.2.2后面的数字是版本号根据实际安装的版本来修改—如果采用下面的步骤,这一步可以省去,不用覆盖也行,覆盖了还是要清空这个配置文件,然后粘贴以下配置信息至文件内

2. 编辑lsyncd配置文件
先删除原配置文件中的内容:

1
> /etc/lsyncd.conf

或使用下面这个清空命令,两个功能一样
cat /dev/null > /etc/lsyncd.conf
再以下配置文件粘贴进去

1
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
settings {
logfile      ="/var/log/lsyncd.log",
statusFile   ="/tmp/lsyncd.stat",
inotifyMode  = "CloseWrite or Modify",
maxProcesses = 5,
maxDelays = 1,
-- nodaemon =true,
}

sync {
default.rsync,
source = "/download",
target = "/download",
-- excludeFrom = "/etc/rsyncd.d/rsync_exclude.lst",
rsync = {
binary = "/usr/bin/rsync",
archive = true,
compress = true,
verbose = true,

}
}

后面有五个示例配置!!!!

设置参数说明:
重要:lsyncd.conf可以有多个sync,各自的source,各自的target,各自的模式,互不影响。-用于向多个目标同步
————————————————————–
settings 全局变量
里面是全局设置,–开头表示注释,下面是几个常用选项说明:
logfile 定义日志文件
stausFile 定义状态文件
nodaemon=true 表示不启用守护模式,默认
statusInterval 将lsyncd的状态写入上面的statusFile的间隔,默认10秒
inotifyMode 指定inotify监控的事件,默认是CloseWrite,还可以是Modify或CloseWrite or Modify
maxProcesses 同步进程的最大个数。假如同时有20个文件需要同步,而maxProcesses = 8,则最大能看到有8个rysnc进程
maxDelays 累计到多少所监控的事件激活一次同步,即使后面的delay延迟时间还未到
————————————————————–
sync 同步参数(该部分可以定义多个,用于向几个接收端发送)
该部分用于定义同步参数,可以在该部分继续使用maxDelays来重写settings的全局变量(向多个接收端同步时,可以使用maxDelays来启用不同策略,避免同一时间流量爆满)。
default.rsync 一般第一个参数指定lsyncd以什么模式运行,rsync、rsyncssh、direct三种模式区别如下:
default.rsync 本地目录之间同步,使用rsync也可以达到使用ssh形式的远程rsync效果,或daemon方式连接远程rsyncd进程,该模式也适用于远程目录同步;
default.direct 本地目录之间间同步,使用cp、rm等命令完成差异文件备份;
default.rsyncssh 同步到远程主机目录,rsync的ssh模式,需要使用key来认证(后附ssh无密码登录说明)
source 同步的源目录(发送端),使用绝对路径。
target 定义目的地址(接收端).对应不同的模式有几种写法:
/tmp/dest 本地目录同步,可用于direct和rsync模式
172.29.88.223:/tmp/dest 同步到远程服务器目录,可用于rsync和rsyncssh模式,拼接的命令类似于:

1
/usr/bin/rsync -ltsd --delete --include-from=- --exclude=* SOURCE TARGET

剩下的就是rsync的内容了,比如指定username,免密码同步等
172.29.88.223::module 同步到远程服务器目录,用于rsync模式,三种模式的示例会在末尾给出。
init 这是一个优化选项,当init = false,只同步进程启动以后发生改动事件的文件,原有的目录即使有差异也不会同步。默认是true
delay 累计事件,等待rsync同步延时时间,默认15秒(最大累计到1000个不可合并的事件)。也就是15s内监控目录下发生的改动,会累积到一次rsync同步,避免过于频繁的同步。(可合并的意思是,15s内两次修改了同一文件,最后只同步最新的文件)
excludeFrom 排除选项,后面指定排除的列表文件,如:

1
excludeFrom = "/etc/lsyncd.exclude"

如果是简单的排除,可以使用exclude = LIST。
这里的排除规则写法与原生rsync有点不同,更为简单:
监控路径里的任何部分匹配到一个文本,都会被排除,例如/bin/foo/bar可以匹配规则foo
如果规则以斜线/开头,则从头开始要匹配全部
如果规则以/结尾,则要匹配监控路径的末尾
?匹配任何字符,但不包括/
*匹配0或多个字符,但不包括/
**匹配0或多个字符,可以是/
delete 为了保持target与souce完全同步,Lsyncd默认会delete = true来允许同步删除。它除了false,还有startup、running值。
————————————————————–

rsync
(提示一下,delete和exclude本来都是rsync的选项,上面是配置在sync中的,我想这样做的原因是为了减少rsync的开销)
bwlimit 限速,单位kb/s,与rsync相同(这么重要的选项在文档里竟然没有标出)
compress 压缩传输默认为true。在带宽与cpu负载之间权衡,本地目录同步可以考虑把它设为false
perms 默认保留文件权限。
————————————————————–
其它rsync的选项
其它还有rsyncssh模式独有的配置项,如host、targetdir、rsync_path、password_file,见后文示例。rsyncOps={“-avz”,”–delete”}这样的写法在2.2.*版本已经不支持
————————————————————–
配置示例:

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

注意:其中第III个要求像rsync一样配置rsyncd服务端,见本文开头。第IV、V配置ssh方式同步,达到的效果相同,但实际同步时你会发现每次同步都会提示输入ssh的密码,可以通过以下方法解决:

————————————————————–
IV中的用户和权限处理
#新建用户和用户组,专门用于rsync
adduser newname
#修改用户密码
passwd newname
#新建同步源的文件夹,如果使用已经存在的则不用新建

1
mkdir newname

#更改文件的所有者和组

1
chown newname:newname newname

#xxxxx为需要改变权限的文件/目录

#更改文件/目录的读写权限

1
chmod go-rw newname

非root下前面加sudo
————————————————————–
lsyncd的功能不仅仅是同步,官方手册高级功能提到:还可以监控某个目录下的文件,根据触发的事件自己定义要执行的命令,example是监控某个某个目录,只要是有jpg、gif、png格式的文件参数,就把它们转成pdf,然后同步到另一个目录。
另外个问题,同时设置了maxDelays和delay,当监控目录一直没有文件变化了,也会发生同步操作,虽然没有可rsync的文件。

使用:
启动同步服务

1
systemctl start rsyncd

使用命令加载配置文件,启动守护进程,自动同步目录操作

1
lsyncd -log Exec /usr/local/lsyncd-2.2.2/etc/lsyncd.conf


设置开机启动

1
systemctl enable rsyncd

查看日志

1
tail -10 /var/log/lsyncd.log

———————————————————————–
Part 2 接收端配置教程
———————————————————————–

1. 安装rsync

1
yum install rsync -y

2. 安装Lsyncd
3. 安装lsyncd依赖包

1
yum install lua lua-devel pkgconfig gcc asciidoc -y

4. 安装lsyncd

1
yum install lsyncd -y

5. 编辑同步配置文件

1
vi /etc/rsyncd.conf

配置参数:

6. 创建同步文件夹,已有则忽略

1
mkdir /usr/blues

7. 启动同步服务

1
systemctl start lsyncd

8. 设置开机启动

1
systemctl enable lsyncd

9. 开启防火墙端口

1
firewall-cmd --permanent --add-port=873/tcp

查看端口是否开启

1
netstat -an | grep 873

参考

原文链接:https://xiaohost.com/1874.html,转载请注明出处。
0

评论0

请先