码迷,mamicode.com
首页 > 系统相关 > 详细

Shell + crontab 实现日志压缩归档

时间:2017-05-01 14:16:56      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:style   crontab   sub   date   color   min   shell   tmp   net   

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

 

Shell + crontab 实现日志压缩归档

标签:style   crontab   sub   date   color   min   shell   tmp   net   

原文地址:http://www.cnblogs.com/standby/p/6791756.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!