lsync目录文件实时同步工具

参考文档:https://vastxiao.github.io/article/2017/09/02/Linux/lsyncd_usage/

防止连接丢失,已保存至百度网络-郑州-XXXXX

建议首先查看  参考文档连接,写的比较全了

1、简介

lsync
官网:https://axkibe.github.io/lsyncd/

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

2、安装

# yum安装需要epel源
# 阿里epel源地址:http://mirrors.aliyun.com/help/epel
yum install lsyncd
源码安装:
yum install -y lua cmake rsync
VERSION=2.1.5
wget -O lsyncd-release-${VERSION?err}.tar.gz https://github.com/axkibe/lsyncd/archive/release-${VERSION?err}.tar.gz
tar -zxvf lsyncd-release-${VERSION?err}.tar.gz
cd lsyncd-release-${VERSION?err}
cmake .
make
sudo make install

3、yum安装文件结构

        路径              说明
/etc/lsyncd.conf    主配置文件
/etc/sysconfig/lsyncd       init环境变量和启动选项配置文件
/etc/logrotate.d/lsyncd    日志滚动配置文件
/usr/share/doc/lsyncd-*/examples/    目录下有lsyncd.conf配置例子
/etc/init.d/lsyncd         lsyncd的init启动脚本
/usr/bin/lsyncd     lsyncd命令路径
/var/run/lsyncd/    可放lsyncd.pid的目录
/var/log/lsyncd/    默认的日志目录

4、文件配置

lsyncd的一个配置文件总体分类:

settings(global):settings是全局进程设置。
sync(layer 4):sync里面是定义同步参数,可以有多个sync,各自的sync配置,互不影响。
onAction(layer 3): 用于定义sync触发的事件动作,定义后的Action应用到sync配置下。
(例如监控某个目录下的文件,根据触发的事件自己定义要执行的命令。)
AdvancedonAction(layer 2):自定义事件模型,定义后可应用到layer3和layer4。
Inlets(layer 1):自定义事件函数,一般在自定义事件模型(layer 2)中使用自定义事件函数。
注释:–开头表示注释
[root@zz ~]# cat /etc/lsync.conf 
----
-- User configuration file for lsyncd.
--
-- Simple example for default rsync, but executing moves through on the target.
--
-- For more examples, see /usr/share/doc/lsyncd*/examples/
-- 
settings {
--pidfile = "/var/run/lsyncd/lsyncd.pid"
--nodaemon = false,
inotifyMode = "CloseWrite",
maxProcesses = 8,
statusFile ="/tmp/lsyncd.status",
statusInterval = 10,
logfile = "/var/log/lsyncd/lsyncd.log"
}

-- # 监测本地目录发生变化就用touch更新一下mtime时间。
flushModifyTime = {
delay = 10,
maxProcesses = 10,
onCreate = "touch ^sourcePathname",
onModify = "touch ^sourcePathname", 
}

sync {
default.rsync,
source = "/opt/share",
target = "[email protected]::beian",
delete = "running",
-- exclude = { "logs" },
delay = 10,
init = false,
rsync = {
binary = "/usr/bin/rsync",
archive = true,
compress = true,
verbose = true,
password_file = "/etc/rsyncd.d/rsync.pwd",
_extra = { "--bwlimit=200" }
}
}
-----------------------------------------------------------------------
启动:
lsyncd -pidfile /var/run/lsyncd.pid /etc/lsyncd.conf

一个真实的例子:本地目录备份

[root@tomcat2 ~]# cat /etc/lsyncd.conf 
settings {
   --pidfile = "/var/run/lsyncd/lsyncd.pid",
   --nodaemon  = false,
   inotifyMode = "CloseWrite",
   maxProcesses = 8,
   statusFile = "/tmp/lsyncd.status",
   statusInterval = 10,
   logfile = "/var/log/lsyncd/lsyncd.log"
}

sync {
   default.rsync,
   source = "/opt/webapplication",
   target = "/back_up/webapplication",
   delete = "false",
   --exclude = { "logs" },
   delay = 5,
   init = false,
   rsync    = {
    binary = "/usr/bin/rsync",
    archive = true,
    compress = true,
    verbose = true,
        bwlimit = 2000
    }
}

sync {
   default.rsync,
   source = "/data",
   target = "/back_up/data",
   delete = "false",
   --exclude = { "logs" },
   delay = 5,
   init = false,
   rsync    = {
    binary = "/usr/bin/rsync",
    archive = true,
    compress = true,
    verbose = true,
        bwlimit = 2000
    }
}
[root@tomcat2 ~]# 

猜你喜欢

转载自www.cnblogs.com/huangyanqi/p/9753508.html