Advertisement
Guest User

ghettoVCB gautelund version

a guest
Mar 30th, 2010
1,426
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 14.96 KB | None | 0 0
  1. This is my patch against the 11/14/2009 version of ghettoVCB.sh by William Lam (http://communities.vmware.com/docs/DOC-8760)
  2.  
  3. Future "versions" of my patch will reside at http://www.idrift.no/gaute/ghettovcb/
  4.  
  5. The main purposes of the patch are:
  6. - Config has been decoupled from the script itself. You may still edit config variables directly in the script(s). But you may also choose to edit only the ghettoVCB.conf and leave the script untouched. Alleviates future updates, esp. if you have several servers.
  7. - More logging. E.g. if the script isn't executed, for whatever trivial reason, this gets logged as an error if logging is specified.
  8. - "clean up" of logging to console/file: William's logger()-function is used more extensively to log to file or console according to how the script is called. If file is chosen, only critical errors regarding syntax/invocation are logged to console.
  9. - non-successful exits return exit codes for those who use some kind of wrapper script.
  10. - All error messages contain ERROR:, all warnings contain WARNING:
  11. - As backup progresses, statuses are kept, so that a final status can be logged: did all, some or none of the backups succeed?
  12. - There's a minor improvement to allow inline comments in the list of VMs
  13.  
  14.  
  15. --- ghettoVCB.sh.org    2010-02-27 18:41:22.000000000 +0100
  16. +++ ghettoVCB.sh    2010-03-02 13:25:15.000000000 +0100
  17. @@ -7,6 +7,10 @@
  18.  LAST_MODIFIED_DATE=11/14/2009
  19.  
  20. +# Config variables may be edited here, directly in the script, or put in a separate
  21. +# ghettoVCB.conf in the directory with the script if you want to keep config separate
  22. +# from the script.
  23. +
  24.  # directory that all VM backups should go (e.g. /vmfs/volumes/SAN_LUN1/mybackupdir)
  25. -VM_BACKUP_VOLUME=/vmfs/volumes/dlgCore-NFS-bigboi.VM-Backups/WILLIAM_BACKUPS
  26. +VM_BACKUP_VOLUME=/vmfs/volumes/thecus/ghettoVCB/
  27.  
  28.  # Format output of VMDK backup
  29. @@ -15,5 +19,5 @@
  30.  # thin
  31.  # eagerzeroedthick
  32. -DISK_BACKUP_FORMAT=zeroedthick
  33. +DISK_BACKUP_FORMAT=thin
  34.  
  35.  # Number of backups for a given VM before deleting
  36. @@ -31,5 +35,5 @@
  37.  # the script will wait before executing a hard power off, this will be a multiple of 60seconds
  38.  # (e.g) = 3, which means this will wait up to 180seconds (3min) before it just powers off the VM
  39. -ITER_TO_WAIT_SHUTDOWN=3
  40. +ITER_TO_WAIT_SHUTDOWN=5
  41.  
  42.  # Number of iterations the script will wait before giving up on powering down the VM and ignoring it for backup
  43. @@ -45,5 +49,5 @@
  44.  
  45.  # Disk adapter type: buslogic, lsilogic or ide
  46. -ADAPTER_FORMAT=buslogic
  47. +ADAPTER_FORMAT=lsilogic
  48.  
  49.  # Include VMs memory when taking snapshot
  50. @@ -77,5 +81,17 @@
  51.  ########################## DO NOT MODIFY PAST THIS LINE ##########################
  52.  
  53. +# Source an external config file, if it exists. You may put all configuration in
  54. +# the config file ghettoVCB.conf and not change the script itself.
  55. +# Using technique from
  56. +# http://stackoverflow.com/questions/59895/can-a-bash-script-tell-what-directory-its-in
  57. +# This is the most stable way, working in ash (ESXi) and bash (ESX) alike.
  58. +# It does a cd, and it doesn't work if ghettoVCB.sh is sourced, not run.
  59. +reldir=`dirname $0`
  60. +cd $reldir
  61. +directory=`pwd`
  62. +[ -f "$directory/ghettoVCB.conf" ] && source "$directory/ghettoVCB.conf"
  63. +
  64.  LOG_LEVEL="info"
  65. +LOG_TO_STDOUT=1
  66.  VMDK_FILES_TO_BACKUP="all"
  67.  # default 15min timeout
  68. @@ -115,5 +131,4 @@
  69.          echo -e "\t$0 -f vms_to_backup -d dryrun"
  70.          echo
  71. -        exit 1
  72.  }
  73.  
  74. @@ -132,30 +147,46 @@
  75.  }
  76.  
  77. +log_syntaxerror () {
  78. +   logger "info" "ERROR: ###### No backup performed due to syntax error! ######"
  79. +}
  80. +
  81. +log_misconfigured () {
  82. +   logger "info" "ERROR: ###### No backup performed due to misconfiguration! ######"
  83. +}
  84. +
  85.  sanityCheck() {
  86.          NUM_OF_ARGS=$1
  87.  
  88. +   #log to stdout or to logfile - moved to top of sanityCheck
  89. +   if [ -z "${LOG_OUTPUT}" ]; then
  90. +       LOG_TO_STDOUT=1
  91. +       REDIRECT=/dev/null
  92. +   else
  93. +       LOG_TO_STDOUT=0
  94. +       REDIRECT=${LOG_OUTPUT}
  95. +       echo "Logging output to \"${LOG_OUTPUT}\" ..."
  96. +       touch "${LOG_OUTPUT}"
  97. +   fi
  98. +
  99.     if [[ ${NUM_OF_ARGS} -ne 2 ]] && [[ ${NUM_OF_ARGS} -ne 4 ]] && [[ ${NUM_OF_ARGS} -ne 6 ]] && [[ ${NUM_OF_ARGS} -ne 8 ]]; then
  100.                  printUsage
  101. +       logger "debug" "ERROR: Wrong number of arguments!"
  102. +       log_syntaxerror
  103. +       exit 1
  104.          fi
  105.  
  106.     if [[ ! -f "${VM_FILE}" ]] && [[ "${USE_CONF}" -eq 0 ]]; then
  107. -       echo -e "Error: \"${VM_FILE}\" is not valid file!\n"
  108. +       echo -e "ERROR: \"${VM_FILE}\" is not a valid file!\n"
  109. +       logger "info" "ERROR: \"${VM_FILE}\" is not a valid file!"
  110. +       log_misconfigured
  111.         printUsage
  112. +       exit 2
  113.     fi
  114.  
  115.     if [[ ! -d "${CONFIG_DIR}" ]] && [[ "${USE_CONF}" -eq 1 ]]; then
  116. -       echo -e "Error: \"${CONFIG_DIR}\" is not valid directory!\n"
  117. +       logger "info" "ERROR: \"${CONFIG_DIR}\" is not a valid directory!"
  118. +       log_misconfigured
  119.                  printUsage
  120. -           fi
  121. -
  122. -   #log to stdout or to logfile
  123. -        if [ -z "${LOG_OUTPUT}" ]; then
  124. -                LOG_TO_STDOUT=1
  125. -                REDIRECT=/dev/null
  126. -        else
  127. -                LOG_TO_STDOUT=0
  128. -                REDIRECT=${LOG_OUTPUT}
  129. -                echo "Logging output to \"${LOG_OUTPUT}\" ..."
  130. -                touch "${LOG_OUTPUT}"
  131. +       exit 2
  132.          fi
  133.  
  134. @@ -168,6 +199,6 @@
  135.          else
  136.                  logger "info" "ERROR: Unable to locate *vimsh*! You're not running ESX(i) 3.5+ or 4.0+!"
  137. -                echo "ERROR: Unable to locate *vimsh*! You're not running ESX(i) 3.5+ or 4.0+!"
  138. -                exit
  139. +       log_misconfigured
  140. +       exit 2
  141.          fi
  142.  
  143. @@ -180,6 +211,7 @@
  144.                          VER=3
  145.                  else
  146. -                        echo "You're not running ESX(i) 3.5+ or 4.0+!"
  147. -                        exit
  148. +           logger "info" "ERROR: You're not running ESX(i) 3.5+ or 4.0+!"
  149. +           log_misconfigured
  150. +           exit 2
  151.                  fi
  152.          fi
  153. @@ -195,7 +227,7 @@
  154.  
  155.          if [ ! "`whoami`" == "root" ]; then
  156. -                logger "info" "This script needs to be executed by \"root\"!"
  157. -       echo "ERROR: This script needs to be executed by \"root\"!"
  158. -                exit 1
  159. +       logger "info" "ERROR: This script needs to be executed by \"root\"!"
  160. +       log_misconfigured
  161. +       exit 2
  162.          fi
  163.  }
  164. @@ -264,5 +296,5 @@
  165.  dumpHostInfo() {
  166.     logger "debug" "HOST BUILD: $(vmware -v)"
  167. -   logger "debug" "HOSTNAME: $(hostname)\n"
  168. +   logger "debug" "HOSTNAME: $(hostname)"
  169.  }
  170.  
  171. @@ -284,5 +316,5 @@
  172.             done
  173.         IFS="{OLD_IFS2}"
  174. -   fi
  175. +fi
  176.  }
  177.  
  178. @@ -343,5 +375,5 @@
  179.          logger "info" "CONFIG - VM_SNAPSHOT_MEMORY = ${VM_SNAPSHOT_MEMORY}"
  180.          logger "info" "CONFIG - VM_SNAPSHOT_QUIESCE = ${VM_SNAPSHOT_QUIESCE}"
  181. -        logger "info" "CONFIG - VMDK_FILES_TO_BACKUP = ${VMDK_FILES_TO_BACKUP}\n"
  182. +   logger "info" "CONFIG - VMDK_FILES_TO_BACKUP = ${VMDK_FILES_TO_BACKUP}"
  183.  }
  184.  
  185. @@ -357,5 +389,5 @@
  186.  
  187.     IFS='
  188. -'
  189. +   '
  190.     for DIR in ${LIST_BACKUPS};
  191.     do
  192. @@ -388,5 +420,5 @@
  193.  
  194.          IFS='
  195. -'
  196. +   '
  197.          for DIR in ${LIST_BACKUPS};
  198.          do
  199. @@ -411,4 +443,7 @@
  200.  ghettoVCB() {
  201.     VM_INPUT=$1
  202. +   VM_OK=0
  203. +   VM_FAILED=0
  204. +   VMDK_FAILED=0
  205.  
  206.     captureDefaultConfigurations
  207. @@ -425,6 +460,6 @@
  208.     ORIG_IFS=${IFS}
  209.     IFS='
  210. -'
  211. -   for VM_NAME in `cat "${VM_INPUT}" | grep -v "#" | sed '/^$/d' | sed -e 's/^[[:blank:]]*//;s/[[:blank:]]*$//'`;
  212. +   '
  213. +   for VM_NAME in `cat "${VM_INPUT}" | grep -v "^#" | sed '/^$/d' | sed -e 's/#.*//' | sed -e 's/^[[:blank:]]*//;s/[[:blank:]]*$//'`;
  214.          do
  215.         VM_ID=`grep -E "\"${VM_NAME}\"" /tmp/vms_list | awk -F ";" '{print $1}' | sed 's/"//g'`
  216. @@ -447,5 +482,6 @@
  217.         #checks to see if we can pull out the VM_ID
  218.         if [ -z ${VM_ID} ]; then
  219. -           logger "info" "Error: failed to locate and extract VM_ID for ${VM_NAME}!\n"
  220. +           logger "info" "ERROR: failed to locate and extract VM_ID for ${VM_NAME}!"
  221. +           VM_FAILED=1
  222.  
  223.         elif [ "${LOG_LEVEL}" == "dryrun" ]; then
  224. @@ -467,10 +503,10 @@
  225.             IFS="${OLD_IFS}"
  226.             VMDKS=""   
  227. -           logger "dryrun" "###############################################\n"
  228. +           logger "dryrun" "###############################################"
  229.  
  230.                  #checks to see if the VM has any snapshots to start with
  231.                  elif ls "${VMX_DIR}" | grep -q delta > /dev/null 2>&1; then
  232. -                   logger "info" "Snapshot found for ${VM_NAME}, backup will not take place\n"
  233. -
  234. +           logger "info" "ERROR: Snapshot found for ${VM_NAME}, backup will not take place"
  235. +           VM_FAILED=1
  236.                  elif [[ -f "${VMX_PATH}" ]] && [[ ! -z "${VMX_PATH}" ]]; then
  237.             #nfs case and backup to root path of your NFS mount    
  238. @@ -478,6 +514,7 @@
  239.                         BACKUP_DIR="/vmfs/volumes/${NFS_LOCAL_NAME}/${NFS_VM_BACKUP_DIR}/${VM_NAME}"
  240.                                  if [[ -z ${VM_NAME} ]] || [[ -z ${NFS_LOCAL_NAME} ]] || [[ -z ${NFS_VM_BACKUP_DIR} ]]; then
  241. -                                        logger "info" "Variable BACKUP_DIR was not set properly, please ensure all required variables for non-persistent NFS backup option has been defined"
  242. -                                        exit 1
  243. +                   logger "info" "ERROR: Variable BACKUP_DIR was not set properly, please ensure all required variables for non-persistent NFS backup option has been defined."
  244. +                   log_misconfigured
  245. +                   exit 2
  246.                                  fi
  247.  
  248. @@ -486,6 +523,7 @@
  249.                         BACKUP_DIR="${VM_BACKUP_VOLUME}/${VM_NAME}"
  250.                                  if [[ -z ${VM_BACKUP_VOLUME} ]]; then
  251. -                                        logger "info" "Variable VM_BACKUP_DIR was not defined"
  252. -                                        exit 1
  253. +                   logger "info" "ERROR: Variable VM_BACKUP_DIR was not defined"
  254. +                   log_misconfigured
  255. +                   exit 2
  256.                                  fi
  257.                     fi
  258. @@ -495,6 +533,7 @@
  259.                 mkdir -p "${BACKUP_DIR}"
  260.                 if [ ! -d "${BACKUP_DIR}" ]; then
  261. -                   logger "info" "Unable to create \"${BACKUP_DIR}\"! - Ensure VM_BACKUP_VOLUME was defined correctly"
  262. -                   exit 1
  263. +                   logger "info" "ERROR: Unable to create \"${BACKUP_DIR}\"! - Ensure VM_BACKUP_VOLUME was defined correctly."
  264. +                   log_misconfigured
  265. +                   exit 2
  266.                 fi
  267.                     fi
  268. @@ -523,5 +562,5 @@
  269.                     if [ ${ENABLE_HARD_POWER_OFF} -eq 1 ]; then
  270.                         if [ ${START_ITERATION} -ge ${ITER_TO_WAIT_SHUTDOWN} ]; then
  271. -                           logger "info" "Hard power off occured for ${VM_NAME}, waited for $((ITER_TO_WAIT_SHUTDOWN*60)) seconds"
  272. +                           logger "info" "WARNING: Hard power off occured for ${VM_NAME}, waited for $((ITER_TO_WAIT_SHUTDOWN*60)) seconds"
  273.                             ${VMWARE_CMD} vmsvc/power.off ${VM_ID} > /dev/null 2>&1
  274.                             #this is needed for ESXi, even the hard power off did not take affect right away
  275. @@ -537,5 +576,6 @@
  276.                     #after certain timeout period
  277.                     if [ ${START_ITERATION} -ge ${POWER_DOWN_TIMEOUT} ]; then
  278. -                       logger "info" "Unable to power off ${VM_NAME}, waited for $((POWER_DOWN_TIMEOUT*60)) seconds! Ignoring ${VM_NAME} for backup!"
  279. +                       logger "info" "ERROR: Unable to power off ${VM_NAME}, waited for $((POWER_DOWN_TIMEOUT*60)) seconds! Ignoring ${VM_NAME} for backup!"
  280. +                       VM_FAILED=1
  281.                         CONTINUE_TO_BACKUP=0
  282.                         break
  283. @@ -566,5 +606,5 @@
  284.                     do
  285.                         if [ ${START_ITERATION} -ge ${SNAPSHOT_TIMEOUT} ]; then
  286. -                           logger "info" "Snapshot timed out, failed to create snapshot: \"${SNAPSHOT_NAME}\" for ${VM_NAME}"
  287. +                           logger "info" "ERROR: Snapshot timed out, failed to create snapshot: \"${SNAPSHOT_NAME}\" for ${VM_NAME}"
  288.                             SNAP_SUCCESS=0
  289.                             break
  290. @@ -616,4 +656,5 @@
  291.                                             elif [ "${DISK_BACKUP_FORMAT}" == "thin" ]; then
  292.                                                     ${VMKFSTOOLS_CMD} -i "${SOURCE_VMDK}" -a "${ADAPTER_FORMAT}" -d thin "${DESTINATION_VMDK}" 2>&1 | tee -a -i "${REDIRECT}"
  293. +echo
  294.                                             elif [ "${DISK_BACKUP_FORMAT}" == "eagerzeroedthick" ]; then
  295.                                     if [ "${VER}" == "4" ]; then
  296. @@ -625,4 +666,5 @@
  297.                             else
  298.                                                 logger "info" "WARNING: A physical RDM \"${SOURCE_VMDK}\" was found for ${VM_NAME}, which will not be backed up"
  299. +                               VMDK_FAILED=1
  300.                                         fi
  301.                         fi
  302. @@ -668,13 +710,17 @@
  303.                 endTimer
  304.                 if [ ${SNAP_SUCCESS} -eq 1 ]; then
  305. -                   logger "info" "Successfully completed backup for ${VM_NAME}!\n"
  306. +                   logger "info" "Successfully completed backup for ${VM_NAME}!"
  307. +                   VM_OK=1
  308.                 else
  309. -                   logger "info" "Error: Unable to backup ${VM_NAME} due to snapshot creation!\n"
  310. +                   logger "info" "ERROR: Unable to backup ${VM_NAME} due to failed snapshot creation!"
  311. +                   VM_FAILED=1
  312.                 fi
  313.                     else
  314.                 if [ ${CONTINUE_TO_BACKUP} -eq 0 ]; then
  315. -                   logger "info" "Error: Failed to backup ${VM_NAME}!\n"
  316. +                   logger "info" "ERROR: Failed to backup ${VM_NAME}!"
  317. +                   VM_FAILED=1
  318.                 else
  319. -                               logger "info" "Error: Failed to lookup ${VM_NAME}!\n"
  320. +                   logger "info" "ERROR: Failed to lookup ${VM_NAME}!"
  321. +                   VM_FAILED=1
  322.                 fi
  323.                     fi 
  324. @@ -706,5 +752,6 @@
  325.  while getopts ":f:c:l:d:" ARGS; do
  326.          case $ARGS in
  327. -                f)      VM_FILE="${OPTARG}"
  328. +       f)
  329. +       VM_FILE="${OPTARG}"
  330.                          ;;
  331.         c)
  332. @@ -720,8 +767,12 @@
  333.                  :)
  334.                          echo "Option -${OPTARG} requires an argument."
  335. +       logger "debug" "Option -${OPTARG} requires an argument."
  336. +       log_syntaxerror
  337.                          exit 1
  338.                          ;;
  339.                  *)
  340.                          usage
  341. +       logger "debug" "Erroneous command line options!"
  342. +       log_syntaxerror
  343.                          exit 1
  344.                          ;;
  345. @@ -732,5 +783,30 @@
  346.  sanityCheck $#
  347.  
  348. -logger "info" "============================== ghettoVCB LOG START ==============================\n"
  349. +logger "info" "============================== ghettoVCB LOG START =============================="
  350.  ghettoVCB ${VM_FILE}
  351. -logger "info" "============================== ghettoVCB LOG END ================================\n"
  352. +
  353. +if [[ "${LOG_TYPE}" == "dryrun" ]]; then
  354. +   FINAL_STATUS="###### Final status: OK, only a dryrun. ######"
  355. +   EXIT=0
  356. +elif [[ $VM_OK == 1 ]] && [[ $VM_FAILED == 0 ]] && [[ $VMDK_FAILED == 0 ]]; then
  357. +   FINAL_STATUS="###### Final status: All VMs backed up OK! ######"
  358. +   EXIT=0
  359. +elif [[ $VM_OK == 1 ]] && [[ $VM_FAILED == 0 ]] && [[ $VMDK_FAILED == 1 ]]; then
  360. +   FINAL_STATUS="###### Final status: WARNING: All VMs backed up, but some disk(s) failed! ######"
  361. +   EXIT=3
  362. +elif [[ $VM_OK == 1 ]] && [[ $VM_FAILED == 1 ]] && [[ $VMDK_FAILED == 0 ]]; then
  363. +   FINAL_STATUS="###### Final status: ERROR: Only some of the VMs backed up! ######"
  364. +   EXIT=4
  365. +elif [[ $VM_OK == 1 ]] && [[ $VM_FAILED == 1 ]] && [[ $VMDK_FAILED == 1 ]]; then
  366. +   FINAL_STATUS="###### Final status: ERROR: Only some of the VMs backed up, and some disk(s) failed! ######"
  367. +   EXIT=5
  368. +elif [[ $VM_OK == 0 ]] && [[ $VM_FAILED == 1 ]]; then
  369. +   FINAL_STATUS="###### Final status: ERROR: All VMs failed! ######"
  370. +   EXIT=6
  371. +elif [[ $VM_OK == 0 ]]; then
  372. +   FINAL_STATUS="###### Final status: ERROR: No VMs backed up! ######"
  373. +   EXIT=7
  374. +fi
  375. +logger "info" "$FINAL_STATUS"
  376. +logger "info" "============================== ghettoVCB LOG END ================================"
  377. +exit $EXIT
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement