AWS CLI がある事が前提
大体 EC2 インスタンスにロール持たせていると思う
次のスニペットをよしなに(※)貼る
# custom configuration for s3 dump
# make sure it uses expected suffix format
# Include hour to avoid having overwrite
dateext
dateformat -%Y%m%d%H
compress
# unlike postrotate, lastaction will run **after the compress**
lastaction
dateext="-$(date +%Y%m%d%H)"
bucket="bucket/prefix"
for filepath in /path/to/file /path/to/another/file
do
filename=$(basename $filepath)
[ -r ${filepath}${dateext}.gz ] && aws s3 cp ${filepath}${dateext}.gz s3://${bucket}/${filename}${dateext}.gz >/dev/null || true
done
endscript
(※) ... ubuntu ベースの AMI を使ったら /etc/logrotate.d
の中に syslog
とか nginx
とかあったので、そこら辺によしなに
(※) ... 毎回 compress が必要かというと、そうでもない場合も多いと思うので、そこら辺よしなに
(※) ... ファイルパスの指定とか、S3 のパスの指定とか、そこら辺よしなに
lastaction
logrotate を調べると postrotate
がよく出てくるけど、postrotate
はファイルを圧縮する前に実行されてしまう。
lastaction
はファイル圧縮後に実行されるので、ディスクスペースの無駄な消費を避けるためにこちらを使っている。
Reference
助けてもらった記事
Logrotate の基礎について https://qiita.com/zom/items/c72c7bac63462225971b#世代管理をする
lastaction について https://stackoverflow.com/a/12072083