前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >实时同步文件到远程服务器:Lsyncd - Live Syncing (Mirror) Daemon

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

作者头像
PedroQin
发布2020-04-14 16:36:56
2K0
发布2020-04-14 16:36:56
举报
文章被收录于专栏:WriteSimpleDemoWriteSimpleDemo

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

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-04-08,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 WriteSimpleDemo 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Lsyncd
  • 实现效果
  • 实现
    • 安装lua 和 lua-lib
      • 安装lsyncd
        • 配置
          • 设置免密登录
            • 注册服务
            • 题外话
            • 参考链接
            相关产品与服务
            文件存储
            文件存储(Cloud File Storage,CFS)为您提供安全可靠、可扩展的共享文件存储服务。文件存储可与腾讯云服务器、容器服务、批量计算等服务搭配使用,为多个计算节点提供容量和性能可弹性扩展的高性能共享存储。腾讯云文件存储的管理界面简单、易使用,可实现对现有应用的无缝集成;按实际用量付费,为您节约成本,简化 IT 运维工作。
            领券
            问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档