apacheログローテーションスクリプト

概要

rotatelogsを使用した日例ローテーションを行っているサーバからのログ切り出し。

log_ctrl.sh

#!/bin/sh
# ------------------------
#  Log Control Script
# ------------------------
# Version : 1.0
# date    : 2008/05/16

export LANG=C
export PATH=/bin:/sbin:/usr/bin:/usr/sbin:$PATH

# date

DATEY=`date +%Y --date '1 days ago'`
DATEM=`date +%m --date '1 days ago'`
DATED=`date +%d --date '1 days ago'`

TODAY=`date +%Y%m%d`
LASTDAY=`date +%Y%m%d --date '1 days ago'`

# Script
LOGCUT="/home/system/script/logs/log_cut.sh"
MKDIR="/home/system/script/logs/make_date-dir.sh"

LOGFILE="/home/system/logs/log-cut_"$DATEYDATEM".txt"


# Site
BASEDIR="/home/httpd/【SiteName】"
date +%Y/%m/%d\ %H:%M:%S >> $LOGFILE
echo $BASE >> $LOGFILE

$MKDIR $BASEDIR/log_archive/ >> $LOGFILE
$LOGCUT $LASTDAY $BASEDIR/logs/access.log.2.gz $BASEDIR/log_archive/$DATEY/$DATEM/【SiteName】_$LASTDAY.log >> $LOGFILE
$LOGCUT $LASTDAY $BASEDIR/logs/access.log.1.gz $BASEDIR/log_archive/$DATEY/$DATEM/【SiteName】_$LASTDAY.log >> $LOGFILE
$LOGCUT $LASTDAY $BASEDIR/logs/access.log $BASEDIR/log_archive/$DATEY/$DATEM/【SiteName】_$LASTDAY.log >> $LOGFILE
gzip $BASEDIR/log_archive/$DATEY/$DATEM/【SiteName】_$LASTDAY.log

exit 0
# EOF

log_cut.sh

#!/bin/sh
# ------------------------
# Log Cutting Script
# ------------------------
# Version:2.0
# update :2005.09.29 

export LANG=C
export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin

# Hint
# Commnad  [DATE] [FLAG] [OUTPUT] [INPUT]

## -- Check --

# $1
# target date
if [ -z $1 ]; then
 echo "ERR : target date not found"
 echo "USE : ./log_cut.sh [DATE] [INPUT] [OUTPUT]"
 exit 0
else
 DATEWC=`echo $1 | wc -m`
 if [ $DATEWC = "9" ]; then
  TARGETDATE="$1"
  DATEY=`date +%Y --date $TARGETDATE`
  DATEM=`date +%m --date $TARGETDATE`
  DATED=`date +%d --date $TARGETDATE`
  DATEE=`date +%e --date $TARGETDATE | sed "s/ //"`
  DATEN=`date +%b --date $TARGETDATE`
  DATEW=`date +%a --date $TARGETDATE`
 else
  echo "ERR : "$1" is abnormality format"
  exit 0
 fi 
fi

# $2
# date format
TARGET=$DATED/$DATEN/$DATEY


# $3
# input file
if [ -z $2 ]; then
 echo "ERR : Input not found"
 echo "USE : ./log_cut.sh [DATE] [INPUT] [OUTPUT]"
 exit 0
else
 if [ -f $2 ]; then
  INFILE="$2"
 else
  echo "ERR : "$2" not read | not found"
  exit 0
 fi 
fi


# $4
# output path
if [ -z $3 ]; then
 echo "ERR : output not found"
 echo "USE : ./log_cut.sh [DATE] [INPUT] [OUTPUT]"
 exit 0
else
 test -w $3 || touch $3
 if [ -w $3 ]; then
  OUTFILE="$3"
 else
  echo "ERR : output file write error"
  echo "USE : ./log_cut.sh [DATE] [INPUT] [OUTPUT]"
  exit 0
 fi
fi


# Exec

file $INFILE | grep "gzip" > /dev/null
FILETYPE=`echo $?`
if [ $FILETYPE -eq "0" ]; then
 CAT="zcat"
else
 CAT="cat"
fi

$CAT $INFILE | grep $TARGET >> $OUTPATH/$OUTFILE


exit 0
#EOF

make_date-dir.sh

#!/bin/sh
# --------------------------------
# Make DateName Directory Script
# --------------------------------
# Version : 1.1
# update  : 2007.02.22

export LANG=C
export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin

# Set

DATEY=`date +%Y --date '1 days ago'`
DATEM=`date +%m --date '1 days ago'`
DATED=`date +%d --date '1 days ago'`

TODAY=`date +%Y%m%d`
LASTDAY=`date +%Y%m%d --date '1 days ago'`

# Check

if [ -z $1 ]; then
 echo "ERR : directory not found"
 echo "USE : ./make_date-dir.sh [/PATH/LOGFILE]" 
 exit 0
fi

if [ -d $1 ]; then
 LOGPATH=$1
else
 echo "ERR : directory not found"
 echo "USE : ./make_date-dir.sh [/PATH/LOGFILE]" 
 exit 0
fi

# Ecex

## Make Dir

# Year Dir
test -d $LOGPATH/$DATEY || mkdir $LOGPATH/$DATEY
# Monthly Dir
test -d $LOGPATH/$DATEY/$DATEM || mkdir $LOGPATH/$DATEY/$DATEM
# Dayily Dir
#test -d $LOGPATH/$DATEY/$DATEM/$DATED || mkdir $LOGPATH/$DATEY/$DATEM/$DATED

# EOF

トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2008-05-20 (火) 19:06:17 (5879d)