[TUTORIAL] Editing PBS backups by adding comment/note

lazynooblet

New Member
Jan 23, 2021
14
5
3
43
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:

lazynooblet

New Member
Jan 23, 2021
14
5
3
43
I posted this in the wrong forum :facepalm: -- if a mod sees this would you mind moving it to the PBS forum? Thanks :D
 

Cookiefamily

Active Member
Jan 29, 2020
132
36
33
Germany
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.
 

ProUser

New Member
Sep 1, 2021
1
0
1
32
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.
 

hwinther

New Member
Dec 3, 2020
1
0
1
37
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}')
 

sub2o5

New Member
Nov 5, 2021
24
1
3
39
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?
 

fabian

Proxmox Staff Member
Staff member
Jan 7, 2016
7,707
1,457
164
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.
 

sub2o5

New Member
Nov 5, 2021
24
1
3
39
Thanks, shouldn't be to hard to script something via python or php...
I'll post my result here, if i get it right.
 

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 your own in 60 seconds.

Buy now!