Vzdump-addname.sh

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

Raw Paste

Comments 0
Login to post a comment.
  • No comments yet. Be the first.
Login to post a comment. Login or Register
We use cookies. To comply with GDPR in the EU and the UK we have to show you these.

We use cookies and similar technologies to keep this website functional (including spam protection via Google reCAPTCHA), and — with your consent — to measure usage and show ads. See Privacy.