This example shows how to invoke curl from a bash script to back up the vCenter Server instance. A bash script can be invoked regularly as a cron job in the vCenter Server Appliance.

This example depends on certain variables that specify the source and destination for the backup operation. For simplicity, the variables are hard-coded at the start of the bash script.

This script does not encrypt the backup image.

#!/bin/bash
 ##### EDITABLE BY USER to specify vCenter Server instance and backup destination. #####
 VC_ADDRESS=vc_server_ip
 VC_USER=sso_user
 VC_PASSWORD=sso_pass
 FTP_ADDRESS=storage_server
 FTP_USER=ftpuser
 FTP_PASSWORD=ftpuser
 BACKUP_FOLDER=backup
 ############################

 # Authenticate with basic credentials.
 curl -u "$VC_USER:$VC_PASSWORD" \
    -X POST \
    -k --cookie-jar cookies.txt \
    "https://$VC_ADDRESS/rest/com/vmware/cis/session"

 # Create a message body for the backup request.
 TIME=$(date +%Y-%m-%d-%H-%M-%S)
 cat << EOF >task.json
 { "piece":
      {
          "location_type":"FTP",
          "comment":"Automatic backup",
          "parts":["seat"],
          "location":"ftp://$FTP_ADDRESS/$BACKUP_FOLDER/$TIME",
          "location_user":"$FTP_USER",
          "location_password":"$FTP_PASSWORD"
      }
 }
 EOF

 # Issue a request to start the backup operation.
 echo Starting backup $TIME >>backup.log
 curl -k --cookie cookies.txt \
    -H 'Accept:application/json' \
    -H 'Content-Type:application/json' \
    -X POST \
    --data @task.json 2>>backup.log >response.txt \
    "https://$VC_ADDRESS/rest/appliance/recovery/backup/job"
 cat response.txt >>backup.log
 echo '' >>backup.log

 # Parse the response to locate the unique identifier of the backup operation.
 ID=$(awk '{if (match($0,/"id":"\w+-\w+-\w+"/)) \
           print substr($0, RSTART+6, RLENGTH-7);}' \
          response.txt)
 echo 'Backup job id: '$ID

 # Monitor progress of the operation until it is complete.
 PROGRESS=INPROGRESS
 until [ "$PROGRESS" != "INPROGRESS" ]
 do
      sleep 10s
      curl -k --cookie cookies.txt \
        -H 'Accept:application/json' \
        --globoff \
        "https://$VC_ADDRESS/rest/appliance/recovery/backup/job/$ID" \
        >response.txt
      cat response.txt >>backup.log
      echo ''  >>backup.log
      PROGRESS=$(awk '{if (match($0,/"state":"\w+"/)) \
                      print substr($0, RSTART+9, RLENGTH-10);}' \
                     response.txt)
      echo 'Backup job state: '$PROGRESS
 done

 # Report job completion and clean up temporary files.
 echo ''
 echo "Backup job completion status: $PROGRESS"
 rm -f task.json
 rm -f response.txt
 rm -f cookies.txt
 echo ''  >>backup.log