This example shows how to invoke curl from a bash script to restore a vCenter Server instance in a newly deployed vCenter Server Appliance. This operation is the second phase of restoring the appliance from a backup file.

The example uses local user name and password authentication because the vSphere Automation API endpoint is not yet running when you restore the appliance. The client must connect to port 5480 for this operation.

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

This example assumes the backup image is not encrypted.

#!/bin/bash
##### EDITABLE BY USER to specify vCenter Server instance and backup location. #####
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
BACKUP_SUBFOLDER=2016-07-08-09-10-11
############################

# Create a message body for the restore request.
cat << EOF > task.json
{ "piece":
    {
        "location_type":"FTP",
        "location":"ftp://$FTP_ADDRESS/$BACKUP_FOLDER/$BACKUP_SUBFOLDER",
        "location_user":"$FTP_USER",
        "location_password":"$FTP_PASSWORD"
    }
}
EOF

# Issue a request to start the restore operation.
TIME=$(date +%Y-%m-%d-%H-%M-%S)
echo Starting restore $TIME $VC_B64_PASS >> restore.log
curl -k -u "$VC_USER:$VC_PASSWORD" \
  -H 'Accept:application/json' \
  -H 'Content-Type:application/json' \
  -X POST \
  "https://$VC_ADDRESS:5480/rest/com/vmware/appliance/recovery/restore/job" \
  --data @task.json 2>restore.log >response.txt
cat response.txt >> restore.log
echo '' >> restore.log

# Monitor progress of the operation until it is complete. 
STATE=INPROGRESS
PROGRESS=0
until [ "$STATE" != "INPROGRESS" ]
do
    echo "Restore job state: $STATE ($PROGRESS%)"
    sleep 10s
    curl -s -k -u "$VC_USER:$VC_PASSWORD" \
      -H 'Accept:application/json' \
      -H 'Content-Type:application/json' \
      -X POST -d '' \
      "https://$VC_ADDRESS:5480/rest/com/vmware/appliance/recovery/restore/job?~action=get" \
      >response.txt
    cat response.txt >> restore.log
    echo '' >> restore.log
    PROGRESS=$(awk \
               '{if (match($0,/"progress":\w+/)) print substr($0, RSTART+11,RLENGTH-11);}' \
               response.txt)
    STATE=$(awk \
            '{if (match($0,/"state":"\w+"/)) print substr($0, RSTART+9, RLENGTH-10);}' \
            response.txt)
done
# Report job completion and clean up temporary files.
echo ''
echo Restore job completed.
rm -f task.json
rm -f response.txt
echo '' >> restore.log