首页 > 代码库 > Shell + crontab 实现日志压缩归档
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 do15 ip_tmp=`echo $ip | grep -v ‘^10\.‘`16 if [[ ! -z $ip_tmp ]];then17 break18 fi19 done20 exter_ip=$ip_tmp21 echo $exter_ip22 }23 IP=`get_exter_ip`24 # rename log file25 cd $logdir26 for i in `ls access.log_*.old`27 do28 DATE=`echo $i |awk -F "[-.]" ‘{print $(NF-4)$(NF-3)}‘ |sed ‘s/[hms]//g‘`29 mv $i live_${IP}_${DATE}_05min.log30 done31 32 subPath=$logdir/$TODAY/33 # gzip and archive log file34 [ ! -d $subPath ] && mkdir -p $subPath35 for i in `ls live_*.*_05min.log`36 do37 gzip $i38 mv $i".gz" $subPath39 done40 41 # clean log files which 7 days ago42 i=043 while [[ i -lt 7 ]]44 do45 weekDay[i]=`date -d "-${i} day" +"%Y%m%d"`46 let i++47 done48 for dir in `ls | grep -E "([0-9]{8})"`49 do50 [[ "${weekDay[@]}" =~ $dir ]] || rm -rf $dir51 done
Shell + crontab 实现日志压缩归档
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。