Linux System Environment
[root@nfs ~]# cat /etc/redhat-release #==》系統(tǒng)版本
CentOS release 6.7 (Final)
[root@nfs ~]# uname –r #==》內核版本
3.10.0-862.el7.x86_64
[root@nfs ~]# uname -m #==》系統(tǒng)架構
x86_64
[root@nfs ~]# echo $LANG #==》系統(tǒng)字符集
en_US.UTF-8
[root@nfs backup]# lsyncd --version #==》lsyncd版本
Version: 2.2.2
Lsyncd簡介
Lsyncd功能跟Inotify和Sersync相同,屬于實時同步工具,但是Lsyncd功能更加強大,Lysncd 實際上是lua語言封裝了 inotify 和 rsync 工具,采用了 Linux 內核(2.6.13 及以后)里的 inotify 觸發(fā)機制,然后通過rsync去差異同步,達到實時的效果。Lsyncd最強大之處在于簡單高效傳輸海量數(shù)據(jù)并且Lsyncd支持多種工作模式。
Lsyncd官網(wǎng)
https://github.com/axkibe/lsyncd
Lsyncd配置文件參數(shù)說明
標注:lsyncd.conf可以有多個sync,各自的模式,互不影響
settings #==》全局設置
{
logfile #==》定義日志文件
stausFile #==》定義狀態(tài)文件
nodaemon=true #==》默認不啟用守護模式
statusInterval #==》將lsyncd的狀態(tài)寫入上面的statusFile的間隔,默認10秒
inotifyMode #==》指定inotify監(jiān)控的事件,默認是CloseWrite,還可以是Modify或CloseWrite or Modify
maxProcesses #==》同步進程的最大個數(shù)Ls。假如同時有20個文件需要同步,而maxProcesses = 8,則最大能看到有8個rysnc進程
maxDelays #==》累計到多少所監(jiān)控的事件激活一次同步,即使后面的delay延遲時間還未到
}
sync #==》里面是定義同步參數(shù),可以繼續(xù)使用maxDelays來重寫settings的全局變量。一般第一個參數(shù)指定lsyncd以什么模式運行:rsync、rsyncssh、direct三種模式
{
default.rsync #==》本地目錄間同步,使用rsync,也可以達到使用ssh形式的遠程rsync效果,或daemon方式連接遠程rsyncd進程;
default.direct #==》本地目錄間同步,使用cp、rm等命令完成差異文件備份;
default.rsyncssh #==》同步到遠程主機目錄,rsync的ssh模式,需要使用key來認證
source #==》同步的源目錄,使用絕對路徑。
target #==》定義目的地址.對應不同的模式有幾種寫法:
(1)、/tmp/dest #==》本地目錄同步,可用于direct和rsync模式
(2)、172.29.88.223:/tmp/dest #==》同步到遠程服務器目錄,可用于rsync和rsyncssh模式,拼接的命令類似于/usr/bin/rsync -ltsd --delete --include-from=- --exclude=* SOURCE TARGET,剩下的就是rsync的內容了,比如指定username,免密碼同步
(3)、172.29.88.223::module #==》同步到遠程服務器目錄,用于rsync模式
三種模式的示例會在后面給出。
delete #==》為了保持target與souce完全同步,Lsyncd默認會delete = true來允許同步刪除。它除了false,還有startup、running值,請參考 Lsyncd 2.1.x ‖ Layer 4 Config ‖ Default Behavior。
init #==》這是一個優(yōu)化選項,當init = false,只同步進程啟動以后發(fā)生改動事件的文件,原有的目錄即使有差異也不會同步。默認是true
delay #==》累計事件,等待rsync同步延時時間,默認15秒(最大累計到1000個不可合并的事件)。也就是15s內監(jiān)控目錄下發(fā)生的改動,會累積到一次rsync同步,避免過于頻繁的同步。(可合并的意思是,15s內兩次修改了同一文件,最后只同步最新的文件)
excludeFrom #==》排除選項,后面指定排除的列表文件,如excludeFrom = "/etc/lsyncd.exclude"如果是簡單的排除,可以使用exclude = LIST。這里的排除規(guī)則寫法與原生rsync有點不同,更為簡單:監(jiān)控路徑里的任何部分匹配到一個文本,都會被排除,例如/bin/foo/bar可以匹配規(guī)則foo
(1)、如果規(guī)則以斜線/開頭,則從頭開始要匹配全部
(2)、如果規(guī)則以/結尾,則要匹配監(jiān)控路徑的末尾
(3)、?匹配任何字符,但不包括/
(4)、*匹配0或多個字符,但不包括/
(5)、**匹配0或多個字符,可以是/
}
Rsync
{
(1)、bwlimit 限速,單位kb/s,與rsync相同(這么重要的選項在文檔里竟然沒有標出)
(2)、compress 壓縮傳輸默認為true。在帶寬與cpu負載之間權衡,本地目錄同步可以考慮把它設為false
(3)、perms 默認保留文件權限。
}
一、Lsyncd配置
1、配置阿里云yum源
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum makecache
2、yum安裝Lsyncd
[root@nfs ~]# yum -y install lsyncd
[root@nfs ~]# rpm -qa lsyncd
lsyncd-2.2.2-1.el7.x86_64
[root@nfs ~]# rpm -qc lsyncd
/etc/logrotate.d/lsyncd
/etc/lsyncd.conf #==》Lsyncd主配置文件
/etc/sysconfig/lsyncd
3、寫入Lsyncd配置文件/etc/lsyncd.conf
[root@nfs ~]# vim /etc/lsyncd.conf
settings {
logfile = "/var/log/lsyncd/lsyncd.log",
statusFile = "/var/log/lsyncd/lsyncd.status",
inotifyMode = "CloseWrite",
maxProcesses = 8,
}
sync {
default.rsync,
source = "/backup",
target = "rsync_backup@10.0.0.60::backup",
delete = true,
exclude = { ".*" },
delay = 1,
rsync = {
binary = "/usr/bin/rsync",
archive = true,
compress = true,
verbose = true,
password_file = "/etc/rsync.passwd",
_extra = {"--bwlimit=200"}
}
}
4、啟動Lsyncd并設置開機自啟動
[root@nfs backup]# systemctl start lsyncd
[root@nfs backup]# systemctl enable lsyncd
[root@nfs backup]# systemctl status lsyncds
5、測試
#==》Lsycnd服務端創(chuàng)建
[root@nfs ~]# touch /backup/test1{1..5}.txt
[root@nfs ~]# ls -l /backup/
total 0
-rw-r--r-- 1 root root 0 Aug 30 17:18 test11.txt
-rw-r--r-- 1 root root 0 Aug 30 17:18 test12.txt
-rw-r--r-- 1 root root 0 Aug 30 17:18 test13.txt
-rw-r--r-- 1 root root 0 Aug 30 17:18 test14.txt
-rw-r--r-- 1 root root 0 Aug 30 17:18 test15.txt
#==》Rsync守護進程主機查看
[root@rsync ~]# ls -l /backup/
total 0
-rw-r--r--. 1 www www 0 Aug 30 2019 test11.txt
-rw-r--r--. 1 www www 0 Aug 30 2019 test12.txt
-rw-r--r--. 1 www www 0 Aug 30 2019 test13.txt
-rw-r--r--. 1 www www 0 Aug 30 2019 test14.txt
-rw-r--r--. 1 www www 0 Aug 30 2019 test15.txt