PowerCLISimpliVityVMware

DR Automation using SimpliVity’s REST API

I was working with a partner last week brainstorming different ways the SimpliVity REST API could be leveraged to simplify and automate the recovery of workloads protected using SimpliVity’s data protection to a Disaster Recovery site. From that discussion I put together a quick powershell script to demostrate how the SimpliVity REST API could be used to automate the recovery of a predefined set of protected VMs to a disaster recovery datacenter in a SimpliVity Federation.

This script is fairly simple, but it shows how powershell and the SimpliVity REST API can be used to help simplify the recovery process. There are other actions which would also need to be taken as part of the recovery, such as powering on the VMs, connecting the network adapters to a specific PortGroup in the recovery datacenter, or changing the VMs IP addresses, each of these can also be automated using powershell and PowerCLI.

The powershell script to recover a set of VMs from SimpliVity backups is included in this post but the most up-to-date version can be found here https://github.com/herseyc/SVT/blob/master/SVTRestore-to-DR.ps1

This script allows you to define a set of VMs to restore by setting $vmstorestore to the vCenter inventory names of the VMs you want to restore into the $recoverydatacenter. The $recoverydatacenter can be set to any datacenter in the a SimpliVity Federation where SimpliVity backups for the defined virtual machines have been replicated.

When the script is run it will prompt you for an OmniStack Virtual Controller (OVC) management IP address, which can be any OVC available in the environment, and credentials to access the OVC. The script will then restore each of the $vmstorestore from the most recent backup to the $recoverydatacenter.

##################################################################
# Use PowerShell and the SimpliVity REST API  to 
# Restore Predefined VMs to DR Datacenter
#
# Usage: SVTRestore-to-DR.ps1
#
# http://www.vhersey.com/
# 
# http://www.simplivity.com/
#
##################################################################
############## BEGIN USER VARIABLES ############## 

#Define VMs to Restore
$vmstorestore = "VM01", "VM02", "VM03"

#Define Recovery Datacenter
$recoverydatacenter = "Raleigh"

############### END USER VARIABLES ###############
$ovc = Read-Host "Enter OVC Management IP Address"

$username = Read-Host "Enter OVC Username"
$pass_word = Read-Host "Enter OVC Password"

#Ignore Self Signed Certificates and set TLS
Try {
Add-Type @"
       using System.Net;
       using System.Security.Cryptography.X509Certificates;
       public class TrustAllCertsPolicy : ICertificatePolicy {
           public bool CheckValidationResult(
               ServicePoint srvPoint, X509Certificate certificate,
               WebRequest request, int certificateProblem) {
               return true;
           }
       }
"@
   [System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
   [System.Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
} Catch {
}

# Authenticate - Get SVT Access Token
$uri = "https://" + $ovc + "/api/oauth/token"
$base64 = [Convert]::ToBase64String([System.Text.UTF8Encoding]::UTF8.GetBytes("simplivity:"))
$body = @{username="$username";password="$pass_word";grant_type="password"}
$headers = @{}
$headers.Add("Authorization", "Basic $base64")
$response = Invoke-RestMethod -Uri $uri -Headers $headers -Body $body -Method Post 
    
$atoken = $response.access_token

# Create SVT Auth Header
$headers = @{}
$headers.Add("Authorization", "Bearer $atoken")

# Restore Defined VMs in DR Datacenter
foreach ($vm in $vmstorestore) {

   # Get last backup for VM in DR datacenter
   $uri = "https://" + $ovc + "/api/backups?virtual_machine_name=" + $vm + "&omnistack_cluster_name=" + $recoverydatacenter + "&limit=1"
   $response = Invoke-RestMethod -Uri $uri -Headers $headers -Method Get

   $backuptorestore = $response.backups[0].id
   $recoverydatastore = $response.backups[0].datastore_id

   if ( $backuptorestore ) {
          
      $uri = "https://" + $ovc + "/api/backups/" + $backuptorestore + "/restore?restore_original=false"

      $restoredate = Get-Date -format MMddyyyyHHmmss
      $restorename = $vm + "-dr-" + $restoredate

      $body = @{}
      $dsid = $recoverydatastore
      #### Write-Host "Datastore id: " $dsid

      $body.Add("datastore_id", "$dsid")
      $body.Add("virtual_machine_name", "$restorename") 
      $body = $body | ConvertTo-Json
      
      Write-Host "Restoring VM $vm from $backuptorestore ... "
      $response = Invoke-RestMethod -Uri $uri -Headers $headers -Body $body -Method Post -ContentType 'application/vnd.simplivity.v1+json'
   
   } else {
          
      Write-Host "Backup for $vm not found."
      
   }
   
}

Again the script will be kept up to date here: https://github.com/herseyc/SVT/blob/master/SVTRestore-to-DR.ps1

This script was developed based on a conversation around disaster recovery, but it could also be used to restore a specific set of VMs into a test/dev datacenter or to simply test the SimpliVity backups of a set of VMs.

OmniStack 3.5.2 is scheduled to be released next week which will contain a number of enhancements to SimpliVity’s REST API. Looking forward to it.

2 thoughts on “DR Automation using SimpliVity’s REST API

  • Collin

    does the job works with the 3.5.2 version ?

    when running the scipt I have got that message :

    Invoke-RestMethod : The remote server returned an error: (400) Bad Request.
    At C:\Users\UserXX\Desktop\VM.ps1:86 char:19
    + $response = Invoke-RestMethod -Uri $uri -Headers $headers -Body $body -Met …
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebExc
    eption
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

    Reply
  • when trying to run the script it works perfectly except that the restore VM is not the last one but the first backup create. any idea ?

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

5 × two =