HP Matrix 7.2 KVM Private Cloud Backup and Restore

Table Of Contents
.OUTPUTS
The absolute path of the download file
.EXAMPLE
download-backup-without-curl $backupResource $sessionID https://11.111.11.111
#>
# appends URI ( obtained from previous function) to IP address
$downloadURI = $hostname + $backupResource.downloadUri
$downloadTimeout = 43200000 # 12 hours
$bufferSize = 65536 # bytes
# creates a new webrequest with appropriate headers
[net.httpsWebRequest]$downloadRequest = [net.webRequest]::create($downloadURI)
$downloadRequest.method = "GET"
$downloadRequest.AllowAutoRedirect = $TRUE
$downloadRequest.Timeout = $downloadTimeout
$downloadRequest.ReadWriteTimeout = $downloadTimeout
$downloadRequest.Headers.Add("auth", $authValue)
$downloadRequest.Headers.Add("X-API-Version", 1)
# accept either octet-stream or json to allow the response body to contain either the backup
or an exception
$downloadRequest.accept = "application/octet-stream;q=0.8,application/json"
# creates a variable that stores the path to the file location. Note: users may change this
to other file paths.
$fileDir = [environment]::GetFolderPath("Personal")
try
{
# connects to the Appliance, creates a new file with the content of the response
[net.httpsWebResponse]$response = $downloadRequest.getResponse()
$responseStream = $response.getResponseStream()
$responseStream.ReadTimeout = $downloadTimeout
#saves file as the name given by the backup ID
$filePath = $fileDir + "\" + $backupResource.id + ".bkp"
$sr = New-Object System.IO.FileStream ($filePath,[System.IO.FileMode]::create)
$responseStream.CopyTo($sr,$bufferSize)
$response.close()
$sr.close()
if ($global:interactiveMode -eq 1)
{
Write-Host "Backup download complete!"
}
}
catch [Net.WebException]
{
$errorMessage = $error[0].Exception.message
#Try to get more information about the error
try {
$errorResponse = $error[0].Exception.InnerException.Response.getResponseStream()
$sr = New-Object IO.StreamReader ($errorResponse)
$rawErrorStream = $sr.readtoend()
$error[0].Exception.InnerException.Response.close()
$errorObject = $rawErrorStream | convertFrom-Json
if (($errorObject.message.length -gt 0) -and
($errorObject.recommendedActions.length -gt 0))
{
$errorMessage = $errorObject.message + " " + $errorObject.recommendedActions
}
}
catch [System.Exception]
{
#Use exception message
}
if ($global:interactiveMode -eq 1)
{
Write-Host $errorMessage
}
else
{
Write-EventLog -EventId 100 -LogName Application -Source backup.ps1 -Message
$errorMessage
45