0
0

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 1 year has passed since last update.

【Linux】無理矢理ログのアーカイブ【Shell】

Last updated at Posted at 2021-09-13

ローテ設定を使わずにログのアーカイブ(力業)
logディレクトリに「yyyymmdd.log」というログが吐かれているとして、
シェル実行のタイミングで前月のログが存在するかどうか判定し、存在する場合は月ごとにアーカイブせぇ!!
な時のメモ。

##ログアーカイブ処理
THIS_MONTH=`date +'%Y%m01'`
TARGET_MONTH=`date -d "${THIS_MONTH} 1 month ago" +'%Y%m'`
cd /work/log/
ls |grep "${TARGET_MONTH}" |grep log >/dev/null 2>&1
if [ $? = 0 ]; then
  ls |grep "${TARGET_MONTH}" |grep ".log" |tar -cvz -T - --null -f ${TARGET_MONTH}.tar.gz --remove-files
  echo "Create_Archive_Log : ${TARGET_MONTH}.tar.gz"
else
  echo "NO_Archive_Log"
fi

・TARGET_MONTH変数にはスクリプトを実行した時点からみて前月のyyyymmが入ります。
・lsはファイルがあると0、ないと1を返すのでそれで判定。grep2つでyyyymmかつ、.logで当てはまるファイルを検索検索ぅ!※ここはfindとかでもOK
・0を返した場合は同じようにlsして出てきたファイルをまとめてtar.gzにした後、削除する
といった感じです。

以上お。

参考↓

0
0
0

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?