Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- This is my patch against the 11/14/2009 version of ghettoVCB.sh by William Lam (http://communities.vmware.com/docs/DOC-8760)
- Future "versions" of my patch will reside at http://www.idrift.no/gaute/ghettovcb/
- The main purposes of the patch are:
- - 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.
- - More logging. E.g. if the script isn't executed, for whatever trivial reason, this gets logged as an error if logging is specified.
- - "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.
- - non-successful exits return exit codes for those who use some kind of wrapper script.
- - All error messages contain ERROR:, all warnings contain WARNING:
- - As backup progresses, statuses are kept, so that a final status can be logged: did all, some or none of the backups succeed?
- - There's a minor improvement to allow inline comments in the list of VMs
- --- ghettoVCB.sh.org 2010-02-27 18:41:22.000000000 +0100
- +++ ghettoVCB.sh 2010-03-02 13:25:15.000000000 +0100
- @@ -7,6 +7,10 @@
- LAST_MODIFIED_DATE=11/14/2009
- +# Config variables may be edited here, directly in the script, or put in a separate
- +# ghettoVCB.conf in the directory with the script if you want to keep config separate
- +# from the script.
- +
- # directory that all VM backups should go (e.g. /vmfs/volumes/SAN_LUN1/mybackupdir)
- -VM_BACKUP_VOLUME=/vmfs/volumes/dlgCore-NFS-bigboi.VM-Backups/WILLIAM_BACKUPS
- +VM_BACKUP_VOLUME=/vmfs/volumes/thecus/ghettoVCB/
- # Format output of VMDK backup
- @@ -15,5 +19,5 @@
- # thin
- # eagerzeroedthick
- -DISK_BACKUP_FORMAT=zeroedthick
- +DISK_BACKUP_FORMAT=thin
- # Number of backups for a given VM before deleting
- @@ -31,5 +35,5 @@
- # the script will wait before executing a hard power off, this will be a multiple of 60seconds
- # (e.g) = 3, which means this will wait up to 180seconds (3min) before it just powers off the VM
- -ITER_TO_WAIT_SHUTDOWN=3
- +ITER_TO_WAIT_SHUTDOWN=5
- # Number of iterations the script will wait before giving up on powering down the VM and ignoring it for backup
- @@ -45,5 +49,5 @@
- # Disk adapter type: buslogic, lsilogic or ide
- -ADAPTER_FORMAT=buslogic
- +ADAPTER_FORMAT=lsilogic
- # Include VMs memory when taking snapshot
- @@ -77,5 +81,17 @@
- ########################## DO NOT MODIFY PAST THIS LINE ##########################
- +# Source an external config file, if it exists. You may put all configuration in
- +# the config file ghettoVCB.conf and not change the script itself.
- +# Using technique from
- +# http://stackoverflow.com/questions/59895/can-a-bash-script-tell-what-directory-its-in
- +# This is the most stable way, working in ash (ESXi) and bash (ESX) alike.
- +# It does a cd, and it doesn't work if ghettoVCB.sh is sourced, not run.
- +reldir=`dirname $0`
- +cd $reldir
- +directory=`pwd`
- +[ -f "$directory/ghettoVCB.conf" ] && source "$directory/ghettoVCB.conf"
- +
- LOG_LEVEL="info"
- +LOG_TO_STDOUT=1
- VMDK_FILES_TO_BACKUP="all"
- # default 15min timeout
- @@ -115,5 +131,4 @@
- echo -e "\t$0 -f vms_to_backup -d dryrun"
- echo
- - exit 1
- }
- @@ -132,30 +147,46 @@
- }
- +log_syntaxerror () {
- + logger "info" "ERROR: ###### No backup performed due to syntax error! ######"
- +}
- +
- +log_misconfigured () {
- + logger "info" "ERROR: ###### No backup performed due to misconfiguration! ######"
- +}
- +
- sanityCheck() {
- NUM_OF_ARGS=$1
- + #log to stdout or to logfile - moved to top of sanityCheck
- + if [ -z "${LOG_OUTPUT}" ]; then
- + LOG_TO_STDOUT=1
- + REDIRECT=/dev/null
- + else
- + LOG_TO_STDOUT=0
- + REDIRECT=${LOG_OUTPUT}
- + echo "Logging output to \"${LOG_OUTPUT}\" ..."
- + touch "${LOG_OUTPUT}"
- + fi
- +
- if [[ ${NUM_OF_ARGS} -ne 2 ]] && [[ ${NUM_OF_ARGS} -ne 4 ]] && [[ ${NUM_OF_ARGS} -ne 6 ]] && [[ ${NUM_OF_ARGS} -ne 8 ]]; then
- printUsage
- + logger "debug" "ERROR: Wrong number of arguments!"
- + log_syntaxerror
- + exit 1
- fi
- if [[ ! -f "${VM_FILE}" ]] && [[ "${USE_CONF}" -eq 0 ]]; then
- - echo -e "Error: \"${VM_FILE}\" is not valid file!\n"
- + echo -e "ERROR: \"${VM_FILE}\" is not a valid file!\n"
- + logger "info" "ERROR: \"${VM_FILE}\" is not a valid file!"
- + log_misconfigured
- printUsage
- + exit 2
- fi
- if [[ ! -d "${CONFIG_DIR}" ]] && [[ "${USE_CONF}" -eq 1 ]]; then
- - echo -e "Error: \"${CONFIG_DIR}\" is not valid directory!\n"
- + logger "info" "ERROR: \"${CONFIG_DIR}\" is not a valid directory!"
- + log_misconfigured
- printUsage
- - fi
- -
- - #log to stdout or to logfile
- - if [ -z "${LOG_OUTPUT}" ]; then
- - LOG_TO_STDOUT=1
- - REDIRECT=/dev/null
- - else
- - LOG_TO_STDOUT=0
- - REDIRECT=${LOG_OUTPUT}
- - echo "Logging output to \"${LOG_OUTPUT}\" ..."
- - touch "${LOG_OUTPUT}"
- + exit 2
- fi
- @@ -168,6 +199,6 @@
- else
- logger "info" "ERROR: Unable to locate *vimsh*! You're not running ESX(i) 3.5+ or 4.0+!"
- - echo "ERROR: Unable to locate *vimsh*! You're not running ESX(i) 3.5+ or 4.0+!"
- - exit
- + log_misconfigured
- + exit 2
- fi
- @@ -180,6 +211,7 @@
- VER=3
- else
- - echo "You're not running ESX(i) 3.5+ or 4.0+!"
- - exit
- + logger "info" "ERROR: You're not running ESX(i) 3.5+ or 4.0+!"
- + log_misconfigured
- + exit 2
- fi
- fi
- @@ -195,7 +227,7 @@
- if [ ! "`whoami`" == "root" ]; then
- - logger "info" "This script needs to be executed by \"root\"!"
- - echo "ERROR: This script needs to be executed by \"root\"!"
- - exit 1
- + logger "info" "ERROR: This script needs to be executed by \"root\"!"
- + log_misconfigured
- + exit 2
- fi
- }
- @@ -264,5 +296,5 @@
- dumpHostInfo() {
- logger "debug" "HOST BUILD: $(vmware -v)"
- - logger "debug" "HOSTNAME: $(hostname)\n"
- + logger "debug" "HOSTNAME: $(hostname)"
- }
- @@ -284,5 +316,5 @@
- done
- IFS="{OLD_IFS2}"
- - fi
- +fi
- }
- @@ -343,5 +375,5 @@
- logger "info" "CONFIG - VM_SNAPSHOT_MEMORY = ${VM_SNAPSHOT_MEMORY}"
- logger "info" "CONFIG - VM_SNAPSHOT_QUIESCE = ${VM_SNAPSHOT_QUIESCE}"
- - logger "info" "CONFIG - VMDK_FILES_TO_BACKUP = ${VMDK_FILES_TO_BACKUP}\n"
- + logger "info" "CONFIG - VMDK_FILES_TO_BACKUP = ${VMDK_FILES_TO_BACKUP}"
- }
- @@ -357,5 +389,5 @@
- IFS='
- -'
- + '
- for DIR in ${LIST_BACKUPS};
- do
- @@ -388,5 +420,5 @@
- IFS='
- -'
- + '
- for DIR in ${LIST_BACKUPS};
- do
- @@ -411,4 +443,7 @@
- ghettoVCB() {
- VM_INPUT=$1
- + VM_OK=0
- + VM_FAILED=0
- + VMDK_FAILED=0
- captureDefaultConfigurations
- @@ -425,6 +460,6 @@
- ORIG_IFS=${IFS}
- IFS='
- -'
- - for VM_NAME in `cat "${VM_INPUT}" | grep -v "#" | sed '/^$/d' | sed -e 's/^[[:blank:]]*//;s/[[:blank:]]*$//'`;
- + '
- + for VM_NAME in `cat "${VM_INPUT}" | grep -v "^#" | sed '/^$/d' | sed -e 's/#.*//' | sed -e 's/^[[:blank:]]*//;s/[[:blank:]]*$//'`;
- do
- VM_ID=`grep -E "\"${VM_NAME}\"" /tmp/vms_list | awk -F ";" '{print $1}' | sed 's/"//g'`
- @@ -447,5 +482,6 @@
- #checks to see if we can pull out the VM_ID
- if [ -z ${VM_ID} ]; then
- - logger "info" "Error: failed to locate and extract VM_ID for ${VM_NAME}!\n"
- + logger "info" "ERROR: failed to locate and extract VM_ID for ${VM_NAME}!"
- + VM_FAILED=1
- elif [ "${LOG_LEVEL}" == "dryrun" ]; then
- @@ -467,10 +503,10 @@
- IFS="${OLD_IFS}"
- VMDKS=""
- - logger "dryrun" "###############################################\n"
- + logger "dryrun" "###############################################"
- #checks to see if the VM has any snapshots to start with
- elif ls "${VMX_DIR}" | grep -q delta > /dev/null 2>&1; then
- - logger "info" "Snapshot found for ${VM_NAME}, backup will not take place\n"
- -
- + logger "info" "ERROR: Snapshot found for ${VM_NAME}, backup will not take place"
- + VM_FAILED=1
- elif [[ -f "${VMX_PATH}" ]] && [[ ! -z "${VMX_PATH}" ]]; then
- #nfs case and backup to root path of your NFS mount
- @@ -478,6 +514,7 @@
- BACKUP_DIR="/vmfs/volumes/${NFS_LOCAL_NAME}/${NFS_VM_BACKUP_DIR}/${VM_NAME}"
- if [[ -z ${VM_NAME} ]] || [[ -z ${NFS_LOCAL_NAME} ]] || [[ -z ${NFS_VM_BACKUP_DIR} ]]; then
- - logger "info" "Variable BACKUP_DIR was not set properly, please ensure all required variables for non-persistent NFS backup option has been defined"
- - exit 1
- + logger "info" "ERROR: Variable BACKUP_DIR was not set properly, please ensure all required variables for non-persistent NFS backup option has been defined."
- + log_misconfigured
- + exit 2
- fi
- @@ -486,6 +523,7 @@
- BACKUP_DIR="${VM_BACKUP_VOLUME}/${VM_NAME}"
- if [[ -z ${VM_BACKUP_VOLUME} ]]; then
- - logger "info" "Variable VM_BACKUP_DIR was not defined"
- - exit 1
- + logger "info" "ERROR: Variable VM_BACKUP_DIR was not defined"
- + log_misconfigured
- + exit 2
- fi
- fi
- @@ -495,6 +533,7 @@
- mkdir -p "${BACKUP_DIR}"
- if [ ! -d "${BACKUP_DIR}" ]; then
- - logger "info" "Unable to create \"${BACKUP_DIR}\"! - Ensure VM_BACKUP_VOLUME was defined correctly"
- - exit 1
- + logger "info" "ERROR: Unable to create \"${BACKUP_DIR}\"! - Ensure VM_BACKUP_VOLUME was defined correctly."
- + log_misconfigured
- + exit 2
- fi
- fi
- @@ -523,5 +562,5 @@
- if [ ${ENABLE_HARD_POWER_OFF} -eq 1 ]; then
- if [ ${START_ITERATION} -ge ${ITER_TO_WAIT_SHUTDOWN} ]; then
- - logger "info" "Hard power off occured for ${VM_NAME}, waited for $((ITER_TO_WAIT_SHUTDOWN*60)) seconds"
- + logger "info" "WARNING: Hard power off occured for ${VM_NAME}, waited for $((ITER_TO_WAIT_SHUTDOWN*60)) seconds"
- ${VMWARE_CMD} vmsvc/power.off ${VM_ID} > /dev/null 2>&1
- #this is needed for ESXi, even the hard power off did not take affect right away
- @@ -537,5 +576,6 @@
- #after certain timeout period
- if [ ${START_ITERATION} -ge ${POWER_DOWN_TIMEOUT} ]; then
- - logger "info" "Unable to power off ${VM_NAME}, waited for $((POWER_DOWN_TIMEOUT*60)) seconds! Ignoring ${VM_NAME} for backup!"
- + logger "info" "ERROR: Unable to power off ${VM_NAME}, waited for $((POWER_DOWN_TIMEOUT*60)) seconds! Ignoring ${VM_NAME} for backup!"
- + VM_FAILED=1
- CONTINUE_TO_BACKUP=0
- break
- @@ -566,5 +606,5 @@
- do
- if [ ${START_ITERATION} -ge ${SNAPSHOT_TIMEOUT} ]; then
- - logger "info" "Snapshot timed out, failed to create snapshot: \"${SNAPSHOT_NAME}\" for ${VM_NAME}"
- + logger "info" "ERROR: Snapshot timed out, failed to create snapshot: \"${SNAPSHOT_NAME}\" for ${VM_NAME}"
- SNAP_SUCCESS=0
- break
- @@ -616,4 +656,5 @@
- elif [ "${DISK_BACKUP_FORMAT}" == "thin" ]; then
- ${VMKFSTOOLS_CMD} -i "${SOURCE_VMDK}" -a "${ADAPTER_FORMAT}" -d thin "${DESTINATION_VMDK}" 2>&1 | tee -a -i "${REDIRECT}"
- +echo
- elif [ "${DISK_BACKUP_FORMAT}" == "eagerzeroedthick" ]; then
- if [ "${VER}" == "4" ]; then
- @@ -625,4 +666,5 @@
- else
- logger "info" "WARNING: A physical RDM \"${SOURCE_VMDK}\" was found for ${VM_NAME}, which will not be backed up"
- + VMDK_FAILED=1
- fi
- fi
- @@ -668,13 +710,17 @@
- endTimer
- if [ ${SNAP_SUCCESS} -eq 1 ]; then
- - logger "info" "Successfully completed backup for ${VM_NAME}!\n"
- + logger "info" "Successfully completed backup for ${VM_NAME}!"
- + VM_OK=1
- else
- - logger "info" "Error: Unable to backup ${VM_NAME} due to snapshot creation!\n"
- + logger "info" "ERROR: Unable to backup ${VM_NAME} due to failed snapshot creation!"
- + VM_FAILED=1
- fi
- else
- if [ ${CONTINUE_TO_BACKUP} -eq 0 ]; then
- - logger "info" "Error: Failed to backup ${VM_NAME}!\n"
- + logger "info" "ERROR: Failed to backup ${VM_NAME}!"
- + VM_FAILED=1
- else
- - logger "info" "Error: Failed to lookup ${VM_NAME}!\n"
- + logger "info" "ERROR: Failed to lookup ${VM_NAME}!"
- + VM_FAILED=1
- fi
- fi
- @@ -706,5 +752,6 @@
- while getopts ":f:c:l:d:" ARGS; do
- case $ARGS in
- - f) VM_FILE="${OPTARG}"
- + f)
- + VM_FILE="${OPTARG}"
- ;;
- c)
- @@ -720,8 +767,12 @@
- :)
- echo "Option -${OPTARG} requires an argument."
- + logger "debug" "Option -${OPTARG} requires an argument."
- + log_syntaxerror
- exit 1
- ;;
- *)
- usage
- + logger "debug" "Erroneous command line options!"
- + log_syntaxerror
- exit 1
- ;;
- @@ -732,5 +783,30 @@
- sanityCheck $#
- -logger "info" "============================== ghettoVCB LOG START ==============================\n"
- +logger "info" "============================== ghettoVCB LOG START =============================="
- ghettoVCB ${VM_FILE}
- -logger "info" "============================== ghettoVCB LOG END ================================\n"
- +
- +if [[ "${LOG_TYPE}" == "dryrun" ]]; then
- + FINAL_STATUS="###### Final status: OK, only a dryrun. ######"
- + EXIT=0
- +elif [[ $VM_OK == 1 ]] && [[ $VM_FAILED == 0 ]] && [[ $VMDK_FAILED == 0 ]]; then
- + FINAL_STATUS="###### Final status: All VMs backed up OK! ######"
- + EXIT=0
- +elif [[ $VM_OK == 1 ]] && [[ $VM_FAILED == 0 ]] && [[ $VMDK_FAILED == 1 ]]; then
- + FINAL_STATUS="###### Final status: WARNING: All VMs backed up, but some disk(s) failed! ######"
- + EXIT=3
- +elif [[ $VM_OK == 1 ]] && [[ $VM_FAILED == 1 ]] && [[ $VMDK_FAILED == 0 ]]; then
- + FINAL_STATUS="###### Final status: ERROR: Only some of the VMs backed up! ######"
- + EXIT=4
- +elif [[ $VM_OK == 1 ]] && [[ $VM_FAILED == 1 ]] && [[ $VMDK_FAILED == 1 ]]; then
- + FINAL_STATUS="###### Final status: ERROR: Only some of the VMs backed up, and some disk(s) failed! ######"
- + EXIT=5
- +elif [[ $VM_OK == 0 ]] && [[ $VM_FAILED == 1 ]]; then
- + FINAL_STATUS="###### Final status: ERROR: All VMs failed! ######"
- + EXIT=6
- +elif [[ $VM_OK == 0 ]]; then
- + FINAL_STATUS="###### Final status: ERROR: No VMs backed up! ######"
- + EXIT=7
- +fi
- +logger "info" "$FINAL_STATUS"
- +logger "info" "============================== ghettoVCB LOG END ================================"
- +exit $EXIT
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement