[原创]让cron自动压缩apache日志的shell脚本

#!/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空间。



posted on 2008-01-11 18:10 黑虫 阅读(1382) 评论(5)  编辑 收藏 引用 所属分类: Unix/Linux

评论

# re: [原创]让cron自动压缩apache日志的shell脚本 2011-11-15 18:46 Donovan23Rosemarie

Cars and houses are not very cheap and not everyone is able to buy it. However, <a href="http://goodfinance-blog.com/topics/mortgage-loans">mortgage loans</a> was created to support people in such kind of situations.   回复  更多评论   

只有注册用户登录后才能发表评论。
<2008年1月>
303112345
6789101112
13141516171819
20212223242526
272829303112
3456789

导航

统计

常用链接

留言簿(4)

随笔分类(31)

随笔档案(31)

awmsky

搜索

最新评论

阅读排行榜

评论排行榜