BASH 21
Vzdump-addname.sh By jimmyd on 18th September 2021 07:19:32 AM
  1. #!/bin/bash
  2.  
  3. ################################################################################
  4. # Script created by XpineX (xpinex at mail-e.dk) 28th of July 2014             #
  5. # Provided as is, with no warranty, guarantee or restrictions                  #
  6. #                        * USE AT YOUR OWN RISK *                              #
  7. # Feedback and suggestions are welcome                                         #
  8. #                                                                              #
  9. # The script is created for Proxmox PVE 3.2, but might work in other           #
  10. # environments as well.                                                        #
  11. # It is a hook script for vzdump (see the vzdump man page) that ensures that   #
  12. # the VM/CT name is added to the output file created by vzdump                 #
  13. ################################################################################
  14.  
  15. # Static paths to programs used by this script
  16. DIRNAME="/usr/bin/dirname"
  17. BASENAME="/usr/bin/basename"
  18. TR="/usr/bin/tr"
  19. GREP="/bin/grep"
  20. AWK="/usr/bin/awk"
  21. MV="/bin/mv"
  22. LS="/bin/ls"
  23. HEAD="/usr/bin/head"
  24. RM="/bin/rm"
  25.  
  26. STORAGECFG="/etc/pve/storage.cfg"
  27.  
  28. # function that will return true/0 (not error) if the argument is numeric
  29. function is_numeric {
  30.     if [ "$1" -eq "$1" ] 2>/dev/null; then
  31.         return 0;
  32.     else
  33.         return 1;
  34.     fi
  35. }
  36.  
  37. # function that will add the VM/CT name to the filename output by vzdump
  38. # first argument is the VMID
  39. # second argument is the full name of the file
  40. function addname {
  41.     NEWNAME=""
  42.     for parts in $($BASENAME "$2" | $TR "-" " "); do
  43.         if [ "$parts" == "$1" ]; then
  44.             NEWNAME=$(echo "$NEWNAME-$parts-$HOSTNAME")
  45.         else
  46.             NEWNAME=$(echo "$NEWNAME-$parts")
  47.         fi
  48.     done
  49.     DIR=$($DIRNAME "$2")
  50.     echo "$DIR/${NEWNAME:1}"
  51. }
  52.  
  53. # If the backup has finished, we can rename the files
  54. if [[ "$1" == "log-end" ]]; then
  55.     VMID="$3"
  56.     # Ensure that the VMID is in fact numeric before continuing
  57.     if is_numeric "$VMID"; then
  58.         DIRNAM=$($DIRNAME "$LOGFILE")
  59.         NEWLOG=$(addname $VMID "$LOGFILE")
  60.         NEWTAR=$(addname $VMID "$TARFILE")
  61.         # read the maxfiles value from the storage configuration file
  62.         MAXFILES=$($GREP -A 5 "$STOREID" $STORAGECFG | $GREP maxfiles | $AWK '{print $2}')
  63.         # if the MAXFILES variable is not numeric, we set it to 0
  64.         if ! is_numeric "$MAXFILES"; then MAXFILES=0; fi
  65.  
  66.         # rename the backup- and logfile
  67.         echo "HOOK: rename $LOGFILE to"
  68.         echo "             $NEWLOG"
  69.         $MV "$LOGFILE" "$NEWLOG"
  70.         echo "HOOK: rename $TARFILE to"
  71.         echo "             $NEWTAR"
  72.         $MV "$TARFILE" "$NEWTAR"
  73.  
  74.         # if MAXFILES is greater than 0, then we should ensure that only $MAXFILES backups
  75.         # exist, which means deleting the oldest backups
  76.         if [ "$MAXFILES" -gt 0 ]; then
  77.             # list all logfiles with VMID except the NUMFILES newest
  78.             for delfile in $($LS -tr $DIRNAM/*$VMID*.log | $HEAD -n-$MAXFILES); do
  79.                 echo "HOOK: delete old log '$delfile'"
  80.                 $RM -rf "$delfile"
  81.             done
  82.             # list all non logfiles with VMID except the NUMFILES newest
  83.             for delfile in $($LS -tr $DIRNAME/*$VMID* | $GREP -v '.log$' | $HEAD -n-$MAXFILES); do
  84.                 echo "HOOK: delete old backup '$delfile'"
  85.                 $RM -rf "$delfile"
  86.             done
  87.         fi
  88.     else
  89.         echo "HOOK-ERROR: VMID not found"
  90.     fi
  91. fi

PasteIT is for source code and general debugging text.

Login or Register to edit, delete and keep track of your pastes and more.

Raw Paste

Login or Register to edit or fork this paste. It's free.