def getStoredBytesHistory(): params = {} payload = {} r = c42Lib.executeRequest("get", "/api/storedBytesHistory", params, payload) content = r.content binary = json.loads(content) latestStoredNumber = binary['data'][0][1]['archiveBytes'] return latestStoredNumber
c42Lib.cp_password = getpass.getpass() c42Lib.cp_logLevel = "INFO" c42Lib.cp_logFileName = "pushRestore.log" c42Lib.setLoggingLevel() sourceComputer="C02M50TZFH04" targetComputer="ROBERTS-WIN7" restoreFile="/Users/amir.kader/Documents/test/amir.png" restorePath="C:/Users/Robert/Desktop/restore" print 'calling api/Computer to get guid of target computer: ' + targetComputer params = {} params['q'] = targetComputer params['active'] = 'true' payload = {} r = c42Lib.executeRequest("get", c42Lib.cp_api_computer, params, payload) content = r.content binary = json.loads(content) targetGUID = binary['data']['computers'][0]['guid'] print 'target computers guid: ' + targetGUID print 'calling api/Computer to get guid of source computer: ' + sourceComputer + ' and serverName...' params = {} params['q'] = sourceComputer params['incBackupUsage'] = 'true' params['active'] = 'true' payload = {} r = c42Lib.executeRequest("get", c42Lib.cp_api_computer, params, payload) content = r.content binary = json.loads(content)
c42Lib.cp_password = getpass.getpass() c42Lib.cp_logLevel = "INFO" c42Lib.cp_logFileName = "pushRestore.log" c42Lib.setLoggingLevel() sourceComputer = "C02M50TZFH04" targetComputer = "ROBERTS-WIN7" restoreFile = "/Users/amir.kader/Documents/test/amir.png" restorePath = "C:/Users/Robert/Desktop/restore" print 'calling api/Computer to get guid of target computer: ' + targetComputer params = {} params['q'] = targetComputer params['active'] = 'true' payload = {} r = c42Lib.executeRequest("get", c42Lib.cp_api_computer, params, payload) content = r.content binary = json.loads(content) targetGUID = binary['data']['computers'][0]['guid'] print 'target computers guid: ' + targetGUID print 'calling api/Computer to get guid of source computer: ' + sourceComputer + ' and serverName...' params = {} params['q'] = sourceComputer params['incBackupUsage'] = 'true' params['active'] = 'true' payload = {} r = c42Lib.executeRequest("get", c42Lib.cp_api_computer, params, payload) content = r.content binary = json.loads(content)
def storage_server(self, plan_uid=None, device_guid=None): authority_host = self.console.cp_host authority_port = self.console.cp_port if not device_guid and not plan_uid: raise Exception( "You need a device_guid or plan_uid to authorize a storage server." ) if device_guid: self.log.debug('>>> Get backup planUid from deviceGuid %s.' % device_guid) # Get backup planUid from deviceGuid. params = {'sourceComputerGuid': device_guid, 'planTypes': 'BACKUP'} r = self.console.executeRequest("get", self.console.cp_api_plan, params, {}) backup_plans_response = json.loads(r.content.decode("UTF-8")) backup_plans = backup_plans_response[ 'data'] if 'data' in backup_plans_response else None if backup_plans and len(backup_plans) > 0: plan_uid = backup_plans[0]['planUid'] if not plan_uid: raise Exception( "There are no backup planUid's for this device. Backup likely has not started, or deviceGuid is invalid." ) self.log.debug('>>> Get storage locations for planUid %s.' % plan_uid) # Get Storage locations for planUid. r = self.console.executeRequest( "get", "%s/%s" % (self.console.cp_api_storage, plan_uid), {}, {}) storage_destinations_response = json.loads(r.content.decode("UTF-8")) storage_destinations = storage_destinations_response[ 'data'] if 'data' in storage_destinations_response else None if len(storage_destinations) == 0: raise Exception( "There is no storage location for this device archive. Backup has likely not started." ) if not self.__all_destinations: self.log.debug( '>>> Get information about all storage destinations.') # Get information about all storage destinations. r = self.console.executeRequest("get", self.console.cp_api_destination, {}, {}) all_destinations_response = json.loads(r.content.decode("UTF-8")) self.__all_destinations = all_destinations_response['data'][ 'destinations'] if 'data' in all_destinations_response else None all_destinations = self.__all_destinations if not self.__all_servers: self.log.debug('>>> Get information about all storage servers.') # Get information about all storage servers. r = self.console.executeRequest("get", self.console.cp_api_server, {}, {}) all_servers_response = json.loads(r.content.decode("UTF-8")) self.__all_servers = all_servers_response['data'][ 'servers'] if 'data' in all_servers_response else None all_servers = self.__all_servers # TODO: We need to ping each server and choose the fastest one, rather than going down the list serially. for destination_guid, server in storage_destinations.items(): destination = [ x for x in all_destinations if x['guid'] == destination_guid ][0] server_full = ( [x for x in all_servers if x['websiteHost'] == server['url']][:1] or [None])[0] if server_full and server_full['type'] == 'SERVER': # No special authorization is required for MASTER servers. self.log.debug( ">>> Private MASTER server authorization complete.") # Private MASTER server authorization complete. break else: # Storage is on a separate server, and we need to authorize against it. self.log.debug( ">>> Checking connection URL accuracy for storage server.") # Checking connection URL accuracy for storage server. payload = { "planUid": str(plan_uid), "destinationGuid": destination_guid } r = c42Lib.executeRequest("post", c42Lib.cp_api_storageAuthToken, {}, payload) storage_singleUseToken_response = json.loads( r.content.decode("UTF-8")) if not 'data' in storage_singleUseToken_response: raise Exception( "Failed to get storage auth token from destinationGuid %s - [%s]: %s." % (destination_guid, storage_singleUseToken_response[0]['name'], storage_singleUseToken_response[0]['description'])) storage_url = urllib.parse.urlparse( storage_singleUseToken_response['data']['serverUrl']) storage_host = "%s://%s" % (storage_url.scheme, storage_url.hostname) storage_port = str( storage_url.port) if storage_url.port else '' storage_singleUseToken = storage_singleUseToken_response[ 'data']['loginToken'] self.console.cp_host = storage_host self.console.cp_port = storage_port if destination['type'] == 'PROVIDER': self.log.debug( ">>> Authorizing for a PROVIDER server (Code42 Hybrid Cloud, etc. not owned by the master server)." ) # Authorizing for a PROVIDER server (Code42 Hybrid Cloud, etc. not owned by the master server). c42Lib.cp_authorization = "LOGIN_TOKEN %s" % storage_singleUseToken r = c42Lib.executeRequest("post", c42Lib.cp_api_authToken, {}, {}) storage_authToken_response = json.loads( r.content.decode("UTF-8")) c42Lib.cp_authorization = "TOKEN %s-%s" % ( storage_authToken_response['data'][0], storage_authToken_response['data'][1]) self.log.debug( ">>> Shared PROVIDER server authorization complete.") # Shared PROVIDER server authorization complete. else: self.log.debug( ">>> Private STORAGE server authorization complete.") # Private STORAGE server authorization complete. break try: yield self.console finally: self.console.cp_host = authority_host self.console.cp_port = authority_port
def storage_server(self, plan_uid=None, device_guid=None): authority_host = self.console.cp_host authority_port = self.console.cp_port if not device_guid and not plan_uid: raise Exception("You need a device_guid or plan_uid to authorize a storage server.") if device_guid: self.log.debug('>>> Get backup planUid from deviceGuid %s.' % device_guid) # Get backup planUid from deviceGuid. params = { 'sourceComputerGuid': device_guid, 'planTypes': 'BACKUP' } r = self.console.executeRequest("get", self.console.cp_api_plan, params, {}) backup_plans_response = json.loads(r.content.decode("UTF-8")) backup_plans = backup_plans_response['data'] if 'data' in backup_plans_response else None if backup_plans and len(backup_plans) > 0: plan_uid = backup_plans[0]['planUid'] if not plan_uid: raise Exception("There are no backup planUid's for this device. Backup likely has not started, or deviceGuid is invalid.") self.log.debug('>>> Get storage locations for planUid %s.' % plan_uid) # Get Storage locations for planUid. r = self.console.executeRequest("get", "%s/%s" % (self.console.cp_api_storage, plan_uid), {}, {}) storage_destinations_response = json.loads(r.content.decode("UTF-8")) storage_destinations = storage_destinations_response['data'] if 'data' in storage_destinations_response else None if len(storage_destinations) == 0: raise Exception("There is no storage location for this device archive. Backup has likely not started.") if not self.__all_destinations: self.log.debug('>>> Get information about all storage destinations.') # Get information about all storage destinations. r = self.console.executeRequest("get", self.console.cp_api_destination, {}, {}) all_destinations_response = json.loads(r.content.decode("UTF-8")) self.__all_destinations = all_destinations_response['data']['destinations'] if 'data' in all_destinations_response else None all_destinations = self.__all_destinations if not self.__all_servers: self.log.debug('>>> Get information about all storage servers.') # Get information about all storage servers. r = self.console.executeRequest("get", self.console.cp_api_server, {}, {}) all_servers_response = json.loads(r.content.decode("UTF-8")) self.__all_servers = all_servers_response['data']['servers'] if 'data' in all_servers_response else None all_servers = self.__all_servers # TODO: We need to ping each server and choose the fastest one, rather than going down the list serially. for destination_guid, server in storage_destinations.items(): destination = [x for x in all_destinations if x['guid'] == destination_guid][0] server_full = ([x for x in all_servers if x['websiteHost'] == server['url']][:1] or [None])[0] if server_full and server_full['type'] == 'SERVER': # No special authorization is required for MASTER servers. self.log.debug(">>> Private MASTER server authorization complete.") # Private MASTER server authorization complete. break else: # Storage is on a separate server, and we need to authorize against it. self.log.debug(">>> Checking connection URL accuracy for storage server.") # Checking connection URL accuracy for storage server. payload = { "planUid": str(plan_uid), "destinationGuid": destination_guid } r = c42Lib.executeRequest("post", c42Lib.cp_api_storageAuthToken, {}, payload) storage_singleUseToken_response = json.loads(r.content.decode("UTF-8")) if not 'data' in storage_singleUseToken_response: raise Exception("Failed to get storage auth token from destinationGuid %s - [%s]: %s." % (destination_guid, storage_singleUseToken_response[0]['name'], storage_singleUseToken_response[0]['description'])) storage_url = urllib.parse.urlparse(storage_singleUseToken_response['data']['serverUrl']) storage_host = "%s://%s" % (storage_url.scheme, storage_url.hostname) storage_port = str(storage_url.port) if storage_url.port else '' storage_singleUseToken = storage_singleUseToken_response['data']['loginToken'] self.console.cp_host = storage_host self.console.cp_port = storage_port if destination['type'] == 'PROVIDER': self.log.debug(">>> Authorizing for a PROVIDER server (Code42 Hybrid Cloud, etc. not owned by the master server).") # Authorizing for a PROVIDER server (Code42 Hybrid Cloud, etc. not owned by the master server). c42Lib.cp_authorization = "LOGIN_TOKEN %s" % storage_singleUseToken r = c42Lib.executeRequest("post", c42Lib.cp_api_authToken, {}, {}) storage_authToken_response = json.loads(r.content.decode("UTF-8")) c42Lib.cp_authorization = "TOKEN %s-%s" % (storage_authToken_response['data'][0], storage_authToken_response['data'][1]) self.log.debug(">>> Shared PROVIDER server authorization complete.") # Shared PROVIDER server authorization complete. else: self.log.debug(">>> Private STORAGE server authorization complete.") # Private STORAGE server authorization complete. break try: yield self.console finally: self.console.cp_host = authority_host self.console.cp_port = authority_port