1. 程式人生 > 實用技巧 >Lsyncd實現檔案實時同步

Lsyncd實現檔案實時同步

Lsyncd實現檔案實時同步

目錄

一、概述

Lysncd 實際上是lua語言封裝了 inotify 和 rsync 工具,採用了 Linux 核心(2.6.13 及以後)裡的 inotify 觸發機制,然後通過rsync去差異同步,達到實時的效果。我認為它最令人稱道的特性是,完美解決了 inotify + rsync海量檔案同步帶來的檔案頻繁傳送檔案列表的問題 —— 通過時間延遲或累計觸發事件次數實現。

實驗環境:CentOS7
傳送端:192.168.1.100
接收端:192.168.1.101
實驗場景:傳送端實時同步檔案到接收端所在伺服器指定目錄。

傳送端需要安裝lsyncd,而接收端不需要安裝。

lsyncd是基於rsync,故傳送端安裝lsyncd後會也自動依賴安裝rsync。
本文實驗兩種模式:Lsyncd+SSH, Lsyncd+Rsync。
兩種模式區別是:Lsyncd+SSH需要配置ssh免密登入;而Lsyncd+Rsync 接收端需要配置啟動Rsync服務。

Rsync安裝使用可參考我的上一篇博文:https://www.cnblogs.com/huligong1234/p/13513395.html

lsyncd官方文件 https://axkibe.github.io/lsyncd/

二、Lsyncd+SSH配置實現

2.1.傳送端Lsyncd安裝

yum -y install epel-release
yum -y install lsyncd
lsyncd --version #檢視lsyncd版本

2.2.傳送端設定SSH免密登入

如果想要將傳送端的資料同步到接收端,傳送端必須擁有免密碼登入接收端的許可權,可以設定金鑰登入來完成。
只需要兩條命令即可,下面的命令在傳送端伺服器(192.168.1.100)執行:

#傳送端生成金鑰檔案
ssh-keygen -t rsa

#將公鑰拷貝到接收端
ssh-copy-id [email protected]

#測試免密登入
ssh '[email protected]'

如果伺服器不是使用的22作為SSH埠,需要-p引數指定埠號

2.3.接收端安裝rsync

#僅安裝好即可,無需額外配置
yum -y install rsync

2.4.傳送端配置/etc/lsyncd.conf

注意:lsyncd.conf是lua語法格式檔案,"--"為行註釋語法

----
-- 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/
-- 
--sync{default.rsyncssh, source="/var/www/html", host="localhost", targetdir="/tmp/htmlcopy/"}

settings {
   logfile = "/var/log/lsyncd/lsyncd.log",
   statusFile = "/var/log/lsyncd/lsyncd.status",
   insist = true,
   statusInterval = 10
}
sync {
   default.rsyncssh,
   source="/data/uploads",
   host="192.168.1.100",
   targetdir="/data/uploads",
   
   rsync = {
     archive = true,
     compress = false,
     whole_file = false
   },
   ssh = {
     port = 22
   }
}

2.4.傳送端啟動lsyncd

systemctl start lsyncd
systemctl enabled lsyncd #配置開機啟動

測試略

三、Lsyncd+Rsync配置實現

3.1.接收端安裝配置/etc/rsyncd.conf

#安裝 yum -y install rsync
#配置/etc/rsyncd.conf

uid = root
gid = root
use chroot = no
max connections = 10 
timeout = 900
ignore nonreadable = yes
log file=/var/log/rsyncd.log
pid file=/var/run/rsyncd.pid
lock file=/var/run/rsyncd.lock
dont compress=*.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2
[uploadsbackup]
comment=uploads file remote backup
path=/data/uploads
ignore errors=yes
hosts allow=192.168.1.100
auth users=rsync_backup
secrets file=/etc/rsyncd.passwd
list=false
read only=no

3.2.接收端配置rsyncd密碼

echo "rsync_backup:123456" >/etc/rsyncd.passwd
chmod 600 /etc/rsyncd.passwd

#啟動rsyncd
systemctl start rsyncd

3.3.傳送端Lsyncd安裝

yum -y install epel-release
yum -y install lsyncd
lsyncd --version #檢視lsyncd版本

3.4.傳送端配置rsync客戶端密碼

echo "123456" >/etc/rsync_client.passwd
chmod 600 /etc/rsync_client.passwd

3.5.傳送端修改配置/etc/lsyncd.conf

settings {
	logfile = "/var/log/lsyncd/lsyncd.log",
	statusFile = "/var/log/lsyncd/lsyncd.status",
	insist = true,
	statusInterval = 10
}

sync {
	default.rsync,
	source="/data/uploads",
	target="[email protected]::uploadsbackup",
	rsync = {
			binary = "/usr/bin/rsync",
			archive = true,
			compress = true,
			verbose   = true,
			--delete =  true,
			 _extra = {"--password-file=/etc/rsync_client.passwd"}
	}
}

3.6.接收端啟動lsyncd

systemctl restart lsyncd #重啟

測試略

四、更多資料參考