Home >Operation and Maintenance >Nginx >How to configure nginx log scheduled backup and deletion

How to configure nginx log scheduled backup and deletion

WBOY
WBOYforward
2023-06-03 10:59:171517browse

一旦开启了nginx日志功能,每天nginx都会生成一定大小的日志文件,如果系统稳定运行,没有任何问题,那么日志基本上不会去查看。但这些日志如不及时清理,日渐积累,对服务器的磁盘空间占用也将是比较恐怖的。为了解决这个问题,利用shell脚本对nginx日志文件定时备份和删除,只保留一段时间。

图1:

How to configure nginx log scheduled backup and deletion

#!/bin/bash
#auth:lzq
#desc:把当前日志按日期备份,重新生成第二天的日志文件
#date:2016-09-18

date=`date +%y%m%d`
nginx_pid=`cat /var/run/nginx.pid`
#如果当前nginx没有运行就退出
if [ "$?" != 0 ]
then
    exit 1;
fi

#nginx 日志所在的路径
log_path='/usr/local/nginx/logs/'
log_name='access.log'
mv ${log_path}${log_name} ${log_path}${log_name}$date

#删除7天前旧的备份文件
function deloldbak()
{
    olddate=`date +"%y%m%d" -d "-$1 day"`
    if [ -e "${log_path}${log_name}$olddate" ]
    then
        rm -f ${log_path}${log_name}$olddate
        echo "${log_path}${log_name}$olddate del ok"
    fi
}

#重载nginx配置,重新生成nginx日志文件
kill -usr1 $nginx_pid

if [ "$?" == 0 ]
then
    deloldbak 7
    exit 0;
fi

The above is the detailed content of How to configure nginx log scheduled backup and deletion. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete