Automating Veeam with vCO and the Restful API

Do some of you remember my Blog Post about automating Veeam with vCO and Powershell (http://www.vcoportal.de/2013/04/vco-and-veeam-backupreplication-a-powerful-combination/)? The problem at this time was that Veeam only offers a PowerShell API. Now, some time is gone and Veeam released the Version 7 of Backup&Replication.  In this Version, Veeam offers an Enterprise Manager which offers a Restful API for automating.  Please be aware that the Restful API is only available in the Enterprise Plus Version of Veeam B&R.  The Veeam Restful API can be accessed over this URLs and ports:

Veeam Restful API HTTP URL  http://<Enterprise-Manager>:9399/api/

Veeam Restful API HTTPS URL https://<Enterprise-Manager>:9398/api/  

A comparison can be found here: http://www.veeam.com/backup-version-standard-enterprise-editions-comparison.html So, let’s start with the integration. First think what we have to do is to connect the vCO and the Veeam B&R server. Therefore we need the vCO Rest Plugin which must be installed over the vCO Plugin page. Details how to do install a plugin can be found in the vCO Documentation for the Plugin (http://pubs.vmware.com/orchestrator-plugins/index.jsp?topic=/com.vmware.using.http_rest.plugin.doc_10/GUID-D303E545-0BFE-4EEB-BAC3-7776EFCDB516.html)

After we have installed the Plugin, we can connect the vCO and the Veeam Server. Therefore we have a build in Workflow “Add a Rest Host”

In the Workflow we have to give our Restful Host a Name and have to define the URL. The settings for the Connection and Operation timeout doesn’t have to be changed.

The Authentication for the B&R Server has to set to Basic

The next thing is to chose a Session mode. You can chose between a “Shared Session” and a “per User Session”. Which you chose depends on your security settings. I will take a shared Session in the Screen-Shots. For the shared session you have to define a user and a password.

If you use a HTTPS connection, you have to accept the SSL Certificate for the B&R Server.

At the end you start the Workflow and if everything goes well, your Workflow finished without any problems.

 

The next thing you can do, is to import the Veeam Rest Schema. The Schema is located in the Path “C:\Program Files\Veeam\Backup and Replication\Enterprise Manager\schemas\RestAPI.xsd” on you B&R Server. To import the file, you have to transfer it to a Webserver so that the vCO Server can import the schema. To start the import we start the Workflow “Add schema to REST Host”. 

In the Workflow, we have to change the REST Host to which we want import the schema.

In the next screen we have to define the Web URL were the schema file is located.

If everything goes well, your workflow finished without any error and the Schema is available under the Rest Host.  

During my test, I had some problems with the Schema. So I decided to implement my task manual…… To implement the Veeam Restful API functions manually, the Veeam documentation is required. The documentation can be found here: http://helpcenter.veeam.com/backup/70/rest/overview.html

First thing which we have to do is to open a Restful API Connection from the vCO Server to the B&R Server. To connect the API two commands are used.

The first command is:

GET http://EnterpriseManager:9399/api/

and the second is

POST http://EnterpriseManager:9399/api/sessionMngr/

To send the commands to the Enterprise Manager you need authentication. Do the facts, that we configured our vCO Server for authentication, vCO handle this Authentication for us. Now, let’s post this request into a vCO Workflow. Therefore we create a new Workflow. For the Workflow we need one Input Parameter from Type REST:RestHost

In the Workflow itself we place a “Scriptable task” to insert our Code.

In this “Scriptable Task” we include this code:


var PostResponse = BRHost.createRequest("POST", "/api/sessionMngr/", null).execute();

System.log("Connection Successful:  " + PostResponse.contentAsString);

So, what does this code do? In the first line, we open the connection to the Enterprise Manager. The Authentication is done from the vCO Server (the provided Credentials during the Rest Host connection). The Second line just gives us the response of the Connections and the available things we can make. Now let’s expand this Workflow and let us see which Backup Jobs are implemented.


var PostResponse = BRHost.createRequest("POST", "/api/sessionMngr/", null).execute();

var BackupResponse = BRHost.createRequest("GET", "api/jobs?type=job", null).execute();

System.log("Backup Jobs " + BackupResponse.contentAsString);

The interesting Part here is the second line witch gets the Backups Jobs from the Server:

Backup Jobs <?xml version=”1.0″ encoding=”utf-8″?><EntityReferences xmlns=”http://www.veeam.com/ent/v1.0″ xmlns:xsd=”http://www.w3.org/2001/XMLSchema” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”><Ref UID=”urn:veeam:Job:a8f5b929-c5c8-4901-b749-d8c7ea9b8462″ Name=”Job1″ Href=”http://192.168.157.207:9399/api/jobs/a8f5b929-c5c8-4901-b749-d8c7ea9b8462″ Type=”JobReference”><Links><Link Href=”http://192.168.157.207:9399/api/backupServers/2b98f7dd-a2e5-4a53-9704-dfec9b30a320″ Name=”192.168.157.207″ Type=”BackupServerReference” Rel=”Up”/><Link Href=”http://192.168.157.207:9399/api/jobs/a8f5b929-c5c8-4901-b749-d8c7ea9b8462?format=Entity” Name=”Job1″ Type=”Job” Rel=”Alternate”/><Link Href=”http://192.168.157.207:9399/api/jobs/a8f5b929-c5c8-4901-b749-d8c7ea9b8462/backupSessions” Type=”BackupJobSessionReferenceList” Rel=”Down”/></Links></Ref></EntityReferences>

As we can see we have a Backup Job with the name “Job1”. Now let’s have a look with VMs are included into the Job. First of all, we need a Input parameter for the Jobname (we will use this to filter the results…)

After we are finished we will extend our code:

var PostResponse = BRHost.createRequest("POST", "/api/sessionMngr/", null).execute();
var BackupResponse = BRHost.createRequest("GET", "api/jobs?type=job", null).execute();
var XMLFile = XMLManager.fromString(BackupResponse.contentAsString);
var XMLelement = XMLFile.documentElement.getElementsByTagName("Ref");

for(i=0;i<XMLelement.getLength();i++){
 var BackupJob = XMLelement.item(i).getAttribute("Name")
if ( BackupJob == BackupJobName){

 var UIDBackup = XMLelement.item(i).getAttribute("UID")
 var UIDBackupJob = UIDBackup.substring(14,50)
 var VMResponse = BRHost.createRequest("GET", "/api/jobs/" + UIDBackupJob + "/includes", null).execute();

System.log("VMs included: " + VMResponse.contentAsString);
}
}

When we make a call to the Restful API we become a XML Response. We call the XMLManager and place the content into the variable XMLFile. On the next row we filter the XML response for the Word “Ref. Within the “Ref” response we search with a loop for the Attribute “Name” and place the response in  a variable. Then we check this name against the Backup Job name an extract the unique identifier for the Job. With this unique identifier we mal a job request and get the list of the included VMs.

The answer from the call is this:

[2014-02-09 23:15:06.281] [I] VMs included: <?xml version=”1.0″ encoding=”utf-8″?><ObjectsInJob xmlns=”http://www.veeam.com/ent/v1.0″ xmlns:xsd=”http://www.w3.org/2001/XMLSchema” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”><ObjectInJob Href=”http://192.168.157.207:9399/api/jobs/a8f5b929-c5c8-4901-b749-d8c7ea9b8462/includes/b39bdbf3-8333-4525-b393-110ef3ea9332″ Type=”ObjectInJob”><Links><Link Href=”http://192.168.157.207:9399/api/jobs/a8f5b929-c5c8-4901-b749-d8c7ea9b8462/includes/b39bdbf3-8333-4525-b393-110ef3ea9332″ Name=”WinXP2″ Type=”ObjectInJob” Rel=”Delete”/><Link Href=”http://192.168.157.207:9399/api/jobs/a8f5b929-c5c8-4901-b749-d8c7ea9b8462?format=Entity” Name=”Job1″ Type=”Job” Rel=”Up”/></Links><ObjectInJobId>b39bdbf3-8333-4525-b393-110ef3ea9332</ObjectInJobId><HierarchyObjRef>urn:VMware:Vm:ea294886-2e12-419b-934f-70b371e0f746.vm-15</HierarchyObjRef><Name>WinXP2</Name><DisplayName>WinXP2</DisplayName><Order>0</Order><GuestProcessingOptions><AppAwareProcessingMode>RequireSuccess</AppAwareProcessingMode><FileSystemIndexingMode>ExceptSpecifiedFolders</FileSystemIndexingMode><IncludedIndexingFolders/><ExcludedIndexingFolders><Path>%windir%</Path><Path>%ProgramFiles%</Path><Path>%TEMP%</Path></ExcludedIndexingFolders><CredentialsId/></GuestProcessingOptions></ObjectInJob></ObjectsInJob>

So with the techniques and methods shows in the post you are able to do a lot more interesting stuff with vCO and Veeam Enterprise Manager. A complete list about all the Enterprise Manager Restful API commands can be found here: http://helpcenter.veeam.com/backup/70/rest/em_web_api_reference.html Have fun with our automation and orchestrate the World 😉