BASH
Basic Log File Rotation and Archiving
Implement a straightforward bash script to rotate and archive application log files, preventing excessive disk usage and maintaining a clean server environment.
#!/bin/bash
LOG_DIR="/var/log/myapp" # Directory containing your application logs
LOG_FILE_PREFIX="access" # Prefix of your log files (e.g., access.log, error.log)
RETENTION_DAYS=7 # Number of days to keep rotated logs
ARCHIVE_DIR="${LOG_DIR}/archive"
mkdir -p "$ARCHIVE_DIR" # Ensure archive directory exists
echo "--- $(date +"%Y-%m-%d %H:%M:%S") --- Starting log rotation for $LOG_DIR"
# Find log files with the specified prefix
find "$LOG_DIR" -maxdepth 1 -type f -name "${LOG_FILE_PREFIX}*.log" | while read -r logfile; do
if [ -f "$logfile" ]; then
FILENAME=$(basename "$logfile")
TIMESTAMP=$(date +"%Y%m%d%H%M%S")
ARCHIVE_FILENAME="${FILENAME%.log}_${TIMESTAMP}.tar.gz"
echo "Rotating and archiving: $logfile"
tar -czf "$ARCHIVE_DIR/$ARCHIVE_FILENAME" "$logfile"
if [ $? -eq 0 ]; then
# Clear the original log file after successful archiving
echo "" > "$logfile"
echo "Archived '$FILENAME' to '$ARCHIVE_DIR/$ARCHIVE_FILENAME' and cleared original."
else
echo "Error: Failed to archive '$FILENAME'."
fi
fi
done
# Clean up old archives
echo "Cleaning up archives older than $RETENTION_DAYS days..."
find "$ARCHIVE_DIR" -type f -name "*.tar.gz" -mtime +"$RETENTION_DAYS" -delete
echo "--- $(date +"%Y-%m-%d %H:%M:%S") --- Log rotation complete."
How it works: This script efficiently manages log file rotation for applications. It identifies log files based on a specified prefix within a given directory, then archives them with a timestamp into gzipped tarballs. After successful archiving, the original log file is emptied. The script also includes a cleanup mechanism, deleting archives older than a defined retention period, which is crucial for preventing disk space exhaustion on servers.