一、lsyncd介绍

二、lsyncd安装

三、lsyncd配置说明

四、lsyncd同步测试


1.lsyncd 介绍

Lsyncd 监视本地目录树事件监控器接口( inotify或fsevents ) 。通过秒级事件聚合,然后生成一个(或多个)进程来同步变化。默认情况下为结合rsync。 Lsyncd轻重量镜解决方案,安装不需要新的文件系统或块设备相对容易,并且不妨碍本地文件系统的性能。

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

wKioL1ddFEDBl0kOAABaqQSbZqU738.png-wh_50

2.lsyncd 安装

安装lua

# yum install lua lua-devel


安装lsyncd

下载:https://github.com/axkibe/lsyncd

# unzip lsyncd-master.zip 
# cd lsyncd-master
# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/lsyncd
# make
# make insatll


3.配置文件说明

settings 配置参数说明:
logfile	=	FILENAME   //记录日志的文件
pidfile	=	FILENAME   //logs进程pid文件
nodaemon	=	true	//非独立进程运行
statusFile	=	FILENAME	//运行状态文件
statusInterval	=	NUMBER	//写入状态的时间间隔,默认是10s
logfacility	=	STRING	//syslog 设备,默认user
logident	=	STRING	//syslog 标识 (tag), 默认 "lsyncd"
inotifyMode	=	STRING	//系统inotify指定监听的变化,可以是"Modify"、"CloseWrite" (默认) 或"CloseWrite or Modify".
maxProcesses	=	NUMBER	//lsyncd 将派生的最大进程数
maxDelays	=	NUMBER //累计到多少所监控的事件激活一次同步,即使后面的delay延迟时间还未到

sync 说明:
lsyncd同步的模式三种模式
default.rsync
   #使用rsync同步的方式来同步本地目录及文件
default.rsyncssh:
   #对于ssh,需要目标主机启动sshd,并通过key来验证。
default.direct:
   #default.direct直接可以用来保持两个本地目录与更好的性能比使用default.rsync。
   在启动时直接使用(就像default.rsync)rsync最初同步源目录和目标目录。操作default.direct使用/bin/cp, /bin/rm and /bin/mv 保持增量同步。
   
Exclusions:
   excludeFrom	=	FILENAME //加载排除规则文件。
   监控路径里的任何部分匹配到一个文本,都会被排除,例如/bin/foo/bar可以匹配规则foo
   如果规则以斜线/开头,则从头开始要匹配全部
   如果规则以/结尾,则要匹配监控路径的末尾
   ?匹配任何字符,但不包括/
   *匹配0或多个字符,但不包括/
   **匹配0或多个字符,可以是/
   
   exclude	=	LIST // 加载排除规则列表
   如:
     sync {
    default.rsync,
    source    = "/home/user/src/",
    targetdir = "/home/user/dst/",
    exclude = { '.bak' , '.tmp' }
   }
   
source :同步源地址,使用绝对路径
targetdir:同步的目标地址,使用绝对路径
host:目标ip地址
port:端口,默认22,可不用加ssh
如: 
   sync {
   default.rsyncssh,
   source="/srcdir",
   host="remotehost",
   excludeFrom="/etc/lsyncd.exclude",
   targetdir="/dstdir",
   rsync = {
     archive = true,
     compress = false,
     whole_file = false
   },
   ssh = {
     port = 1234
   }
 }

 

 4.配置同步

查看配置文件:

settings {
   logfile    = "/tmp/lsyncd.log",
   statusFile = "/tmp/lsyncd.status",
   statusInterval = 5,
   maxDelays = 3,
   delay = 1,
   inotifyMode = Modify,
   maxProcesses = 200
}

sync {
   default.rsyncssh,
   source="/data/ckl/",
   host="192.168.1.88",
   targetdir="/data/ckl/",
   exclude = { '.bak' , 'kk' }
}


ln -s /usr/local/lsyncd/etc/lsyncd.conf /etc/lsyncd.conf

 添加启动脚本

chmod +x /ect/init.d/lsyncd

 启动:

/etc/init.d/lsyncd start
Starting lsyncd:                                           [  OK  ]

 查看进程:

# ps -ef | grep lsync
root     17571     1  0 15:22 ?        00:00:00 /usr/local/lsyncd/bin/lsyncd -pidfile /var/run/lsyncd.pid /etc/lsyncd.conf
root     17577 16692  0 15:22 pts/1    00:00:00 grep lsync


测试同步:

192.168.1.87

192.168.1.87
# cd /data/ckl/
# mkdir list.1
vim spring
chuntian hua hui kai
ll spring 
-rw-r--r-- 1 root root 21 Jun 12 15:33 spring

查看192.168.1.88

# cd /data/ckl/
# ls -la
total 12
drwxr-xr-x 3 root root 4096 Jun 12 15:32 .
drwxr-xr-x 4 root root 4096 Jun 12 15:22 ..
drwxr-xr-x 2 root root 4096 Jun 12 15:33 list.1
过滤的目录未同步
# cd list.1/
# cat spring 
chuntian hua hui kai
# ll spring 
-rw-r--r-- 1 root root 21 Jun 12 15:33 spring

修改文件权限:

192.168.1.87

chmod 755 spring 
# ll spring 
-rwxr-xr-x 1 root root 21 Jun 12 15:33 spring

192.168.1.88

# ll spring 
-rw-r--r-- 1 root root 21 Jun 12 15:33 spring
文件未同步,因为设置的监听变化是:Modify


查看日志:

 tailf /tmp/lsyncd.log 
Sun Jun 12 15:22:05 2016 Normal: recursive startup rsync: /data/ckl/ -> 192.168.1.88:/data/ckl/ excluding
.bak
kk
Sun Jun 12 15:22:05 2016 Normal: Startup of "/data/ckl/" finished: 0
Sun Jun 12 15:32:34 2016 Normal: Rsyncing list
/list.1
Sun Jun 12 15:32:34 2016 Normal: Finished (list): 0
Sun Jun 12 15:33:04 2016 Normal: Rsyncing list
/list.1/.spring.swp
Sun Jun 12 15:33:04 2016 Normal: Deleting list
/data/ckl//list.1/.spring.swpx
Sun Jun 12 15:33:04 2016 Normal: Finished (list): 0
Sun Jun 12 15:33:04 2016 Normal: Finished (list): 0
Sun Jun 12 15:33:10 2016 Normal: Rsyncing list
/list.1/spring
Sun Jun 12 15:33:10 2016 Normal: Deleting list
/data/ckl//list.1/.spring.swp
Sun Jun 12 15:33:10 2016 Normal: Finished (list): 0
Sun Jun 12 15:33:10 2016 Normal: Finished (list): 0
Sun Jun 12 15:37:11 2016 Normal: Rsyncing list
/list.1/spring
Sun Jun 12 15:37:11 2016 Normal: Finished (list): 0