Proxmox backup renaming script

As you know, proxmox by default does not allow to have the VM name in the backup filename.

Fortunately it is possible to add hook scripts which will launch during the backup process.

How to add VM hostname in backup filename

Just add the line below in your /etc/vzdump.conf, replace with the corresponding script path.

script: /opt/backup_rename.sh

The script  below allows you to add the VM hostname at the end of the backup filename :

#!/bin/bash

if [ "$1" == "backup-end" ]; then

tarfile=${TARFILE}
exten=${tarfile#*.}
filename=${tarfile%.*.*}
hostname=${HOSTNAME}

mv $tarfile $filename-$hostname.$exten

echo "Backup renaming successful"

fi

Backups management script

Please note that doing so will break the backup management system built in proxmox, so it won’t limit the number of backup in a specific storage anymore. You can use the script below to remove the old backups, add it to /etc/crontab to make it run once a day for instance.

Change the age variable to the maximum age allowed for a file in days.
Change the path variable to match your setup.

#!/bin/bash
path=/path/to/backup/dump/
age=3

find $path*.lzo -type f -mtime +$age -exec /bin/rm -f {} \;
find $path*.log -type f -mtime +$age -exec /bin/rm -f {} \;

1 thought on “Proxmox backup renaming script”

  1. Hi, I have done something different from your scripts.
    The goal is to :
    1- identify the Proxmox server who made the backup
    2- identify the VM name
    3- use the retention policy from the storage configuration in Proxmox and apply only on backups originated from this server

    Hook Script :
    #/bin/bash
    #
    # @Author: Guyatic
    # @Date: 2017-11-28T16:01:12+01:00
    # @Site: https://www.guyatic.net
    # @Source: https://www.guyatic.net/2016/03/04/proxmox-backup-renaming-script/
    # @Last modified by: obinoby
    # @Last modified time: 2017-11-28T16:01:17+01:00
    if [ “$1” == “backup-end” ]; then
    tarfile=${TARFILE}
    DIR=$(dirname $tarfile)
    tarfile=$(basename $tarfile)
    exten=${tarfile#*.}
    filename=${tarfile%.*.*}
    hostname=${HOSTNAME}
    proxmox=$(cat /etc/hostname)
    mv $DIR/$tarfile $DIR/${hostname}@${proxmox}@${filename}.$exten
    fi
    exit 0

    Script to be run by cron :
    #/bin/bash
    #
    # @Author: obinoby
    # @Date: 2017-11-28T15:09:11+01:00
    # @Last modified by: obinoby
    # @Last modified time: 2017-11-28T15:09:22+01:00
    CRONFILE=”/etc/pve/vzdump.cron”
    STORECONF=”/etc/pve/storage.cfg”
    PROXMOX=$(cat /etc/hostname)
    JOBS=$(cat $CRONFILE | grep “vzdump” | grep -v “^#”)
    while read -r JOB
    do
    JOB=$(echo $JOB | sed ‘s/ \-\-/\|/g’ | sed ‘s/ root vzdump /\|/g’)
    NBF=$((1 + $(echo $JOB | awk -F”|” ‘{print NF-1}’)))
    COMPTEUR=1
    while [ $COMPTEUR -le $NBF ]
    do
    PARAM=$(echo $JOB | cut -d\| -f $COMPTEUR)
    if [ $COMPTEUR -eq 1 ]
    then
    CRON=$PARAM
    elif [ $COMPTEUR -eq 2 ]
    then
    VMs=$PARAM
    else
    VARNAME=$(echo $PARAM | cut -d’ ‘ -f1)
    VALUE=$( echo $PARAM | cut -d’ ‘ -f2)
    case $VARNAME in
    “storage”) STORAGE=$VALUE ;;
    *) ;;
    esac
    fi
    COMPTEUR=$(($COMPTEUR + 1))
    done

    RETENTION=$(cat $STORECONF | grep -A 6 $STORAGE | grep maxfile | awk ‘{print $2}’)
    DIR=”$( cat $STORECONF | grep -A 6 $STORAGE | grep path | awk ‘{print $2}’)/dump”

    for VM in $VMs
    do
    BCKFILES=$(ls $DIR | grep “\-$VM\-” | grep “@$PROXMOX” | grep -v “.log$” |sort)
    OVERNUMBER=$(($(echo “$BCKFILES” | wc -w) – $RETENTION))
    COMPTEUR=1
    while [ $COMPTEUR -le $OVERNUMBER ]
    do
    TODELETE=$(echo $BCKFILES |cut -d’ ‘ -f $COMPTEUR)
    LOGNAME=”$(echo ${TODELETE%.*.*} | cut -d@ -f3).log”
    echo “Suppression de $TODELETE”
    rm -rf $DIR/$TODELETE
    rm -rf $DIR/$LOGNAME
    COMPTEUR=$(($COMPTEUR + 1))
    done
    done
    done < <(printf '%s\n' "$JOBS")
    exit 0

Leave a Reply

Your email address will not be published. Required fields are marked *