Nginx日志切分

1.介绍

   nginx 产生的日志本身不提供切割功能,所以需要自己来实现.

2.配置

2.1 切割并压缩 vim cut_log.sh

cut_log()

{
    domain=$1
    # The Nginx logs path
    logs_path="/home/nginx_logs/"
    logs_name=${domain}'_access.log'
    now_time=$(date -d "${Time}" +%s)
    new_logs_name=${domain}'.'$now_time
    old_full_logs=${logs_path}${logs_name}
    new_full_logs=${logs_path}${new_logs_name}
    compress_log_name=$new_logs_name".tar.gz"
    TODAY=$(date +%F)
    LOG_DIR='/home/logs/'$TODAY

    [ -d $LOG_DIR ];mkdir -p $LOG_DIR

    mv $old_full_logs $new_full_logs
    kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`

    cd $logs_path

    tar zcf $compress_log_name $new_logs_name
    md5=`/usr/bin/md5sum $new_logs_name.tar.gz |awk '{print $1}'`
    mv ${new_logs_name}'.'tar.gz ${new_logs_name}'.'$md5.tar.gz
    mv ${new_logs_name}'.'$md5.tar.gz $LOG_DIR
    rm -rf ${new_logs_name}
}

for name in s.ipd.com r.ipd.com
do
    cut_log $name

done

2.2 切割 vim cut_log.sh

#!/bin/bash
Time=$(date +%Y%m%d --date='-1 day')
logs_path="/home/qiyi/tools/nginx-1.6.2/logs/"
logs_name="access.log"
new_logs_name=${logs_name}'_'$Time
old_full_logs=${logs_path}${logs_name}
new_full_logs=${logs_path}${new_logs_name}
mv $old_full_logs $new_full_logs
kill -USR1 `cat /home/qiyi/tools/nginx-1.6.2/logs/nginx.pid`
find ${logs_path} -type f -ctime +7 -name "access.log_*" -exec rm -f {} \;


3. 编辑crontab定时任务

 5 0 * * * /home/shell/cut_log.sh > /dev/null 2>&1

你可能感兴趣的:(Nginx日志切分)