Shell + crontab 實現日誌壓縮歸檔

來源:互聯網
上載者:User

標籤: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     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個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.