Loading...

Samples for the Developer SDK for Python

The following compressed file contains samples for the Python SDK: Samples_for_Python.zip. The following samples are inside the compressed file and can be modified to meet a specific need.

Authentication

The Commcell_login.py script runs CommCell log on and log off operations.

>>> from cvpysdk.commcell import Commcell
>>>
>>> commcell = Commcell('test.idcprodcert.loc', 'admin', '**********')
>>>
>>>
>>> commcell = Commcell('test.idcprodcert.loc', 'admin')
Please enter the Commcell Password: **********
>>>
>>> commcell.logout()
'User logged out'
>>>

Editing Subclient Properties

The Sub_props.py script updates subclient properties.

>>> from cvpysdk.commcell import Commcell
>>> commcell = Commcell('test.idcprodcert.loc', 'admin', 'password123')
>>> client = commcell.clients.get('test')
>>> agent = client.agents.get('File System')
>>> backupset = agent.backupsets.get('defaultBackupSet')
>>> subclient = backupset.subclients.get('test_sub')
>>>
>>> print(subclient.storage_policy)
test_policy
>>>
>>> subclient.storage_policy = 'vs_sdk_test_policy'
>>>
>>> print(subclient.storage_policy)
vs_sdk_test_policy
>>>
>>>
>>> print(subclient.content)
['C:\\inetpub', 'C:\\ProgramData', 'C:\\Python27', 'C:\\restore_test']
>>>
>>> subclient.content = ['C:\\inetpub', 'C:\\Python27', 'C:\\restore_test']
>>>
>>> print(subclient.content)
['C:\\inetpub', 'C:\\Python27', 'C:\\restore_test']
>>>
>>>
>>> print(subclient.description)

>>> subclient.description = 'Test Subclient for Python SDK'
>>>
>>> print(subclient.description)
Test Subclient for Python SDK
>>>
>>> commcell.logout()
'User logged out'
>>>

Polling the Job Status

The Job.py script runs job operations.

>>> from cvpysdk.commcell import Commcell
>>> commcell = Commcell('test.idcprodcert.loc', 'admin', 'password123')
>>> client = commcell.clients.get('test')
>>> agent = client.agents.get('File System')
>>> backupset = agent.backupsets.get('defaultBackupSet')
>>> subclient = backupset.subclients.get('test_sub')
>>>
>>> job = subclient.backup('Full')
>>>
>>> print(job.status)
Running
>>>
>>> job.pause()
>>>
>>> print(job.status)
Suspend Pending
>>>
>>> print(job.status)
Suspended
>>>
>>>
>>> print(job.pending_reason)
Backup job has been suspended by user admin. Reason:[].<br>Source: test, Process: JobManager
>>>
>>> print(job.delay_reason)
Backup job has been suspended by user admin. Reason:[].<br>Source: test, Process: JobManager
>>>
>>>
>>> print(job.backup_level)
Full
>>>
>>> job.finished
False
>>>
>>> print(job.job_type)
Backup
>>>
>>> print(job.job_id)
745
>>>
>>> job.resume()
>>>
>>> print(job.status)
Running
>>>
>>> job.kill()
>>>
>>> print(job.status)
Completed w/ one or more errors
>>>
>>> print(job.start_time)
Thu Apr 13 12:24:37 2017
>>>
>>> print(job.end_time)
Thu Apr 13 12:28:38 2017
>>>

Running Cloud Apps Backup and Restore

The Ca_bkp_rst.py script runs backup, browse, and restore operations on a Cloud Apps agent subclient.

>>> from cvpysdk.commcell import Commcell
>>> commcell = Commcell('test.idcprodcert.loc', 'admin', '**********')
>>> client = commcell.clients.get('test')
>>> agent = client.agents.get('Cloud Apps')
>>> instance = agent.instances.get('GMail-Instance')
>>> subclient = instance.subclients.get('test_sub')
>>>
>>> job = subclient.backup()
>>>
>>> print(job.status)
Running
>>>
>>> print(job.status)
Completed
>>>
>>>
>>> paths, details_dict = subclient.browse()
>>>
>>> print(paths)
['\\Gmail']
>>>
>>> print(details_dict)
{'\\Gmail': ['Gmail', 'Folder', '0B', '01/01/1970 05:30:00']}
>>>
>>>
>>> paths, details_dict = subclient.browse(paths[0])
>>>
>>> print(paths)
['\\Gmail\\jdoe@commvaultga.com']
>>>
>>> print(details_dict)
{'\\Gmail\\jdoe@commvaultga.com': ['jdoe@commvaultga.com', 'Folder', '0B', '01/01/1970 05:30:00']}
>>>
>>>
>>> job = subclient.restore_out_of_place('test', 'jdoe@commvaultga.com/automation', ['\\Gmail\\jdoe@commvaultga.com\\INBOX'])
>>>
>>> print(job.status)
Running
>>>
>>> print(job.status)
Completed
>>>
>>> commcell.logout()
'User logged out'
>>>

