#!/bin/bash # ----------------------------------------------------------------------------------------------- # --------------------------------- Docker Daily Restart --------------------------------------- # ----------------------------------------------------------------------------------------------- # Restarts or starts specified Docker containers with retry logic. # Containers are configured in Master.conf under DAILY_RESTART_CONTAINERS. # Uses global RETRY_COUNT and SLEEP from Master.conf for retry behaviour. # Sends notifications on completion or failure via common.sh notify(). # Supports --dry-run to preview what would be restarted without taking action. # ----------------------------------------------------------------------------------------------- SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" source "$SCRIPT_DIR/../Master.conf" source "$SCRIPT_DIR/../common.sh" parse_args "$@" # ----------------------------------------------------------------------------------------------- # ━━━ $ICON_GEAR 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 daily restart failed — Docker not found on $(hostname)" "Docker Daily Restart" "warning" exit 1 fi success "Docker found" # ----------------------------------------------------------------------------------------------- # ━━━ $ICON_SUMMARY Status ━━━ # ----------------------------------------------------------------------------------------------- if [[ "$SHOW_STATUS" == true ]]; then echo "" echo "━━━━━ $ICON_SUMMARY STATUS ━━━━━" echo "$ICON_CONTAINERS Containers: ${DAILY_RESTART_CONTAINERS[*]}" echo "$ICON_RETRY Retries: $RETRY_COUNT" echo "$ICON_TIME Sleep: ${SLEEP}s between retries" echo "$ICON_NOTIFY Notifications: unRAID=${NOTIFY_UNRAID:-false} Discord=$([[ -n "${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 # ----------------------------------------------------------------------------------------------- retry_docker() { local attempt=1 while [[ "$attempt" -le "$RETRY_COUNT" ]]; do info "$ICON_RETRY Attempt $attempt of $RETRY_COUNT: $*" if "$@"; 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 } # ----------------------------------------------------------------------------------------------- # ━━━ $ICON_CONTAINERS Daily Restart ━━━ # ----------------------------------------------------------------------------------------------- echo "" echo "━━━ $ICON_CONTAINERS Daily Restart — $(date '+%Y-%m-%d %H:%M:%S') ━━━" echo "$ICON_CONTAINERS Containers: ${DAILY_RESTART_CONTAINERS[*]}" echo "$ICON_RETRY Retries: $RETRY_COUNT" echo "" START=$(date +%s) FAILED=() RESTARTED=() STARTED=() for container in "${DAILY_RESTART_CONTAINERS[@]}"; do echo "━━━ $ICON_CONTAINERS $container ━━━" if ! docker inspect "$container" &>/dev/null; then error "$container does not exist — skipping" FAILED+=("$container") echo "" continue fi STATUS=$(docker inspect -f '{{.State.Running}}' "$container" 2>/dev/null) case "$STATUS" in true) echo "$ICON_RUNNING $container is running — restarting..." if [[ "$DRY_RUN" == true ]]; then warn "DRY RUN — would restart $container" else if retry_docker docker restart "$container"; then echo "$ICON_STARTED $container restarted" RESTARTED+=("$container") else error "Failed to restart $container after $RETRY_COUNT attempts" notify "$container failed to restart on $(hostname)" "Docker Daily Restart" "warning" FAILED+=("$container") fi fi ;; false) echo "$ICON_NOT_RUNNING $container is stopped — starting..." if [[ "$DRY_RUN" == true ]]; then warn "DRY RUN — would start $container" else if retry_docker docker start "$container"; then echo "$ICON_STARTED $container started" STARTED+=("$container") else error "Failed to start $container after $RETRY_COUNT attempts" notify "$container failed to start on $(hostname)" "Docker Daily Restart" "warning" FAILED+=("$container") fi fi ;; *) error "Unknown status for $container: $STATUS" FAILED+=("$container") ;; esac echo "" done END=$(date +%s) # ----------------------------------------------------------------------------------------------- # ━━━ $ICON_SUMMARY Summary ━━━ # ----------------------------------------------------------------------------------------------- echo "━━━━━ $ICON_SUMMARY DAILY RESTART SUMMARY ━━━━━" echo "$ICON_TIME Duration: $(format_duration $((END - START)))" [[ ${#RESTARTED[@]} -gt 0 ]] && echo "$ICON_STARTED Restarted: ${RESTARTED[*]}" [[ ${#STARTED[@]} -gt 0 ]] && echo "$ICON_STARTED Started: ${STARTED[*]}" [[ ${#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 "Daily restart complete — ${#RESTARTED[@]} restarted, ${#STARTED[@]} started on $(hostname)" "Docker Daily Restart" "normal" else echo "$ICON_ERROR Status: $ICON_ERROR ${#FAILED[@]} container(s) failed" notify "Daily restart completed with errors on $(hostname) — failed: ${FAILED[*]}" "Docker Daily Restart" "warning" fi echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" [[ ${#FAILED[@]} -gt 0 ]] && exit 1 exit 0