Last active
December 12, 2025 09:48
-
-
Save danielchc/d316bec6ef9602e37715acef2b01d922 to your computer and use it in GitHub Desktop.
Backup of the vCenter Server Appliance
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| function echo_log () { | |
| echo -e "[$(date +%Y-%m-%d-%H-%M-%S)] $1" | |
| } | |
| SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) | |
| ENV_FILE="$SCRIPT_DIR/vars/$1.env" | |
| if [ ! -f $ENV_FILE ];then | |
| echo_log "The backup job failed. File $ENV_FILE does not exists." | |
| exit 1 | |
| fi | |
| source $ENV_FILE | |
| TIME=$(date +%Y-%m-%d-%H-%M-%S) | |
| VC_HOSTNAME=$(echo $VC_ADDRESS|cut -d'.' -f1) | |
| BACKUP_DIR="$BACKUP_FOLDER/$VC_HOSTNAME/" | |
| ERROR_TOLERANCE=10 | |
| LOG_DIR="$SCRIPT_DIR/logs/$VC_HOSTNAME/" | |
| BACKUP_LOGFILE="$LOG_DIR/${TIME}.log" | |
| NBFLAGS_DIR="$SCRIPT_DIR/nbflags/$VC_HOSTNAME" | |
| BCK_OK="$NBFLAGS_DIR/bck_ok" | |
| BCK_FAILED="$NBFLAGS_DIR/bck_failed" | |
| [[ ! -d $LOG_DIR ]] && mkdir -p $LOG_DIR | |
| [[ ! -d $NBFLAGS_DIR ]] && mkdir -p $NBFLAGS_DIR | |
| rm -f $NBFLAGS_DIR/* | |
| cookies=$(curl -s --output /dev/null -u "$VC_USER:$VC_PASSWORD" -X POST -k -c - "https://$VC_ADDRESS/rest/com/vmware/cis/session") | |
| task=$(cat << EOF | |
| { "piece": | |
| { | |
| "location_type":"$PROTOCOL", | |
| "comment":"Automatic backup $TIME", | |
| "parts":["$BACKUP_MODE"], | |
| "location":"$SCP_ADDRESS/$BACKUP_DIR/$TIME", | |
| "location_user":"$SCP_USER", | |
| "location_password":"$SCP_PASSWORD" | |
| } | |
| } | |
| EOF | |
| ) | |
| job=$(curl -s -k --cookie <(echo "$cookies") \ | |
| -H 'Accept:application/json' \ | |
| -H 'Content-Type:application/json' \ | |
| -X POST \ | |
| --data @<(echo "$task") \ | |
| "https://$VC_ADDRESS/rest/appliance/recovery/backup/job" \ | |
| 2>>$BACKUP_LOGFILE) | |
| ID=$(echo "$job" | awk '{if (match($0,/"id":"\w+-\w+-\w+"/)) print substr($0, RSTART+6, RLENGTH-7);}') | |
| echo_log "$job" >> $BACKUP_LOGFILE | |
| if [ "$ID" == "" ]; then | |
| echo_log "Failed to start backup job. Check log for more details." | tee -a $BACKUP_LOGFILE | |
| touch $BCK_FAILED | |
| exit 1 | |
| fi | |
| echo_log "Backup started with job id: $ID" | tee -a $BACKUP_LOGFILE | |
| STATUS="INPROGRESS" | |
| ERROR_COUNT=0 | |
| until [[ "$STATUS" == "SUCCEEDED" || "$STATUS" == "FAILED" || "$ERROR_COUNT" -gt "$ERROR_TOLERANCE" ]] | |
| do | |
| sleep 10s | |
| response=$(curl -s -k --cookie <(echo "$cookies") \ | |
| -H 'Accept:application/json' \ | |
| --globoff \ | |
| "https://$VC_ADDRESS/rest/appliance/recovery/backup/job/$ID" \ | |
| ) | |
| STATUS=$(echo $response | awk '{if (match($0,/"state":"\w+"/)) print substr($0, RSTART+9, RLENGTH-10);}') | |
| PROGRESS=$(echo $response | awk '{if (match($0,/"progress":[0-9]+/)) print substr($0, RSTART+11, RLENGTH-11);}') | |
| echo_log "$response" >> $BACKUP_LOGFILE | |
| echo_log "Backup job state: [${PROGRESS}%] $STATUS" | |
| if [ "$STATUS" == "" ]; then | |
| ERROR_COUNT=$((ERROR_COUNT+1)) | |
| echo_log "WARNING: Last request response was empty [$ERROR_COUNT/$ERROR_TOLERANCE]" | tee -a $BACKUP_LOGFILE | |
| fi | |
| done | |
| last_jobs=$(find /$BACKUP_DIR/* -maxdepth 0 -type d -ctime +$DAYS_TO_KEEP -printf "%f\n" | tr "\n" " ") | |
| if [ "$STATUS" != "SUCCEEDED" ]; then | |
| echo_log "The backup job failed. Last status was $STATUS. Check log for more details." | tee -a $BACKUP_LOGFILE | |
| touch $BCK_FAILED | |
| exit 1 | |
| fi | |
| close_session=$(curl -s -X DELETE -k --cookie <(echo "$cookies") "https://$VC_ADDRESS/rest/com/vmware/cis/session") | |
| [[ "$close_session" == "" ]] && close_session="OK" | |
| echo_log "Destroying session: $close_session" | tee -a $BACKUP_LOGFILE | |
| echo_log "Deleting last jobs: $last_jobs" | tee -a $BACKUP_LOGFILE | |
| echo_log "The backup task finished successfully." | tee -a $BACKUP_LOGFILE | |
| find /$BACKUP_DIR/* -maxdepth 1 -type d -ctime +$DAYS_TO_KEEP -exec rm -rf {} + | |
| touch $BCK_OK | |
| exit 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # If you use vCSA 6.5 change PROTOCOL to "SCP" | |
| VC_ADDRESS="vcenter.lab.local" | |
| PROTOCOL="SFTP" | |
| BACKUP_FOLDER="/mnt/vmware/" | |
| SCP_ADDRESS="output" | |
| DAYS_TO_KEEP=7 | |
| #BACKUP_MODE="common" # only DB | |
| BACKUP_MODE="seat" # total | |
| VC_USER="[email protected]" | |
| VC_PASSWORD="" | |
| SCP_USER="app-vmware--prd" | |
| SCP_PASSWORD="" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment