zoukankan      html  css  js  c++  java
  • Shell + crontab 实现日志压缩归档

    Shell + crontab 实现日志压缩归档

    crontab

    1 # archive the ats log file, keep 7 days.
    2 */5 * * * * root /bin/sh /path/archive_atslog.sh >/dev/null 2>&1

    shell

     1 #!/bin/bash
     2 # Author      : standby
     3 # Date        : 2017-04-17
     4 # Description : Archive the live log, keep the lastest 7 days.
     5 
     6 logdir="/data/ats/logs"
     7 TODAY=`date -d "6 minutes ago" +%Y%m%d`
     8 
     9 function get_exter_ip()
    10 {
    11     arr=(`/sbin/ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:"`)
    12     exter_ip=${arr[0]}
    13     for ip in ${arr[*]}
    14     do
    15         ip_tmp=`echo $ip | grep -v '^10.'`
    16         if [[ ! -z $ip_tmp ]];then
    17             break
    18         fi
    19     done
    20     exter_ip=$ip_tmp
    21     echo $exter_ip
    22 }
    23 IP=`get_exter_ip`
    24 # rename log file
    25 cd $logdir
    26 for i in `ls access.log_*.old`
    27 do
    28     DATE=`echo $i |awk -F "[-.]" '{print $(NF-4)$(NF-3)}' |sed 's/[hms]//g'`
    29     mv $i live_${IP}_${DATE}_05min.log
    30 done
    31 
    32 subPath=$logdir/$TODAY/
    33 # gzip and archive log file
    34 [ ! -d $subPath ] && mkdir -p $subPath
    35 for i in `ls live_*.*_05min.log`
    36 do
    37     gzip $i
    38     mv $i".gz" $subPath
    39 done
    40 
    41 # clean log files which 7 days ago
    42 i=0
    43 while [[ i -lt 7 ]]
    44 do
    45     weekDay[i]=`date -d "-${i} day" +"%Y%m%d"`
    46     let i++
    47 done
    48 for dir in `ls | grep -E "([0-9]{8})"`
    49 do
    50     [[ "${weekDay[@]}" =~ $dir ]] || rm -rf $dir
    51 done
    作者:Standby一生热爱名山大川、草原沙漠,还有妹子
    出处:http://www.cnblogs.com/standby/

    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

  • 相关阅读:
    让Sendmail和Dovecot使用AD进行用户认证
    在dhcpd.conf中配置静态路由
    IPhone4与Exchange 2010同步失败
    /*从文本中读取文件*/
    EM数据包按规则更新
    Crystal Report Show in Web With ParameterField
    /*读取xml数据*/
    上传文件
    新的网站
    【练习】哥德巴赫猜想验证程序
  • 原文地址:https://www.cnblogs.com/standby/p/6791756.html
Copyright © 2011-2022 走看看