Wake on LAN + Mount + Unmount + Shutdown hook script.

venquessa

New Member
Aug 25, 2023
17
2
3
I thought I'd share this and hopefully receive feedback or... maybe others exploring the same will find this thread.

You can set the backup hook script in the vzdump config. Consult the documentation on same.

For testing you can swap the comments for the variables:
SHUTDOWN_COMMAND
and
UMOUNT_COMMAND

My own feedback is: It could be a little tighter on errors. It can skip a lot of steps if the host happens to be up already.

It could probably take advantage of more PVE API integration.

BUG / ANNOYANCE: Sometimes, even though the NFS share is synced, disabled and unmounted, PVE tries to access it and causes all hell to break loose. This may be caused by me re-enabling the storage to make a change.

Feature Request: Allow edits of backup jobs which target currently disabled storage. Not "create", but "edit" of existing ones.

Bash:
#!/bin/bash

phase=$1

STORAGE_TARGET="remote-backups"
STORAGE_NFS_MNT="/mnt/pve/remote-backups"
NFS_HOST="backups"
REMOTE_NFS_NAME="/backup-zpool/pve/dump"

MAC_FOR_WOL="98:xx:xx:c1:36:xx"

ATTEMPTS_BEFORE_TIMEOUT=30
ATTEMPTS_DELAY_SECONDS=5

#SHUTDOWN_COMMAND="echo DISABLED: ssh root@backups shutdown -h now"
SHUTDOWN_COMMAND="ssh root@backups shutdown -h now"
#UMOUNT_COMMAND="echo umount"
UMOUNT_COMMAND="umount"

if [ "$phase " == " " ]
then
        echo "No phase provided"
        exit 1;
fi

if [ "$phase " == "job-init " ] && [ "$STOREID " == "${STORAGE_TARGET} " ]
then
        echo "job-init phase waking ${NFS_HOST} if necessary"

        ping -c 1 ${NFS_HOST}
        avail=$?
        if [ ${avail} -eq 0 ]
        then
                echo "NFS Host ${NFS_HOST} already awake."
        else
                t_out=${ATTEMPTS_BEFORE_TIMEOUT}
                while [ $avail -ne 0 ] && [ $t_out -ne 0 ]
                do
                        t_out=$(( $t_out - 1 ))
                        wakeonlan ${MAC_FOR_WOL}
                        sleep ${ATTEMPTS_DELAY_SECONDS}
                        ping -c 1 ${NFS_HOST}
                        avail=$?
                done
                if [ $t_out -eq 1 ]
                then
                        echo "Could not wake the ${NFS_HOST} NFS machine."
                        exit 2;
                fi
        fi

        pvesm scan nfs ${NFS_HOST} | grep ${REMOTE_NFS_NAME}
        avail=$?
        t_out=${ATTEMPTS_BEFORE_TIMEOUT}
        while [ $avail -ne 0 ] && [ $t_out -ne 0 ]
        do
                t_out=$(( $t_out - 1 ))
                sleep ${ATTEMPTS_DELAY_SECONDS}
                pvesm scan nfs ${NFS_HOST} | grep ${REMOTE_NFS_NAME}
                avail=$?
        done
        if [ $t_out -eq 0 ]
        then
                echo "Could not find the storage export ${REMOTE_NFS_NAME} on host ${NFS_HOST}"
                exit 2;
        fi

        avail=1
        t_out=${ATTEMPTS_BEFORE_TIMEOUT}
        while [ $avail -ne 0 ] && [ $t_out -ne 0 ]
        do
                t_out=$(( $t_out - 1 ))
                pvesm  set ${STORAGE_TARGET} --disable false
                sleep ${ATTEMPTS_DELAY_SECONDS}
                mount | grep ${STORAGE_NFS_MNT}
                avail=$?
        done
        if [ $t_out -eq 0 ]
        then
                echo "Could not mount the ${STORAGE_TARGET} share."
                exit 2;
        fi
        exit 0
fi

if [ "$phase " == "job-end " ] || [ "$phase " == "job-abort " ]
then
        if [ "$STOREID " == "${STORAGE_TARGET} " ]
        then
                echo "$phase phase syncing disks"
                sync
                echo "disabling storage $STORAGE_TARGET}"
                pvesm  set ${STORAGE_TARGET} --disable true
                echo "Unmounting ${STORAGE_NFS_MNT} in case pvesm didnt"
                umount ${STORAGE_NFS_MNT}

                ${SHUTDOWN_COMMAND}
                exit 0;
        fi
fi
echo "Phase: $phase - no action"
 
Last edited:
  • Like
Reactions: Nik0 and Dunuin
I am thinking of an alternative.

Local SSD storage is used for the PVE backup job. The job-end/job-abort phase hook then launches an "atd" job to wake, rsync the backups and shut the backup node down.

Not high priority but might improve performance.
 

About

The Proxmox community has been around for many years and offers help and support for Proxmox VE, Proxmox Backup Server, and Proxmox Mail Gateway.
We think our community is one of the best thanks to people like you!

Get your subscription!

The Proxmox team works very hard to make sure you are running the best software and getting stable updates and security enhancements, as well as quick enterprise support. Tens of thousands of happy customers have a Proxmox subscription. Get yours easily in our online shop.

Buy now!