[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
 

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!