When running a website you may end up with Apache access and error logs that end up being gigabytes in since when they are not correctly rotated. This issue can be solved by using scripts to automate the rotation of log files. Below is a Bash script that solves this problem on Linux.
!/bin/bash
# location of your log file
ACCESSFILE="/var/logs/mywebsite.com/access.log"
# Location of your error file
ERRORFILE="/var/logs/mywebsite.com/error.log"
# Location where your backups will be stored
BACKUP_DIR="/var/logs/mywebsite.com/backup/"
# Current date for file renaming
DATE=$(date +"%Y%m%d_%H%M%S")
# Create a backup of the log file
cp $ACCESSFILE $BACKUP_DIR/access.log.$DATE
cp $ERRORFILE $BACKUP_DIR/error.log.$DATE
#Truncate access log file
echo "" > $ACCESSFILE
#Truncate error log file
echo "" > $ERRORFILE
Please follow and like us: