• 搜索
  • 夜间模式
    ©2025  Ethan's Blog Theme by OneBlog
    搜索
    标签
    # Amule # Gargoyle # LUCI # VIM # Python # Nginx # 反代 # Ansible # Apache # LNMP
  • 首页>
  • Linux>
  • 正文
  • Lsyncd镜像同步利器

    2018年03月14日 2 阅读 0 评论 2342 字

    发现了lsyncd这个程序,比较好用,于是就寻思这用这个来替代。
    A若要实现免密码登录B,那么需要在A上面生成密钥,然后将密钥传给B,这样A就可以实现免秘钥登录B了。

    ssh-keygen -t rsa
    ssh-copy-id username@host

    编译要求:
    Lua >= 5.2
    Lsyncd depends on Lua 5.2 or greater; that is 5.2 or 5.3. For most distributions you need to install the liblua??, the liblua??-dev and the lua?? package, with ?? being the respective Lua version.

    cmake >= 2.8
    To configure Lsyncd to your system, cmake >= 2.8 is required

    rsync >= 3.1
    During runtime Lsyncd needs rsync > 3.1 installed both on source and target systems

    yum -y install lua-devel cmake rsync

    从github同步源码并编译安装

    $ git clone https://github.com/LoveyCat/lsyncd.git
    $ cd lsyncd && mkdir build && cd build && cmake .. -DCMAKE_INSTALL_PREFIX=/opt/lsyncd && make
    $ sudo make install

    创建配置文件

    $ sudo vim /etc/lsyncd.conf
    settings {
    logfile = "/var/lsyncd.log",
    statusFile = "/var/lsyncd.status",
    inotifyMode = "CloseWrite",
    }
    sync {
    default.rsync,
    source = "/home/Data",
    target = "root@172.16.0.1:/mnt/sdb1",
    rsync = {
    binary= "/usr/bin/rsync",
    archive = true,
    -- compress = true,
    }
    }

    简单说下logfile是日志文件,statusFile是运行状态文件,这个可有可无,source就是你要监视并同步的目录,target就是你要同步的目标,比如本次主机B就是我的目标,这种属于远程同步,想要了解更多,请参阅官方文档。

    保存后,直接用命令启动就可以了

    $ sudo /opt/lsyncd/bin/lsyncd /etc/lsyncd.conf

    这样可以直接加载配置文件运行lsyncd,如果不用配置文件,最简单的命令就是官方提供的部分例子了:
    Although for standard use or quick testing it can be cursorily configured by command line options. The following will keep a local source and destination directory in sync using rsync:

    lsyncd -rsync /home/USER/src /home/USER/dst

    The target can here be anything that Rsync recognizes.

    lsyncd -rsync /home/USER/src remotehost:dst

    Two (or more) targets are configured by calling -rsync twice (or several times).

    lsyncd -rsync /home/USER/src remotehost1:dst -rsync /home/USER/src remotehost2:dst

    写出了自启动脚本,更新下:

    [Unit]
    Description=lsyncd service
    After=network.target
    
    [Service]
    Type=simple
    ExecStart=/opt/lsyncd/bin/lsyncd -nodaemon /etc/lsyncd.conf
    ExecReload=
    ExecStop=/bin/kill -WINCH ${MAINPID}
    
    [Install]
    WantedBy=multi-user.target

    参考的文章还是要说下的:http://seanlook.com/2015/05/06/lsyncd-synchronize-realtime/

    至于更多的用法,请自己参阅官方文档吧:https://axkibe.github.io/lsyncd/manual/invoking/

    本文著作权归作者 [ Ethan ] 享有,未经作者书面授权,禁止转载,封面图片来源于 [ 互联网 ] ,本文仅供个人学习、研究和欣赏使用。如有异议,请联系博主及时处理。
    — END —
    Copyright©2025  All Rights Reserved.  Load:0.006 s
    Theme by OneBlog V3.6.3
    夜间模式

    开源不易,请尊重作者版权,保留基本的版权信息。