Taken as a whole, this is automated backup group management in Proxmox.
Is this new? Nobody's built this before? I sure couldn't get much help with it ...
Here it is.
- In your PVE GUI, create role-designated Pools (UAT/Prod/etc). Create a NewVMs Pool (that should be empty).
- Add all VMs to a Pool. Everything gets Pool membership.
- All Backup Jobs must be Pool-based. Create a Backup Job for every Pool. (Just disable the Backup Job for Pools that you don't want to backup, but you must create the Job.) Make a Job for NewVMs Pool (which should be empty) too.
The script logic is thus ...
- If the script finds that you don't have a Backup, you must not be in a Pool ...
- So you get added to the NewVMs pool, where you safely get backups ...
... even though we forgot about you. And then we come along later to figure out which Pool you really need to go in.
Forgive my coding if there are glaring mistakes. I'll fix anything really stupid if you tell me.
And for you one-liner freaks out there, this code is optimized to be understood, not for minimal byte-count.
Code:
#2024 Generic Network Systems Proxmox team. Free to the world.
#This script adds VMs that are not backed up to a Pool called NewVMs.
#Create the Pool and a Backup Job for it before using this script.
#List YAML vmids not backed up then strip em w grep
notBackedUp="$(pvesh get /cluster/backup-info/not-backed-up -output-format=yaml \
| grep vmid)"
#strip the text, collapse the line, add commas
addToPool="$(echo "$notBackedUp" \
| cut -f4 -d" " \
| paste -sd,)"
#execute
pvesh set /pools/NewVMs -vms $addToPool
The above code runs, I don't get errors, and it does what's expected.
You'll need a cron job and save the script somewhere.
Your mileage may vary.
---------------------------------------
Other stuff:
Error checking could be added. Or logging. Or both. Sorry, its a hack. I'll quite likely build them and add them here.
The "
set /pools/" command throws errors if you are already in a pool (which shouldn't happen, but what if ..)
We have an option to override the errors and force a Pool change with "
allow-move". Pool membership is exclusive, you can only be in one.
Forcing pool membership changes could lead to unintended consequences if users that are unaware of the script were to add new VMs to the cluster and do unexpected things with Pools and Backup Jobs.
On the other hand, forcing NewVMs Pool membership on not-backed-up machines may help reign in users that are not correctly provisioning new VMs and is in fact part of the overall intent here. This is an option to be considered.
--------------------------------------
References:
pvesh
https://pve.proxmox.com/pve-docs/pvesh.1.html
pvesh /cluster/backup-info/not-backed-up
https://pve.proxmox.com/pve-docs/api-viewer/index.html#/cluster/backup-info/not-backed-up
pvesh set /pool/{poolid}
https://pve.proxmox.com/pve-docs/api-viewer/index.html#/pools/{poolid}
Proxmox API
https://pve.proxmox.com/wiki/Proxmox_VE_API