简介:
在服务器管理中,对于Nginx日志的切割和MySQL数据库的定期备份是非常重要的任务。本文将介绍如何编写一个用于Nginx日志切割的脚本,并提供一个用于定期备份MySQL数据库的脚本。
Nginx日志切割脚本:
#!/bin/bash
# Nginx日志切割脚本
# 配置参数
log_dir="/var/log/nginx" # 日志文件目录
date_format=$(date +"%Y%m%d") # 日期格式,用于生成切割后的文件名
# 切割access.log
access_log_file="${log_dir}/access.log"
access_log_backup="${access_log_file}.${date_format}.bak"
if [ -f "$access_log_file" ]; then
mv "$access_log_file" "$access_log_backup"
kill -USR1 $(cat /var/run/nginx.pid)
echo "Nginx access.log has been successfully rotated."
else
echo "Nginx access.log file does not exist."
fi
# 切割error.log
error_log_file="${log_dir}/error.log"
error_log_backup="${error_log_file}.${date_format}.bak"
if [ -f "$error_log_file" ]; then
mv "$error_log_file" "$error_log_back
本文介绍了如何编写Nginx日志切割和MySQL数据库定期备份的脚本,包括日志重命名及MySQL全库备份与压缩。通过执行这些脚本并配置定时任务,可以提升服务器的管理和安全性。
订阅专栏 解锁全文
110

被折叠的 条评论
为什么被折叠?



