Files
Varaverk/Watchdogs/System/conf_cache_watchdog.sh
T
Gmer4Lfe f92ee4064b Add full banner headers to all scripts across the codebase
Every script now has the established header format: PURPOSE with ─────── separator,
OPERATIONAL MODEL, DESIGN PRINCIPLES, OPERATIONAL SAFEGUARDS, CONFIGURATION, and
RUNTIME MODES — structured with full ====== banner sections throughout.

Orchestrators converted from compact ── inline format to full banners. Stale
emby-fallback and dirty sync references removed from Plugin/user_script_plug-in.sh.
2026-06-26 18:50:05 -04:00

96 lines
3.9 KiB
Bash
Executable File

#!/bin/bash
# ==============================================================================================
# ============================= Conf Cache Watchdog ============================================
# ==============================================================================================
#
# PURPOSE
# ─────────────────────────────────────────────────────────────────────────────
# Maintains the persistent partner conf backup at $PERSISTENT_CONF_CACHE.
# Runs every 15 minutes via SYSTEM_WATCHDOG_SCRIPTS.
#
# When remote is OFFLINE:
# Writes/refreshes partner confs from RAM cache → persistent location.
# Means if this host reboots while remote is still down, conf_cache_restore.sh
# can load the partner vars into RAM so fallback.sh has what it needs.
#
# When remote is ONLINE:
# Removes the persistent backup if present — not needed, conf_sync.sh will
# pull fresh on next boot.
#
# Silent when remote is online and no backup exists (normal state).
# No-op when FALLBACK_ENABLED=false or CONF_SYNC_ENABLED=false.
#
# ==============================================================================================
# DESIGN PRINCIPLES
# ==============================================================================================
#
# Online = No Backup Needed
# When the remote is reachable, conf_sync.sh will pull fresh on the next boot.
# The persistent backup is removed — a stale backup is worse than no backup
# because it can mask a connectivity problem that conf_sync.sh would catch.
#
# Offline = Stay Ready
# While the remote is down, the RAM cache is the best available copy of partner
# vars. Refreshing the persistent backup every 15 minutes ensures it reflects
# the last-known-good state, not an old copy from days earlier.
#
# ==============================================================================================
# OPERATIONAL SAFEGUARDS
# ==============================================================================================
#
# require_partnership — exits early if PARTNERSHIP_ENABLED=false
# FALLBACK_ENABLED gate — exits if fallback is disabled
# CONF_SYNC_ENABLED gate — exits if conf sync is disabled
# REMOTE_ID presence check — exits if partner identity is unset
# --dry-run mode — shows what would happen without touching the backup
#
# ==============================================================================================
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/../../load_config.sh"
parse_args "$@"
detect_hosts
require_partnership
[[ "${FALLBACK_ENABLED:-false}" != "true" ]] && exit 0
[[ "${CONF_SYNC_ENABLED:-true}" != "true" ]] && exit 0
[[ -z "${REMOTE_ID:-}" ]] && exit 0
RAM_CACHE="/tmp/.cache/vv/d"
SAVE_DIR="$PERSISTENT_CONF_CACHE"
if ping_remote; then
if [[ -d "$SAVE_DIR" ]]; then
[[ "$DRY_RUN" == true ]] && warn "DRY RUN — would remove $SAVE_DIR (remote back online)" && exit 0
rm -rf "$SAVE_DIR"
log "Remote online — persistent conf backup removed"
fi
exit 0
fi
# Remote offline — write/refresh backup from RAM cache
if [[ ! -d "$RAM_CACHE" ]]; then
log "Remote offline but RAM cache not populated — nothing to back up"
exit 0
fi
saved=0
for conf in "$RAM_CACHE"/host*.conf; do
[[ -f "$conf" ]] || continue
base="$(basename "$conf")"
[[ "${base,,}" == "${MY_ID,,}.conf" ]] && continue
if [[ "$DRY_RUN" == true ]]; then
warn "DRY RUN — would write $base$SAVE_DIR/"
(( saved++ ))
continue
fi
mkdir -p "$SAVE_DIR"
cp "$conf" "$SAVE_DIR/$base" && (( saved++ ))
done
[[ "$saved" -gt 0 ]] && log "Remote offline — partner conf backup refreshed ($saved file(s))"
exit 0