BASH

Archive and Clean Old Log Files

Maintain server health and free up disk space by automatically archiving and removing old log files, ensuring efficient log management.

#!/bin/bash
# Configuration
LOG_DIR="/var/log/nginx" # Directory containing log files to manage
ARCHIVE_DIR="/var/log/nginx/archive" # Directory to store archived logs
RETENTION_DAYS=30 # Logs older than this will be archived and removed
# Find patterns for files to archive (e.g., access.log, error.log, *.log)
LOG_FILE_PATTERNS=("access.log" "error.log" "*.log")

# Create archive directory if it doesn't exist
mkdir -p "$ARCHIVE_DIR"

echo "Starting log file management for $LOG_DIR..."
TIMESTAMP=$(date +%Y%m%d%H%M%S)

for PATTERN in "${LOG_FILE_PATTERNS[@]}"; do
    echo "Processing pattern: $PATTERN"
    # Find files older than RETENTION_DAYS
    find "$LOG_DIR" -maxdepth 1 -type f -name "$PATTERN" -mtime +$RETENTION_DAYS -print0 | while IFS= read -r -d $'\0' file; do
        if [ -f "$file" ]; then
            FILENAME=$(basename "$file")
            ARCHIVE_PATH="$ARCHIVE_DIR/${FILENAME}_${TIMESTAMP}.gz"

            echo "Archiving $file to $ARCHIVE_PATH..."
            gzip -c "$file" > "$ARCHIVE_PATH"
            if [ $? -eq 0 ]; then
                echo "Removing original file $file..."
                rm "$file"
            else
                echo "ERROR: Archiving $file failed. Original file not removed."
            fi
        fi
    done
done

# Optional: Clean up archives older than, say, 90 days from ARCHIVE_DIR
# echo "Cleaning up archives older than 90 days..."
# find "$ARCHIVE_DIR" -type f -mtime +90 -delete

echo "Log file management finished."
How it works: This script helps manage log files by archiving old logs and removing the originals to free up disk space. It identifies files in a specified `LOG_DIR` that match certain `LOG_FILE_PATTERNS` and are older than `RETENTION_DAYS`. These files are then compressed using `gzip` and moved to an `ARCHIVE_DIR`, with the original files being deleted.

Need help integrating this into your project?

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

Hire DigitalCodeLabs