Last active
March 6, 2025 18:29
-
-
Save ZsZs73/8bf255eeea82004c4150f6add07af527 to your computer and use it in GitHub Desktop.
Proxmox backup server on-demand wakeonlan wake-on-lan
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# This script expects that the ssh key authentication is set up between root@pve and root@pbs | |
# | |
# PBS server name | |
PBS_NAME=pve0 | |
# Broadcast ip of the interface of the local server where the PBS is reachable | |
PBS_BROADCASTIP=192.168.99.255 | |
# MAC address of the PBS server | |
PBS_MAC=90:1b:0e:aa:bb:cc | |
# Name of the backup datastore on the local PVE (pvesm status) | |
PVE_BUDS=pbs0 | |
# Timeout waiting for the PBS datastoce become active | |
PVE_BUDS_TIMEOUT=180 | |
loginfo() { echo "${1}" | tee >(logger -t ${0##*/} -p user.info) ; } | |
logerr() { echo "${1}" | tee >(logger -t ${0##*/} -p user.err) ; } | |
budsstatus() { echo $(pvesm status --storage ${PVE_BUDS} 2> /dev/null | tail -1 | awk '{print $3}') ; } | |
SECONDS=0 # reset 'timer'. This is a bash built-in timer counting seconds since the start of the shell | |
if [ "$1" == "job-init" ]; then | |
loginfo "== JOB-INIT start ==" | |
loginfo "Enabling backup datastore: ${PVE_BUDS}" | |
pvesm set ${PVE_BUDS} --disable 0 | |
loginfo "Waking up the backup server: ${PBS_NAME}" | |
loginfo "$(/usr/bin/wakeonlan -i ${PBS_BROADCASTIP} ${PBS_MAC})" | |
loginfo "Waiting for backup datastore: ${PVE_BUDS}" | |
until [ "$(budsstatus)" == "active" ]; do | |
if [ ${SECONDS} -gt ${PVE_BUDS_TIMEOUT} ]; then | |
logerr "ERROR: Timeout [${PVE_BUDS_TIMEOUT}s] waiting for backup datastore: ${PVE_BUDS}" | |
exit 1 | |
fi | |
loginfo "Not active, waiting 15 secs" | |
sleep 15 | |
done | |
loginfo "Backup datastore status is: $(budsstatus)" | |
loginfo "== JOB-INIT end ==" | |
fi | |
if [ "$1" == "job-end" ]; then | |
loginfo "== JOB-INIT end ==" | |
# Wait for other backup related tasks (GC collection, prune, etc) to finish before shutting down the PBS | |
loginfo "Waiting for all tasks at ${PBS_NAME} to finish" | |
until [ "$(ssh root@${PBS_NAME} proxmox-backup-manager task list | grep running | wc -l)" == "0" ]; do | |
loginfo "Tasks are still running, waiting 15 secs" | |
sleep 15 | |
done | |
loginfo "Disabling backup datastore: ${PVE_BUDS}" | |
pvesm set ${PVE_BUDS} --disable 1 | |
loginfo "Shutting down backup server: ${PBS_NAME}" | |
ssh root@${PBS_NAME} 'init 0' | |
loginfo "== JOB-END end ==" | |
fi | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment