116 lines
3.7 KiB
Bash
116 lines
3.7 KiB
Bash
|
|
#!/bin/bash
|
||
|
|
# =============================================================
|
||
|
|
# plex32_reboot.sh — Remote reboot of PLEX32 from NAS08
|
||
|
|
# Location: /usr/scripts/nas/plex32_reboot.sh
|
||
|
|
# Run via: OMV Scheduled Jobs (disabled — run manually)
|
||
|
|
# =============================================================
|
||
|
|
|
||
|
|
PLEX32_IP="192.168.150.45"
|
||
|
|
PLEX32_USER="rufusking"
|
||
|
|
SSH_KEY="/root/.ssh/id_ed25519"
|
||
|
|
LOG_DIR="/var/log/plex32-reboot"
|
||
|
|
LOG_FILE="$LOG_DIR/reboot-$(date +%Y%m%d-%H%M%S).log"
|
||
|
|
|
||
|
|
mkdir -p "$LOG_DIR"
|
||
|
|
|
||
|
|
log() {
|
||
|
|
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" | tee -a "$LOG_FILE"
|
||
|
|
}
|
||
|
|
|
||
|
|
# --- Clean up logs older than 90 days ---
|
||
|
|
find "$LOG_DIR" -name "reboot-*.log" -mtime +90 -delete
|
||
|
|
|
||
|
|
log "========================================"
|
||
|
|
log "PLEX32 Remote Reboot — Initiated"
|
||
|
|
log "Target: $PLEX32_USER@$PLEX32_IP"
|
||
|
|
log "========================================"
|
||
|
|
|
||
|
|
# --- Stop Plex and Tautulli containers gracefully before reboot ---
|
||
|
|
log "Stopping Docker containers on PLEX32..."
|
||
|
|
ssh -i "$SSH_KEY" -o ConnectTimeout=10 -o BatchMode=yes \
|
||
|
|
"$PLEX32_USER@$PLEX32_IP" \
|
||
|
|
"cd ~/docker/plex && docker compose down" >> "$LOG_FILE" 2>&1
|
||
|
|
|
||
|
|
if [ $? -eq 0 ]; then
|
||
|
|
log "Docker containers stopped cleanly."
|
||
|
|
else
|
||
|
|
log "WARNING: docker compose down failed or timed out — proceeding with reboot anyway."
|
||
|
|
fi
|
||
|
|
|
||
|
|
# --- Issue reboot ---
|
||
|
|
log "Issuing reboot command to PLEX32..."
|
||
|
|
ssh -i "$SSH_KEY" -o ConnectTimeout=10 -o BatchMode=yes \
|
||
|
|
"$PLEX32_USER@$PLEX32_IP" \
|
||
|
|
"sudo reboot" >> "$LOG_FILE" 2>&1
|
||
|
|
|
||
|
|
# SSH will return a non-zero exit code when the connection drops due to reboot — that's expected
|
||
|
|
log "Reboot command sent. PLEX32 is going down."
|
||
|
|
log ""
|
||
|
|
|
||
|
|
# --- Wait for PLEX32 to come back online ---
|
||
|
|
log "Pinging Server -- Waiting for PLEX32 to come back online..."
|
||
|
|
WAIT_MAX=180 # seconds to wait total
|
||
|
|
WAIT_INTERVAL=10
|
||
|
|
ELAPSED=0
|
||
|
|
ONLINE=false
|
||
|
|
|
||
|
|
sleep 30 # Give it time to actually go down before polling
|
||
|
|
|
||
|
|
while [ $ELAPSED -lt $WAIT_MAX ]; do
|
||
|
|
if ping -c 1 -W 2 "$PLEX32_IP" > /dev/null 2>&1; then
|
||
|
|
log "PLEX32 is back online (ping responded after ~$((ELAPSED + 30))s)."
|
||
|
|
ONLINE=true
|
||
|
|
break
|
||
|
|
fi
|
||
|
|
sleep $WAIT_INTERVAL
|
||
|
|
ELAPSED=$((ELAPSED + WAIT_INTERVAL))
|
||
|
|
done
|
||
|
|
|
||
|
|
if [ "$ONLINE" = false ]; then
|
||
|
|
log "WARNING: PLEX32 did not respond to ping within $((WAIT_MAX + 30)) seconds."
|
||
|
|
log "It may still be booting, or there may be a problem. Check manually."
|
||
|
|
exit 1
|
||
|
|
fi
|
||
|
|
|
||
|
|
# --- Start Docker containers on PLEX32 ---
|
||
|
|
log "Starting Docker containers on PLEX32..."
|
||
|
|
ssh -i "$SSH_KEY" -o ConnectTimeout=15 -o BatchMode=yes \
|
||
|
|
"$PLEX32_USER@$PLEX32_IP" \
|
||
|
|
"cd ~/docker/plex && docker compose up -d" >> "$LOG_FILE" 2>&1
|
||
|
|
|
||
|
|
if [ $? -eq 0 ]; then
|
||
|
|
log "Docker containers started."
|
||
|
|
else
|
||
|
|
log "WARNING: docker compose up -d failed — check PLEX32 manually."
|
||
|
|
fi
|
||
|
|
|
||
|
|
# --- Poll for Plex port 32400 to respond ---
|
||
|
|
log "Waiting for Plex (port 32400) to become available..."
|
||
|
|
PLEX_WAIT_MAX=90
|
||
|
|
PLEX_ELAPSED=0
|
||
|
|
PLEX_UP=false
|
||
|
|
|
||
|
|
while [ $PLEX_ELAPSED -lt $PLEX_WAIT_MAX ]; do
|
||
|
|
HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" --max-time 5 "http://$PLEX32_IP:32400/web")
|
||
|
|
if [ "$HTTP_CODE" = "200" ] || [ "$HTTP_CODE" = "301" ] || [ "$HTTP_CODE" = "302" ]; then
|
||
|
|
log "Plex is responding (HTTP $HTTP_CODE) after ${PLEX_ELAPSED}s — reboot successful."
|
||
|
|
PLEX_UP=true
|
||
|
|
break
|
||
|
|
fi
|
||
|
|
sleep 5
|
||
|
|
PLEX_ELAPSED=$((PLEX_ELAPSED + 5))
|
||
|
|
done
|
||
|
|
|
||
|
|
if [ "$PLEX_UP" = false ]; then
|
||
|
|
log "NOTE: Plex did not respond within ${PLEX_WAIT_MAX}s. Containers may still be starting."
|
||
|
|
log "Allow another minute then check http://$PLEX32_IP:32400/web manually."
|
||
|
|
fi
|
||
|
|
|
||
|
|
log ""
|
||
|
|
log "========================================"
|
||
|
|
log "Reboot sequence complete."
|
||
|
|
log "Log saved to: $LOG_FILE"
|
||
|
|
log "========================================"
|
||
|
|
|
||
|
|
exit 0
|