Add persistent partner conf backup across reboots

conf_cache_save.sh runs first on array stop — snapshots partner confs from RAM
cache to /boot/config/varaverk/conf_bak/ before anything else shuts down.

conf_cache_restore.sh runs after conf_sync.sh on array start — if partner was
unreachable and RAM cache is incomplete, loads the backup into RAM then removes
it. Normal reboots: backup written, fresh pull succeeds, backup deleted unused.
Edge case (partner down at boot): backup fills the gap so fallback.sh has the
partner vars it needs to operate correctly.
This commit is contained in:
Gmer4Lfe
2026-06-19 17:40:46 -04:00
parent ad98d41041
commit 7332eb81e0
4 changed files with 144 additions and 0 deletions
+77
View File
@@ -0,0 +1,77 @@
#!/bin/bash
# ==============================================================================================
# ============================= Conf Cache Restore =============================================
# ==============================================================================================
#
# PURPOSE
# ─────────────────────────────────────────────────────────────────────────────
# Runs at array start after conf_sync.sh. Checks whether partner confs made it
# into the RAM cache. If any are missing (partner was unreachable at boot) and
# a persistent backup exists from the previous shutdown, loads the missing confs
# from backup into the RAM cache so scripts like fallback.sh have partner vars.
#
# Always removes the persistent backup when done — used or not.
#
# Normal reboot (partner up):
# conf_sync.sh pulls fresh → RAM cache complete → backup not needed → removed
#
# Edge case (partner down at boot):
# conf_sync.sh fails to pull → RAM cache missing partner → backup loaded into
# RAM → backup removed → fallback.sh runs with last-known-good partner vars
#
# Own conf is never in the backup — it's always on disk.
# ==============================================================================================
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/../load_config.sh"
parse_args "$@"
detect_hosts
RAM_CACHE="/tmp/.vv/config/cached/.confs"
SAVE_DIR="/boot/config/varaverk/conf_bak"
[[ "$DRY_RUN" == true ]] && warn "DRY RUN — no changes will be made"
if [[ ! -d "$SAVE_DIR" ]]; then
log "No persistent conf backup found — nothing to restore"
exit 0
fi
restored=0
for conf in "$SAVE_DIR"/host*.conf; do
[[ -f "$conf" ]] || continue
base="$(basename "$conf")"
[[ "${base,,}" == "${MY_ID,,}.conf" ]] && continue
if [[ -f "$RAM_CACHE/$base" ]]; then
log "$base already in RAM cache (conf_sync succeeded) — skipping"
continue
fi
if [[ "$DRY_RUN" == true ]]; then
warn "DRY RUN — would restore $base → RAM cache"
(( restored++ ))
continue
fi
mkdir -p "$RAM_CACHE"
if cp "$conf" "$RAM_CACHE/$base"; then
echo "Restored $base from persistent backup → RAM cache ✅"
(( restored++ ))
else
warn "Failed to restore $base"
fi
done
[[ "$restored" -gt 0 ]] && \
warn "Loaded $restored partner conf(s) from persistent backup — partner was unreachable at array start"
if [[ "$DRY_RUN" == false ]]; then
rm -rf "$SAVE_DIR"
log "Persistent conf backup cleared"
else
warn "DRY RUN — would remove $SAVE_DIR"
fi
exit 0