nginx 日志分割,可以分割一年内没有分割的日志,以每天一个日志文件打包
转载请注明出处:
#!/bin/bash#hls_nginx_log_cut.sh#by Louis 2016/10/10logs_path='/data/store/logs/www/'#日志文件所在路径files=`ls $logs_path`backup_path='/data/store/backuplogs/'#备份存放目录month_array=(`date +%b` `date -d '1 month ago' +%b`)#当前月份和上月月份current_year=`date +%Y`#当前年份current_date=`date +%Y_%m_%d`#当前日期check_dir(){[ ! -d $backup_path ] && mkdir $backup_path}trans_month(){ case $1 in Jan) return 01;; Feb) return 02;; Mar) return 03;; Apr) return 04;; May) return 05;; Jun) return 06;; Jul) return 07;; Aug) return 08;; Sep) return 09;; Oct) return 10;; Nov) return 11;; Dec) return 12;; esac}#英文月份转换为数字月份,nginx日志中有两种日期,所以需要转换cut_history_logs(){ day=$1 for month in ${month_array[@]} do trans_month $month digit_month=`printf "%02d" $?` format_day=`printf "%02d" $day` dir_name=${current_year}_${digit_month}_${format_day} for file in $files do count=`awk '/'$format_day'\/'$month'\/'$current_year'|'$current_year'\/'$digit_month'\/'$format_day'/' ${logs_path}${file}|wc -w` #统计配到的设定日期的记录数量,如果count=0就跳到下一个文件 if [[ $count != 0 && $current_date != $dir_name ]];then [ ! -d /tmp/${dir_name} ] && mkdir /tmp/${dir_name} awk '/'$format_day'\/'$month'\/'$current_year'|'$current_year'\/'$digit_month'\/'$format_day'/' ${logs_path}${file}>>/tmp/${current_year}_${digit_month}_${format_day}/$file sed -ri '/'$format_day'\/'$month'\/'$current_year'|'$current_year'\/'$digit_month'\/'$format_day'/d' ${logs_path}${file} fi done [ -d /tmp/${dir_name} ] && { cd /tmp/${dir_name} tar czf /${backup_path}/${current_year}_${digit_month}_${format_day}_nginx.tar.gz * rm -rf /tmp/${dir_name} } done }tar_logs(){ for day in {1..31} do cut_history_logs $day done}check_dirtar_logs/usr/local/nginx/sbin/nginx -s reload