Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

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

More than 5 years have passed since last update.

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で書けるとか。。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away