BASH

Automate Log File Rotation and Compression

Implement a bash script to automatically rotate, compress, and archive web server or application log files, optimizing disk usage and simplifying log management.

#!/bin/bash

# Configuration
LOG_DIR="/var/log/nginx" # Directory containing log files
LOG_FILE_PATTERN="access.log" # Pattern for log files to rotate (e.g., '*.log', 'access.log')
RETENTION_DAYS=30 # Number of days to keep compressed logs

echo "Starting log rotation and compression in $LOG_DIR"

# Find log files matching the pattern and rotate/compress them
find $LOG_DIR -maxdepth 1 -type f -name "$LOG_FILE_PATTERN" ! -name "*.gz" | while read LOG_FILE; do
    if [ -f "$LOG_FILE" ]; then
        TIMESTAMP=$(date +%Y%m%d_%H%M%S)
        NEW_LOG_FILE="${LOG_FILE%.log}.${TIMESTAMP}.log"
        COMPRESSED_LOG_FILE="${NEW_LOG_FILE}.gz"

        echo "Rotating and compressing $LOG_FILE to $COMPRESSED_LOG_FILE"
        mv "$LOG_FILE" "$NEW_LOG_FILE" || { echo "Error: Failed to move $LOG_FILE"; continue; }
        gzip "$NEW_LOG_FILE" || { echo "Error: Failed to compress $NEW_LOG_FILE"; continue; }

        # Optional: create a new empty log file for the application to write to
        # touch "$LOG_FILE" || { echo "Error: Failed to create new log file $LOG_FILE"; }
        # Note: For many services like Nginx, a 'logrotate' configuration handles this more robustly.
    fi
done

# Clean up old compressed logs
echo "Cleaning up old compressed logs (older than $RETENTION_DAYS days)..."
find $LOG_DIR -type f -name "${LOG_FILE_PATTERN}*.gz" -mtime +$RETENTION_DAYS -delete
echo "Log rotation and cleanup complete."
How it works: This script automates the rotation and compression of log files within a specified directory. It identifies log files matching a given pattern, renames them with a timestamp, and then compresses them using `gzip`. This helps in managing disk space. Finally, it cleans up old compressed log archives based on a defined retention period. While specialized tools like `logrotate` are often preferred for critical services, this script demonstrates the core logic in bash for custom needs.

Need help integrating this into your project?

Our team of expert developers can help you build your custom application from scratch.

Hire DigitalCodeLabs