[TUTORIAL] Auto-Poweroff Proxmox Backup Server after Backup finishes [Hook Script Option]

twl

New Member
Oct 30, 2021
14
11
3
My physical PBS server is set in it's BIOS to boot daily at 22:00 shortly after that, at 22:15 my PVE will start the daily backup job. To save energy I want to shutdown the PBS once the backup is finish. Here is how I was able t achieved it, with the help of @bluesite (Original Thread (German))

Preperation
Proxmox Virtual Enviroment (PVE) must be able to access the Proxmox Backup Server (PBS) without a Password using SSH.
(You need to generate a SSH-Key) Here is how to do that..


Make the Hook Skript
On the PVE crete a hook script file.

nano /usr/local/bin/vzdump-hook-script

The content of the file must be as follows:

Code:
#!/bin/bash

if [ "$1" == "job-end" ]; then
ssh root@IP-ADDRESS-OF-PBS "poweroff < /dev/null &"
fi

exit 0
NOTE: "IP-ADDRESS-OF-PBS" must be replaced with the IP of your PBS.

If needed you can add additional arguments to your script, here is a example:

Code:
if [ "$1" == "backup-start" ]; then
etherwake xx:xx:xx:xx:xx   #(mac Address of PBS Server)
ping pbsserver -c 60
fi


if [ "$1" == "backup-end" ]; then

fi

Now the file needs to be made executable with:
chmod +x /usr/local/bin/vzdump-hook-script


Add the Hook Skript to your Backup Job
Still on the PVE machine we need to add a entry to the jobs.cfg config file.

nano /etc/pve/jobs.cfg

add this entry at the bottom of your config file:

script /usr/local/bin/vzdump-hook-script

