nginx日志切割脚本

#!/bin/bash

#Backup Nginx logs
Name=`hostname`
Log_Path="/logs/nginx"
Backup_Path="/data/Backup_Logs/nginx"
D=`date -d "yesterday" "+%F"`

test -d $Backup_Path/$D || mkdir -p $Backup_Path/$D

if [ -d $Log_Path ];then
    for log in `find $Log_Path/ -name "*.log"`
        do
            mv $log $Backup_Path/$D/ 
            if [ -f /usr/local/nginx/logs/nginx.pid ];then
                kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`
            else
                /usr/local/nginx/sbin/nginx -s reload
            fi
        done
    
    cd $Backup_Path/$D
    sleep 2
    tar -zcf $Name.tar.gz *.log && rm -rf *.log
    find $Backup_Path -type d -mtime +30 -exec rm -rf {} \;
else
    echo "The Nginx logs directory is not exists!"
    exit 0
fi

 

posted @ 2018-04-22 20:24  梦轻尘  阅读(200)  评论(0编辑  收藏  举报