Azure Log Backup Scripts
Overview
This document provides scripts to automate the backup of logs to Azure Blob Storage. The scripts find and upload logs older than 24 hours and optionally delete them after uploading.
Installation
Before running the scripts, install the Azure CLI:
curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash
Script 1: Backup Logs from /var/www/logs
#!/bin/bash
set -xe
# Configuration
LOG_DIR="/var/www/logs"
AZURE_STORAGE_ACCOUNT=""
AZURE_CONTAINER_NAME=""
SAS_TOKEN=""
SERVER_IP="$(curl -s ifconfig.me)"
DATE="$(date +%Y-%m-%d)"
# Upload logs older than 24 hours from LOG_DIR
find "$LOG_DIR" -type f -mtime +0 | while read file; do
az storage blob upload \
--account-name "$AZURE_STORAGE_ACCOUNT" \
--container-name "$AZURE_CONTAINER_NAME" \
--file "$file" \
--name "DATE-$DATE/SERVER-IP-$SERVER_IP/$(basename "$file")" \
--sas-token "$SAS_TOKEN" \
--overwrite
done
# Optional: Delete original logs after upload (Uncomment if needed)
find "$LOG_DIR" -type f -mtime +0 -delete
echo "Backup completed successfully."
Script 2: Backup Logs from /var/log
#!/bin/bash
set -xe
# Configuration
LOG_DIR="/var/log"
AZURE_STORAGE_ACCOUNT=""
AZURE_CONTAINER_NAME=""
SAS_TOKEN=""
SERVER_IP="$(curl -s ifconfig.me)"
DATE="$(date +%Y-%m-%d)"
# Upload logs older than 24 hours
find "$LOG_DIR" -type f -mtime +0 | while read file; do
az storage blob upload \
--account-name "$AZURE_STORAGE_ACCOUNT" \
--container-name "$AZURE_CONTAINER_NAME" \
--file "$file" \
--name "DATE-$DATE/SERVER-IP-$SERVER_IP/$(basename "$file")" \
--sas-token "$SAS_TOKEN" \
--overwrite
done
# Optional: Delete original logs after upload (Uncomment if needed)
find "$LOG_DIR" -type f -mtime +0 -delete
echo "Backup completed successfully."
Notes
- Ensure that
AZURE_STORAGE_ACCOUNT
,AZURE_CONTAINER_NAME
, andSAS_TOKEN
are properly set. - Uncomment the delete command if you want logs to be removed after upload.
- The backup process organizes logs by date and server IP for easier tracking.
Conclusion
These scripts help automate log backups to Azure Blob Storage, ensuring efficient log management and retention.