- #!/bin/bash
- ################################################################################
- # Script created by XpineX (xpinex at mail-e.dk) 28th of July 2014 #
- # Provided as is, with no warranty, guarantee or restrictions #
- # * USE AT YOUR OWN RISK * #
- # Feedback and suggestions are welcome #
- # #
- # The script is created for Proxmox PVE 3.2, but might work in other #
- # environments as well. #
- # It is a hook script for vzdump (see the vzdump man page) that ensures that #
- # the VM/CT name is added to the output file created by vzdump #
- ################################################################################
- # Static paths to programs used by this script
- DIRNAME="/usr/bin/dirname"
- BASENAME="/usr/bin/basename"
- TR="/usr/bin/tr"
- GREP="/bin/grep"
- AWK="/usr/bin/awk"
- MV="/bin/mv"
- LS="/bin/ls"
- HEAD="/usr/bin/head"
- RM="/bin/rm"
- STORAGECFG="/etc/pve/storage.cfg"
- # function that will return true/0 (not error) if the argument is numeric
- function is_numeric {
- if [ "$1" -eq "$1" ] 2>/dev/null; then
- return 0;
- else
- return 1;
- fi
- }
- # function that will add the VM/CT name to the filename output by vzdump
- # first argument is the VMID
- # second argument is the full name of the file
- function addname {
- NEWNAME=""
- for parts in $($BASENAME "$2" | $TR "-" " "); do
- if [ "$parts" == "$1" ]; then
- NEWNAME=$(echo "$NEWNAME-$parts-$HOSTNAME")
- else
- NEWNAME=$(echo "$NEWNAME-$parts")
- fi
- done
- DIR=$($DIRNAME "$2")
- echo "$DIR/${NEWNAME:1}"
- }
- # If the backup has finished, we can rename the files
- if [[ "$1" == "log-end" ]]; then
- VMID="$3"
- # Ensure that the VMID is in fact numeric before continuing
- if is_numeric "$VMID"; then
- DIRNAM=$($DIRNAME "$LOGFILE")
- NEWLOG=$(addname $VMID "$LOGFILE")
- NEWTAR=$(addname $VMID "$TARFILE")
- # read the maxfiles value from the storage configuration file
- MAXFILES=$($GREP -A 5 "$STOREID" $STORAGECFG | $GREP maxfiles | $AWK '{print $2}')
- # if the MAXFILES variable is not numeric, we set it to 0
- if ! is_numeric "$MAXFILES"; then MAXFILES=0; fi
- # rename the backup- and logfile
- echo "HOOK: rename $LOGFILE to"
- echo " $NEWLOG"
- $MV "$LOGFILE" "$NEWLOG"
- echo "HOOK: rename $TARFILE to"
- echo " $NEWTAR"
- $MV "$TARFILE" "$NEWTAR"
- # if MAXFILES is greater than 0, then we should ensure that only $MAXFILES backups
- # exist, which means deleting the oldest backups
- if [ "$MAXFILES" -gt 0 ]; then
- # list all logfiles with VMID except the NUMFILES newest
- for delfile in $($LS -tr $DIRNAM/*$VMID*.log | $HEAD -n-$MAXFILES); do
- echo "HOOK: delete old log '$delfile'"
- $RM -rf "$delfile"
- done
- # list all non logfiles with VMID except the NUMFILES newest
- for delfile in $($LS -tr $DIRNAME/*$VMID* | $GREP -v '.log$' | $HEAD -n-$MAXFILES); do
- echo "HOOK: delete old backup '$delfile'"
- $RM -rf "$delfile"
- done
- fi
- else
- echo "HOOK-ERROR: VMID not found"
- fi
- fi
Recent Pastes