Running File System Backup and Restore

The Fs_bkp_brws_rest.py script runs backup, browse, and restore operations on a File System agent subclient.

>>> from cvpysdk.commcell import Commcell
>>> commcell = Commcell('test.idcprodcert.loc', 'admin', '**********')
>>> client = commcell.clients.get('test')
>>> agent = client.agents.get('File System')
>>> backupset = agent.backupsets.get('defaultBackupSet')
>>> subclient = backupset.subclients.get('test_sub')
>>>
>>> job = subclient.backup()
>>>
>>> print(job.status)
Running
>>>
>>> print(job.status)
Completed
>>>
>>>
>>> paths, details_dict = subclient.browse()
>>>
>>> print(paths)
['C:']
>>>
>>> print(details_dict)
{'C:': ['C:', 'Folder', '980.68 MB', '10/04/2017 12:28:37']}
>>>
>>>
>>> paths, details_dict = subclient.browse(paths[0])
>>>
>>> print(paths)
['C:\\inetpub', 'C:\\ProgramData', 'C:\\Python27', 'C:\\restore_test']
>>>
>>> job = subclient.restore_out_of_place('test', 'C:\\sdk_restore', ['C:\\inetpub', 'C;\\restore_test'])
>>>
>>> print(job.status)
Running
>>>
>>> print(job.status)
Completed
>>>
>>> commcell.logout()
'User logged out'
>>>

Associating a Plan with an Entity

The plan_assoc_entity.py script associates a plan with an entity.

>>> from cvpysdk import commcell
>>> commcell = commcell.Commcell("test.xyz.com", "admin", "******")
>>> test_plan = commcell.plans.get("test_plan_name")
>>> client = commcell.clients.get('test')
>>> agent = client.agents.get('File System')
>>> backupset = agent.backupsets.get('defaultBackupSet')
>>> subclient = backupset.subclients.get('test_sub')

>>> # Associates a plan to a backupset
…backupset.plan = test_plan

>>> # Associates a plan to a subclient
…subclient.plan = test_plan

Schedule Policies

The sch_pol.py script returns a list of schedule policies, a schedule object that is going to change, adds schedule policies associated with a subclient, and removes schedule policies associated with a subclient.

>>>from cvpysdk.commcell import Commcell

>>>from cvpysdk.policies.schedule_policies import OperationType

>>>commcellobj = Commcell('test.idcprodcert.loc', 'admin', '******')

>>>print(commcellobj.schedule_policies) #Returns all the schedule policies in the CommCell

>>>sch_pol_obj = commcellobj.schedule_policies.get('testsch1') #Returns a schedule object that is going to change

>>>associations = [{'clientName': 'test', 'instanceName': "DefaultInstanceName", 'appName':"File System",
'backupsetName':"defaultBackupSet" , 'subclientName': "test_sub"}]

>>>sch_pol_obj.update_associations(associations, OperationType.DELETE) #Removes schedule policies associated with a subclient

>>>associations = [{'clientName': 'test', 'instanceName': "DefaultInstanceName", 'appName':"File System",
'backupsetName':"defaultBackupSet" , 'subclientName': "test_sub"}]

>>>sch_pol_obj.update_associations(associations, OperationType.INCLUDE)#Adds schedule policies associated with a subclient

Disassociating a Plan from an Entity

The plan_disassoc_entity.py script disassociates a plan from an entity.

>>> from cvpysdk import commcell
>>> commcell = commcell.Commcell("test.xyz.com", "admin", "******")
>>> test_plan = commcell.plans.get("test_plan_name")
>>> client = commcell.clients.get('test')
>>> agent = client.agents.get('File System')
>>> backupset = agent.backupsets.get('defaultBackupSet')
>>> subclient = backupset.subclients.get('test_sub')

>>> # Dissociates any plan if associated with the backupset
…backupset.plan = None

>>> # Dissociates any plan if associated with the subclient
…subclient.plan = None

Viewing Entity Associations for a Plan

