6
8

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 5 years have passed since last update.

rotatelogsが2.4で全部こなせるようになったのでメモ

6
Last updated at Posted at 2014-02-01

apache2.4.7に付いてきたrotatelogsがリンクとプログラム経由を提供していたので
日付でrotateしつつgzipしてdeleteまでしてみた。

公式はこちら
http://httpd.apache.org/docs/2.4/programs/rotatelogs.html

apache側のカスタムログ指定

http.conf
CustomLog "|/home/apache/bin/outputlog.sh access" custom
ErrorLog "|/home/apache/bin/outputlog.sh error"

ここではオリジナルのshellにパイプしています。

outputlog.sh
# !/bin/sh

# ex) sh outputlog.sh access foobar.com
LOG_PATH=/var/log/httpd
ROTATELOGS_BIN=/etc/httpd/bin/rotatelogs
GZIP_SHELL=/home/apache/bin/gziplog.sh
if [ $# -le 0 ]; then
  echo "args error.$#"
  exit 1
fi

type=$1
domain=$2

case "$type" in
  "access" | "access_log" ) type="access";;
  "error" | "error_log" ) type="error";;
  "transition" | "transition_log" ) type="transition";;
  * ) exit 1;;
esac

# sg admin "$ROTATELOGS_BIN -cfl -p ${GZIP_SHELL} -L ${LOG_PATH}/${domain}${domain:+-}${type}_log ${LOG_PATH}/${type}/${domain}${domain:+-}${type}_log.%Y-%m-%d.%H:%M:%S 60"
sg admin "$ROTATELOGS_BIN -cfl -p ${GZIP_SHELL} -L ${LOG_PATH}/${domain}${domain:+-}${type}_log ${LOG_PATH}/${type}/${domain}${domain:+-}${type}_log.%Y-%m-%d 86400"

結果的には

sg admin "/etc/httpd/bin/rotatelogs -cfl -p /home/apache/bin/gziplog.sh \
-L /var/log/httpd/access_log /var/log/httpd/access/access_log.%Y-%m-%d 86400"

/var/log/httpd直下に現在進行形ログが残り
以下種類別のディレクトリに本体が置かれるようになっています。

ファイルの圧縮と削除

次に上記で指定している-p /home/apache/bin/gziplog.shの部分

gziplog.sh
# !/bin/sh
LOG_PATH=/var/log/httpd
GZIP_BIN=/bin/gzip
## Retention period.
TERM=7

## Ignore first.
if [ $# -le 1 ]; then
  exit 0
fi
newfile=$1
oldfile=$2

### gzip ####
gzip ${oldfile} >/dev/null 2>&1

#### file delete ####
filename=${oldfile##*/}
find ${oldfile%/*} -maxdepth 1 -mtime +${TERM} -name "${filename%_log*}_log*.gz" -delete

公式にある通り、第二引数に切り替わる前のファイルのfullpathが来るので
それをそのままgzipしてます。

次にその引数からファイルの検索&削除

以上。

6
8
2

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
6
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?