定时按天压缩Nginx日志并清理n天前的日志

Nginx日志每天定时压缩并清理n天前的日志

以下是shell脚本,脚本执行也会生成一个执行日志,路径自行更改。
脚本主要将指定日志目录的所有*.log文件按天压缩,并通过Nginx信号量控制重读日志
(kill -USR1 cat **/nginx.pid),最后删除n天以前的日志压缩文件

用crond添加定时任务

##不同用户有各自的定时任务
crontab -e
## 每天定时凌晨1点执行脚本
0 1 * * * sh (绝对路径)/cutNginxLog.sh
#!/bin/bash
##function: cut nginx log files

## set the log path for this shell Script
cutNginxLog_path="/applog/cutNginxLog"
cutNginxLog_log="/applog/cutNginxLog/cutNginxLog.log"

## set nginx log path and nginx pid path
nginx_log_path="/applog/nginx"
nginx_log_pid="/applog/nginx/nginx.pid"
nginx_log_dir=${nginx_log_path}

## set how long you want to save
save_days=7


###############################################
## Please do not modify the following Script ##
###############################################

## create the log path for this Script
### -x
if [ ! -x "$cutNginxLog_path" ];then
  mkdir "$cutNginxLog_path"
fi
### -d
if [ ! -d "$cutNginxLog_path" ];then
  mkdir "$cutNginxLog_path"
fi
### -f
if [ ! -d "$cutNginxLog_log" ];then
  touch "$cutNginxLog_log"
fi

echo "-------------------------------------------------------------" >> $cutNginxLog_log
echo "-------- start cut the log files $(date +"%Y-%m-%d %H:%M:%S") --------" >> $cutNginxLog_log
echo "-------------------------------------------------------------" >> $cutNginxLog_log

## print the number of day need to save
echo "save days is $save_days" >> $cutNginxLog_log

## get yesterday and print
YESTERDAY=$(date -d "yesterday" +"%Y-%m-%d")
echo "yesterday is $YESTERDAY" >> $cutNginxLog_log

### function: get the nginx log files you want to cut ###
function fGetLogFilesName()
{
    log_path=$1;
    log_fullFileNames=$(ls ${log_path}*.log);
    c=0;
    for log_fullFileName in $log_fullFileNames
    do
        log_fullFileName_tmp=${log_fullFileName##*/};
        log_filename=${log_fullFileName_tmp%.*};
        log_name[$c]="$log_filename";
        ((c++));
    done
    echo ${log_name[*]};
}
#########################################################

### function: print the array of log files name ###
function fPrintLogFilesName()
{
    log_name_arr=$1;
    for log_value in ${log_name_arr[*]}
    do
        echo "fileName: $log_value" >> $cutNginxLog_log
    done
}
###################################################

### function: cut nginx log files ###
#parameters:[1]arraySize,[2]logPath,[3]logNameArray
function fCutLog()
{
    log_num=$1;
    log_path=$2;
    log_name_arr=($3);
    for((i=0;i<$log_num;i++));
    do
        echo "mv ${log_path}${log_name_arr[i]}.log ${log_path}${log_name_arr[i]}_${YESTERDAY}.log result is " >> $cutNginxLog_log
        mv ${log_path}${log_name_arr[i]}.log ${log_path}${log_name_arr[i]}_${YESTERDAY}.log
        if [ $? -ne 0 ]; then
            echo "fail" >> $cutNginxLog_log
        else
            echo "success" >> $cutNginxLog_log
        fi
    done
}
#####################################

### function: Send USR1 signal to Nginx main process. The USR1 signal is to reopen the log file ###
function fSendTheSignalOfUSR1()
{
    pid_fullName=$1;
    echo "kill -USR1 $(cat ${pid_fullName}) result is " >> $cutNginxLog_log
    kill -USR1 $(cat ${pid_fullName})
    if [ $? -ne 0 ]; then
        echo "fail" >> $cutNginxLog_log
    else
        echo "success" >> $cutNginxLog_log
    fi
}
###################################################################################################

### function: gzip the yesterday log file ###
function fGzipLogFile()
{
    log_num=$1;
    log_path=$2;
    log_name_arr=($3);
    for((i=0;i<$log_num;i++));
    do
        echo "gzip -f ${log_path}${log_name_arr[i]}_${YESTERDAY}.log result is " >> $cutNginxLog_log
        gzip -f ${log_path}${log_name_arr[i]}_${YESTERDAY}.log
        if [ $? -ne 0 ]; then
            echo "fail" >> $cutNginxLog_log
        else
            echo "success" >> $cutNginxLog_log
        fi
    done
}
#############################################

### function: delete the save days ago nginx log files ###
function fDelDaysAgoLogFile()
{
    log_path=$1;
    echo "find $log_path -mtime +$save_days -type f -name \*.log.gz |xargs rm -f result is " >> $cutNginxLog_log
    find $log_path -mtime +$save_days -type f -name \*.log.gz |xargs rm -f
    if [ $? -ne 0 ]; then
        echo "fail" >> $cutNginxLog_log
    else
        echo "success" >> $cutNginxLog_log
    fi
}
##########################################################



## set nginx log files you want to cut
echo "------------------------------" >> $cutNginxLog_log
nginx_log_name=($(fGetLogFilesName $nginx_log_dir))

## print the value of nginx_log_name
echo "nginx log files[($nginx_log_path)] is " >> $cutNginxLog_log
fPrintLogFilesName "${nginx_log_name[*]}"

## print the number of nginx_log_name array
nginx_log_num=${#nginx_log_name[*]}
echo "nginx_log_name array size is $nginx_log_num" >> $cutNginxLog_log


## cut the nginx log files
echo "------------------------------" >> $cutNginxLog_log
fCutLog $nginx_log_num $nginx_log_path "${nginx_log_name[*]}"


## Send USR1 signal to Nginx main process
echo "------------------------------" >> $cutNginxLog_log
fSendTheSignalOfUSR1 $nginx_log_pid


## gzip the yesterday log file
echo "------------------------------" >> $cutNginxLog_log
fGzipLogFile $nginx_log_num $nginx_log_path "${nginx_log_name[*]}"


## delete the save days ago nginx log files
echo "------------------------------" >> $cutNginxLog_log
fDelDaysAgoLogFile $nginx_log_path


echo "-------------------------------------------------------------" >> $cutNginxLog_log
echo "--------- end cut the log files $(date +"%Y-%m-%d %H:%M:%S") ---------" >> $cutNginxLog_log
echo "-------------------------------------------------------------" >> $cutNginxLog_log
echo "#############################################################" >> $cutNginxLog_log

你可能感兴趣的:(shell)