scheduled backup on PVE and NAS Wake-on-Lan (SMB storage offline)

enry86cami

Member
May 25, 2023
22
2
8
Hi all,
I have a NAS that I keep powered off. I can successfully turn it on via Wake-on-LAN (WoL) and shut it down using an SSH command.
My idea is to use a hook script: when the scheduled backup starts, the script should wake the NAS and then proceed with the backup.
The problem is that when the backup (from PVE) process begins, the SMB storage is still offline, causing the backup to fail immediately with an error.

Does anyone have any suggestions on how to handle this delay? Thanks!
 
I'm using this:
Code:
if [ "$PHASE" = "job-start" ]; then
    echo "1. Inviando segnale WOL al NAS ($NAS_MAC)..."
    etherwake $NAS_MAC

    echo "2. Avvio Health Probe: attendo che lo storage $STOREID sia online..."
    
    # Tentiamo per un massimo di 18 volte con un'attesa di 10 secondi (3 minuti totali)
    for i in {1..18}; do
        # Il comando pvesm status interroga direttamente Proxmox sullo stato dello storage
        if pvesm status --storage $STOREID | grep -q "active"; then
            echo "SUCCESS: Lo storage $STOREID è ONLINE dopo $((i*10)) secondi!"
            exit 0
        fi

        echo "Tentativo $i: NAS non ancora pronto, attendo 10s..."
        sleep 10
    done

    echo "ERROR: Il NAS non si è avviato entro il tempo limite. Backup annullato."
    exit 1
fi

but this is the error: TASK ERROR: could not activate storage 'NAS_NETGEAR': storage 'NAS_NETGEAR' is not online
 
but this is the error: TASK ERROR: could not activate storage 'NAS_NETGEAR': storage 'NAS_NETGEAR' is not online
You have a possible race where "pvesm" status is possibly not yet propagated to PVE cluster view.
I've asked Claude to help you, obviously I have not tested this for you.

Good luck
Code:
#!/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


Blockbridge : Ultra low latency all-NVME shared storage for Proxmox - https://www.blockbridge.com/proxmox