(Most settings in this file can be adjusted using the Graphical User Interface (GUI)

Done.
 
Last edited:
Thank you for this tutorial!

To wakeup my pbs I use this script instead:

Code:
#!/bin/bash

if [ "$1" == "job-init" ]; then
wakeonlan xx:xx:xx:xx:xx   #(mac Address of PBS Server)
ping IP-ADDRESS-OF-PBS -c 60
fi

if [ "$1" == "job-end" ]; then
ssh root@IP-ADDRESS-OF-PBS "poweroff < /dev/null &"
fi

exit 0

"job-init" has only been implemented very recently ("backup-start" doesn't work for me)
 
  • Like
Reactions: twl and cazz
It seems script doesn't run if PBS datastore is offline.
Didn't investigate further, ended up using crontab to wake up the PBS.
 
  • Like
Reactions: kwinz
This works great, but if I have more than 1CT/VM in /etc/pve/jobs.conf for backup, the script wait until first VM backup is done and after that shutdown the server, how can I make this script to wait until all VMs backup are done ? Thank you!
 
normaly there are more parts in this script

Code:
#!/bin/bash
vmid=$3

if [ "$1" == "job-init" ]; then
echo Job started
fi

if [ "$1" == "backup-start" ]; then
    echo "Backup started"
fi

if [ "$1" == "backup-end" ]; then
    echo "Backup finished"
fi

if [ "$1" == "job-end" ]; then
    echo "Job ended"
fi

exit 0

first part is job-init

then your backups running in the part backup-start and backup-end
if all backups are finished then
the last part job-ended is running

here is an example of the hook script hook script

for better understanding
 
  • Like
Reactions: _gabriel and merox
normaly there are more parts in this script

Code:
#!/bin/bash
vmid=$3

if [ "$1" == "job-init" ]; then
echo Job started
fi

if [ "$1" == "backup-start" ]; then
    echo "Backup started"
fi

if [ "$1" == "backup-end" ]; then
    echo "Backup finished"
fi

if [ "$1" == "job-end" ]; then
    echo "Job ended"
fi

exit 0

first part is job-init

then your backups running in the part backup-start and backup-end
if all backups are finished then
the last part job-ended is running

here is an example of the hook script hook script

for better understanding
Thank you! But I don’t understand exactly for which is used “vmid=$3”
 
this is if you want do something before or after the backup but in your case you dont need it
 
  • Like
Reactions: merox
I tried with the next script, but it's still the same, after the backup of first VM is done the server is shutdown, doesn't wait for all of them to be done, can you check please ? Thank you so much!

Code:
#!/bin/bash


if [ "$1" == "backup-start" ]; then

    echo "Backup started"

fi


if [ "$1" == "backup-end" ]; then

    echo "Backup finished"

fi


if [ "$1" == "job-end" ]; then

ssh root@server_ip "poweroff < /dev/null &"

fi


exit 0
 
can you post your /etc/pve/jobs,cfg
vzdump: backup-0f89dddd-cc02
schedule mon 21:40
enabled 1
mailnotification always
mailto mail@address
mode snapshot
notes-template {{guestname}}
prune-backups keep-last=3
script /usr/local/bin/vzdump-hook-script
storage PulsarBACKUP
vmid 101,100,830,831,832

P.S: It s a proxmox cluster with 3 nodes, all have the same script and VMs are stored on ceph
 
hm, i dont know why it does not work on your server, maybe the support can help you
can you post your backup log
you can find it under /var/log/pve/tasks
 
/var/log/pve/tasks look like this:
Code:
root@server1: /var/log/pve/tasks# ls -la total 128
drwxr-xr-× 18 root root
4096 Mar 20 23:18 .
drwxr-xr-x
3 root root
4096 Feb 27 21:38 ..
drwxr-xr-x
2 root root
4096 Mar 20 18:12 0
drwxr-xr-x
2 root root
4096 Mar 20 13:28 1
drwxr-xr-x
2 root root
4096 Mar 20 21:40 2
drwxr-xr-x
2 root root
4096 Mar 20 22:36 3
drwxr-xr-x
2 root root
4096 Mar 20 21:41 4
drwxr-xr-x
2 root root
4096 Mar 20 16:24 5
drwxr-xr-x
2 root root
4096 Mar 20 15:04 6
drwxr-xr-x
2 root root
4096 Mar 20 16:13 7
drwxr-xr-x
2 root root
4096 Mar 16 12:48 8
drwxr-xr-x
2 root root
4096 Mar 20 16:13 9
drwxr-xr-x
2 root root
4096 Mar 20 13:56 A
-KW-Yーーとーー
1 root root
1858 Mar 20 23:18 active
- rw-r--r-- 1 root root
0 Feb 27 21:39 .active. lock
drwxr-xr-× 2 root root 4096 Mar 20 16:50 B drwxr-xr-x
2 root root
4096 Mar 20 18:08 C
drwxr-xr-x
2 root root
4096 Mar 20 17:05 D
drwx-xr-x
2 root root
4096 Mar 20 15:58 E
drwxr-xr-×
2 root root
4096 Mar 20 13:51 F
-YW-Yーートーー
1 root root 48021 Mar 20 23:18 index

I think it can be related because not all VMs are in one node ? For example: 2 vm s are on server1, 2 on server 2 and 1 vm on server 3

Maybe the script should be somehow adapted for working on proxmox cluster?
 
Look very useful !!

I'd like to know if PBS needs "uptime on its own" to do housekeeping prune...
If Yes, where is the schedule defined ?
I'd like to make my PBS server "mostly powered down" except when backups jobs need it ( like this one) but also allow it to do its housekeeping.

Thanks
 
I created an enhanced bash script using the instructions above. On startup it checks the status of the PBS via ping, issues wake on lan packets, waits for the PBS to respond and waits for it to settle. On shutdown it checks the status of the PBS, issues the ssh shutdown command and waits for completion. All configurable and includes error checking / reporting, although the script always returns 0 to the host.

Bash:
#!/bin/bash

# vzdump (--script option)
# hook script to startup / shutdown remote PBS during backup operations
# v1.0.0, JamieR007, 2024

pbs_ip_address='192.168.1.100'          # ip address of the remote PBS server
pbs_mac_address='aa:bb:cc:dd:ee:ff'     # mac address of the remote PBS server
startup_ping_timeout_in_secs='60'       # max time to wait for ping response after sending wol
startup_settle_time_in_secs='60'        # time to wait after successful ping response
shutdown_wait_time_in_secs='2'          # time to wait after issuing shutdown command
ping_retries='2'                        # max number of ping attempts to confirm online status
wol_retries='2'                         # number of wol packets to send

# echoerr($msg)
echoerr()
{
  echo -e "$@" 1>&2;
}

# ping_timeout($ip_address $timeout_in_secs)
# return:
#   0 = success (host responded)
#   1 = fail (host didn't respond)
#   x = error
ping_timeout()
{
  local count=$2
  while : ; do
    echo -n "."
    ping -c 1 -w 1 -q $1 > /dev/null
    local rc=$?
    ((count = count - 1))
    if [ $rc -eq 0 ] || [ $count -lt 1 ]; then  # success or timeout
        break
    else
      if [ $rc -ne 1 ]; then
        echoerr "\nping failed ($rc)"
      fi

      sleep 1
    fi
  done

  echo
  return $rc
}

# wakeup($mac_address retries)
# return:
#   0 = success (wol packet sent)
#   x = error
wakeup()
{
  local count=$2
  while : ; do
    echo -n "."
    wakeonlan $1 > /dev/null
    local rc=$?
    ((count = count - 1))
    if [ $count -lt 1 ]; then # success or timeout
      break;
    else
      if [ $rc -ne 0 ]; then
        echoerr "\nwakeonlan failed ($rc)"
      fi

      sleep 1
    fi
  done

  echo
  return $rc
}


if [ "$1" == "job-init" ]; then
  echo "PBS Startup ($1)"
  echo -n "Checking PBS status"
  ping_timeout $pbs_ip_address $ping_retries
  if [ $? -ne 0 ]; then
    echo -n "PBS is offine, waking"
    wakeup $pbs_mac_address $wol_retries
    echo -n "Waiting for PBS to respond"
    ping_timeout $pbs_ip_address $startup_ping_timeout_in_secs
    if [ $? -eq 0 ]; then
      if [ $startup_settle_time_in_secs -ne 0 ]; then
        echo "PBS is now online, waiting to settle..."
        sleep $startup_settle_time_in_secs
      else
        echo "PBS is now online"
      fi
    else
      echoerr "PBS did not respond in time (no ping)"
    fi
  else
    echo "PBS is already online"
  fi

  echo "PBS Startup Done"
fi

if [ "$1" == "job-end" ] || [ "$1" == "job-abort" ]; then
  echo "PBS Shutdown ($1)"
  echo -n "Checking PBS status"
  ping_timeout $pbs_ip_address $ping_retries
  if [ $? -eq 0 ]; then
    echo "PBS host is online, shutting down..."
    ssh root@$pbs_ip_address "poweroff < /dev/null"
    if [ $? -eq 0 ]; then
      if [ $shutdown_wait_time_in_secs -ne 0 ]; then
        echo "PBS is now offline, waiting..."
        sleep $shutdown_wait_time_in_secs
      else
        echo "PBS is now offline"
      fi
    else
      echoerr "PBS shutdown command failed $?"
    fi
  else
    echo "PBS is already offline"
  fi

  echo "PBS Shutdown Done"
fi

exit 0
 
Last edited:

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!