nginx日志切割并备份

[root@lecode-pre55 bin]# cat nginx-log.sh 
#!bin/bash
#auther:ansheng
#desc: nginx日志备份,注意脚本中文件的路径。

#备份以时间命名。每天晚上两点执行备份。时间减1
date=`date -d "-1day" +%F`

#日志切割
cd /data/servers/logs/nginx
if [ ! -d log-bak ] ; then
        mkdir -p log-bak
fi

#不同的环境需要更换日志名称
mv access.log log-bak/access_$date.log 
mv error.log log-bak/error_$date.log
mv 4-layer-proxy.log  log-bak/4-layer-proxy_$date.log
mv ajsh-pre-access.log  log-bak/ajsh-pre-acces__$date.log
mv lecode-pre-access.log log-bak/lecode-pre-access_$date.log

#发送信号。注意pid文件路径
kill -USR1 $(cat /usr/local/nginx/logs/nginx.pid)

#打包日志
tar zcf log-bak/$date.tar.gz   /data/servers/logs/nginx/log-bak/*.log


#删除旧的备份文件
find /data/servers/logs/nginx/log-bak  -type  f  -name  '*.tar.gz'  -mtime +7  |xargs rm -f
find /data/servers/logs/nginx/log-bak  -type  f  -name  '*.log'  |xargs rm -f

#写入定时任务
[root@lecode-pre55 bin]# crontab -l
00 02 * * * /usr/bin/bash /root/bin/nginx-log.sh &>/dev/null

posted @ 2022-11-02 09:58  安生丶  阅读(190)  评论(0编辑  收藏  举报