centos7使用lsyncd實時同步(from internet)
- lsyncd是lua語言封裝了inotify和rsync的工具,採用了linux內核(2.6.13及以後)裏的inotify處罰機制,然後通過rsync去差異同步,達到實時的效果。
- 他完美解決了inotify+rsync海量文件同步帶來的文件頻繁發送文件列表的問題–通過時間延遲或累計觸發事件次數實現。
- lsyncd工作模式分爲本地目錄cp,本地目錄rsync,遠程目錄rsync,遠程目錄rsyncssh。
- github地址,詳細說明
安裝
直接安裝
yum install lsyncd
源碼編譯安裝
- 下載源碼,並編譯安裝
# 未測試
uzip lsyncd-master.zip
cd lsyncd-master
cmake -DCMATE_INSTALL_PREFIX=/usr/local/lsyncd
make && make install
配置
配置文件示例
- 創建配置文件
touch 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 {
logfile = "/tmp/auction_lsyncd.log",
statusFile = "/tmp/auction_lsyncd.status",
insist = true,
statusInterval = 10,
inotifyMode = "CloseWrite",
nodaemon = true,
maxProcesses = 10,
}
sync {
default.rsyncssh,
source="/files",
host="192.168.1.9",
targetdir="/disk/files",
rsync = {
binary = "/usr/bin/rsync",
archive = true,
compress = false,
verbose = true,
whole_file = false
},
ssh = {
port = 22
}
}
- 本地目錄同步cp
sync {
default.direct,
source = "/tmp/src",
target = "/tmp/dest",
delay = 1
maxProcesses = 1
}
- 本地目錄同步rsync
sync {
default.rsync,
source = "/tmp/src",
target = "/tmp/dest",
excludeFrom = "/etc/rsyncd.d/rsync_exclude.lst",
rsync = {
binary = "/usr/bin/rsync",
archive = true,
compress = true,
bwlimit = 2000
}
}
選項說明
- settings
- logfile 定義日誌文件
- statusFile 定義狀態文件
- nodaemon=true 表示不啓用守護模式,默認
- statusInterval 將lsyncd的狀態寫入上面的statusFile的間隔,默認10秒
- inotifyMode 指定inotify監控的事件,默認是CloseWrite,還可以是Modify或CloseWrite or Modify
- maxProcesses 同步進程的最大個數
- maxDelay 累計到多少所監控的事件激活一次同步,即使後面的delay延遲時間還未到
- sync
運行模式:rsync,rsyncssh,direct
- default.sync 本地間同步,使用rsync
- default.direct 本地間同步,使用cp
- default.rsyncssh 同步到遠程主機目錄,rsync的ssh模式,需要使用key來認證
其他 - source 同步的源目錄,使用絕對路徑
- target 定義目的地址
- /tmp/dest :本地目錄同步,可用於direct和rsync模式
- 172.29.88.223:/tmp/dest :同步到遠程服務器目錄,可用於rsync和rsyncssh模式,拼接的命令類似於/usr/bin/rsync -ltsd –delete –include-from=- –exclude=* SOURCE TARGET,剩下的就是rsync的內容了,比如指定username,免密碼同步
- 172.29.88.223::module :同步到遠程服務器目錄,用於rsync模式
- rsync
- bwlimit 限速,單位kb/s
- compress 壓縮傳輸。默認true
- perms 默認保留文件權限
- host 主機地址
啓動lsyncd
lsyncd lsyncd.conf >> /dev/null 2>&1
lsyncd其他功能
可監控目錄下的文件,根據觸發的時間自己定義要執行的命令