linux就该这么学吧 关注:12,619贴子:3,479
  • 0回复贴,共1

大神教你:Lsyncd复制并实时同步到远程服务器

只看楼主收藏回复

Lsyncd监视本地目录树事件监视器接口(inotify或fsevents)。它聚合并将事件组合在一起几秒钟,然后生成一个(或多个)进程来同步这些更改。默认情况下,由rsync实现同步。因此,Lsyncd是一种轻量级的实时镜像解决方案,相对容易安装,不需要新的文件系统或块设备,也不会妨碍本地文件系统的性能。
Rsync+ssh是一种高级操作配置,它使用ssh来执行文件和目录直接在目标上移动,而不是在线路上重新传输移动目标。细粒度的定制可以通过配置文件实现。自定义动作configs甚至可以从头编写,从shell脚本到Lua语言编写的代码。这种方法简单,强大,灵活的配置可以被解决。
Lsyncd 2.2.2要求在所有源和目标机器上rsync >= 3.1。
系统环境:
RenwoleServer:10.28.204.65 服务端
RenwoleClient:10.28.204.66 客户端
OS:CentOS Linux release 7.4.1708 (Core) x64
rsync的安装 请参阅:《CentOS7 Configuring Rsync Server》。安装扩展依赖包$ yum install -y gcc gcc-c++ lua lua-devel cmake libxml2 libxml2-devel源代码编译安装lsyncd$ wget https://github.com/axkibe/lsyncd/archive/release-2.2.2.tar.gz$ tar xvf release-2.2.2.tar.gz$ cd lsyncd-release-2.2.2$ cmake -DCMAKE_INSTALL_PREFIX=/usr/local/lsyncd$ make && make install$ ln -s /usr/local/lsyncd/bin/lsyncd /usr/bin/lsyncd安装过程可能报错:-- Configuring incomplete, errors occurred!
安装lua-devel即可。
设置无密码SSH登录
因为这里使用rsyncssh进行同步,所以还需要配置root账号无密码ssh登录。详细配置请参阅:
《如何在Linux中设置SSH无密码登录》
配置lsyncd
以下是三种常用配置案例
1.远程同步rsyncssh模式配置方案:
$ vim /etc/lsyncd.confsettings { logfile = "/var/log/lsyncd.log", --日志路径 statusFile = "/var/log/lsyncd.status", --状态文件 pidfile = "/var/run/lsyncd.pid", --pid文件路径 statusInterval = 1, --状态文件写入最短时间 nodaemon = false, --daemon运行 maxProcesses = 1, --最大进程 maxDelays = 1, --最大延迟}sync { default.rsyncssh, --默认rsync+ssh,rsync版本需要升级3以上版本 source = "/apps/www/renwoleblog/", --源目录 delete = true, --保持完全同步 host = "root@10.28.204.66", targetdir = "/apps/www/renwoleblog/bak/", --目标目录 exclude={ ".txt" --需排除的文件 },rsync = { binary = "/usr/bin/rsync", --需先安装好rsync archive = true, --归档 compress = false, --压缩 owner = true, --属主 perms = true, --权限 whole_file = false },ssh = { port = 22 }}
2.本地目录同步配置方案:
sync { default.rsync, source = "/apps/www/renwoleblog/", target = "/apps/www/renwoleblog/bak/",}
3.远程同步rsync-daemon模式配置方案
sync { default.rsync, source = "/apps/www/renwoleblog/", target = "renwole@10.28.204.65::renwolecom", delete="true", exclude = { ".bak*" }, delay = 30, init = false, rsync = { binary = "/usr/bin/rsync", archive = true, compress = true, verbose = true, perms = true, password_file = "/etc/rsync.password", _extra = {"--bwlimit=200"} }}
重点参数说明:
-- # 注释符settings # 是全局配置sync # 定义同步参数rsync # 定义同步文件参数ssh # 定义服务器远程端口
lsyncd配置文件允许多个sync互不影响。
说明:如果是一对多,请参阅本地同步,修改目标目录即可。
创建systemctl系统单元文件
为了实现systemctl进行管理,请创建配置文件以及脚本启动文件,命令如下:
$ vim /etc/sysconfig/lsyncd添加如下内容:LSYNCD_OPTIONS="/etc/lsyncd.conf"创建启动文件:$ vim /usr/lib/systemd/system/lsyncd.service添加如下内容:[Unit]Description=Live Syncing (Mirror) DaemonAfter=network.target[Service]Type=simpleEnvironmentFile=-/etc/sysconfig/lsyncdExecStart=/usr/local/lsyncd/bin/lsyncd -nodaemon $LSYNCD_OPTIONS[Install]WantedBy=multi-user.target启动lsyncd并加入开机自启动$ systemctl start lsyncd$ systemctl enable lsyncd接下来你就可以往源服务器/apps/www/renwoleblog/内上传任意文件,完成后立刻就会同步到客户端 10.28.204.66 /apps/www/renwoleblog/bak/目录内,也可以查看服务端的lsyncd日志文件分析是否同步成功。例如:[root@RenwoleServer ~] $ cat /var/log/lsyncd.log...Fri Dec 22 01:19:22 2017 Normal: Calling rsync with filter-list of new/modified files/dirs/PCHunter_renwole.com.tar.gz/Fri Dec 22 01:19:24 2017 Normal: Finished (list): 0Fri Dec 22 01:19:32 2017 Normal: Calling rsync with filter-list of new/modified files/dirs/PCHunter_renwole.com.tar.gz/Fri Dec 22 01:19:34 2017 Normal: Finished (list): 0Fri Dec 22 01:19:34 2017 Normal: Calling rsync with filter-list of new/modified files/dirs/PCHunter_renwole.com.tar.gz/Fri Dec 22 01:19:36 2017 Normal: Finished (list): 0
日志内容显示PCHunter_renwole.com.rar文件成功同步。
另外lsyncd是基于inotify + rsync的开源同步软件,相对于其他同步软件更加安全可靠,占用资源更少,但配置略麻烦。
lsyncd 还支持当监控到某个指定事件时就执行什么样的命令,由于是通过时间延迟和累计事件命中次数来触发同步,在设计上要优于inotify,另外他的同步速度完全取决于你的网络质量。
原文来自:https://renwole.com/archives/1001
本文地址:https://www.linuxprobe.com/lsyncd-linux-server.html


1楼2018-05-30 21:16回复