#!/bin/bash
# Host-level restic backup for azkaban -> Backblaze B2 (b2:ctg-backups:/azkaban/).
#
# SCOPE: host configuration only. Guest disks live on zvols under /dev/zvol and
# are handled by vzdump (job `az-guests`), not here.
#
# /var/lib/vz/dump IS included, so the nightly guest images produced by the
# `az-guests` vzdump job reach B2. This is the only offsite copy of the router
# config, DHCP leases and authoritative DNS records.
#
# Two guests are excluded, both for upload volume rather than importance:
#   102 captainjack - 257 GB of incompressible LUKS data.
#   101 bastion     - ~25-35 GB compressed. Each nightly vzdump is a fresh
#                     archive, so compressed output dedupes poorly and restic
#                     would push that much EVERY night; with keep-daily 7 the
#                     repo would run to hundreds of GB over a home uplink.
# Net effect: only the small guests (100 OpenWrt, 103/104 the DNS pair) go
# offsite -- roughly 2.5 GB nightly. Both excluded guests still have local
# nightly images under keep-last=3.
#
# NOTE on VMIDs: they were reassigned during the migration from kingscrossstation.
# Here 100 is OpenWrt (tiny) and 102 is captainjack (huge) -- the reverse of an
# earlier assumption. Verify with `qm list` before changing these excludes; getting
# it backwards means either skipping the router or uploading 257 GB.
#
# Run via systemd (restic-backup.service, package ctg-pve-backup), which applies resource caps.
#
# PLATFORM (2026-11-27, azkaban Proxmox -> Horcrux): the same script and the SAME B2 repo (ctg-backups:/azkaban) on both.
# `ctg-guestctl platform` decides at run time (absent or unknown = the Proxmox behaviour below, unchanged):
#   proxmox: /etc /root /var/lib/pve-cluster + the vzdump directory.
#   horcrux: /etc (incl. /etc/horcrux) /root + a state dump (guest/container configs, backup jobs, declarative export,
#            consistent copies of the manager's databases: `ctg-guestctl state-dump`) + every guest-backup directory the
#            manager reports (`ctg-guestctl dump-dirs`, e.g. /bulk/dump/dump). Never the live /var/lib/horcrux files.
#   Extra excludes for dump archives: CTG_DUMP_EXCLUDE="glob glob" in /etc/default/pve-backup.

set -euo pipefail

source /root/.restic-env
export HOME=/root

PLAT=proxmox
if command -v ctg-guestctl >/dev/null 2>&1; then
    PLAT=$(ctg-guestctl platform 2>/dev/null || echo proxmox)
fi

if [ "$PLAT" = horcrux ]; then
    STATE_DIR=${CTG_HORCRUX_STATE_DIR:-/var/lib/ctg-backup/horcrux-state}
    rm -rf "$STATE_DIR"
    ctg-guestctl state-dump "$STATE_DIR"
    paths=(/etc /root /usr/local/bin /usr/local/sbin /var/spool/cron "$STATE_DIR")
    while read -r d; do [ -n "$d" ] && paths+=("$d"); done < <(ctg-guestctl dump-dirs)
    ex=()
    for g in ${CTG_DUMP_EXCLUDE:-}; do ex+=(--exclude "$g"); done
    existing=()
    for p in "${paths[@]}"; do [ -e "$p" ] && existing+=("$p"); done
    exec restic \
        --password-file /root/.resticpwd \
        --verbose \
        --retry-lock "${CTG_RETRY_LOCK:-3h}" \
        --limit-upload "${CTG_LIMIT_UPLOAD:-6144}" \
        backup \
        --exclude-caches \
        --exclude /root/.cache \
        --exclude /root/.restic-env \
        --exclude /root/.resticpwd \
        "${ex[@]}" \
        "${existing[@]}"
fi

# Flag placement: --password-file/--option/--verbose are GLOBAL and must precede
# the subcommand; --exclude*/--read-concurrency belong to `backup` and follow it.
exec restic \
    --password-file /root/.resticpwd \
    --verbose \
    --retry-lock "${CTG_RETRY_LOCK:-3h}" \
    --limit-upload "${CTG_LIMIT_UPLOAD:-6144}" \
    backup \
    --exclude-caches \
    --exclude /root/.cache \
    --exclude /root/.restic-env \
    --exclude /root/.resticpwd \
    --exclude /etc/pve/.rrd \
    --exclude /etc/pve/.clusterlog \
    --exclude /etc/pve/.vmlist \
    --exclude /etc/pve/.members \
    --exclude /etc/pve/.version \
    --exclude '/var/lib/vz/dump/vzdump-qemu-101-*' \
    --exclude '/var/lib/vz/dump/vzdump-qemu-102-*' \
    /etc /root /var/lib/pve-cluster /usr/local/bin /usr/local/sbin /var/spool/cron /var/lib/vz/dump
