原文: http://axkibe.github.io/lsyncd/
描述
Lsyncd監視本地目錄樹事件監視器接口(inotify或fsevents)。它聚集並組合事件幾秒鍾,然后生成一個(或多個)進程(es)來同步這些更改。默認情況下,這是rsync。因此Lsyncd是一個輕量級的實時鏡像解決方案,相對而言易於安裝,不需要新的文件系統或塊設備,也不會影響本地文件系統的性能。
Rsync + ssh是一種高級操作配置,它使用SSH來操作文件和目錄,直接在目標上移動,而不是通過線路重新傳輸移動目標。
細粒度的定制可以通過配置文件來實現。自定義操作配置甚至可以從層級層次(從shell腳本到用Lua語言編寫的代碼)從頭開始編寫。這樣可以實現簡單,強大和靈活的配置。
Lsyncd 2.2.1在所有源計算機和目標計算機上都需要rsync> = 3.1。
許可證:GPLv2或任何后續的GPL版本。
何時使用
Lsyncd旨在將本地目錄樹與對遠程鏡像的預期更改的低配置文件進行同步。Lsyncd特別適用於將數據從安全區域同步到不太安全的區域。
其他同步工具
DRBD在塊設備級別上運行。這對同步負載較重的系統很有用。另一方面,Lsyncd不要求您更改塊設備和/或掛載點,允許您更改傳輸文件的uid / gid,通過rsync的單向性質分隔接收器。如果您要同步數據庫,DRBD可能是更好的選擇。
GlusterFS和BindFS使用FUSE-Filesystem來插入內核/用戶空間文件系統事件。
鏡像是一種異步同步工具,它使用類似Lsyncd的inotify通知。主要區別在於:它是專門為master-master開發的,因此在兩個系統上運行守護進程,使用自己的傳輸層而不是rsync,而使用Java代替Lsyncd的C核和Lua腳本。
Lsyncd使用示例
lsyncd -rsync /home remotehost.org::share/
這將監視並rsyncs本地目錄/ home與所有子目錄,並使用rsync-share'share'將它們傳輸到'remotehost'。
lsyncd -rsyncssh /home remotehost.org backup-home/
這也將rsync / watch'/ home',但它使用ssh連接在遠程主機上進行本地移動,而不是通過網絡重新傳輸移動的文件。
放棄
除了許可證中通常的免責聲明之外,我們還要特別強調,作者或任何與作者相關的組織都不能並且將對因Lsyncd可能出現的故障而導致的數據丟失負責。
編譯
要求
Lua> = 5.2
Lsyncd依賴於Lua 5.2或更高版本; 即5.2或5.3。對於大多數發行版,您需要安裝liblua ??,liblua ?? - dev和lua ?? 包,用?? 作為相應的Lua版本。
cmake> = 2.8
要將Lsyncd配置到您的系統,需要cmake> = 2.8
rsync> = 3.1
在運行期間,Lsyncd需要在源系統和目標系統上同時安裝rsync> 3.1。
編譯
滿足這些要求Lsyncd應該是一個直接的過程。解壓下載的tar.gz文件並運行:
cmake . make sudo make install
調用
作為大多數Unix工具,Lsyncd將在用-help調用時打印其命令行選項的摘要。
lsyncd --help lsyncd -help
Lsyncd中的兩個連字符是冗余的。它沒有短的一個字母選項,並且一個連字符總是與指定兩個相同。
也像大多數Unix工具一樣,--version
或者-version
讓Lsyncd打印它的版本號。
lsyncd -version
Lsyncd 2.1被設計為主要通過配置文件進行配置(見下文)。配置文件因此可以是唯一的命令行選項。
lsyncd CONFIGFILE
盡管對於標准使用或快速測試,它可以通過命令行選項進行光標配置。以下內容將使用rsync保持本地源和目標目錄同步:
lsyncd -rsync /home/USER/src /home/USER/dst
目標可以是Rsync可以識別的任何東西。
lsyncd -rsync /home/USER/src remotehost:dst
通過調用兩次(或多次)-rsync配置兩個(或更多)目標。
lsyncd -rsync /home/USER/src remotehost1:dst -rsync /home/USER/src remotehost2:dst
Rsync同步的一個缺點是,通常目錄和文件移動會導致移動源的刪除和導線移動目標的重新傳輸。但是,Lsyncd 2可以使用ssh命令在目標上本地移動目錄和文件。要-rsyncssh
在本地源目錄,遠程主機和目標目錄之后使用此用法。REMOTEHOST可以包含一個用戶me@remotehost.com
。
lsyncd -rsyncssh /home/USER/src REMOTEHOST TARGETDIR
測試Lsyncd配置時-nodaemon
是一個非常方便的標志。使用此選項,Lsyncd不會分離,並且不會成為守護進程。所有日志消息都是在控制台上打印的配置日志記錄工具(stdout和stderr)之外的。
lsyncd -nodaemon CONFIGFILE
使用-nodaemon運行時的行為有所不同。Lsyncd不會/
像它在成為守護程序時那樣將其工作目錄更改為。因此,相對目標./target
會起作用,-nodaemon
但必須指定絕對路徑才能在守護進程模式下工作。源目錄也將由Lsyncd轉換為絕對路徑。源代碼不解析為絕對路徑的原因是因為Lsyncd本身並不關心目標說明符的格式,該目標說明符也可以是遠程主機,rsyncd模塊等。它不透明地傳遞給rsync。它關心觀察到的目錄。
所有日志消息按類別排序。默認情況下,Lsyncd缺少日志消息。通過指定,您可以將Lsyncd轉換為motormouth -log all
。
lsyncd -log all CONFIGFILE
這可能很容易變得太多。一個特別有用的類別是“Exec”,它將記錄Lsyncd產生的所有進程的命令行。
lsyncd -log Exec CONFIGFILE
當默認初始啟動同步失敗時,Lsyncd將以錯誤消息終止。它是這樣設計的,所以配置故障可見地報告給可能的初始用戶。但是,在生產過程中可能會完成遠程目標,但是您希望Lsyncd始終啟動並不斷嘗試同步到遠程目標,直到它啟動。
lsyncd -insist -rsync /home/USER/src remotehost:dst
在生產模式下,建議堅持。它也可以在配置文件中的settings {}命令中指定。
配置文件
Lsyncd配置文件是有效的Lua語法。它的設計簡單而有力。雖然豐富的配置和簡單性本身並不是對立的,但一些折衷是不可避免的。為了盡可能實現這兩個目標,Lsyncd配置可以在不同的層完成。較低的層次增加了適應性,而界面變得更有吸引力。
設置
對於所有圖層的腳本,settings
可以使用該調用來更改守護程序范圍的配置。
例如,以下代碼將指示Lsyncd登錄/tmp/lsyncd.log
,定期/tmp/lsyncd.status
使用其狀態更新該文件,並且不會將其作為守護程序分離。
settings { logfile = "/tmp/lsyncd.log", statusFile = "/tmp/lsyncd.status", nodaemon = true, }
警告 如果你從2.0.x升級,請注意它settings
是一個變量的函數,所以你必須刪除settings
和之間的等號'=' {
。
有效的設置鍵是:
logfile | = | FILENAME | logs into this file |
pidfile | = | FILENAME | logs PID into this file |
nodaemon | = | BOOL | does not detach |
statusFile | = | FILENAME | periodically writes a status report to this file |
statusInterval | = | NUMBER | writes the status file at shortest after this number of seconds has passed (default: 10) |
logfacility | = | STRING | syslog facility, default "user" |
logident | = | STRING | syslog identification (tag), default "lsyncd" |
insist | = | BOOL | keep running at startup although one or more targets failed due to not being reachable. |
inotifyMode | = | STRING | Specifies on inotify systems what kind of changes to listen to. Can be "Modify", "CloseWrite" (default) or "CloseWrite or Modify". |
maxProcesses | = | NUMBER | Lysncd will not spawn more than these number of processes. This adds across all sync{}s. |
此外,還可以配置一些參數,這些參數由所有Syncs繼承(請參閱第3層)
maxDelays | = | 數 | 當這個延遲事件的數量排隊時,動作將被產生,甚至低於延遲計時器。 |
配置第4層:默認配置
您可以簡單地從三個默認實現中選擇:rsync,rsyncssh和direct。
要使用默認的rsync行為來同步本地目錄,只需將其添加到配置文件即可:
sync { default.rsync, source = "DIRNAME", target = "DIRNAME" }
論點的順序並不重要。如果target是本地目錄,請注意它是絕對路徑名。您可以通過這種方式添加多個同步。源目錄可能相同或不同,沒有問題。source
是每個同步必須給出的通用參數。所有其他sync
參數可能因所選行為而異。或者,您可以覆蓋默認或設置值maxDelays
或maxProcesses
每次同步。
也可以通過將默認init
功能設置為false來跳過初始rsync過程:
sync { default.rsync, source = "DIRNAME", target = "DIRNAME", init = false }
這是一個危險的優化; 因此,請僅在確定源和目標在Lsyncd啟動時同步的情況下使用它。
您可以選擇的默認行為如下:
default.rsync
默認的rsync配置會將事件聚合到delay
秒或1000個獨立的不可折疊事件,這些事件首先發生。然后它會產生一個Rsync,並帶有所有已更改文件的過濾器。過濾器列表通過管道傳輸到Rsync。從Lsyncd到Rsync的調用將如下所示:
/usr/bin/rsync -ltsd --delete --include-from=- --exclude=* SOURCE TARGET
您可以更改調用Rsync的選項以及與rsync
參數調用的Rsync二進制文件。
例:
sync { default.rsync, source = "/home/user/src/", target = "foohost.com:~/trg/", delay = 15, rsync = { binary = "/usr/local/bin/rsync", archive = true, compress = true } }
以下是rsync
參數選項的表格。請查看Rsync文檔以獲得深入的解釋。
parameter | = | TYPE | default value | comment |
acls | = | BOOL | false | |
append | = | BOOL | false | (Lsyncd >= 2.2.0) |
append-verify | = | BOOL | false | (Lsyncd >= 2.2.0) |
archive | = | BOOL | false | |
backup | = | BOOL | false | (Lsyncd >= 2.2.0) |
backup_dir | = | DIR | false | (Lsyncd >= 2.2.0) |
binary | = | FILENAME | "/usr/bin/rsync" | Lsyncd calls this binary as rsync |
checksum | = | BOOL | false | |
chmod | = | STRING | (Lsyncd >= 2.2.0) | |
chown | = | USER:GROUP | (Lsyncd >= 2.2.0) | |
compress | = | BOOL | false | |
copy_dirlinks | = | BOOL | false | (Lsyncd >= 2.2.0) |
copy_links | = | BOOL | false | |
cvs_exclude | = | BOOL | ||
dry_run | = | BOOL | false | |
exclude | = | PATTERN | TABLE of PATTERNs also allowed | |
excludeFrom | = | FILENAME | ||
executability | = | BOOL | false | |
existing | = | BOOL | false | (Lsyncd >= 2.2.0) |
group | = | BOOL | false | |
groupmap | = | STRING | (Lsyncd >= 2.2.0) | |
hard_links | = | BOOL | false | |
ignore_times | = | BOOL | false | |
inplace | = | BOOL | false | (Lsyncd >= 2.1.6) |
ipv4 | = | BOOL | false | |
ipv6 | = | BOOL | false | |
links | = | BOOL | true | |
one_file_system | = | BOOL | false | |
owner | = | BOOL | false | |
password_file | = | FILENAME | (Lsyncd >= 2.1.2) | |
perms | = | BOOL | false | |
protect_args | = | BOOL | true | |
prune_empty_dirs | = | BOOL | false | |
quiet | = | BOOL | false | |
rsh | = | COMMAND | ||
rsync_path | = | PATH | (path to rsync on remote host) | |
sparse | = | BOOL | false | |
suffix | = | SUFFIX | (Lsyncd >= 2.2.0) | |
temp_dir | = | DIR | ||
times | = | BOOL | true | |
update | = | BOOL | false | |
usermap | = | STRING | (Lsyncd >= 2.2.0) | |
verbose | = | BOOL | false | |
whole_file | = | BOOL | false | |
xattrs | = | BOOL | false | |
_extra | = | TABLE of STRINGS. | 如果絕對需要,可以將其他參數指定為STRINGS表(例如:{“--omit-dir-times”,“--omit-link-times”})。請注意,下划線將此解釋為解決方法。如果您需要上述選項未涵蓋的內容,請通過項目網站上的功能請求申請。最值得注意的是,不要添加-r遞歸或-a這意味着遞歸,因為Lsyncd會自己處理它。另外,不要為相對添加-R,這會破壞Lsyncd < - > Rsync通信。 |
default.rsyncssh
此配置不同於標准rsync配置,因為它使用ssh命令在目標主機本地移動文件或目錄,而不是再次刪除和傳輸。這種配置產生了類似於default.rsync的Rsync進程,但是會產生/usr/bin/ssh HOST mv ORIGIN DESTINATION
命令。
與default.rsync不同,它不需要統一的target
參數,但需要host
並targetdir
分開。
Rsync的選項可以通過rsync
上面所描述的default.rsync中的參數進行更改。
除此之外,可以通過ssh
參數配置ssh 。
binary | = | FILENAME | Lsyncd calls this binary as ssh (default: /usr/bin/ssh) |
identityFile | = | FILE | Uses this file to identify for public key authentication. |
options | = | TABLE | A table of addition extended options to pass to ssh's -o option. |
port | = | PORT | Adds --port=PORT to the ssh call. |
_extra | = | STRING TABLE | Similar to rsync._extra this can be used as quick workaround if absolutely needed. |
例:
settings { logfile = "/var/log/lsyncd.log", statusFile = "/var/log/lsyncd-status.log", statusInterval = 20 } sync { default.rsyncssh, source="/srcdir", host="remotehost", excludeFrom="/etc/lsyncd.exclude", targetdir="/dstdir", rsync = { archive = true, compress = false, whole_file = false }, ssh = { port = 1234 } }
請注意rsync
參數集和ssh
參數集之間的逗號。
警告 如果你從2.0.x升級,請注意它settings
是一個變量的函數,所以你必須刪除settings
和之間的等號'=' {
。
Lsyncd將調用xargs
遠程主機來處理單個連接中的多個任務。Xargs選項可以由xargs參數指定。
binary | = | FILENAME | Lsyncd calls this binary as xargs on the remote host (default: /usr/bin/xargs) |
delimiter | = | DELIMITER | delimiting character to separate filenames. By default the 0 character is used. Very old holds may need newline instead. |
_extra | = | STRING TABLE | By default { '-0', 'rm -rf' }. Remove the -0 if you chose newline delimiter instead. Otherwise leave it as is. |
sync {例:
default.rsyncssh, source = "/home/user/src/", host = "foohost.com", targetdir = "~/trg/", }
default.direct
Default.direct可以用來保持兩個本地目錄同步,比使用default.rsync更好的性能。Default.direct在啟動時使用(就像default.rsync一樣)rsync來初始化目標目錄與源目錄的同步。但是,在正常操作期間,default.direct使用/ bin / cp,/ bin / rm和/ bin / mv來保持同步。所有參數就像default.rsync一樣。
例:
sync { default.direct, source = "/home/user/src/", target = "/home/user/trg/" }
排除
可以指定兩個附加參數來同步{}:
excludeFrom | = | FILENAME | loads exclusion rules from this file, on rule per line |
exclude | = | LIST | loads exclusion rules from this list of strings |
排除規則在rsync的排除模式之后建模,但稍微簡單一些。Lsyncd支持這些功能:
- 通常,如果某個事件的路徑名的任何部分(參見第3層以下)與文本匹配,則將其排除。例如文件“/ bin / foo / bar”匹配規則“foo”。
- 如果規則以斜杠開始,則只會在路徑名的開頭匹配
- 如果規則以斜線結尾,則只會在路徑名的末尾匹配
- ?匹配不是斜杠的任何字符。
*
匹配零個或多個不是斜線的字符**
匹配零個或多個字符,這可以是斜杠。
例:
sync { default.rsync, source = "/home/user/src/", targetdir = "/home/user/dst/", exclude = { '*.bak' , '*.tmp' } }
缺失
默認情況下,Lsyncd將刪除目標上不存在的文件,因為這是保持目標與源同步的基本部分。但是,由於各種原因,許多用戶請求例外,因此所有默認實現都delete
作為附加參數。
有效值為delete
:
delete | = | true | Default. Lsyncd will delete on the target whatever is not in the source. At startup and what's being deleted during normal operation. |
delete | = | false | Lsyncd will not delete any files on the target. Not on startup nor on normal operation. (Overwrites are possible though) |
delete | = | 'startup' | Lsyncd will delete files on the target when it starts up but not on normal operation. |
delete | = | 'running' | Lsyncd will not delete files on the target when it starts up but will delete those that are removed during normal operation. |
配置第3層:簡單的onAction
簡單的行動
在這一層中,可以創建自定義配置。這個例子將使用bash命令來保持本地目錄同步。
bash = { delay = 5, maxProcesses = 3, onCreate = "cp -r ^sourcePathname ^targetPathname", onModify = "cp -r ^sourcePathname ^targetPathname", onDelete = "rm -rf ^targetPathname", onMove = "mv ^o.targetPathname ^d.targetPathname", onStartup = '[[ if [ "$(ls -A ^source)" ]; then cp -r ^source* ^target; fi]]', }
這個例子一步一步解釋。從技術上講,任何Lsyncd配置都是一個填充了一組鍵的Lua表。因此,它首先創建一個名為變量的變量bash
並為其分配一個= {...}的表格。
bash = { ... }
現在表格中充滿了條目。每個條目都有一個等號的左鍵和它的右值。如果沒有指定延遲,這意味着對Lsyncd的即時操作。這個例子想要將更改匯總5秒,因此下一個條目是:
delay = 5,
自從標記條目結束以來,需要逗號。
操作
操作由6個鍵指定:
onAttrib | called when only attributes changed |
onCreate | called on a new file or directory |
onModify | called when a file has changed |
onDelete | called when a file or directory has been deleted |
onMove | called when a file or directory has been moved within the observed directory tree |
onStartup | called on the start of Lsyncd |
要采取的動作被指定為Lua字符串。因此,動作可以用Lua允許的任何內容進行分隔,這些是onStartup
上面示例中使用的'TEXT','TEXT'或'[[TEXT]] 。當沒有onMove
或移動進出觀察的目錄樹時,它被分成onDelete
移動起點和onCreate
移動目的地的一個。那就是如果任一個在觀察目錄樹內。onStartup
將始終阻止此Sync的所有其他操作直至完成。
任何以“/”開始的動作都會指示Lsyncd在開始時直接調用二進制文件,而不是產生額外的shell。例如
onCreate = "/usr/bin/zip /usr/var/all.zip ^sourcePath" onModify = "/usr/bin/zip /usr/var/all.zip ^sourcePath"
將使用絕對路徑名將所有新創建和修改的文件添加到/usr/var/all.zip。任何不以“/”開始的操作都會導致Lsyncd產生一個shell來執行命令。
變量
變量參數用插入符號^指定。它已經被選擇超過$或其他符號,與標准的shell約定沖突較少。
請注意,變量將始終用雙引號隱式引用,因此如果您希望它們成為另一個雙引號字符串的一部分,則必須更深入一層,例如
onCreate = '[[ su user -c "/usr/bin/zip /usr/var/all.zip ^o.sourcePath " ]],
將擴大到su user -c "/usr/bin/zip /usr/var/all.zip "source""
哪個是不正確的,並將打破。你必須將上面的敘述更深一層地重寫
onCreate = function(event) spawnShell('[[ su user -c "/usr/bin/zip /usr/var/all.zip \"$1\"" ]], event.sourcePath) end
所有可能的變量
^source | the absolute path of the observed source directory |
^target | the "target" attribute of the config |
^path | the relative path of the file or directory to the observed directory; directories have a slash at the end. |
^pathname | the relative path of the file or directory to the observed directory; directories have no slash at the end. |
^sourcePath | the absolute path of the observed source directory and the relative path of the file or directory; this equals the absolute local path of the file or directory. Directories have a slash at the end. |
^sourcePathname | same as ^sourcePath, but directories have no slash at the end. |
^targetPath | The "target" attributed of the config appended by the relative path of the file or directory. Directories have a slash at the end. |
^targetPathname | same as ^targetPath, but directories have no slash at the end. |
從上面的示例中,它將移動目標目錄中的文件或目錄。對於onMoves
一個。和或者d。可以預置為path,pathname,sourcePath sourcePathname,targetPath和targetPathname以指定移動起始位置或目標位置。沒有任何變量指的是移動的起源。
onMove = "mv ^o.targetPathname ^d.targetPathname",
執行控制(退出代碼)
關於示例啟動的幾句話。它看起來有點復雜,但它只是一些bash腳本,沒有什么特別的Lsyncd。它只是將源的遞歸副本做到目標,但首先測試源文件中是否有任何東西。否則,該命令會返回一個非零的錯誤代碼。
onStartup = '[[if [ "$(ls -A ^source)" ]; then cp -r ^source* ^target; fi]],
默認情況下,Lsyncd忽略除了onStartup之外的所有退出代碼,它必須返回0才能繼續。您可以通過添加一個exitcodes
表來改變這種行為。
exitcodes = {[0] = "ok", [1] = "again", [2] = "die"}
這些鍵為退出代碼指定了所需操作的字符串。
again | respawns the action after seconds, or 1 second if delay is immediate |
die | lets Lsyncd terminate. |
所有其他值讓Lsyncd正常繼續。
配置第2層:高級onAction
雖然第4層和第3層感覺像正常的配置文件,但第2層和第1層進入編碼領域。因此,假設您在使用第2層或第1層時有一些編碼知識。
而不是像第3層那樣將動作指定為字符串Lua函數可以用於在Lsyncd中執行一些小腳本。
這個例子會將任何帶有在目錄中創建的“.ps”后綴的文件轉換為PDF。
autopdf = { onCreate = function(event) log("Normal", "got an onCreate Event") if string.ends(event.pathname, ".ps") then spawn(event, "/usr/bin/ps2pdf", event.sourcePath) end end }
該函數可以使用任何有效的Lua代碼。
Lsyncd提供了一組用於用戶腳本的函數。
日志(類別,...)
將一條消息記錄到文件/ stdout / syslog中。第一個參數是日志記錄類別,其他都是要記錄的字符串。日志記錄類別必須以大寫字母開頭。“正常”和“錯誤”是日志消息的標准類別。所有其他都是用於調試的類別。
產卵(事件,二進制,...)
產生與事件(或事件列表,參見下文)相關的新進程作為第一個參數。第二個參數指定要調用的二進制文件。所有其他人都是二進制文件的參數。
如果第三個參數是“<”,那么第四個參數將不會作為參數傳遞給二進制文件。第四個參數是一個字符串,它將通過stdin傳遞給二進制文件。
不要使用Lua os.execute
,而要使用Lsyncd,spawn()
它會阻塞整個Lsyncd守護進程,直到命令完成。另一方面,Lsyncd spawn
在子進程運行時立即返回。
spawnShell(Event,Command,...)
和spawn()一樣,只有它會調用一個shell。任何參數在命令中都被稱為$ 1,$ 2,$ 3等等。
順便說一句,這是spawnShell的簡單實現:
function spawnShell(agent, command, ...) return spawn(agent, "/bin/sh", "-c", command, "/bin/sh", ...) end
終止(退出碼)
讓Lsyncd終止exitcode
。
事件
這些行為的變量由事件字段給出。它有以下領域。
領域 | 含義 |
---|---|
event.config | 使用sync {}調用的配置 |
event.inlet | 請參閱第1層關於入口 |
event.etype | 事件類型。可以是'ATTRIB','CREATE','MODIFY','DELETE','MOVE' |
event.status | 事件的狀態。當它准備好產生時'等待',如果有與該事件相關的進程正在運行,則'等待' |
event.isdir | 如果事件與目錄相關,則為true |
event.name | 文件名,目錄以斜杠結尾 |
event.basename | 文件名,目錄不以斜杠結尾 |
event.path | 請參閱第3層的 ^路徑 |
event.pathname | 請參閱第3層的 ^路徑名 |
event.source | 請參閱第3層的 ^源 |
event.sourcePath | 請參閱第3層的 ^ sourcePath |
event.sourcePathname | 請參閱第3層的 ^ sourcePathname |
event.target | 請參閱第3層的 ^目標 |
event.targetPath | 請參閱第3層的 ^ targetPath |
event.targetPathname | 請參閱第3層的 ^ targetPathname |
onMove操作有兩個事件作為參數,移動的起點和終點。
這個例子會在觀察到的目錄樹中討論所有的移動。
tattleMove = { onMove = function(oEvent, dEvent) log("Normal", "A moved happened from ", oEvent.pathname, " to ", dEvent.pathname) end, }
行動功能必須簡短而快速。他們正在Lsyncd的唯一主線程中運行。如果您不得不再花費更多時間計算spawn {}子進程。
只能有一個與事件關聯的子進程。
第3層就是Lsyncd在初始化時自動為您寫入第2層功能。-log FWrite
使用第3層配置啟動Lsyncd 以查看它為您動態寫入和加載的功能。因此第3層和第2層也可以隨意混合。
配置第1層:入口
第2層允許您為每個事件創建一個進程。但是,與默認的rsync行為一樣,您可能需要為多個事件調用一個進程。這可以通過入口完成。當任何事件准備就緒時,Lsyncd action
使用inlet
as參數調用條目。該inlet
可以用來准備搶單的事件或事件列表。
例如,這是default.rsync使用的操作:
action = function( inlet ) local elist = inlet.getEvents( ) local config = inlet.getConfig( ) local paths = elist.getPaths( ) log( "Normal", "rsyncing list\n", table.concat( paths, '\n' ) ) spawn(elist, '/usr/bin/rsync', '<', table.concat( paths, '\000' ), '--delete', config.rsync._computed, '--from0', '--include-from=-', '--exclude=*', config.source, config.target ) end
入口功能是:
功能 | 描述 |
---|---|
inlet.getEvent() | event 在第2層配置中檢索下一個。多次調用getEvent()將返回相同的事件,除非它已經產生了一個動作。 |
inlet.getEvents(測試) | 返回已准備好的所有事件的列表。test 對於每個事件都要調用的函數來測試它是否應該包含在列表中是可選的。它有一個參數,event 如果應該包含事件,則返回true。如果沒有任何准備好的活動將被列入清單 |
inlet.discardEvent() | 丟棄一個事件。下一次調用getEvent將會收到另一個事件,即使這個事件沒有產生任何動作 |
inlet.getConfig() | 返回相同event.config 。同步{}的配置 |
inlet.addExclude() | 向此同步添加排除模式(請參閱排除) |
inlet.rmExclude() | 從此同步中移除排除模式 |
inlet.createBlanketEvent() | 放在event 延遲FIFO的頂部,阻止所有事件並被所有事件阻塞。這用於onStartup。 |
getEvents返回的列表可以作為代理傳遞給spawn {} 以及單數事件。
列表具有以下功能
功能 | 描述 |
---|---|
elist.getPaths(分隔符) | 返回路徑的字符串(如在event.path 由分離delimiter 。缺省情況下\ n被用作分隔符。 |
elist.getSourcePaths(分隔符) | 返回sourcePaths的字符串(如在event.sourcePath 由分離delimiter 。缺省情況下\ n被用作分隔符。 |
請小心調用getEvents()及其函數,因為根據事件的數量,它們會導致相當多的CPU負載。
如果用戶腳本本身沒有提供,那么第2層功能就是在默認情況下加載的第1層操作。
----- -- Default action calls user scripts on**** functions. -- action = function( inlet ) -- in case of moves getEvent returns the origin and destination of the move local event, event2 = inlet.getEvent( ) local config = inlet.getConfig( ) local func = config[ 'on'.. event.etype ] if func then func( event, event2 ) end -- if function didnt change the wait status its not interested -- in this event -> drop it. if event.status == "wait" then inlet.discardEvent( event ) end end,
如果在配置中找不到“onMove”字段,Lsyncd將自動將Move事件拆分為Create和Delete事件。在處理第1層action
函數中的移動時,只需將“onMove”設置為“true”即可。
除了初始化時的每個同步的action
Lsyncd調用init
。這是在用戶腳本沒有的情況下加載的默認init函數。它為第2層和第3層提供onStartup()功能。
----- -- called on (re)initalizing of lsyncd. -- init = function( inlet ) local config = inlet.getConfig( ) -- calls a startup if provided by user script. if type( config.onStartup ) == "function" then local event = inlet.createBlanketEvent( ) config.onStartup( event ) if event.status == 'wait' then -- user script did not spawn anything -- thus the blanket event is deleted again. inlet.discardEvent( event ) end end end,
作為另一個例子,這是init的init default.rsync
。作為專業,它改變了配置,因為它已經添加了一個斜線。
----- -- Spawns the recursive startup sync -- init = function( inlet ) local config = inlet.getConfig( ) local event = inlet.createBlanketEvent( ) if string.sub(config.target, -1) ~= "/" then config.target = config.target .. "/" end log("Normal", "recursive startup rsync: ", config.source, " -> ", config.target) spawn(event, "/usr/bin/rsync", "--delete", config.rsync._computed .. "r", config.source, config.target ) end,
當子進程完成並收集其僵屍進程時,Lsyncd調用該collect
入口的函數。當收回“再次”時,代理(事件或事件列表)的狀態將再次設置為“等待”,並且將在delay
數秒內(或者如果更小,則為1秒)准備就緒。
默認收集函數在exitcodes []表中查找退出代碼的條目。否則,大多數不幸的更長的代碼不會做任何事情,但會做出好的日志消息
----- -- Called when collecting a finished child process -- collect = function(agent, exitcode) local config = agent.config if not agent.isList and agent.etype == "Blanket" then if exitcode == 0 then log("Normal", "Startup of '",agent.source,"' finished.") elseif config.exitcodes and config.exitcodes[exitcode] == "again" then log("Normal", "Retrying startup of '",agent.source,"'.") return "again" else log("Error", "Failure on startup of '",agent.source,"'.") terminate(-1) -- ERRNO end return end local rc = config.exitcodes and config.exitcodes[exitcode] if rc == "die" then return rc end if agent.isList then if rc == "again" then log("Normal", "Retrying a list on exitcode = ",exitcode) else log("Normal", "Finished a list = ",exitcode) end else if rc == "again" then log("Normal", "Retrying ",agent.etype, " on ",agent.sourcePath," = ",exitcode) else log("Normal", "Finished ",agent.etype, " on ",agent.sourcePath," = ",exitcode) end end return rc end,
Examples
Layer 4 Examples
bash sync:
sync{bash, source="/home/lonewolf/teste1", target="/home/lonewolf/teste2"}
rsyncssh option:
sync{default.rsyncssh, source="/var/www/live_site_resources", host="192.168.129.90", targetdir="/var/www/live_site_resources", delete="running", exclude={ ".*", "*.tmp" }, rsync = { compress = false, checksums = false, _extra = {"--bwlimit=50000"}, } }
Layer 3 Examples
- GForce: forces a local directory tree to be read/writable by a group.
Layer 2 Examples
Layer 1 Examples
- Auto Image Magic: creates a “magic” directory in which all images placed into will be converted to other file formats
原文: http://axkibe.github.io/lsyncd/