Shell + crontab 实现日志压缩归档
Posted 青山应回首
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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
以上是关于Shell + crontab 实现日志压缩归档的主要内容,如果未能解决你的问题,请参考以下文章