佐々木です。
前回に引き続き lsyncd に関するtips
lsyncdで複数サーバーと同期を行う場合、下記のような設定をよく見かけます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
sync { default.rsync, source = '/foo/bar/sync_path/', target = 'xxx.xxx.xxx.xxx::dest_path/', delete = true, rsync={ archive = true, owner = true, compress = true, _extra = { "--timeout=600", "--contimeout=60", }, }, } sync { default.rsync, source = '/foo/bar/sync_path/', target = 'yyy.yyy.yyy.yyy::dest_path/', delete = true, rsync={ archive = true, owner = true, compress = true, _extra = { "--timeout=600", "--contimeout=60", }, }, } |
これは次のように書き換えることができます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
sync_base = { default.rsync, source = '/foo/bar/sync_path/', delete = true, rsync={ archive = true, owner = true, compress = true, _extra = { "--timeout=600", "--contimeout=60", }, }, } sync { sync_base, target = 'xxx.xxx.xxx.xxx::dest_path/' } sync { sync_base, target = 'yyy.yyy.yyy.yyy::dest_path/' } |
同一設定をまとめることでスッキリしました。
上記で大分見通しがよくなりメンテナンスが行い易くなりましたが、できれば同期先の設定リストを別ファイルとして分けて読み込めるようにしたいものです。
lsyncdの設定ファイルはlua言語で書かれたコードであるため、プログラムを記載すれば実現可能です。
以下にサンプルコードを記載します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
-- ベースとなる基本設定 sync_base = { default.rsync, source = '/foo/bar/sync_path/', delete = true, rsync={ archive = true, owner = true, compress = true, _extra = { "--timeout=600", "--contimeout=60", }, }, } -- IPチェック用関数 function isIP(ipaddr) is_ip = false for key,val in pairs({string.match(ipaddr, '^(%d+)\.(%d+)\.(%d+)\.(%d+)$')}) do is_ip = true val = tonumber(val) if ( val 255 ) then is_ip = false break end end return is_ip end -- 同期先IPを列挙したテキストファイルを開く f = io.open('/etc/sync-server.list', 'r') -- 同期先IPを列挙したテキストファイルを読み込む for target_ip in f:lines() do -- 読み込んだ行がIPアドレスであればsyncで同期指定 if isIP(target_ip) then sync { sync_base, target= target_ip .. '::dest_path/', } end end f:close() |
/etc/sync-server.list には同期先のIPアドレスを記載します。
xxx.xxx.xxx.xxx
yyy.yyy.yyy.yyy
設定ファイルと思っていたものがプログラムになってしまいましたが、上記のようにしておけば何か別のバッチなどでIPアドレスのリストを吐き出せるようにするだけ同期先を追加できるようになります。
以上、ご参考になれば幸いです。
- amoxicillin 500mg capsules
- levitra buy canada
- get free trial levitra
- cialis and levitra taken together
この記事を書いた人

-
高校時代より独学でC、アセンブラを学ぶ。
パソコンスクール講師 → カノープス株式会社(現グラスバレー、MEDIAEDGE)を経て、当時の取締役の誘いでパクレゼルヴに入社する。
事業分社以前は事業部を統括。現グルーポン・ジャパン立ち上げ時のサーバーインフラなども担当。
分社化後は現社長の的場大昌に委ね技術者として専念する。
趣味は週末娘と遊ぶこと。
最新記事
開発日誌2015.02.12lsyncdを複数起動する
開発日誌2015.01.27lsyncdで複数サーバーをターゲットに同期する際のtips
開発日誌2015.01.26lsyncdを使う時はtimeoutを設定しましょう