Solved

Automate backup of selected VMs from a VMGroup using RESTAPI

  • 13 November 2021
  • 8 replies
  • 293 views

Userlevel 2
Badge +4

Hello All

 

We take adhoc backups every day for patching activities and we search for each and every client in Command Center and trigger incremental backups for those clients(more than 100 every day). Is there a way that we can trigger the backup for these clients using RESTAPI. Foreach through these clients and trigger the incremental backup. I was able to authenticate and generate the authentication token but cannot trigger the backups for those clients. Its my first time with RESTAPIs. Would be grateful if someone can point in the right direction. I am using PoweShell.

 

Thanks

icon

Best answer by Onno van den Berg 15 November 2021, 16:22

View original

8 replies

Userlevel 7
Badge +19

Hi Abdul,

There is a special SDK available for Powershell that can do the trick. This will save you a lot of time because there is no need to look into the API documentation. It is available on Github see: GitHub - Commvault/CVPowershellSDK: Developer SDK - Powershell

Regards,

Onno
 

Userlevel 2
Badge +4

Hi Abdul,

There is a special SDK available for Powershell that can do the trick. This will save you a lot of time because there is no need to look into the API documentation. It is available on Github see: GitHub - Commvault/CVPowershellSDK: Developer SDK - Powershell

Regards,

Onno
 

I tried PowerShell module but that is very slow and is not flexible. It gets the job done but the objects it returns lack the values that I need so I have to run multiple GET commands to prepare the command that I need to trigger the backup. For instance:

If I need to run the backup for VirtualMachine1 hosted on Hyperviser1.

  • I have to first use Get-CVVirtualMachine which takes 36 seconds to return the object. 
  • I then have to get the psudoclient name from the returned object and run Get-CVClient to get its Name(Not the displayName). Takes 3 seconds to return the object. 
  • At this point I have all the info that I need to trigger the backup with Backup-CVVirtualMachine.

It takes nearly 45 seconds to trigger a VM backup. I tried running this from inside the Commserv server. 

Userlevel 7
Badge +19

Hi Abdul,

Not sure if the "performance" is related to you specific environment due to high load on the CommServe or a high amount of client computers but I bet you that direct API communication gives back the same result when it comes to timings. The SDKs are there to simplify the consumption of the APIs and int hat sense also use the APIs.

I'm not sure why you have to run the Get-CVVirtualMachine command but if you already now the name of the VM that needs to be protected then you can submit the VM directly. It is clear that retrieving a list of all the VMs in the environment with their properties takes time so if that is the "issue" than you will have to program some kind of cache yourself in where your store the results so in that case you save 36 seconds. Additionally but I assume I do not have to point you to the idea to store the results in a variable. 

Last suggestion I can give is to post your script here so it can be reviewed.

Onno

PS: Do you know the API is documented on https://api.commvault.com ?

Userlevel 2
Badge +4

Hi Abdul,

Not sure if the "performance" is related to you specific environment due to high load on the CommServe or a high amount of client computers but I bet you that direct API communication gives back the same result when it comes to timings. The SDKs are there to simplify the consumption of the APIs and int hat sense also use the APIs.

I'm not sure why you have to run the Get-CVVirtualMachine command but if you already now the name of the VM that needs to be protected then you can submit the VM directly. It is clear that retrieving a list of all the VMs in the environment with their properties takes time so if that is the "issue" than you will have to program some kind of cache yourself in where your store the results so in that case you save 36 seconds. Additionally but I assume I do not have to point you to the idea to store the results in a variable. 

Last suggestion I can give is to post your script here so it can be reviewed.

Onno

PS: Do you know the API is documented on https://api.commvault.com ?

Thanks for the response. The reason I have to run Get-CVVirtualMachine is that Backup-CVVirtualMachine requires to add the ClientName in the command parameters. Here is the script:


function triggerCVBackups {
param (
$Servers
)
$JObs = @()
foreach ($server in $servers) {
$JOb = $null
$Vm = Get-CVVirtualMachine -Name $server
$pseudoClient = (get-cvclient -id $Vm.pseudoClient.clientId).get_Item("clientName")
$backupJob = Backup-CVVirtualMachine -Name $vm.client.clientName -ClientName $pseudoClient
$Job = [PSCustomObject]@{
VMName = $VM.Name
JobId = $backupJob.jobIds
}
$Jobs += $Job
}
return $JObs
}




 

Userlevel 7
Badge +19

Hi Abdul,

You should take out the Get-CVVirtualMachine out of the existing function and enrich the metadata that is required to execute the Backup-CVVirtualMachine upfront so that you only have to parse the required information into the function. 

That way you only execute the Get-CVVirtualMachine one time. 

Onno
 

Userlevel 2
Badge +4

Hi Abdul,

You should take out the Get-CVVirtualMachine out of the existing function and enrich the metadata that is required to execute the Backup-CVVirtualMachine upfront so that you only have to parse the required information into the function. 

That way you only execute the Get-CVVirtualMachine one time. 

Onno
 

Thank you. That will improve the performance. And to your point about the performance of PowerShell and API calls, I did a quick comparison:
 

Both the command return the same number of results but PowerShell commands take more time. 

Userlevel 7
Badge +19

I have not looked in the lines of code in the SDK but I assume adding 16 seconds per VM is still to much depending on how many VMs you want to process ;-)

Anyway best is to run it outside of the function so you only have to pull the information into memory ones. 

Badge +6

Hi,

 

I am calling the same script through Workflow but its failing with error message: 

 

Below is the script:

$string = {xpath:{/workflow/inputs/VM/clientName}}
$string = $string -replace('\[','')
$string = $string -replace('\]','')
$string = $string -replace(' ','')
#$input=$input -replace('[','')
$Servers =$string.Split(",")
$plainText = "”
$secureString = ConvertTo-SecureString $plainText -AsPlainText -Force
cd 'D:\CVPowershellSDK-master'
$Jobs = @()
Connect-CVServer -Server test -User abc -Password $secureString
$joboutput= $(foreach ($i in $Servers)
{
$JOb = $null
        $Vm = Get-CVVirtualMachine -Name $($i)
        $pseudoClient = (get-cvclient -id $Vm.pseudoClient.clientId).get_Item("clientName")
        $backupJob = Backup-CVVirtualMachine -Name $Vm.client.clientName -ClientName $pseudoClient
        $Job = [PSCustomObject]@{
                VMName = $Vm.Name
                JobId = $backupJob.jobIds
        }    
        $Jobs += $Job
    })
    echo $joboutput

 

 

Error Message:

server1
server2
INFO: Get-CVVirtualMachine: virtual machine not found having name [server1]
INFO: Get-CVClient: client not found having id [0]
You cannot call a method on a null-valued expression.

 

Similary for server2

Reply