Bug fixes across the ecosystem after v1→v2 architecture migration and Unraid 7.2.5 upgrade:
- common.sh: fix _alias_array() phantom empty element (removed [@]:-} pattern), fix
resolve_remote_ip() with Tailscale FQDN lowercase + awk fallback, add FANART/LASTFM
key aliases in detect_hosts(), global [@]:-} sweep across 10+ scripts
- webgui_restart.sh: fix emhttp detection (pgrep emhttpd) and restart command for 7.2.5
(/usr/local/sbin/emhttp stop && start — rc.emhttp removed in 7.2.5)
- bandwidth_monitor.sh, continuous_scripts_status.sh: fix 'local' keyword outside function
- backup_verify.sh: fix resolve_remote_ip() called before detect_hosts()
- Orchestrators: fix script display duplication bug in status output (${entry##*/})
- rsync_stop.sh, git_pull_execute.sh, partnership_manager.sh, coffee_report: lowercase all
tailscale ip -4 call sites to match Tailscale's lowercase device names
- master_host1.conf: fix SSH key path (gmer4lfe_rsync_automation), add FANART/LASTFM keys
- master_host2.conf: add FANART/LASTFM API keys
New: Media/lidarr_missing_art.sh
- Full ecosystem port of standalone Lidarr artwork fetcher
- Fetches missing album art via fanart.tv + Last.fm APIs
- @tsv batch extraction: 1 jq call per API response vs N*albums (8050 albums in 24s)
- HOST guard (HOST1 only), --status, --dry-run, acquire_lock
New: Initial_run/ssh_setup.sh
- Generates {hostname}_rsync_automation ed25519 keypair (skip if exists, --force to regen)
- ssh-copy-id to remote via Tailscale IP, auto-updates master_host*.conf
- --validate mode: strike tracking (SSH_MAX_STRIKES, SSH_STRIKE_RESET_HRS),
notify at limit — Tailscale-unreachable remote does NOT count as SSH strike
New: Initial_run/partnership_onboard.sh
- Orchestrator: ssh_setup.sh then partnership_manager.sh --onboard in one command
Partnership/partnership_manager.sh: FolderView3 integration
- Derive partner folder name at runtime (strip unraid- prefix case-insensitively)
- --onboard: create {Mirror}-Failover folder with failover tier containers
- --offboard (both paths): stop + rm containers in folder, remove JSON entry
- --check: calls ssh_setup.sh --validate when IP resolves but SSH state empty
- --status: shows FolderView3 folder state and containers inline
master.conf: SSH_MAX_STRIKES, SSH_STRIKE_RESET_HRS, PARTNERSHIP_FOLDERVIEW3,
PARTNERSHIP_FOLDERVIEW3_URL added to Partnership section
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
314 lines
14 KiB
Bash
314 lines
14 KiB
Bash
#!/bin/bash
|
|
# ==============================================================================================
|
|
# ================================= Docker Weekly Restart ======================================
|
|
# ==============================================================================================
|
|
# Restarts all running containers in HOST*_WEEKLY_RESTART_CONTAINERS.
|
|
# Called by weekly_sync_maintenance.sh via WEEKLY_MAINTENANCE_SCRIPTS every Sunday at 2:30am.
|
|
# Can also be run manually for ad hoc weekly restarts.
|
|
#
|
|
# ── CONTEXT ───────────────────────────────────────────────────────────────────────────────────
|
|
# weekly_sync_maintenance.sh stops containers before syncing and restarts them after.
|
|
# This script runs AFTER that restart — targeting a different set of less critical services
|
|
# that benefit from a weekly restart but don't need to be stopped for the sync itself.
|
|
# These containers are typically already running when this script executes.
|
|
#
|
|
# ── BEHAVIOUR ─────────────────────────────────────────────────────────────────────────────────
|
|
# Running containers → docker restart (graceful stop + start)
|
|
# Stopped containers → left stopped — was down intentionally, do not bring back up
|
|
# Missing containers → logged and skipped — not treated as fatal
|
|
# Each action uses RETRY_COUNT + SLEEP from master.conf for retry logic.
|
|
#
|
|
# The "was running → restart, was stopped → leave stopped" rule is consistent
|
|
# across the entire ecosystem — container state is always respected.
|
|
#
|
|
# ── SAFEGUARDS ────────────────────────────────────────────────────────────────────────────────
|
|
# Dependency ordering — containers restart in dependency-safe order using
|
|
# HOST*_WATCHDOG_DEPENDENCIES from master_host*.conf. Dependencies restart
|
|
# first with CONTAINER_DELAY before their dependents.
|
|
#
|
|
# Restart verification — after each restart, container state is checked after a short
|
|
# settle period. If the container fails to stay running it is marked as failed and
|
|
# a notification is sent rather than silently passing.
|
|
#
|
|
# Timeout protection — all docker commands are wrapped in a 30 second timeout.
|
|
# A hung Docker daemon cannot cause this script to hang indefinitely.
|
|
#
|
|
# ── CONFIGURATION (master_host*.conf) ─────────────────────────────────────────────────────────
|
|
# HOST*_WEEKLY_RESTART_CONTAINERS — list of containers to restart weekly
|
|
# HOST*_WATCHDOG_DEPENDENCIES — dependency ordering for restart sequence
|
|
# Set by detect_hosts() alias → WEEKLY_RESTART_CONTAINERS used by this script
|
|
#
|
|
# ── CONFIGURATION (master.conf) ───────────────────────────────────────────────────────────────
|
|
# RETRY_COUNT — retry attempts before giving up on a container
|
|
# SLEEP — seconds between retry attempts
|
|
# CONTAINER_DELAY — seconds to wait between dependency and dependent restart
|
|
#
|
|
# ── USAGE ─────────────────────────────────────────────────────────────────────────────────────
|
|
# docker_weekly_restart.sh — normal restart
|
|
# docker_weekly_restart.sh --dry-run — preview without restarting
|
|
# docker_weekly_restart.sh --log — verbose output
|
|
# docker_weekly_restart.sh --status — show config and exit
|
|
# ==============================================================================================
|
|
|
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
|
|
source "$SCRIPT_DIR/../load_config.sh"
|
|
|
|
parse_args "$@"
|
|
|
|
# ==============================================================================================
|
|
# ━━━ Setup ━━━
|
|
# ==============================================================================================
|
|
echo ""
|
|
echo "━━━ $ICON_GEAR Setup ━━━"
|
|
|
|
if [[ "$EUID" -ne 0 ]]; then
|
|
error "Must be run as root"
|
|
exit 1
|
|
fi
|
|
success "Running as root"
|
|
|
|
acquire_lock
|
|
|
|
if ! command -v docker &>/dev/null; then
|
|
error "Docker command not found — check PATH or Docker installation"
|
|
notify "Docker weekly restart failed — Docker not found on $(hostname)" "Docker Weekly Restart" "warning"
|
|
exit 1
|
|
fi
|
|
success "Docker found"
|
|
|
|
# detect_hosts() sets MY_ID and aliases HOST*_WEEKLY_RESTART_CONTAINERS → WEEKLY_RESTART_CONTAINERS
|
|
detect_hosts
|
|
|
|
if [[ ${#WEEKLY_RESTART_CONTAINERS[@]} -eq 0 ]]; then
|
|
warn "WEEKLY_RESTART_CONTAINERS is empty for $MY_ID — nothing to restart"
|
|
warn "Check HOST*_WEEKLY_RESTART_CONTAINERS in master_host*.conf"
|
|
exit 0
|
|
fi
|
|
|
|
# ==============================================================================================
|
|
# ━━━ Status ━━━
|
|
# ==============================================================================================
|
|
if [[ "$SHOW_STATUS" == true ]]; then
|
|
echo ""
|
|
echo "━━━━━ $ICON_SUMMARY STATUS ━━━━━"
|
|
echo "$ICON_HOST Identity: $MY_ID ($LOCAL_SERVER_NAME)"
|
|
echo "$ICON_CONTAINERS Containers: ${WEEKLY_RESTART_CONTAINERS[*]}"
|
|
echo "$ICON_RETRY Retries: $RETRY_COUNT"
|
|
echo "$ICON_TIME Sleep: ${SLEEP}s between retries"
|
|
echo "$ICON_NOTIFY Notify: unRAID=${NOTIFY_UNRAID:-false} Discord=$([[ -n "${MY_DISCORD_WEBHOOK:-}" ]] && echo enabled || echo disabled)"
|
|
echo "$ICON_GEAR Dry Run: $DRY_RUN"
|
|
echo "━━━━━━━━━━━━━━━━━━━━━━━"
|
|
exit 0
|
|
fi
|
|
|
|
[[ "$DRY_RUN" == true ]] && warn "DRY RUN — no containers will be restarted"
|
|
|
|
# ==============================================================================================
|
|
# ── FUNCTIONS ─────────────────────────────────────────────────────────────────────────────────
|
|
# ==============================================================================================
|
|
|
|
# Wraps docker commands with a 30 second timeout.
|
|
# Prevents a hung Docker daemon from causing the script to hang indefinitely.
|
|
DOCKER_TIMEOUT=30
|
|
docker_cmd() {
|
|
timeout "$DOCKER_TIMEOUT" "$@"
|
|
local exit_code=$?
|
|
if [[ "$exit_code" -eq 124 ]]; then
|
|
error "Docker command timed out after ${DOCKER_TIMEOUT}s: $*"
|
|
return 1
|
|
fi
|
|
return "$exit_code"
|
|
}
|
|
|
|
# Retries a docker command up to RETRY_COUNT times with SLEEP seconds between attempts.
|
|
# Uses docker_cmd wrapper for timeout protection on each attempt.
|
|
retry_docker() {
|
|
local attempt=1
|
|
|
|
while [[ "$attempt" -le "$RETRY_COUNT" ]]; do
|
|
info "$ICON_RETRY Attempt $attempt of $RETRY_COUNT: $*"
|
|
|
|
if docker_cmd "$@"; then
|
|
success "Succeeded on attempt $attempt"
|
|
return 0
|
|
else
|
|
warn "Attempt $attempt failed"
|
|
(( attempt++ ))
|
|
[[ "$attempt" -le "$RETRY_COUNT" ]] && sleep "$SLEEP"
|
|
fi
|
|
done
|
|
|
|
error "Command failed after $RETRY_COUNT attempts: $*"
|
|
return 1
|
|
}
|
|
|
|
# Verifies a container is still running after restart.
|
|
# Gives the container a short settle period before checking.
|
|
RESTART_VERIFY_WAIT=5
|
|
verify_running() {
|
|
local container="$1"
|
|
sleep "$RESTART_VERIFY_WAIT"
|
|
local state
|
|
state=$(docker inspect -f '{{.State.Running}}' "$container" 2>/dev/null)
|
|
if [[ "$state" != "true" ]]; then
|
|
error "$container failed to stay running after restart — may have crashed"
|
|
return 1
|
|
fi
|
|
return 0
|
|
}
|
|
|
|
# Builds a dependency-safe restart order from WEEKLY_RESTART_CONTAINERS.
|
|
# Containers that are dependencies of others restart first.
|
|
build_restart_order() {
|
|
ORDERED_RESTART=()
|
|
local remaining=("${WEEKLY_RESTART_CONTAINERS[@]}")
|
|
local placed=()
|
|
|
|
# First pass — add dependency containers that appear in our list
|
|
for container in "${remaining[@]}"; do
|
|
[[ -z "$container" ]] && continue
|
|
local is_dependency=false
|
|
for dependent in "${!WATCHDOG_DEPENDENCIES[@]}"; do
|
|
if [[ "${WATCHDOG_DEPENDENCIES[$dependent]}" == *"$container"* ]]; then
|
|
is_dependency=true
|
|
break
|
|
fi
|
|
done
|
|
if [[ "$is_dependency" == true ]]; then
|
|
local already=false
|
|
for p in "${placed[@]}"; do [[ "$p" == "$container" ]] && already=true && break; done
|
|
if [[ "$already" == false ]]; then
|
|
ORDERED_RESTART+=("$container")
|
|
placed+=("$container")
|
|
fi
|
|
fi
|
|
done
|
|
|
|
# Second pass — add remaining containers (dependents and independents)
|
|
for container in "${remaining[@]}"; do
|
|
[[ -z "$container" ]] && continue
|
|
local already=false
|
|
for p in "${placed[@]}"; do [[ "$p" == "$container" ]] && already=true && break; done
|
|
if [[ "$already" == false ]]; then
|
|
ORDERED_RESTART+=("$container")
|
|
placed+=("$container")
|
|
fi
|
|
done
|
|
|
|
log "Restart order: ${ORDERED_RESTART[*]}"
|
|
}
|
|
|
|
# Waits CONTAINER_DELAY if this container depends on the last restarted one.
|
|
check_dependency_delay() {
|
|
local container="$1"
|
|
local last="$2"
|
|
[[ -z "$last" ]] && return
|
|
local deps="${WATCHDOG_DEPENDENCIES[$container]:-}"
|
|
if [[ -n "$deps" ]] && [[ "$deps" == *"$last"* ]]; then
|
|
info "Waiting ${CONTAINER_DELAY}s — $container depends on $last..."
|
|
sleep "$CONTAINER_DELAY"
|
|
fi
|
|
}
|
|
|
|
# ==============================================================================================
|
|
# ━━━ Weekly Restart ━━━
|
|
# ==============================================================================================
|
|
echo ""
|
|
echo "━━━ $ICON_CONTAINERS Weekly Restart — $(date '+%Y-%m-%d %H:%M:%S') ━━━"
|
|
echo "$ICON_CONTAINERS Containers: ${WEEKLY_RESTART_CONTAINERS[*]}"
|
|
echo "$ICON_RETRY Retries: $RETRY_COUNT"
|
|
echo ""
|
|
|
|
START=$(date +%s)
|
|
FAILED=()
|
|
RESTARTED=()
|
|
SKIPPED=()
|
|
|
|
# Build dependency-safe restart order
|
|
build_restart_order
|
|
echo "$ICON_GEAR Restart order: ${ORDERED_RESTART[*]}"
|
|
echo ""
|
|
|
|
LAST_RESTARTED=""
|
|
|
|
for container in "${ORDERED_RESTART[@]}"; do
|
|
[[ -z "$container" ]] && continue
|
|
echo "━━━ $ICON_CONTAINERS $container ━━━"
|
|
|
|
if ! timeout "$DOCKER_TIMEOUT" docker inspect "$container" &>/dev/null; then
|
|
warn "$container does not exist — skipping"
|
|
echo ""
|
|
continue
|
|
fi
|
|
|
|
STATUS=$(timeout "$DOCKER_TIMEOUT" docker inspect -f '{{.State.Running}}' "$container" 2>/dev/null)
|
|
|
|
case "$STATUS" in
|
|
true)
|
|
echo "$ICON_RUNNING $container is running — restarting..."
|
|
|
|
# Wait if this container depends on the last one restarted
|
|
check_dependency_delay "$container" "$LAST_RESTARTED"
|
|
|
|
if [[ "$DRY_RUN" == true ]]; then
|
|
warn "DRY RUN — would restart $container"
|
|
RESTARTED+=("$container")
|
|
else
|
|
if retry_docker docker restart "$container"; then
|
|
if verify_running "$container"; then
|
|
echo "$ICON_STARTED $container restarted and running ✅"
|
|
RESTARTED+=("$container")
|
|
LAST_RESTARTED="$container"
|
|
else
|
|
error "$container restarted but crashed immediately"
|
|
notify "$container crashed after restart on $(hostname)" "Docker Weekly Restart" "warning"
|
|
FAILED+=("$container")
|
|
fi
|
|
else
|
|
error "Failed to restart $container after $RETRY_COUNT attempts"
|
|
notify "$container failed to restart on $(hostname)" "Docker Weekly Restart" "warning"
|
|
FAILED+=("$container")
|
|
fi
|
|
fi
|
|
;;
|
|
false)
|
|
# Container was stopped — leave it stopped
|
|
# Intentionally stopped containers are not restarted
|
|
echo "$ICON_NOT_RUNNING $container is stopped — skipping (respecting stopped state)"
|
|
SKIPPED+=("$container")
|
|
;;
|
|
*)
|
|
error "Unknown status for $container: $STATUS"
|
|
FAILED+=("$container")
|
|
;;
|
|
esac
|
|
|
|
echo ""
|
|
done
|
|
|
|
END=$(date +%s)
|
|
|
|
# ==============================================================================================
|
|
# ━━━ Summary ━━━
|
|
# ==============================================================================================
|
|
echo "━━━━━ $ICON_SUMMARY WEEKLY RESTART SUMMARY ━━━━━"
|
|
echo "$ICON_HOST Identity: $MY_ID ($LOCAL_SERVER_NAME)"
|
|
echo "$ICON_TIME Duration: $(format_duration $((END - START)))"
|
|
[[ ${#RESTARTED[@]} -gt 0 ]] && echo "$ICON_STARTED Restarted: ${RESTARTED[*]}"
|
|
[[ ${#SKIPPED[@]} -gt 0 ]] && echo "$ICON_NOT_RUNNING Skipped: ${SKIPPED[*]} (were stopped)"
|
|
[[ ${#FAILED[@]} -gt 0 ]] && echo "$ICON_ERROR Failed: ${FAILED[*]}"
|
|
|
|
if [[ "$DRY_RUN" == true ]]; then
|
|
echo "$ICON_WARN Status: DRY RUN — no changes made"
|
|
elif [[ ${#FAILED[@]} -eq 0 ]]; then
|
|
echo "$ICON_DONE Status: $ICON_SUCCESS ALL DONE"
|
|
notify "Weekly restart complete — ${#RESTARTED[@]} restarted, ${#SKIPPED[@]} skipped (stopped) on $(hostname)" "Docker Weekly Restart" "normal"
|
|
else
|
|
echo "$ICON_ERROR Status: $ICON_ERROR ${#FAILED[@]} container(s) failed"
|
|
notify "Weekly restart completed with errors on $(hostname) — failed: ${FAILED[*]}" "Docker Weekly Restart" "warning"
|
|
fi
|
|
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
|
|
|
|
[[ ${#FAILED[@]} -gt 0 ]] && exit 1
|
|
exit 0 |