[TUTORIAL] Editing PBS backups by adding comment/note

lazynooblet

Member
Jan 23, 2021
21
7
8
With the current implementation of Proxmox Backup Server the name of the VM itself isn't sent as part of the backup name. So I had an idea of writing a script that can check for new backups and add the VM name to the comment field.

The below bash script uses proxmox-backup-client to check for and update notes on all snapshots of a repository.

The steps are:
  1. List all snapshots
  2. Iterate list, check if there is an existing note
  3. If no note, download qemu-server.conf or pct.conf (depending on name being "ct/" or "vm/")
  4. Parse config file and fetch hostname of backed up server
  5. Add comment to snapshot

Example output of script:

1615472369185.png

Note in PBS:

1615472396671.png

To use this script, you will need PBS credentials that are capable of updating notes on the snapshots. I used the root@pam user via an API token.

Script as text:

Bash:
#!/bin/bash
export PBS_REPOSITORY='root@pam!apitoken@localhost:local'
export PBS_PASSWORD='changeme'

export PROXMOX_OUTPUT_NO_BORDER='.'
export PROXMOX_OUTPUT_NO_HEADER='.'

DEBUG=1

function _debug {
        [ $DEBUG == "1" ] || return
        echo "[$(date +%Y%m%d-%H%M%S)] ($0): $*"
}

function _error {
        echo "[$(date +%Y%m%d-%H%M%S)] ($0) ERROR: $*" 1>&2
}

function _exit {
        ec=0
        if [ "z$1" != "z" ]; then
                ec=$1
                shift
        fi
        if [ "z$1" != "z" ]; then
                _error $*
        fi
        exit ${ec}
}

while read snapshot; do
        if [ "z${snapshot}" == "z" ]; then continue; fi
        note=$(proxmox-backup-client snapshot notes show ${snapshot})
        if [ "z${note}" == "z" ]; then
                name=""
                if [[ $snapshot == "vm/"* ]]; then
                        name=$(proxmox-backup-client restore ${snapshot} qemu-server.conf - | awk '/^name: / {print $2}')
                elif [[ $snapshot == "ct/"* ]]; then
                        name=$(proxmox-backup-client restore ${snapshot} pct.conf - | awk '/^hostname: / {print $2}')
                fi

                if [ "z${name}" == "z" ]; then
                        _exit 1 "Unable to get name for snapshot ${snapshot}"
                fi

                _debug "Updating snapshot '${snapshot}' note: ${name}"
                proxmox-backup-client snapshot notes update ${snapshot} ${name}
        else
                _debug "Note already exists for snapshot '${snapshot}'"
        fi
done < <(proxmox-backup-client snapshot list | awk '{print $1}')
 
Last edited:
I posted this in the wrong forum :facepalm: -- if a mod sees this would you mind moving it to the PBS forum? Thanks :D
 
Hi :) Thank you for your work!
I back up the PVE hosts to the same datastore so I had to add handling for backups of the type "host", maybe you could add that to your script as well :) I just skipped them as the name of the snapshot can be customized at backup creation anyway.
 
Hi :) Thank you for your work!
I back up the PVE hosts to the same datastore so I had to add handling for backups of the type "host", maybe you could add that to your script as well :) I just skipped them as the name of the snapshot can be customized at backup creation anyway.
@Cookiefamily, Could you please provide your edits to the script. I would be interested in using the script as I have a similar setup. Thanks.
 
Thanks @lazynooblet :)

@ProUser - I'm not the person you asked, and I'm late to this thread but I also needed support for host backups today and added it to the script.
Hopefully we will get CLI support for also updating the backup group notes value in a future version of proxmox-backup-client.

I'll leave the changes I made so that atleast the next person can find it here, I've simply added the following two lines:

Bash:
                elif [[ $snapshot == "host/"* ]]; then
                        name=$(echo ${snapshot} | awk '{split($0,a,"/")}; {print a[2]}')

The full script including my changes:

Bash:
#!/bin/bash
export PBS_REPOSITORY='root@pam!apitoken@localhost:local'
export PBS_PASSWORD='changeme'

export PROXMOX_OUTPUT_NO_BORDER='.'
export PROXMOX_OUTPUT_NO_HEADER='.'

DEBUG=1

function _debug {
        [ $DEBUG == "1" ] || return
        echo "[$(date +%Y%m%d-%H%M%S)] ($0): $*"
}

function _error {
        echo "[$(date +%Y%m%d-%H%M%S)] ($0) ERROR: $*" 1>&2
}

function _exit {
        ec=0
        if [ "z$1" != "z" ]; then
                ec=$1
                shift
        fi
        if [ "z$1" != "z" ]; then
                _error $*
        fi
        exit ${ec}
}

while read snapshot; do
        if [ "z${snapshot}" == "z" ]; then continue; fi
        note=$(proxmox-backup-client snapshot notes show ${snapshot})
        if [ "z${note}" == "z" ]; then
                name=""
                if [[ $snapshot == "vm/"* ]]; then
                        name=$(proxmox-backup-client restore ${snapshot} qemu-server.conf - | awk '/^name: / {print $2}')
                elif [[ $snapshot == "ct/"* ]]; then
                        name=$(proxmox-backup-client restore ${snapshot} pct.conf - | awk '/^hostname: / {print $2}')
                elif [[ $snapshot == "host/"* ]]; then
                        name=$(echo ${snapshot} | awk '{split($0,a,"/")}; {print a[2]}')
                fi

                if [ "z${name}" == "z" ]; then
                        _exit 1 "Unable to get name for snapshot ${snapshot}"
                fi

                _debug "Updating snapshot '${snapshot}' note: ${name}"
                proxmox-backup-client snapshot notes update ${snapshot} ${name}
        else
                _debug "Note already exists for snapshot '${snapshot}'"
        fi
done < <(proxmox-backup-client snapshot list | awk '{print $1}')
 
Inside the PBS-gui i am able to change the note of the complete node, not only its items.
That way, even if the folder isn't open, i am able to see which VM is contained.
I wasn't able to find a command to do this via shell, thus i'm not able to extend the script to do this.
Does anyone know how to do it?
 
Inside the PBS-gui i am able to change the note of the complete node, not only its items.
That way, even if the folder isn't open, i am able to see which VM is contained.
I wasn't able to find a command to do this via shell, thus i'm not able to extend the script to do this.
Does anyone know how to do it?
AFAIK that's only available via the API, and not (yet) exposed in the client binary.
 
Thanks, shouldn't be to hard to script something via python or php...
I'll post my result here, if i get it right.
 
Hi, i'm getting the following error after the script exec:

Error: authentication failed - invalid credentials

I've created an api key though the GUI on pBX, an assigned admin permissions to it.
 

proxmox-backup-manager user list-tokens root@pam
┌───────────────────────────────┬────────┬────────┬─────────┐
│ tokenid │ enable │ expire │ comment │
╞═══════════════════════════════╪════════╪════════╪═════════╡
│ root@pam!backup-identificator │ 1 │ never │ │
└───────────────────────────────┴────────┴────────┴─────────┘


proxmox-backup-manager acl list
┌───────────────────────────────┬──────┬───────────┬────────┐
│ ugid │ path │ propagate │ roleid │
╞═══════════════════════════════╪══════╪═══════════╪════════╡
│ root@pam!backup-identificator │ / │ 1 │ Admin │
└───────────────────────────────┴──────┴───────────┴────────┘


The line used ath the script:

export PBS_REPOSITORY='root@pam!backup-identificator@localhost:local'
 
you also need to provide the API token secret value as "password"..
 

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!