16
15

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

lsyncd.confの設定をLuaでテクニカルに記述

Last updated at Posted at 2014-08-28

lsyncd.confのコメントがLuaっぽくて、やってみたらできたので覚書。
複数のサーバにsyncを設定するときに設定ファイルが長くならずにすみます。

前提

CentOS6.5でlsyncdを動かす
こちらの設定ができていることが前提です。

構成

ホスト IPアドレス
サーバ 192.168.33.81
クライアント1 192.168.33.82
クライアント2 192.168.33.83

やりたいこと

syncの対象を以下のように設定したい

lsyncd_test/
├── dir1 # lsync対象  
├── dir2 # lsync対象外
└── dir3 # lsync対象

それ以外 : lsync対象外

lsyncd_testを同期対象にしてexcludeでdir2を除外してもdir4などが作成されると同期されてしまう。ホワイトリスト形式で特定のディレクトリのみクライアント1、クライアント2に同期する。

ディレクトリ作成

[サーバ]
$ mkdir /home/vagrant/lsyncd_test/dir1
$ mkdir /home/vagrant/lsyncd_test/dir2
$ mkdir /home/vagrant/lsyncd_test/dir3

設定を列挙した書き方

/etc/lsyncd/lsyncd.conf

----
-- User configuration file for lsyncd.
--
-- Simple example for default rsync.
--
settings {
        logfile    = "/var/log/lsyncd.log",
        statusFile = "/tmp/lsyncd.stat",
        statusInterval = 1,
        maxProcesses = 2,
        nodaemon     = false,
        insist       = 1,
}

sync{
        default.rsync,
        source="/home/vagrant/lsyncd_test/dir1",
        target="192.168.33.82::test/dir1",
        rsync = {
          _extra = {  "-a" },
        }
}

sync{
        default.rsync,
        source="/home/vagrant/lsyncd_test/dir3",
        target="192.168.33.82::test/dir3",
        rsync = {
          _extra = {  "-a" },
        }
}

sync{
        default.rsync,
        source="/home/vagrant/lsyncd_test/dir1",
        target="192.168.33.83::test/dir1",
        rsync = {
          _extra = {  "-a" },
        }
}

sync{
        default.rsync,
        source="/home/vagrant/lsyncd_test/dir3",
        target="192.168.33.83::test/dir3",
        rsync = {
          _extra = {  "-a" },
        }
}

Luaのテクニカルな書き方

/etc/lsyncd/lsyncd.conf

----
-- User configuration file for lsyncd.
--
-- Simple example for default rsync.
--
settings {
        logfile    = "/var/log/lsyncd.log",
        statusFile = "/tmp/lsyncd.stat",
        statusInterval = 1,
        maxProcesses = 2,
        nodaemon     = false,
        insist       = 1,
}

-- Set lsyncd target files.
lsyncd_target_dirs = {
"dir1/",
"dir3"
}

-- Read lsyncd_target_hosts.lua
dofile("/etc/lsyncd/lsyncd_target_hosts.lua")

for lsyncd_target_host_key, lsyncd_target_host in pairs(lsyncd_target_hosts) do
  for lsyncd_target_dir_key, lsyncd_target_dir in pairs(lsyncd_target_dirs) do
    sync{
      default.rsync,
      source="/home/vagrant/lsyncd_test/" .. lsyncd_target_dir,
      target=lsyncd_target_host .. "::test/" .. lsyncd_target_dir,
      rsync = {
        _extra = {  "-a" },
      }
    }
  end
end

/etc/lsyncd/lsyncd_target_hosts.lua

lsyncd_target_hosts = {
"192.168.33.82",
"192.168.33.83"
}

同期前の状態

[サーバ]
$ ls /home/vagrant/lsyncd_test/
=====================
dir1  dir2  dir3
=====================

[クライアント1]
$ ls /home/vagrant/lsyncd_test/
=====================
=====================

[クライアント2]
$ ls /home/vagrant/lsyncd_test/
=====================
=====================

同期実行後

[サーバ]
$ sudo service lsyncd restart
=====================
Stopping lsyncd:                                           [  OK  ]
Starting lsyncd:                                           [  OK  ]
=====================

[クライアント1]
$ ls /home/vagrant/lsyncd_test/
=====================
dir1  dir3
=====================

[クライアント2]
$ ls /home/vagrant/lsyncd_test/
=====================
dir1  dir3
=====================

Luaイイネ!
Apacheもmod_luaでLuaで書けるとか。。

16
15
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
16
15

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?