nginx日志分割和定时清理脚本

首先是日志定时分割脚本

#@/bin/sh

dat=`date +"%Y%m%d" `
mon=`date +"%Y%m"`
echo $dat
mondir="/usr/openResty/logs/$mon"
/bin/echo $mondir #>> /usr/openRest/hello.info
if [ ! -x "$mondir" ]; then 
    echo "开始创建日期文件夹"
    mkdir "$mondir"
    echo "创建日期文件夹结束"
fi
#/bin/echo ` ls -al $mon` >> /usr/openRest/hello.info
mv /usr/openResty/logs/error_90.log $mondir/error_$dat.log
kill -USR1 `cat /usr/local/openresty/nginx/logs/nginx.pid`

/bin/echo "done " # >> /usr/openRest/hello.info

  其次是定期清理脚本

#/bin/sh
shellinfo=/usr/openResty/hello.info
logdat=` date -d " -1 month" +%Y%m `
/bin/echo $logdat >> $shellinfo
lastlogdat=` date -d " -2 month" +%Y%m`
/bin/echo $lastlogdat >> $shellinfo
logPath="/usr/openResty/logs/"
lastlogTar="${logPath}${lastlogdat}.tar.gz"
/bin/echo $logPath >> $shellinfo
/bin/echo "over" $lastlogTar >> $shellinfo
if [ -x "$logPath$logdat" ]; then
        /bin/echo "开始压缩上月日志文件" >> $shellinfo
        /bin/echo $logPath$logdat "待压缩的文件夹" >> $shellinfo
        tar -zcvf $logPath$logdat.tar.gz $logPath$logdat

        /bin/echo  `ls -al /usr/openResty/` >> $shellinfo
        /bin/echo "换行 开始删除上个月的文件夹" >> $shellinfo
        rm -rf $logPath$logdat
        /bin/echo "换行 删除上个月的文件夹完毕" >> $shellinfo
fi

if [ -a "$lastlogTar" ]; then
        /bin/echo "开始删除上上个月的日志压缩包" >> $shellinfo
        rm -rf $lastlogTar
        /bin/echo "删除上上个月的日志压缩包完毕" >> $shellinfo
fi

crontab -e 根据需要添加定时任务

容器中的nginx日志分割请参考这里

作者:first_semon
         
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。如有问题,欢迎交流
原文地址:https://www.cnblogs.com/first-semon/p/12957492.html