在本文中,我將解釋如何在 Linux 系統上安裝 Lsyncd(Live Syncing Mirror Daemon)以及如何在 Linux 中同步遠程和本地目錄。 同步 是一種輕量級的實時鏡像解決方案,安裝相對容易,不會影響現有本地文件系統的性能。
跟踪任何數據修改並在經常使用新內容更新的目錄之間同步這些數據非常有用。 默認情況下,它僅是 rsync。
所有自定義配置文件都是用Lua語言編寫的,這樣就可以獲得強大、靈活、簡單的配置。 Lsyncd 2.2.1 在所有源和目標機器上都需要 rsync 3.1。
在 RHEL/CentOS 7 上安裝 Lsyncd
為了在 CentOS 7.5 系統上啟用 lsyncd,我們需要啟用 EPEL 存儲庫。 您可以簡單地運行此命令來安裝它。
#yum install epel-release
#yum install lsyncd
您可以通過運行以下命令來確認安裝的版本:
# lsyncd -version
Version: 2.2.2
同步配置
Lsyncd 配置文件在 RHEL/CentOS 7.5 系統上的 /etc/lsyncd.conf 中自動創建。 默認情況下,其內容如下所示:
# cat /etc/lsyncd.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/
我們需要根據我們的目的修改這個配置文件。 正如這個配置文件中提到的,所有 example 腳本位於以下位置: /usr/share/doc/lsyncd-2.2.2/examples/
# cd /usr/share/doc/lsyncd-2.2.2/examples/
[root@li1050-94 examples]# ll
total 40
-rw-r--r--. 1 root root 715 Feb 16 2017 lalarm.lua
-rw-r--r--. 1 root root 1055 Feb 16 2017 lbash.lua
-rw-r--r--. 1 root root 534 Feb 16 2017 lecho.lua
-rw-r--r--. 1 root root 3376 Feb 16 2017 lftp.lua
-rw-r--r--. 1 root root 2278 Feb 16 2017 lgforce.lua
-rw-r--r--. 1 root root 2737 Feb 16 2017 limagemagic.lua
-rw-r--r--. 1 root root 2770 Feb 16 2017 lpostcmd.lua
-rw-r--r--. 1 root root 211 Feb 16 2017 lrsync.lua
-rw-r--r--. 1 root root 204 Feb 16 2017 lrsyncssh.lua
-rw-r--r--. 1 root root 4047 Feb 16 2017 lsayirc.lua
所有這些文件都是 lsyncd example 配置文件。 在這些文件中,我們將在此處詳細解釋這些文件的用法,即 lrsync.lua 和 lrsyncssh.lua。 讓我們看看那些 example 配置文件如下:
本地同步的示例配置:
# cat /usr/share/doc/lsyncd-2.2.2/examples/lrsync.lua
----
-- User configuration file for lsyncd.
--
-- Simple example for default rsync.
--
settings {
statusFile = "/tmp/lsyncd.stat",
statusInterval = 1,
}
sync{
default.rsync,
source="src",
target="trg",
}
遠程同步的示例配置:
# cat /usr/share/doc/lsyncd-2.2.2/examples/lrsyncssh.lua
----
-- User configuration file for lsyncd.
--
-- Simple example for default rsync, but executing moves through on the target.
--
sync{default.rsyncssh, source="src", host="localhost", targetdir="dst/"}
為了保留這個同步過程而不是每 X 分鐘運行一次 cron 作業,lsyncd 使用 linux 內核鉤子在目錄中的任何文件發生更改時獲取通知。 默認情況下,它會在 20 秒內將所有同步命令排隊。 我們甚至可以根據需要修改這個時間間隔 --delay
選項以及您的同步命令。
sync {
default.rsyncssh,
source = "SRC",
target = "DEST",
delete = "running", -- prevents deletion of files on startup (ie when a server comes back online, don't delete files that are new on the backup)
delay = 5, -- run every 5 seconds instead of default 20
}
在使用過程中 default.rsyncssh
始終建議使用同步命令 --delete
防止丟失目標文件夾中的文件的選項。 創建或修改 lsyncd 配置文件後,您必須重新啟動 lsyncd 進程。
由於 Lsyncd 使用 rsync 工具將文件從源複製、移動和刪除到目標。 我們可以利用 rsync 開關來防止在目標上進行不必要的重複,並簡化此過程。 下面解釋了一些重要的 rsync 選項:
--delete:
此選項可確保刪除不在源目錄中的遠程目錄中的任何文件。--times:
如果您要一遍又一遍地運行此腳本,此選項非常重要,因為它將使兩個文件之間的時間保持同步。--force:
此選項允許刪除非空目錄以替換為空目錄。--links:
此選項用於將符號鏈接複製為符號鏈接。--progress2:
它會顯示整個傳輸的整體進度,而不僅僅是正在復制的單個文件。--dry-run:
此選項會在不實際執行任何刪除或傳輸的情況下進行試運行,但會告訴您它將執行什麼操作。 我強烈建議您在編寫後第一次運行任何 rsync 命令時使用此選項。--owner:
確保保留文件的所有者用戶(不是所有者的權限級別)。--group:
確保保留文件的組用戶(不是組的權限級別)。--perms:
保留權限。--sparse:
確保有效傳輸稀疏圖像文件。
同步本地目錄
現在讓我們看看如何使用 lsyncd 同步兩個本地文件夾。 我們可以創建一個源文件夾,即 SRC_DIR
和一個目標文件夾,即 DEST_DIR
更形像地解釋這個過程。 讓我們創建文件夾並將一些文件添加到源目錄以執行同步。
# mkdir SRC_DIR *// Create source directory //*
# mkdir DEST_DIR *// Create target directory //*
# cd SRC_DIR/ *// Move to the source folder and create some random files //*
# touch file{1..10}
~/SRC_DIR# ll *// List out the Source folder contents //*
total 8
drwxr-xr-x 2 root root 4096 Aug 2 07:45 ./
drwx------ 7 root root 4096 Aug 2 07:46 ../
-rw-r--r-- 1 root root 0 Aug 2 07:45 file1
-rw-r--r-- 1 root root 0 Aug 2 07:45 file10
-rw-r--r-- 1 root root 0 Aug 2 07:45 file2
-rw-r--r-- 1 root root 0 Aug 2 07:45 file3
-rw-r--r-- 1 root root 0 Aug 2 07:45 file4
-rw-r--r-- 1 root root 0 Aug 2 07:45 file5
-rw-r--r-- 1 root root 0 Aug 2 07:45 file6
-rw-r--r-- 1 root root 0 Aug 2 07:45 file7
-rw-r--r-- 1 root root 0 Aug 2 07:45 file8
-rw-r--r-- 1 root root 0 Aug 2 07:45 file9
接下來,您可以創建 lsyncd 日誌文件和狀態文件來跟踪進程。 這些步驟是可選的。 但我建議為我們所有的任務維護日誌文件。
# mkdir /var/log/lsyncd
# touch /var/log/lsyncd/lsyncd.{log,status}
現在我們需要修改我們的 lsyncd 配置文件來執行這個本地 rsync。 正如我們之前所討論的,CentOS 系統的默認 lsyncd 配置文件是/etc/lsyncd.conf
. 我們需要使用源目錄和目標目錄以及日誌文件更新這些配置文件。
# cat /etc/lsyncd.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 = "/var/log/lsyncd/lsyncd.log",
statusFile = "/var/log/lsyncd/lsyncd.status"
}
sync {
default.rsync,
source = "/root/SRC_DIR",
target = "/root/DEST_DIR",
}
您可以使用自己的值替換源和目標目錄路徑。 Save 和 close 配置文件。 完成後,重新啟動並啟用 lsyncd 服務。
# systemctl enable lsyncd
lsyncd.service is not a native service, redirecting to systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable lsyncd
# systemctl start lsyncd
現在比較源和目標目錄內容以確認其工作。
SRC_DIR]# ll
total 0
-rw-r--r--. 1 root root 0 Aug 2 13:51 file1
-rw-r--r--. 1 root root 0 Aug 2 13:51 file10
-rw-r--r--. 1 root root 0 Aug 2 13:51 file2
-rw-r--r--. 1 root root 0 Aug 2 13:51 file3
-rw-r--r--. 1 root root 0 Aug 2 13:51 file4
-rw-r--r--. 1 root root 0 Aug 2 13:51 file5
-rw-r--r--. 1 root root 0 Aug 2 13:51 file6
-rw-r--r--. 1 root root 0 Aug 2 13:51 file7
-rw-r--r--. 1 root root 0 Aug 2 13:51 file8
-rw-r--r--. 1 root root 0 Aug 2 13:51 file9
DEST_DIR]# ll
total 0
-rw-r--r--. 1 root root 0 Aug 2 13:51 file1
-rw-r--r--. 1 root root 0 Aug 2 13:51 file10
-rw-r--r--. 1 root root 0 Aug 2 13:51 file2
-rw-r--r--. 1 root root 0 Aug 2 13:51 file3
-rw-r--r--. 1 root root 0 Aug 2 13:51 file4
-rw-r--r--. 1 root root 0 Aug 2 13:51 file5
-rw-r--r--. 1 root root 0 Aug 2 13:51 file6
-rw-r--r--. 1 root root 0 Aug 2 13:51 file7
-rw-r--r--. 1 root root 0 Aug 2 13:51 file8
-rw-r--r--. 1 root root 0 Aug 2 13:51 file9
歡呼! 源目錄 SRC_DIR 的內容已成功同步到目標目錄。
此外,您可以查看日誌和狀態文件以驗證複製狀態以確認複製是否已完成。
# tail -10 /var/log/lsyncd/lsyncd.log
Thu Aug 2 14:03:16 2018 Normal: --- Startup ---
Thu Aug 2 14:03:16 2018 Normal: recursive startup rsync: /root/SRC_DIR/ -> /root/DEST_DIR/
Thu Aug 2 14:03:16 2018 Normal: Startup of /root/SRC_DIR/ -> /root/DEST_DIR/ finished.
# more /var/log/lsyncd/lsyncd.status
Lsyncd status report at Thu Aug 2 14:03:27 2018
Sync1 source=/root/SRC_DIR/
There are 0 delays
Excluding:
nothing.
Inotify watching 1 directories
1: /root/SRC_DIR/
同步多個本地文件夾
為了將多個文件夾同步到一個或多個目標目錄,我們需要使用更多的同步命令語句更新配置文件,其中包含我們所需的源目錄和目標目錄。
sync{ default.rsync, source="source1", target="target1" }
sync{ default.rsync, source=" "source2', target="target2" }
為了 example,請查看我的 Lsyncd 配置文件 /etc/lsyncd.conf 以將我的兩個文件夾(即 /root/SRC 和 /etc/nginx 同步到位於以下 /backup 的目標目錄:
----
-- User configuration file for lsyncd.
--
-- Simple example for default rsync.
--
settings {
logfile = "/var/log/lsyncd/lsyncd.log",
statusFile = "/var/log/lsyncd/lsyncd.status"
}
sync{ default.rsync, source="/root/SRC", target="/backup/SRC" }
sync{ default.rsync, source="/etc/nginx", target="/backup/Nginx_bkup" }
使用所需的更改更新配置文件後,您可以重新啟動 lsyncd 進程以啟動 rsync 進程。 完成後,您可以驗證日誌文件以確認其狀態。
# tail /var/log/lsyncd/lsyncd.log
Mon Aug 6 08:36:16 2018 Normal: recursive startup rsync: /root/SRC/ -> /backup/SRC/
Mon Aug 6 08:36:16 2018 Normal: recursive startup rsync: /etc/nginx/ -> /backup/Nginx_bkup/
Mon Aug 6 08:36:16 2018 Normal: Startup of "/root/SRC/" finished.
Mon Aug 6 08:36:16 2018 Normal: Startup of "/etc/nginx/" finished.
同樣,當您想要將相同的源目錄同步到多個目標或多個源目錄到相同的目標目錄時,您可以多次使用此“default-rsync”同步命令語句。
同步到遠程目錄
為了啟動遠程目錄同步,我們需要設置無密碼 SSH 登錄。 這將有助於 Lsyncd 自動將本地目錄的內容複製到遠程目錄,而無需用戶干預。 由於我們在兩台服務器之間進行同步。 我們可以拿一個源服務器和一個目標服務器來更清楚地解釋這個過程。 請參閱下面的 SRC 和 DEST 服務器 IP:
SRC_ SERVER IP : 45.33.113.94
DEST_SERVER IP: 45.33.121.82
步驟 1) 在源服務器上創建用於無密碼登錄的 SSH 密鑰
我們需要為源服務器創建一個 SSH 密鑰,並將公鑰複製到目標服務器,以在帳戶同步期間增強服務器之間的 SSH 連接。 您可以使用以下命令生成 RSA 密鑰:
#ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:T+p4P/5WTw/JA7B/B+uonDNF6KvqDpSl2frl4i3Ma8Q root@li1050-94
The key's randomart image is:
+---[RSA 2048]----+
| |
| . |
| . + |
| * o o . |
| =.. S..o o + |
| . .E +. o O o|
| o+ o .o = *.|
| o**..=.o . o|
| =O*=+*B. |
+----[SHA256]-----+
步驟 2) 將公鑰複製到目標服務器
現在將公鑰複製到目標服務器以啟用無密碼登錄。
# ssh-copy-id [email protected]
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '45.33.121.82 (45.33.121.82)' can't be established.
ECDSA key fingerprint is SHA256:qI+CBEAw9MX+XfXQ1P0NmXVg0tBkWnmjeE0p1wWHzpM.
ECDSA key fingerprint is MD5:62:d9:cc:a5:8b:7a:ef:fd:5e:b8:be:a2:75:3a:0c:20.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
[email protected]'s password:
Number of key(s) added: 1
這裡,45.33.121.82 是目的服務器 IP。 現在我們將能夠從源連接到我們的遠程目標服務器,而無需任何密碼。
# ssh [email protected]
步驟 3) 為遠程同步創建目標目錄
讓我們在目標服務器上創建一個目標目錄,即 Remote_Dir。 完成後,您可以註銷目標服務器。
#mkdir Remote_Dir
步驟 4) 修改 Lsyncd 配置文件以啟用遠程同步
您可以在以下位置備份當前的 lsyncd 配置文件 /etc/lsyncd.conf
並複制位於以下位置的用於遠程同步的示例 lsyncd 配置文件 /usr/share/doc/lsyncd-2.2.2/examples/lrsyncssh.lua
到主 lsyncd 配置文件 /etc/lsyncd.conf
. 完成後,您可以在配置文件中相應地編輯源目錄、主機和目標目錄。 請按照下面的設置查看我的 lsyncd 配置文件以進行遠程同步:
複製示例配置以保留其正確的語法。
# cp /usr/share/doc/lsyncd-2.2.2/examples/lrsyncssh.lua /etc/lsyncd.conf
相應地編輯配置文件。
# cat /etc/lsyncd.conf
----
-- User configuration file for lsyncd.
--
-- Simple example for default rsync, but executing moves through on the target.
--
settings {
logfile = "/var/log/lsyncd/lsyncd.log",
statusFile = "/var/log/lsyncd/lsyncd.status"
}
sync{default.rsyncssh, source="/root/SRC_DIR", host="45.33.121.82", targetdir="/root/Remote_Dir"}
步驟 5)在源上重啟 lsyncd 服務
正確編輯並保存配置文件後,您可以重新啟動 lsyncd 服務以使這些更改生效。 現在登錄遠程服務器(DEST_SERVER)並確認遠程Rsync目錄中的內容即 /root/Remote_Dir
.
#ssh [email protected]
#cd /root/Remote_Dir
# ll
total 8
drwxr-xr-x 2 root root 4096 Aug 2 14:08 ./
drwx------ 9 root root 4096 Aug 5 06:52 ../
-rw-r--r-- 1 root root 0 Aug 2 13:51 file1
-rw-r--r-- 1 root root 0 Aug 2 13:51 file10
-rw-r--r-- 1 root root 0 Aug 2 13:51 file2
-rw-r--r-- 1 root root 0 Aug 2 13:51 file3
-rw-r--r-- 1 root root 0 Aug 2 13:51 file4
-rw-r--r-- 1 root root 0 Aug 2 13:51 file5
-rw-r--r-- 1 root root 0 Aug 2 13:51 file6
-rw-r--r-- 1 root root 0 Aug 2 13:51 file7
-rw-r--r-- 1 root root 0 Aug 2 13:51 file8
-rw-r--r-- 1 root root 0 Aug 2 13:51 file9
你好! 您可以看到本地系統上源目錄中的所有文件要復製到目標服務器的目標目錄中。 您甚至可以通過查看源上的 lsyncd 日誌文件來驗證 rsync 進程是否成功完成。
# 尾/var/log/lsyncd/lsyncd.log
2018 年 8 月 5 日星期日 07:04:42 正常:— 啟動 —
Sun Aug 5 07:04:42 2018 正常:遞歸啟動 rsync: /root/SRC_DIR/ -> 45.33.121.82:/root/Remote_Dir/
2018 年 8 月 5 日星期日 07:04:43 正常:“/root/SRC_DIR/”的啟動完成:0
2018 年 8 月 5 日星期日 07:13:48 正常:Rsyncing 列表
/
2018 年 8 月 5 日星期日 07:13:49 正常:已完成(列表):0
跨多個遠程服務器同步
之前,我們描述瞭如何跨遠程服務器同步文件夾。 同樣,您可以多次使用 default.rsyncssh 命令語句在多個遠程目標文件夾上同步所需的源文件夾。 但是我們需要確保啟用無密碼 SSH 登錄,以將本地目錄的內容複製到多個遠程目錄,而無需用戶干預。
sync{default.rsyncssh, source="source1", host="host1", targetdir="target1"}
sync{default.rsyncssh, source="source2", host="host2", targetdir="target2}
我將用一個簡單的解釋這個過程 example 的轉移 /etc/nginx
從我的 SRC 服務器到遠程服務器 DEST 1 和 DEST 2 的文件夾。
這個過程的第一步是在我的源服務器上生成一個 RSA 密鑰,並將其公鑰複製到我的遠程服務器 DEST 1 和 DEST 2 上,如上所述。 我們需要對兩個遠程服務器 DEST 1 和 DEST 2 重複從步驟 1 到步驟 3 的相同過程,以確保無密碼 SSH 登錄。
其次,我們需要修改lsyncd配置文件中的多個 default.rsyncssh
帶有所需的源、主機和目標文件夾的命令語句。 請在下面查看我的 lsyncd 配置文件以了解此同步過程:
# cat /etc/lsyncd.conf
----
-- User configuration file for lsyncd.
--
-- Simple example for default rsync, but executing moves through on the target.
--
settings {
logfile = "/var/log/lsyncd/lsyncd.log",
statusFile = "/var/log/lsyncd/lsyncd.status"
}
sync{default.rsyncssh, source="/etc/nginx", host="45.33.121.82", targetdir="/backup/nginx"}
sync{default.rsyncssh, source="/etc/nginx", host="45.33.113.94", targetdir="/backup/nginx"}
進行這些配置更改後,您可以重新啟動 lsyncd 服務以啟動此同步過程。 完成後,您可以驗證日誌文件以確認其狀態。
# tail /var/log/lsyncd/lsyncd.log
Mon Aug 6 09:15:55 2018 Normal: recursive startup rsync: /etc/nginx/ -> 45.33.121.182:/backup/nginx/
Mon Aug 6 09:15:55 2018 Normal: recursive startup rsync: /etc/nginx/ -> 45.33.113.194:/backup/nginx/
Mon Aug 6 09:15:56 2018 Normal: Startup of "/etc/nginx/" finished: 0
Mon Aug 6 09:15:56 2018 Normal: Startup of "/etc/nginx/" finished: 0
在 Debian/Ubuntu 18.04 上安裝 Lsyncd
在 Debian 及其衍生產品(如 Ubuntu、Linux Mint 等)上,您可以使用簡單的 apt 命令安裝 lsyncd,如下所示:
#apt install lsyncd
您可以使用以下命令確認已安裝的 lsyncd 版本:
# lsyncd -version
Version: 2.1.6
Debian/Ubuntu 上的配置
在基於 Ubuntu 的系統上,它不會提供任何默認的 lsyncd 配置文件。 建議根據我們的目的手動創建這些配置文件。 我們可以在該位置獲取示例配置文件。/usr/share/doc/lsyncd/examples/
這些樣本 example 配置文件為我們提供了同步的內容/方式的基本概念。
:/usr/share/doc/lsyncd/examples# ll
total 48
drwxr-xr-x 2 root root 4096 Aug 2 07:34 ./
drwxr-xr-x 3 root root 4096 Aug 2 07:34 ../
-rw-r--r-- 1 root root 715 Oct 15 2015 lalarm.lua
-rw-r--r-- 1 root root 1057 Oct 15 2015 lbash.lua
-rw-r--r-- 1 root root 534 Oct 15 2015 lecho.lua
-rw-r--r-- 1 root root 3376 Oct 15 2015 lftp.lua
-rw-r--r-- 1 root root 2278 Oct 15 2015 lgforce.lua
-rw-r--r-- 1 root root 2737 Oct 15 2015 limagemagic.lua
-rw-r--r-- 1 root root 2770 Oct 15 2015 lpostcmd.lua
-rw-r--r-- 1 root root 213 Oct 15 2015 lrsync.lua
-rw-r--r-- 1 root root 204 Oct 15 2015 lrsyncssh.lua
-rw-r--r-- 1 root root 4047 Oct 15 2015 lsayirc.lua
所有這些配置文件都是用 Lua 編程語言編寫的。 請參閱下面的簡單本地 rsync 的 lsyncd 示例配置:
:/usr/share/doc/lsyncd/examples# cat lrsync.lua
----
-- User configuration file for lsyncd.
--
-- Simple example for default rsync.
--
settings = {
statusFile = "/tmp/lsyncd.stat",
statusInterval = 1,
}
sync{
default.rsync,
source="src",
target="trg",
}
與上述 CentOS 系統相比,此過程完全相同。 首先,我們需要在源服務器上設置生成 RSA 密鑰的無密碼 SSH 登錄。 這將確保 Lsyncd 無需用戶干預即可自動將內容複製到遠程目錄。 在遠程同步比較本地同步時要記住的關鍵點是我們需要將 default.rsync 更改為 default.rsyncssh
要通過 ssh 啟用 rsync,我們應該用“host”和“targeted”變量替換“target”變量。 此外,我們還需要維護配置文件位置為 /etc/lsyncd/lsyncd.conf.lua
在 Ubuntu/Debian 系統上。 其餘的 lsyncd 配置過程在所有 Linux 系統中都是相同的。
另請閱讀:
- 如何為實時和雙向同步安裝“鏡像”
- 排除文件/目錄的 10 個 Linux rsync 示例
- Linux 中的 12 個 Linux Rsync 選項解釋
- 使用 rsync 備份文件/目錄的 Shell 腳本
我希望這篇文章對您有用並提供信息! 請就此發表您的寶貴意見和建議。