REST API knowledge exchange
Post Reply
Kazz
Expert
Posts: 163
Liked: 18 times
Joined: Feb 08, 2018 3:47 am
Full Name: Kazz Beck
Contact:

list of VMs from a job that has not been started

Post by Kazz »

Hi,

Is it possible to get a list of VMs from a job that never ran?

Thanks

Code: Select all

import requests
import json
import urllib3
from urllib.parse import quote

# Disable InsecureRequestWarning
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

# Define ANSI escape codes for colors
GREEN = '\033[92m'  # Green text
RED = '\033[91m'    # Red text
RESET = '\033[0m'   # Reset to default color

# Define your Veeam server API URL and credentials
veeam_api_url = "https://vbr.server.local:9419"
username = "veeamAPI"
password = "******************" 

# Define headers
headers = {
    "Content-Type": "application/x-www-form-urlencoded",
    "accept": "application/json",
    "x-api-version": "1.2-rev0"
}

data = f"grant_type=password&username={quote(username)}&password={quote(password)}"

# Bypass SSL certificate validation (optional)
response = requests.post(f"{veeam_api_url}/api/oauth2/token", data=data, headers=headers, verify=False)
print(response)
if response.status_code == 200:
    token = f"Bearer {json.loads(response.content).get('access_token')}"
    
    # Check if the authentication was successful
    print(f"{GREEN}Successfully authenticated to the Veeam API{RESET}")
    print(f"{GREEN}========================================================================================={RESET}")
    headers['Authorization'] = token  

    #  Get backup jobs
    backup_jobs_url = f"{veeam_api_url}/api/v1/jobs/states"
    backup_jobs_response = requests.get(backup_jobs_url, headers=headers, verify=False)

    if backup_jobs_response.status_code == 200:
        backup_jobs = backup_jobs_response.json()
        # Process and display backup jobs
        for job in backup_jobs.get('data', []):
            job_name = job['name']
            job_id = job['id']
            job_type = job['type']
            job_status = job['status']
            job_lastResult = job['lastResult']
            print(f"Backup Job Name: {job_name}\nType: {job_type}\nStatus: {job_status}\nLast Result: {job_lastResult}\nJOB ID: {job_id}")
            
            # Get backup objects for the job
            backup_objects = f"{veeam_api_url}/api/v1/backups?jobIdFilter={job_id}"
            backup_objects_response = requests.get(backup_objects, headers=headers, verify=False)
            
            if backup_objects_response.status_code == 200:
                backup_objects_data = backup_objects_response.json()
                
                for obj in backup_objects_data.get("data", []):
                    print(f"Backup Object ID: {obj['id']}")
                    # print(f"{GREEN}========================================================================================={RESET}")
                    
                    # Now, fetch the list of VMs in this backup object
                    backup_object_id = obj['id']
                    backup_objects_vms_url = f"{veeam_api_url}/api/v1/backups/{backup_object_id}/objects"
                    backup_objects_vms_response = requests.get(backup_objects_vms_url, headers=headers, verify=False)
                    
                    if backup_objects_vms_response.status_code == 200:
                        vms_data = backup_objects_vms_response.json()
                        print(f" - VMs in Backup Object {backup_object_id}:")
                        for vm in vms_data.get("data", []):
                            vm_name = vm['name']
                            vm_id = vm['id']
                            print(f"VM Name: {vm_name}") # , VM ID: {vm_id}")
                    else:
                        print(f"{RED}Failed to retrieve VMs for backup object {backup_object_id}, status code: {backup_objects_vms_response.status_code}{RESET}")
                        print(backup_objects_vms_response.text)
                    print(f"{GREEN}========================================================================================={RESET}")
            else:
                print(f"Failed to retrieve backup objects, status code: {backup_objects_response.status_code}")
                print(backup_objects_response.text)
    else:
        print(f"{RED}Failed to retrieve backup jobs, status code: {backup_jobs_response.status_code}{RESET}")
        print(backup_jobs_response.text)
else:
    print(f"{RED}Authentication failed with status code: {response.status_code}{RESET}")
    print(f"{RED}{response.text}{RESET}")
    exit()

Mildur
Product Manager
Posts: 10316
Liked: 2754 times
Joined: May 13, 2017 4:51 pm
Full Name: Fabian K.
Location: Switzerland
Contact:

Re: list of VMs from a job that has not been started

Post by Mildur »

Hi Kazz

Yes, use this endpoint from the Backup Server API: https://<VBR Server>/api/v1/jobs/{id}

Your output should contain information about included + excluded VMs:

Code: Select all

"virtualMachines": {
   "includes": [
      {
         "inventoryObject": {
         "type": "VirtualMachine",
         "hostName": "172.17.52.34",
         "name": "srv45",
         "objectId": "vm-61281"
   },
   "size": "152.5 GB"
   }
   ]
Best,
Fabian
Product Management Analyst @ Veeam Software
Post Reply

Who is online

Users browsing this forum: No registered users and 16 guests