Time based expiration

jlauro

Active Member
Feb 10, 2024
163
51
28
Generally PBS retention and expiration works fine. However, if you recycle vms frequently, and don't reuse vmid numbers the last backups never go away. This is somewhat related to https://bugzilla.proxmox.com/show_bug.cgi?id=6074 which isn't exactly the same, but at least provides a way to go through thousands of snapshots and forget the oldest ones...

Anyways, fill in the PBS_REPOSITORY and PBS_PASSWORD along with changing the number of days and it will find all your old backups, generally from vm that have either gone away, or moved to a different namespace or changed vmid. It will do a dry-run as is and you can pipe it's output to bash (be sure to have PBS_REPOSITORY and PBS_PASSWORD export in the environment if you | bash), or you can drop the echo from 4 lines from the bottom so it runs instead of echoing.

This functionality really should be in the gui, but... at least this provides some automation to clearing out old vms...

Bash:
#! /bin/bash

export PBS_REPOSITORY='root@pam@127.0.0.1:8007:backupdata'
export PBS_PASSWORD='REDACTED'
OLDEST=$( date -d "180 days ago" +%s )

for NS in $( proxmox-backup-client namespace list ) ; do
  proxmox-backup-client snapshot list --ns "$NS" |
    grep vm | awk '{ print $2 }' | sed 's#/# #g' |
    while read vm id dt ; do
      if [ "$vm" != "vm" ] ; then
        echo "Found $vm when expecting snapshot to start with vm"
        echo "Aborting script"
        exit 1
      fi
      if [ $( date -d "$dt" +%s ) -lt $OLDEST ] ; then
        echo proxmox-backup-client snapshot forget --ns="$NS" "$vm/$id/$dt"
      fi
    done
done