Help us understand the problem. What is going on with this article?

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

More than 5 years have passed since last update.

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してます。

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

以上。

BANDAI
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした