Skip to content

Instantly share code, notes, and snippets.

@naoaki011
Last active April 16, 2021 04:16
Show Gist options
  • Save naoaki011/7dee8e9fb9dd2748ddc38d548049ed14 to your computer and use it in GitHub Desktop.
Save naoaki011/7dee8e9fb9dd2748ddc38d548049ed14 to your computer and use it in GitHub Desktop.
lsyncd 複数バケット S3転送パターン
-- lsyncd をインストール
-- sudo yum install -y --enablerepo=epel lsyncd
--
-- バケット名を記載した下記ファイルを用意する
-- /powercms/app/tools/lsyncd_s3buckets.lua
-- 内容
-- lsyncd_target_buckets = {
-- "public-singleaz-01-5h19ik0zpa42ulxr", -- vhost01
-- "", -- vhost02
-- }
-- バケット名のテーブル。S3が存在しない仮想ホストは空
settings{
logfile = "/var/log/lsyncd.log",
statusFile = "/var/log/lsyncd.stat",
statusInterval = 1
}
-- Read lsyncd_target_buckets
dofile("/powercms/app/tools/lsyncd_s3buckets.lua")
for lsyncd_target_bucket_key, lsyncd_target_bucket in pairs(lsyncd_target_buckets) do
local s3bucket = lsyncd_target_bucket
if s3bucket ~= "" then
local watchpath = "/powercms/data/sites/" .. string.format("%02d", lsyncd_target_bucket_key) .. "/files"
s3cpexec = function(event)
local src_path = event.sourcePathname
local dst_path = event.sourcePathname
dst_path = string.sub( dst_path , 31 );
spawnShell( event, "aws s3 cp " .. src_path .. " s3://" .. s3bucket .. dst_path .. " --include '*' --exclude '.*' || :" )
-- 最後の「 || :」で強制的に正常終了にしてしまっているので、本来ならコマンドのエラーを正しく戻すのが望ましい。
end
s3rmexec = function(event)
local dst_path = event.sourcePathname
dst_path = string.sub( dst_path , 31 );
spawnShell( event, "aws s3 rm s3://" .. s3bucket .. dst_path .. " --include '*' --exclude '.*' || :" )
-- 最後の「 || :」で強制的に正常終了にしてしまっているので、本来ならコマンドのエラーを正しく戻すのが望ましい。
end
s3sync = function(event)
spawnShell( event, "aws s3 sync " .. watchpath .. "/ s3://" .. s3bucket .. " --include '*' --exclude '.*' --delete || :" )
end
s3_upload = {
maxProcesses = 4,
delay = 0,
--onStartup = s3sync,
onCreate = s3cpexec,
onModify = s3cpexec,
onDelete = s3rmexec
}
sync {
s3_upload,
source = watchpath,
}
end
end
@naoaki011
Copy link
Author

標準の os.execute を使用するとコマンドが完了するまで lsyncd デーモン全体がブロックされてしまうが lsyncd の spawn / spawnShell では子プロセスで実行させ、すぐに制御が返却されると記述されている。 逆言うとコマンド自体のリターンステータスがプログラムからわからないという諸刃。ログファイルにはコードが出力されている。
https://www.shift-the-oracle.com/linux/utility/lsyncd-os-execute.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment