BASH

Daily Web Server Log Archiving

Automate daily archiving and compression of web server access and error logs, reducing disk space usage and organizing logs for easier historical analysis.

#!/bin/bash

# Define log directories and archive root
LOG_DIR_NGINX="/var/log/nginx/"
LOG_DIR_APACHE="/var/log/apache2/"
ARCHIVE_ROOT="/var/log/archives/weblogs/" # Central archive location
DATE=$(date +%Y%m%d)

# Ensure archive directory exists
mkdir -p "$ARCHIVE_ROOT"

# --- Nginx Log Archiving ---
LOG_FILES_NGINX=("${LOG_DIR_NGINX}access.log" "${LOG_DIR_NGINX}error.log")
for LOG_PATH in "${LOG_FILES_NGINX[@]}"; do
    if [ -f "$LOG_PATH" ]; then
        FILENAME=$(basename "$LOG_PATH")
        ARCHIVE_PATH="${ARCHIVE_ROOT}nginx-${FILENAME}-${DATE}.gz"

        echo "Archiving Nginx log: $LOG_PATH"

        # Move the current log file to a temporary name
        sudo mv "$LOG_PATH" "${LOG_PATH}.old"
        # Compress the old log file
        sudo gzip "${LOG_PATH}.old"
        # Move the compressed log to the archive directory
        sudo mv "${LOG_PATH}.old.gz" "$ARCHIVE_PATH"
        # Recreate an empty log file with correct permissions and ownership
        sudo touch "$LOG_PATH"
        sudo chmod 640 "$LOG_PATH"
        # Adjust ownership (e.g., www-data:adm for Debian/Ubuntu, nginx:nginx for RHEL/CentOS)
        sudo chown www-data:adm "$LOG_PATH"
        echo "Archived $LOG_PATH to $ARCHIVE_PATH"
    else
        echo "Nginx log file $LOG_PATH not found, skipping." >&2
    fi
done

# --- Apache Log Archiving (uncomment and adjust if using Apache) ---
# LOG_FILES_APACHE=("${LOG_DIR_APACHE}access.log" "${LOG_DIR_APACHE}error.log")
# for LOG_PATH in "${LOG_FILES_APACHE[@]}"; do
#     if [ -f "$LOG_PATH" ]; then
#         FILENAME=$(basename "$LOG_PATH")
#         ARCHIVE_PATH="${ARCHIVE_ROOT}apache-${FILENAME}-${DATE}.gz"
#         echo "Archiving Apache log: $LOG_PATH"
#         sudo mv "$LOG_PATH" "${LOG_PATH}.old"
#         sudo gzip "${LOG_PATH}.old"
#         sudo mv "${LOG_PATH}.old.gz" "$ARCHIVE_PATH"
#         sudo touch "$LOG_PATH"
#         sudo chmod 640 "$LOG_PATH"
#         sudo chown www-data:adm "$LOG_PATH" # Or apache:apache for RHEL/CentOS
#         echo "Archived $LOG_PATH to $ARCHIVE_PATH"
#     else
#         echo "Apache log file $LOG_PATH not found, skipping." >&2
#     fi
# done

# Remove archives older than 30 days
echo "Removing archives older than 30 days from $ARCHIVE_ROOT..."
find "$ARCHIVE_ROOT" -type f -name "*.gz" -mtime +30 -delete

echo "Log archiving and cleanup completed."
How it works: This script automates the daily archiving and compression of web server log files (e.g., Nginx access and error logs). It moves the current log file to a temporary name, compresses it, moves the gzipped file to a dated archive directory, and then recreates an empty log file with correct permissions and ownership for ongoing server operations. It also includes a cleanup step to remove old archives (e.g., older than 30 days), preventing excessive disk usage.

Need help integrating this into your project?

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

Hire DigitalCodeLabs