实时同步文件到远程服务器:Lsyncd - Live Syncing (Mirror) Daemon

2020-04-14 16:36:56 浏览数 (1)

最近由于业务需求,另外架设了台服务器,多个服务器共同承担生产环境的测试。多个服务器服务于同一生产环境就需要这多个服务器之间保持生产测试程式的同步,甚至各种生产记录如测试log等的同步。

在这种情境下,简单的scp明显无法满足需求,需要更加效率的自动同步方式。

Lsyncd

Lsyncd uses a filesystem event interface (inotify or fsevents) to watch for changes to local files and directories. Lsyncd collates these events for several seconds and then spawns one or more processes to synchronize the changes to a remote filesystem. The default synchronization method is rsync. Thus, Lsyncd is a light-weight live mirror solution. Lsyncd is comparatively easy to install and does not require new filesystems or block devices. Lysncd does not hamper local filesystem performance. lsyncd 可实现本地和远程目录同步,本文从实际需求出发,主要实现远程目录同步。

实现效果

  • 一主多从:一个服务器为主服务器,完成更新程式,同步变更等操作。在主服务器开启lsyncd服务,检测到监控文件变更就自动同步到所有从服务器。此为本文实现内容。
  • 多主多从:各个服务器都可能更新程式甚至log,在其中一个服务器监控的文件夹发生变动时,自动同步到其他服务器。本人只实验过二主三从,理论上多主多从也可实现。

实现

安装lua 和 lua-lib
代码语言:javascript复制
[root@centos76 ~]# wget http://www.lua.org/ftp/lua-5.3.4.tar.gz
[root@centos76 ~]# cd lua-5.3.4
[root@centos76 lua-5.3.4]# yum install -y gcc gcc-c   readline-devel
[root@centos76 lua-5.3.4]# make linux
[root@centos76 lua-5.3.4]# make install
[root@centos76 lua-5.3.4]# wget http://mirror.centos.org/centos/8/BaseOS/x86_64/os/Packages/lua-libs-5.3.4-11.el8.x86_64.rpm
[root@centos76 lua-5.3.4]# rpm -ivh lua-libs-5.3.4-11.el8.x86_64.rpm 
安装lsyncd
代码语言:javascript复制
[root@centos76 ~]# yum install cmake
[root@centos76 ~]# git clone https://github.com/axkibe/lsyncd.git
[root@centos76 ~]# cd lsyncd
[root@centos76 lsyncd]# ./distclean.sh
[root@localhost lsyncd]# cmake .
[root@localhost lsyncd]# make 
[root@localhost lsyncd]# make install
配置

新建配置文件如下,

代码语言:javascript复制
[root@labserver .ssh]# cat /etc/lsyncd.conf
settings {
    logfile = "/var/log/lsyncd.log",        --日志路径
    statusFile = "/var/log/lsyncd.status",  --状态文件
    pidfile = "/var/run/lsyncd.pid",        --pid文件路径
    statusInterval = 1,                     --状态文件写入最短时间
    nodaemon = false,                       --daemon运行
    maxProcesses = 1,                       --最大进程
    maxDelays = 10,                         --最大延迟
    insist = 1,                             --有错误不退出
}
sync {
    default.rsyncssh,
    source = "/tmp/test/",                  --源目录
    delete = true,                          --保持完全同步
    host = "root@172.22.27.181",
    targetdir = "/tmp/test/",               --目标目录
    exclude={
             "*.sw*"                        --不同步文件
    },
    rsync = {
        binary = "/usr/bin/rsync",          --使用rsync同步
        archive = true,                     --归档
        compress = false,                   --压缩
        owner = true,                       --属主
        perms = true,                       --权限
        links = true,                       --copy link
        whole_file = false,
        },
    ssh = {
        port = 22
        }
}
sync {                                      --另一个同步对象
    default.rsyncssh,
    source = "/tmp/test/",
    delete = true,
    host = "root@172.22.27.75",
    targetdir = "/tmp/test/",
    exclude={
             "*.sw*"
    },
    rsync = {
        binary = "/usr/bin/rsync",
        archive = true,
        compress = false,
        owner = true,
        perms = true,
        links = true,
        whole_file = false,
        },
    ssh = {
        port = 22
        }
}

重点参数说明:

参数

含义

--

注释符

settings

是全局配置

sync

定义同步参数

rsync

定义同步文件参数

ssh

定义服务器远程端口

注:lsyncd配置文件允许多个sync互不影响。

设置免密登录

该操作目的是为了让 lsyncd 服务在使用 ssh 做远程同步时省去输入密码的步骤,方便将 lsyncd 注册为服务。通过如下操作可实现本机ssh登录172.22.27.60免密码。

代码语言:javascript复制
[root@labserver .ssh]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:8HOIswkaHD3InjBvJz/P8zUaxk150IZpIXqlPa6B3lM root@labserver
The key's randomart image is:
 ---[RSA 2048]---- 
|        . o      |
| . o   . = =     |
|o   o o o B o    |
| * o . *   =     |
|  O o   S E .    |
| . * o * O .     |
|  . o   B        |
|      .. = .     |
|      ooo        |
 ----[SHA256]----- 
[root@labserver .ssh]# ls
id_rsa  id_rsa.pub  known_hosts  known_hosts.old
[root@labserver .ssh]# ssh-copy-id root@172.22.27.60
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@172.22.27.60's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@172.22.27.60'"
and check to make sure that only the key(s) you wanted were added.

[root@labserver .ssh]#
注册服务

新建文件/usr/lib/systemd/system/lsyncd.service

代码语言:javascript复制
[root@labserver .ssh]# cat /usr/lib/systemd/system/lsyncd.service
[Unit]
Description=Live Syncing (Mirror) Daemon
After=network.target

[Service]
Type=simple
ExecStart=/usr/local/bin/lsyncd -nodaemon /etc/lsyncd.conf

[Install]
WantedBy=multi-user.target

开启服务并设置开机自启

代码语言:javascript复制
[root@labserver .ssh]# systemctl start lsyncd.service
[root@labserver .ssh]# systemctl enable lsyncd.service 
Created symlink from /etc/systemd/system/multi-user.target.wants/lsyncd.service to /usr/lib/systemd/system/lsyncd.service.

题外话

lsyncd是基于inotify rsync的开源同步软件,相对于其他同步软件更加安全可靠,占用资源更少,但配置略麻烦。 另外lsyncd 还支持当监控到某个指定事件时就执行什么样的命令,由于是通过时间延迟和累计事件命中次数来触发同步,在设计上要优于inotify,另外他的同步速度完全取决于你的网络质量。

参考链接

https://axkibe.github.io/lsyncd/

0 人点赞