#!/bin/sh
#==================================
#
#Author: accepting
#
#Email: accepting@126.com
#
#Date: 2008.01.11
#
#Http://accepting.sharera.com
#
#==================================
#
LOG_FILE="/root/log/" //存放apache日志目录
end_filename="_access.log" //apache日志的命名格式后一部分
#======================================================================
if [ $# -eq 0 ] ; then
CurYear=`date +%Y`
CurMonth=`date +%m`
CurDay=`date +%d`
GetYear="$CurYear"
GetMonth="$CurMonth"
GetDay="`expr $CurDay - 1`"
if [ "$GetDay" -le 0 ] ; then
GetMonth=`expr $CurMonth - 1`
if [ "$GetMonth" -le 0 ] ; then
GetYear=`expr $CurYear - 1`
GetMonth=12
fi
case "$GetMonth"
in
1|3|5|7|8|10|12)
GetDay=31;;
4|6|9|11)
GetDay=30;;
2)
if [ `expr "$CurYear" % 400` -eq 0 ] ; then
GetDay=29
elif [ `expr "$CurYear" % 4` -eq 0 -a `expr "$CurYear" % 100` -ne 0 ] ; then
GetDay=29
else
GetDay=28
fi
esac
fi
if [ `echo "$GetMonth" | wc -m` -ne 3 ] ; then
GetMonth=0$GetMonth
fi
if [ `echo "$GetDay" | wc -m` -ne 3 ] ; then
GetDay=0$GetDay
fi
//得到apache日志命名格式前一部分
first_filename=$GetYear$GetMonth$GetDay
//判断apache日志是否存在
if [ -f $LOG_FILE$first_filename$end_filename ] ; then
gzip $LOG_FILE$first_filename$end_filename
echo
echo =========== $first_filename$end_filename ===========
echo Compress $first_filename$end_filename is successfull!
echo ===========================================
echo
else
echo
echo ======== $first_filename$end_filename ========
echo $first_filename$end_filename not exist!
echo =====================================
echo
fi
fi
这是我在为了方便维护服务器写的shell脚本,想让该脚本为你实现自动压缩请设置cron,apache还要设置时间记录日志格式,如:CustomLog "|/usr/sbin/rotatelogs /var/log/apache/default_log.%y%m%d 86400 480|gzip" combined
。
我不是程序员,只是喜欢玩一下新奇的玩法,请各路高手不要见笑。现在分享给有需要的朋友,如果有转载,请注明出处,本脚本第一次发布在
http://accepting.sharera.com空间。