#!/bin/bash
# ─── CONFIG ──────────────────────────────────────────
NAS_MAC="AA:BB:CC:DD:EE:FF" # Replace with your NAS MAC address
STOREID="NAS_NETGEAR" # Replace with your PVE storage ID
NAS_IP="192.168.1.100" # Replace with your NAS IP address
WOL_WAIT=30 # Seconds to wait after WoL before probing (boot grace period)
PROBE_INTERVAL=10 # Seconds between each probe attempt
PROBE_MAX_ATTEMPTS=24 # Max attempts (24 × 10s = 4 min)
# ─────────────────────────────────────────────────────
network_probe() {
# ┌─────────────────────────────────────────────────────────────┐
# │ REPLACE THIS FUNCTION with your actual protocol check. │
# │ Return 0 = NAS is ready, Return 1 = NAS is not ready yet │
# │ │
# │ Examples (uncomment one): │
# │ SMB/CIFS: smbclient -L $NAS_IP -N &>/dev/null │
# │ NFS: showmount -e $NAS_IP &>/dev/null │
# │ TCP port: nc -z -w3 $NAS_IP 445 │
# │ ICMP ping: ping -c1 -W2 $NAS_IP &>/dev/null │
# └─────────────────────────────────────────────────────────────┘
ping -c1 -W2 "$NAS_IP" &>/dev/null # <-- REPLACE THIS LINE
}
pve_storage_probe() {
# Asks PVE's API for this host's view of the storage status.
# Returns 0 if the node reports the storage as active.
pvesh get /nodes/localhost/storage/"$STOREID"/status \
--output-format json 2>/dev/null \
| grep -q '"active"'
}
# ─── HOOK LOGIC ──────────────────────────────────────
if [ "$PHASE" = "job-start" ]; then
echo "[1/4] Sending WoL magic packet to NAS ($NAS_MAC)..."
etherwake "$NAS_MAC"
echo "[2/4] Grace period: waiting ${WOL_WAIT}s for NAS to begin booting..."
sleep "$WOL_WAIT"
echo "[3/4] Network probe: waiting for NAS ($NAS_IP) to be reachable..."
for i in $(seq 1 "$PROBE_MAX_ATTEMPTS"); do
if network_probe; then
echo " ✔ NAS reachable after ~$(( WOL_WAIT + i * PROBE_INTERVAL ))s"
break
fi
echo " Attempt $i/$PROBE_MAX_ATTEMPTS: not reachable yet, retrying in ${PROBE_INTERVAL}s..."
sleep "$PROBE_INTERVAL"
if [ "$i" -eq "$PROBE_MAX_ATTEMPTS" ]; then
echo "ERROR: NAS did not come online within the time limit. Aborting backup."
exit 1
fi
done
echo "[4/4] PVE storage probe: waiting for '$STOREID' to be active on this node..."
for i in $(seq 1 "$PROBE_MAX_ATTEMPTS"); do
if pve_storage_probe; then
echo " ✔ PVE reports storage '$STOREID' as active!"
break
fi
echo " Attempt $i/$PROBE_MAX_ATTEMPTS: storage not yet active, retrying in ${PROBE_INTERVAL}s..."
sleep "$PROBE_INTERVAL"
if [ "$i" -eq "$PROBE_MAX_ATTEMPTS" ]; then
echo "ERROR: Storage '$STOREID' never became active in PVE. Aborting backup."
exit 1
fi
done
echo "SUCCESS: Storage '$STOREID' is online. Proceeding with backup."
exit 0
fi