Files
Heimdall/docker/scripts/backup-alarik.sh
T

140 lines
6.0 KiB
Bash
Raw Normal View History

#!/usr/bin/env bash
# ─── Heimdall — Alarik/S3 Mirror Backup (weekly) ────────────────
# Mirrors the heimdall bucket to a backup prefix with versioning.
# This captures snapshots, recordings, and other S3-stored data.
#
# Usage:
# ./backup-alarik.sh # defaults from env / .env
# ./backup-alarik.sh --dry-run # show what would be done
# BACKUP_RETENTION_WEEKS=8 ./backup-alarik.sh
#
# Required env vars:
# ALARIK_ENDPOINT (or S3_ENDPOINT), ALARIK_ACCESS_KEY (or S3_ACCESS_KEY),
# ALARIK_SECRET_KEY (or S3_SECRET_KEY), ALARIK_BUCKET (default: heimdall)
#
# Optional:
# BACKUP_RETENTION_WEEKS (default: 12)
# DRY_RUN (set to 1 for dry-run)
# ──────────────────────────────────────────────────────────────────
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
PROJECT_DIR="$(cd "$SCRIPT_DIR/../.." && pwd)"
# ── Load .env if present ────────────────────────────────────────
ENV_FILE="${PROJECT_DIR}/docker/.env"
if [[ -f "$ENV_FILE" ]]; then
set -a
# shellcheck disable=SC1091
source "$ENV_FILE"
set +a
fi
# ── Configuration ───────────────────────────────────────────────
S3_ENDPOINT="${ALARIK_ENDPOINT:-${S3_ENDPOINT:-}}"
S3_ACCESS_KEY="${ALARIK_ACCESS_KEY:-${S3_ACCESS_KEY:-}}"
S3_SECRET_KEY="${ALARIK_SECRET_KEY:-${S3_SECRET_KEY:-}}"
S3_BUCKET="${ALARIK_BUCKET:-${S3_BUCKET:-heimdall}}"
BACKUP_PREFIX="backup/alarik"
RETENTION_WEEKS="${BACKUP_RETENTION_WEEKS:-12}"
TIMESTAMP="$(date -u +%Y-%m-%dT%H-%M-%SZ)"
WEEK_TAG="$(date -u +%Y-W%V)"
IS_DRY_RUN="${DRY_RUN:-0}"
# ── Helpers ─────────────────────────────────────────────────────
log() { echo "[alarik-backup] $(date -u +%H:%M:%S) $*"; }
warn() { log "WARN: $*"; }
die() { log "FATAL: $*"; exit 1; }
# ── Preflight ───────────────────────────────────────────────────
[[ -z "$S3_ENDPOINT" ]] && die "S3_ENDPOINT not set — cannot mirror Alarik"
if command -v mc >/dev/null 2>&1; then
CLIENT="mc"
mc alias set heimdall-backup "$S3_ENDPOINT" "$S3_ACCESS_KEY" "$S3_SECRET_KEY" --api S3v4 2>/dev/null
elif command -v aws >/dev/null 2>&1; then
CLIENT="aws"
else
die "Neither 'mc' (MinIO client) nor 'aws' CLI found"
fi
# ── Step 1: Mirror bucket to backup prefix ──────────────────────
log "Mirroring s3://${S3_BUCKET} → s3://${S3_BUCKET}/${BACKUP_PREFIX}/${WEEK_TAG}/"
if [[ "$IS_DRY_RUN" == "1" ]]; then
log "[DRY-RUN] Would mirror all objects from ${S3_BUCKET} to ${S3_BUCKET}/${BACKUP_PREFIX}/${WEEK_TAG}/"
else
if [[ "$CLIENT" == "mc" ]]; then
# mc mirror: copies new/changed objects, skips unchanged
# --overwrite: always copy (we want a point-in-time snapshot)
# --exclude "backup/*": don't mirror our own backups (avoid recursion)
mc mirror \
"heimdall-backup/${S3_BUCKET}" \
"heimdall-backup/${S3_BUCKET}/${BACKUP_PREFIX}/${WEEK_TAG}" \
--overwrite \
--exclude "backup/*" \
2>&1 | tail -10
else
# AWS CLI: sync with exclusion
aws --endpoint-url "$S3_ENDPOINT" s3 sync \
"s3://${S3_BUCKET}" \
"s3://${S3_BUCKET}/${BACKUP_PREFIX}/${WEEK_TAG}" \
--exclude "backup/*" \
2>&1 | tail -10
fi
log "Mirror complete"
fi
# ── Step 2: Prune old weekly backups ────────────────────────────
CUTOFF_WEEK=$(date -u -d "${RETENTION_WEEKS} weeks ago" +%Y-W%V 2>/dev/null || \
date -u -v-"${RETENTION_WEEKS}"w +%Y-W%V 2>/dev/null || echo "")
if [[ -n "$CUTOFF_WEEK" ]]; then
log "Pruning weekly backups older than ${RETENTION_WEEKS} weeks (cutoff: ${CUTOFF_WEEK})..."
if [[ "$IS_DRY_RUN" != "1" ]]; then
if [[ "$CLIENT" == "mc" ]]; then
mc ls "heimdall-backup/${S3_BUCKET}/${BACKUP_PREFIX}/" 2>/dev/null | \
awk '{print $NF}' | \
tr -d '/' | \
while read -r week; do
if [[ -n "$week" && "$week" < "$CUTOFF_WEEK" ]]; then
log " Deleting: ${BACKUP_PREFIX}/${week}/"
mc rm --recursive --force "heimdall-backup/${S3_BUCKET}/${BACKUP_PREFIX}/${week}/" 2>/dev/null || true
fi
done
fi
fi
fi
# ── Step 3: Create manifest ────────────────────────────────────
MANIFEST_KEY="${BACKUP_PREFIX}/${WEEK_TAG}/MANIFEST.txt"
if [[ "$IS_DRY_RUN" != "1" ]]; then
log "Creating manifest..."
OBJECT_COUNT=0
TOTAL_SIZE=0
if [[ "$CLIENT" == "mc" ]]; then
STATS=$(mc ls "heimdall-backup/${S3_BUCKET}/${BACKUP_PREFIX}/${WEEK_TAG}/" --recursive --summarize 2>/dev/null | tail -2)
OBJECT_COUNT=$(echo "$STATS" | grep -oP 'Objects: \K\d+' || echo "0")
TOTAL_SIZE=$(echo "$STATS" | grep -oP 'Size: \K\d+' || echo "0")
fi
MANIFEST="Heimdall Alarik Backup Manifest
================================
Week: ${WEEK_TAG}
Created: ${TIMESTAMP}
Bucket: ${S3_BUCKET}
Objects: ${OBJECT_COUNT}
Total: ${TOTAL_SIZE} bytes
"
if [[ "$CLIENT" == "mc" ]]; then
echo "$MANIFEST" | mc pipe "heimdall-backup/${S3_BUCKET}/${MANIFEST_KEY}" 2>/dev/null
else
echo "$MANIFEST" | aws --endpoint-url "$S3_ENDPOINT" s3 cp - "s3://${S3_BUCKET}/${MANIFEST_KEY}" 2>/dev/null
fi
log "Manifest written: ${MANIFEST_KEY}"
fi
log "Alarik backup complete ✓"
log " Location: s3://${S3_BUCKET}/${BACKUP_PREFIX}/${WEEK_TAG}/"