はじめに
lsyncd
を使って2つのディレクトリを同期させたいことがあったのでやってみる。
環境
Windows 11 Professional
WSL2 Ubuntu 24.04 LTS
rsync version 3.2.7
lsyncd version 2.2.3
lsyncdのインストール
sudo apt install lsyncd
ログ
sudo apt install lsyncd
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
liblua5.3-0 lua5.3
The following NEW packages will be installed:
liblua5.3-0 lsyncd lua5.3
0 upgraded, 3 newly installed, 0 to remove and 54 not upgraded.
Need to get 355 kB of archives.
After this operation, 1157 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://archive.ubuntu.com/ubuntu noble/universe amd64 liblua5.3-0 amd64 5.3.6-2build2 [143 kB]
Get:2 http://archive.ubuntu.com/ubuntu noble/universe amd64 lua5.3 amd64 5.3.6-2build2 [145 kB]
Get:3 http://archive.ubuntu.com/ubuntu noble/universe amd64 lsyncd amd64 2.2.3-1 [67.4 kB]
Fetched 355 kB in 2s (225 kB/s)
Selecting previously unselected package liblua5.3-0:amd64.
(Reading database ... 77147 files and directories currently installed.)
Preparing to unpack .../liblua5.3-0_5.3.6-2build2_amd64.deb ...
Unpacking liblua5.3-0:amd64 (5.3.6-2build2) ...
Selecting previously unselected package lua5.3.
Preparing to unpack .../lua5.3_5.3.6-2build2_amd64.deb ...
Unpacking lua5.3 (5.3.6-2build2) ...
Selecting previously unselected package lsyncd.
Preparing to unpack .../lsyncd_2.2.3-1_amd64.deb ...
Unpacking lsyncd (2.2.3-1) ...
Setting up lua5.3 (5.3.6-2build2) ...
update-alternatives: using /usr/bin/lua5.3 to provide /usr/bin/lua (lua-interpreter) in auto mode
update-alternatives: using /usr/bin/luac5.3 to provide /usr/bin/luac (lua-compiler) in auto mode
Setting up liblua5.3-0:amd64 (5.3.6-2build2) ...
Setting up lsyncd (2.2.3-1) ...
Processing triggers for man-db (2.12.0-4build2) ...
Processing triggers for libc-bin (2.39-0ubuntu8.3) ...
設定ファイルの準備
vim ~/lsyncd.conf.lua
lsyncd.conf.lua
settings {
logfile = "/var/log/lsyncd.log",
statusFile = "/var/run/lsyncd.status",
nodaemon = true
}
sync {
default.rsync,
source = "/home/kbushi/test/source",
target = "/home/kbushi/test/target",
rsync = {
archive = true,
compress = true
}
}
- source: 同期元のディレクトリを指定。
- target: 同期先のディレクトリを指定。
- nodaemon = true: デバッグ用にフォアグラウンドで動作させる。
実行とテスト
実行
sudo lsyncd lsyncd.conf.lua
sudo lsyncd lsyncd.conf.lua
19:17:37 Normal: --- Startup ---
19:17:37 Normal: recursive startup rsync: /home/kbushi/test/source/ -> /home/kbushi/test/target/
19:17:37 Normal: Startup of /home/kbushi/test/source/ -> /home/kbushi/test/target/ finished.
テスト
~/test/source $ touch testFile
~/test/source ll
-rw-r--r-- 1 kbushi kbushi 0 Jan 15 19:15 test
-rw-r--r-- 1 kbushi kbushi 0 Jan 15 19:18 testFile
~/test/target ll
-rw-r--r-- 1 kbushi kbushi 0 Jan 15 19:15 test
-rw-r--r-- 1 kbushi kbushi 0 Jan 15 19:18 testFile
同期できているのでOK
※1分間隔で実行するcronタスクにrsync
を仕込むでもよさそうか?
参考
- lsyncd / lsyncd
https://github.com/lsyncd/lsyncd
おわりに
WSL2
でlsyncd
を使ってディレクトリの同期をとる方法を実施した。
サーバでの設定もいつかやってみよう…。