帮我写一个脚本分割Nginx的access.log日志并保留30天一个月时长,自动删除多余的日志
时间: 2023-02-25 17:01:47 浏览: 102
你好!下面是一个可以分割 Nginx 的 access.log 日志并保留 30 天一个月时长,自动删除多余的日志的 bash 脚本示例:
```
#!/bin/bash
# This script is used to split Nginx access.log and keep the logs for the last 30 days
# Define the log file location
log_file=/var/log/nginx/access.log
# Define the backup directory
backup_dir=/var/log/nginx/backup
# Create the backup directory if it doesn't exist
if [ ! -d "$backup_dir" ]; then
mkdir -p "$backup_dir"
fi
# Get the current date and time
current_date=$(date +%Y-%m-%d)
# Split the log file and save it in the backup directory
cp "$log_file" "$backup_dir/access-$current_date.log"
truncate -s 0 "$log_file"
# Find the log files older than 30 days and delete them
find "$backup_dir" -name "access-*.log" -mtime +30 -exec rm {} \;
```
使用该脚本,您可以通过计划任务定期运行它,以确保它在每天的同一时间运行。希望对您有所帮助!
阅读全文