nginx 访问日志切割
#!/bin/bash
# nginx日志切片压缩备份
# Every hour running time.
# by bobliu.
# The Nginx logs path
log_year=$(date +"%Y")
log_month=$(date +"%m")
log_day=$(date +"%d")
log_hour=$(date +"%H")
log_min=$(date +"%M")
logs_path="/var/log/nginx/"
save_path="/data/nginx/log/${log_year}-${log_month}-${log_day}/"
#访问日志备份 (每小时)
if [ ! -d "$save_path" ]; then
mkdir -p "$save_path"
fi
gzip ${logs_path}access.log && mv ${logs_path}access.log.gz ${save_path}access_${log_year}${log_month}${log_day}_${log_hour}:${log_min}.log.gz
#mv ${logs_path}access.log ${save_path}access_${log_year}${log_month}${log_day}_${log_hour}_${log_min}.log && gzip ${save_path}access_${log_year}${log_month}${log_day}_${log_hour}_${log_min}.log
#错误日志备份
#if [ $log_hour = 00 ]; then
#mv ${logs_path}error.log ${save_path}error_$log_year$log_month$log_day.log
#fi
kill -USR1 `cat /run/nginx.pid`
定时任务:
0 * * * * sh /root/nginxlog.sh
* 3 * * * /usr/bin/find /data/nginx/log/ -type d -mtime +15 -name "2023*" | xargs rm -rf