Files
Varaverk/System_Essentials/conf_cache_save.sh
T
Gmer4Lfe 6623d1e776 Fix dead/incorrect vars and consolidate duplicated logic into common.sh
Codebase-wide audit pass: fixed real bugs (SSH hangs missing BatchMode,
local-outside-function no-ops, variable name collisions, a truncated
ratio calc, wrong state-dir path, DARK vs NO_INTERNET drift, and more),
then pulled logic that was duplicated across multiple scripts — arr
cleanup safety gates, docker restart ordering, container maintenance
stop/restart, watchdog state-file helpers, partnership role resolution,
cert expiry checks, remote node discovery, and TMDB discovery scoring —
into common.sh so each now has a single implementation.
2026-07-03 23:52:33 -04:00

86 lines
3.4 KiB
Bash
Executable File

#!/bin/bash
# ==============================================================================================
# ============================= Conf Cache Save ================================================
# ==============================================================================================
#
# PURPOSE
# ─────────────────────────────────────────────────────────────────────────────
# Snapshots the partner conf RAM cache to $PERSISTENT_CONF_CACHE on
# array stop. Survives reboot. Used by conf_cache_restore.sh at next array start
# to reload partner vars into RAM when the partner is unreachable at boot time.
#
# Runs as the first step in ARRAY_STOP_SCRIPTS — while the RAM cache is fresh
# and before anything else changes. If partner is reachable at next start,
# conf_sync.sh gets a fresh copy and the backup is never used. If partner is
# down, the backup fills the gap so fallback.sh has the vars it needs.
#
# Only partner confs are saved — own conf is always on disk.
# Path adapts to storage mode: $SCRIPTS_DIR/.cache/vv/d (internal or appdata).
#
# ==============================================================================================
# DESIGN PRINCIPLES
# ==============================================================================================
#
# Save at Shutdown, Not at Boot
# The backup is created at array stop while the RAM cache is at its freshest.
# By the next boot the partner may be unreachable — but the conf is already
# preserved and available the moment conf_cache_restore.sh runs.
#
# Partner Only
# Own conf is on disk — always present, never needs saving. Only partner confs
# live in RAM and can be missing at the next boot.
#
# ==============================================================================================
# OPERATIONAL SAFEGUARDS
# ==============================================================================================
#
# require_partnership — exits early if PARTNERSHIP_ENABLED=false
# detect_hosts() — determines which confs to save (partner confs only)
# No-cache guard — exits cleanly if RAM cache is empty or missing
#
# ==============================================================================================
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/../load_config.sh"
parse_args "$@"
detect_hosts
require_partnership
RAM_CACHE="$CONF_RAM_CACHE_DIR"
SAVE_DIR="$PERSISTENT_CONF_CACHE"
[[ "$DRY_RUN" == true ]] && warn "DRY RUN — no files will be written"
if [[ ! -d "$RAM_CACHE" ]]; then
log "No RAM conf cache found — nothing to save"
exit 0
fi
saved=0
for conf in "$RAM_CACHE"/host*.conf; do
[[ -f "$conf" ]] || continue
base="$(basename "$conf")"
is_own_conf_file "$base" && continue
if [[ "$DRY_RUN" == true ]]; then
warn "DRY RUN — would save $base$SAVE_DIR/"
(( saved++ ))
continue
fi
mkdir -p "$SAVE_DIR"
if cp "$conf" "$SAVE_DIR/$base"; then
echo "Saved $base$SAVE_DIR ✅"
(( saved++ ))
else
warn "Failed to save $base"
fi
done
if [[ "$saved" -eq 0 ]]; then
log "No partner confs in RAM cache — nothing saved"
else
echo "Conf cache saved: $saved partner conf(s) → $SAVE_DIR"
fi