The plan_entities.py script returns a list of entities associated with a plan and a list of plans associated with an entity.

>>> from cvpysdk import commcell
>>> commcell = commcell.Commcell("test.xyz.com", "admin", "******")
>>> test_plan = commcell.plans.get("test_plan_name")

>>> # Returns list of entities associated with test_plan
…print(test_plan. associated_entities)
[{'subclientId': 1862, 'applicationId': 33, 'clientName': 'name_of_client, 'displayName': ''name_of_client', 'backupsetId': 1847, 'instanceId': 1, 'clientId': 2, 'subclientName': 'subclient_name1', 'backupsetName': '443', 'instanceName': 'DefaultInstanceName', '_type_': 7, 'appName': 'Windows File System'}, {'subclientId': 219, 'applicationId': 33, 'clientName': ' name_of_client ', 'displayName': 'name_of_client', 'backupsetId': 212, 'instanceId': 1, 'clientId': 20, 'subclientName': 'default', 'backupsetName': 'defaultBackupSet', 'instanceName': 'DefaultInstanceName', '_type_': 7, 'appName': 'Windows File System'}]

>>> client = commcell.clients.get('test')
>>> agent = client.agents.get('File System')
>>> backupset = agent.backupsets.get('defaultBackupSet')
>>> subclient = backupset.subclients.get('test_sub')

>>> # Returns the plan associated with the backupset
…print(backupset.plan)
'test_plan_name'

>>> # Returns the plan associated with the subclient
…print(subclient.plan)
'test_plan_name'

Viewing Instant Clones

The clones.py script returns a list of instant clones in XML and JSON formats.

Note: You can also use an XML file to perform clone operations (such as view, extend, and delete) using the Command Line Interface. For information about how to run Oracle Agent operations, see Oracle Commvault Command Line.

>>> import requests
>>> import sys
>>> import xml.etree.ElementTree as ET
>>> import base64
>>> user = <<username>>
>>> pwd = <<password>>
>>> server = <<CommServeName>>
>>> service = 'http://<>:81/SearchSvc/CVWebService.svc/'
>>> service = service.replace("<>", server)
>>> loginReq = '<DM2ContentIndexing_CheckCredentialReq username="<<username>>" password="<<password>>" />'
>>> loginReq = loginReq.replace("<<username>>", user)

#encode password in base64 for Python 1 and 2

>>> loginReq = loginReq.replace("<>", base64.b64encode(pwd))

#encode password in base64 for Python 3

>>> pwd = bytes(pwd, encoding='utf8')

>>> pwd = str(base64.b64encode(pwd), encoding='utf-8')

>>> loginReq = loginReq.replace("<<password>>", pwd)

#Login request built. Send the request now:
>>> r = requests.post(service + '/Login', data=loginReq)
>>> token = None

#Check response code and check if the response has an attribute "token" set


>>> if r.status_code == 200:
... root = ET.fromstring(r.text)
... if 'token' in root.attrib:
.. token = root.attrib['token']
... print("Login Successful")
... else:
... print("Login Failed")
... sys.exit(0)
... else:
... print("There was an error loggin in")

#Login successful.

#2.To get the clones list in xml format


>>> clonesRes = service + "/databases/clones"
>>> headers = {'Cookie2': token}
>>> r1 = requests.get(clonesRes, headers=headers)


>>> if r1.status_code == 200:
print('Clone list received!')
... else:
print('There was a problem in fetching the clones')
sys.exit(0)

>>> resp = r1.text
>>> print(resp)

#To get specific information of the clones

>>> clones = ET.fromstring(resp)


>>> clonedetails = clones.findall(".//clones")
>>> clonesourcedetails = clones.findall(".//clones/sourceInstance")
>>> clonestatus = clones.findall(".//clones/cloneStatus")
>>> targetinstancedetails = clones.findall(".//clones/targetInstance")

>>> res = {'clones': None}
>>> empres = []

>>> i = 0

>>> while(i< len(clonedetails)):
... tmp = {
... 'sourceInstance': None,
... 'targetInstance': None,
... 'cloneStatus': None
...
... }

... tmp.update(clonedetails[i].attrib)
... tmp['sourceInstance'] = clonesourcedetails[i]
... tmp['cloneStatus'] = clonestatus[i]
... tmp['targetInstance'] = targetinstancedetails[i]
... tempres.append(tmp)

... i+=1;

>>> res['clones'] = tempres
>>> print(res)

Last modified: 5/18/2020 2:09:48 PM