Skip to content

Instantly share code, notes, and snippets.

@jeremysherriff
Last active March 16, 2022 19:14
Show Gist options
  • Save jeremysherriff/b0f9307cf83ebe318680a0eac412c7fa to your computer and use it in GitHub Desktop.
Save jeremysherriff/b0f9307cf83ebe318680a0eac412c7fa to your computer and use it in GitHub Desktop.
#!/bin/bash
# Credit for original script:
# https://pubs.vmware.com/vsphere-6-5/topic/com.vmware.vsphere.vcsapg-rest.doc/GUID-222400F3-678E-4028-874F-1F83036D2E85.html
# Enhanced to add error checking, status display etc - Jeremy Sherriff 2018
# Depends on jq (jq-linux64) from https://stedolan.github.io/jq
# TO-DO: Move to PowerCli/Powershell version.
# See https://blogs.vmware.com/PowerCLI/2018/07/automate-file-based-backup-of-vcsa.html
MYNAME=`hostname | cut -d '.' -f 1`
##### EDITABLE BY USER to specify vCenter Server instance and backup destination. #####
VC_ADDRESS=127.0.0.1
VC_USER=Administrator@VSPHERE.LOCAL
VC_PASSWORD=xxxxxxxxxxx
FTP_ADDRESS=vprdesxbackup01.yourdomain.com
FTP_USER=anonymous
FTP_PASSWORD=$MYNAME@
BACKUP_FOLDER=$MYNAME
EMAILTO=alerts@yourdomain.com
EMAILFROM=noreply_$MYNAME@yourdomain.com
############################
echo Important messages are recorded in /root/backup.log, not shown here.
# TO-DO: Add log file roll (currently just grows and grows)
echo `date +"%D %T"` ------------------------------->>/root/backup.log
echo 'Important: JSON responses parsed by ./jq see https://stedolan.github.io/jq/'>>/root/backup.log
rm -f task.json.tmp
rm -f responsebackup.tmp
rm -f responseloop.tmp
rm -f cookies.tmp
rm -f responselogon.tmp
rm -f responseversion.tmp
rm -f mailmsg.tmp
TIMESTAMP=$(date +%Y%m%d-%H%M%S)
# Authenticate with basic credentials.
echo `date +"%D %T"` Authenticating to the REST API on https://$VC_ADDRESS >>/root/backup.log
curl -s -u "$VC_USER:$VC_PASSWORD" \
-X POST \
-H 'Content-Type: application/json' \
-H 'Accept: application/json' -k \
--cookie-jar cookies.tmp \
-o responselogon.tmp \
"https://$VC_ADDRESS/rest/com/vmware/cis/session"
# Create a message body for the backup request.
cat << EOF >task.json.tmp
{ "piece":
{
"location_type":"FTP",
"comment":"Automatic backup",
"parts":["common"],
"location":"ftp://$FTP_ADDRESS/$BACKUP_FOLDER/$TIMESTAMP",
"location_user":"$FTP_USER",
"location_password":"$FTP_PASSWORD"
}
}
EOF
# Issue a request to start the backup operation.
echo `date +"%D %T"` Fetching VC Appliance version>>/root/backup.log
curl -s -k --cookie cookies.tmp \
-H 'Accept:application/json' \
-X GET \
-o responseversion.tmp \
"https://$VC_ADDRESS/rest/appliance/system/version"
echo `date +"%D %T"` ' Appliance Summary: ' `/root/jq-linux64 -r '.value.summary' responseversion.tmp`>>/root/backup.log
echo `date +"%D %T"` ' Appliance Type: ' `/root/jq-linux64 -r '.value.type' responseversion.tmp`>>/root/backup.log
echo `date +"%D %T"` ' Appliance Product: ' `/root/jq-linux64 -r '.value.product' responseversion.tmp`>>/root/backup.log
echo `date +"%D %T"` ' Appliance Version: ' `/root/jq-linux64 -r '.value.version' responseversion.tmp`>>/root/backup.log
echo `date +"%D %T"` ' Appliance Build: ' `/root/jq-linux64 -r '.value.build' responseversion.tmp`>>/root/backup.log
echo `date +"%D %T"` ' Appliance ReleaseDate:' `/root/jq-linux64 -r '.value.releasedate' responseversion.tmp`>>/root/backup.log
# Issue a request to start the backup operation.
echo `date +"%D %T"` Requesting backup task start to ftp://$FTP_ADDRESS/$BACKUP_FOLDER/$TIMESTAMP>>/root/backup.log
curl -s -k --cookie cookies.tmp \
-H 'Accept:application/json' \
-H 'Content-Type:application/json' \
-X POST \
--data @task.json.tmp -o responsebackup.tmp \
"https://$VC_ADDRESS/rest/appliance/recovery/backup/job"
# Parse the response to locate the unique identifier of the backup operation.
ID=`/root/jq-linux64 -r '.value.id' responsebackup.tmp`
BKMSG=`/root/jq-linux64 -r '.value.messages[0].default_message' responsebackup.tmp`
echo `date +"%D %T"` Backup task ID is: [$ID]>>/root/backup.log
echo `date +"%D %T"` Message was: $BKMSG>>/root/backup.log
if [[ ! $ID || $ID = "null" ]]
then
# The backup request failed for whatever reason. Print the reason, then log off REST session and exit.
echo There was a problem, check /root/backup.log for info
echo `date +"%D %T"` Backup request not successful>>/root/backup.log
echo `date +"%D %T"` Response from server was:>>/root/backup.log
/root/jq-linux64 -r '.value' responsebackup.tmp>>/root/backup.log
curl -X DELETE \
-H 'Content-Type: application/json' \
-H 'Accept: application/json' -k \
--cookie cookies.tmp \
"https://$VC_ADDRESS/rest/com/vmware/cis/session"
# Send an email
cat << EOF >mailmsg.tmp
From: <$EMAILFROM>
To: <$EMAILTO>
MIME-Version: 1.0
Content-Type: text/plain
Subject: $MYNAME Backup - Failed
Backup to ftp://$FTP_ADDRESS/$BACKUP_FOLDER/$TIMESTAMP did not start.
Details in log: $MYNAME:/root/backup.log
Last message from backup engine was:
`/root/jq-linux64 -r '.value' responsebackup.tmp`
EOF
cat mailmsg.tmp | sendmail -i -t
echo Alert email sent to $EMAILTO
rm -f task.json.tmp
rm -f responsebackup.tmp
rm -f responseloop.tmp
rm -f cookies.tmp
rm -f responselogon.tmp
rm -f responseversion.tmp
# rm -f mailmsg.tmp
exit 2
else
echo Backup running to ftp://$FTP_ADDRESS/$BACKUP_FOLDER/$TIMESTAMP
fi
# Monitor progress of the operation until it is complete.
echo Backup status is checked every 2s:
PROGRESS=INPROGRESS
until [ "$PROGRESS" != "INPROGRESS" ]
do
sleep 2s
curl -s -k --cookie cookies.tmp \
-H 'Accept:application/json' \
--globoff \
-o responseloop.tmp \
"https://$VC_ADDRESS/rest/appliance/recovery/backup/job/$ID"
PROGRESS=`/root/jq-linux64 -r '.value.state' responseloop.tmp`
echo ' ' `/root/jq-linux64 -r '.value.state' responseloop.tmp` `/root/jq-linux64 -r '.value.progress' responseloop.tmp`'% ' `/root/jq-linux64 -r '.value.messages[0].args' responseloop.tmp`
done
echo `date +"%D %T"` Backup task ended with status [$PROGRESS]>>/root/backup.log
# Report job completion and clean up temporary files.
if [ "$PROGRESS" == "SUCCEEDED" ]
then
echo Everything worked!
EXITCODE=0
# Send an email?
else
echo There was a problem, check /root/backup.log for info
echo `date +"%D %T"` Backup not successful>>/root/backup.log
echo `date +"%D %T"` Response from server was:>>/root/backup.log
/root/jq-linux64 -r '.value' responseloop.tmp>>/root/backup.log
/root/jq-linux64 -r '.value' responsebackup.tmp>>/root/backup.log
EXITCODE=1
# Send an email??
fi
# Create a message body for the backup request.
cat << EOF >mailmsg.tmp
From: <$EMAILFROM>
To: <$EMAILTO>
MIME-Version: 1.0
Content-Type: text/plain
Subject: $MYNAME Backup - $PROGRESS
Backup ran to ftp://$FTP_ADDRESS/$BACKUP_FOLDER/$TIMESTAMP
Final status was: $PROGRESS
Details in log: vprdvcsa01:/root/backup.log
Last message from backup engine was:
`/root/jq-linux64 -r '.value' responseloop.tmp`
`/root/jq-linux64 -r '.value' responsebackup.tmp`
EOF
cat mailmsg.tmp | sendmail -i -t
echo Alert email sent to $EMAILTO
# Log off the REST session
echo `date +"%D %T"` Logging off the REST session>>/root/backup.log
curl -X DELETE \
-H 'Content-Type: application/json' \
-H 'Accept: application/json' -k \
--cookie cookies.tmp \
"https://$VC_ADDRESS/rest/com/vmware/cis/session"
rm -f task.json.tmp
rm -f responsebackup.tmp
rm -f responseloop.tmp
rm -f cookies.tmp
rm -f responselogon.tmp
rm -f responseversion.tmp
# rm -f mailmsg.tmp
exit $EXITCODE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment