天天看點

實時同步lsyncd

實時同步lsyncd

1 lsyncd

1.1 lsyncd 簡介

Lsyncd使用檔案系統事件接口(inotify或fsevents)來監視對本地檔案和目錄的更改。Lsyncd将這些事件整理幾秒鐘,然後生成一個或多個程序以将更改同步到遠端檔案系統。

預設同步方法是rsync Lsyncd是一種輕量級的實時鏡像解決方案。Lsyncd相對容易安裝,不需要新的檔案系統或塊裝置。Lysncd不會妨礙本地檔案系統性能,可以通過配置檔案實作細粒度的自定義。自定義操作配置甚至可以從頭開始編寫,從shell腳本到用Lua語言編寫的代碼。

1.2 環境準備

rsync服務端:預設就有rsync無需安裝

lsyncd用戶端:安裝lsyncd軟體

在這裡,備份伺服器為服務端,存儲伺服器為用戶端

[root@nfs ~]#yum install -y lsyncd

1.3 rsync配置檔案

backup配置檔案

[root@backup ~]#cat /etc/rsyncd.conf

uid = rsync

gid = rsync

port = 873

fake super = yes

use chroot = no

max connections = 200

timeout = 300

pid file = /var/run/rsyncd.pid

lock file = /var/run/rsync.lock

log file = /var/log/rsyncd.log

ignore errors

read only = false

list = false

hosts allow = 172.16.1.0/24

auth users = rsync_backup

secrets file = /etc/rsync.password

[backup]

comment = "backup dir by oldboy"

path = /backup

1.4 建立虛拟使用者rsync

id rsync #檢視以下是否有rsync這個使用者

useradd rsync -M -s /sbin/nologin #建立rsync虛拟使用者

1.5 建立密碼檔案并修改通路權限為600

echo "rsync_backup:123456" >/etc/rsync.password

chmod 600 /etc/rsync.password #隻能root使用者才能檢視密碼

1.6 建立備份目錄/修改目錄屬主和屬組資訊

mkdir /backup

chown rsync.rsync /backup

1.7 啟動服務程式/重新開機服務程式

systemctl start rsyncd

systemctl restart rsyncd

systemctl enable rsyncd

1.8 lsyncd配置檔案

lsyncd配置檔案

root@nfs ~]#cat /etc/lsyncd.conf

settings {

logfile = "/var/log/lsyncd/lsyncd.log",

statusFile = "/var/log/lsyncd/lsyncd.status",

inotifyMode = "CloseWrite",

maxProcesses = 8,

}

sync {

default.rsync,

source = "/data",

target = "[email protected]::backup",

delete= true,

exclude = { ".*" },

delay = 1,

rsync = {

binary = "/usr/bin/rsync",

archive = true,

compress = true,

verbose = true,

password_file = "/etc/rsync.password",

_extra = {"--bwlimit=200"}

1.9 存儲伺服器建立備份的目錄

mkdir /data

1.10 建立密碼檔案,并修改權限

echo "123456" >/etc/rsync.password #建立密碼檔案

chmod 600 /etc/rsync.password

1.11免互動式傳輸密碼檔案

rsync -avz /etc/passwd [email protected]::backup --password-file=/etc/rsync.password

執行上述指令出現下圖,則證明部署服務成功

image.png

1.12 存儲伺服器進行測試

[root@nfs data]#touch test.txt

[root@nfs data]#ls

test.txt

++++++++++++++++++++++++++++++

[root@backup backup]#ls

原文位址

https://www.cnblogs.com/basa/p/11297415.html