ucscsdk/utils/ucscbackup.py
def _backup(handle, file_dir, file_name, timeout=600,
            remote_enabled=False, protocol=None,
            hostname="localhost", username=None, password="",
            remove_from_ucsc=False,
            preserve_pooled_values=False):
    """
    _backup internal method helps create UcsCentral full-state backup and
    download it locally or to remote location.

    Args:
        handle (UcscHandle): UcsCentral Connection handle
        file_dir (str): directory to download backup file to
        file_name (str): name for the backup file
        timeout (number) : time in seconds for which method waits
                           for the backUp file to generate before it exits.
        remote_enabled (boolean): True if Remote backup is enabled
                                  False - by default
        protocol (str): Transfer protocol for remote backup
                        ['ftp','sftp','tftp','scp']
        hostname (str): Hostname/IP for the remote backup
        username (str): Username for remote backup
        password (str): Password for remote backup
        remove_from_ucsc (boolean): True/False, False - by default
        preserve_pooled_values (boolean): True/False, False - by default

    Example:
        file_dir = "/home/user/backup"\n
        file_name = "config_backup.tgz"\n
        _backup(handle, file_dir=file_dir, file_name=file_name)\n

        _backup(handle, file_dir=file_dir, file_name=file_name,
                remote_enabled=True, protocol='scp',hostname='192.168.1.1',
                username='admin',password='password')\n

    """
    from ..mometa.mgmt.MgmtBackup import MgmtBackup, MgmtBackupConsts
    from ..mometa.top.TopSystem import TopSystem

    backup_type = "full-state"

    if not file_dir:
        raise UcscValidationException("Missing file_dir argument")
    if not file_name:
        raise UcscValidationException("Missing file_name argument")

    top_system = TopSystem()

    if remote_enabled:
        _validate_remote_host_args(protocol, hostname, username, password)
        if (not file_name.endswith('.tgz')):
            raise UcscValidationException(
                "file_name must be .tgz format")

        file_path = os.path.join(file_dir, file_name)
        mgmt_backup = MgmtBackup(
                parent_mo_or_dn=top_system,
                hostname=hostname,
                admin_state=MgmtBackupConsts.ADMIN_STATE_ENABLED,
                proto=protocol,
                type=backup_type,
                remote_file=file_path,
                user=username,
                pwd=password)
    else:
        if not os.path.exists(file_dir):
            os.makedirs(file_dir)

        file_string = platform.node().lower() \
            + datetime.datetime.now().strftime("%Y%m%d%H%M%S")
        file_path = "/" + file_string + "_" + backup_type + "_backup.tgz"

        mgmt_backup = MgmtBackup(
                parent_mo_or_dn=top_system,
                hostname=hostname,
                admin_state=MgmtBackupConsts.ADMIN_STATE_ENABLED,
                proto=MgmtBackupConsts.PROTO_HTTP,
                type=backup_type,
                remote_file=file_path)

    if preserve_pooled_values:
        mgmt_backup.preserve_pooled_values = \
            MgmtBackupConsts.PRESERVE_POOLED_VALUES_YES
    else:
        mgmt_backup.preserve_pooled_values = \
            MgmtBackupConsts.PRESERVE_POOLED_VALUES_NO

    handle.add_mo(mgmt_backup, modify_present=True)
    handle.commit()

    mgmt_backup = handle.query_dn(dn=mgmt_backup.dn)
    admin_state_temp = mgmt_backup.admin_state
    # Checking for the backup to compete.
    duration = timeout
    poll_interval = 2

    log.debug("Starting Backup ")
    while True:
        mgmt_backup = handle.query_dn(dn=mgmt_backup.dn)
        admin_state_temp = mgmt_backup.admin_state

        # Break condition:- if state id disabled then break
        if admin_state_temp == MgmtBackupConsts.ADMIN_STATE_DISABLED:
            break

        time.sleep(min(duration, poll_interval))
        duration = max(0, (duration - poll_interval))
        if duration == 0:
            handle.remove_mo(mgmt_backup)
            handle.commit()
            raise UcscOperationError(
                  "Backup UcsCentral", " operation timed out")

    if remote_enabled:
        if mgmt_backup.over_all_status == \
                MgmtBackupConsts.OVER_ALL_STATUS_FAILED:
            log.debug("Backup failed")
            handle.remove_mo(mgmt_backup)
            handle.commit()
            raise UcscOperationError(
                "Backup UcsCentral", "%s" % mgmt_backup.fsm_rmt_inv_err_descr)

    if not remote_enabled:
        file_source = "backupfile" + file_path
        if handle.is_local_download_supported():
            try:
                log.debug("Starting Download ")
                handle.file_download(url_suffix=file_source,
                                     file_dir=file_dir,
                                     file_name=file_name)
            except Exception as err:
                log.debug("Download backup Failed")
                UcscWarning(str(err))
        else:
            log.error("Local download not supported from sdk for this "
                      "version of UcsCentral, skipping it")

    if remove_from_ucsc:
        # remove backup from UcsCentral
        log.debug("Removing backup from UcsCentral")
        handle.remove_mo(mgmt_backup)
        handle.commit()

    _backup internal method helps create UcsCentral full-state backup and
    download it locally or to remote location.

    Args:
        handle (UcscHandle): UcsCentral Connection handle
        file_dir (str): directory to download backup file to
        file_name (str): name for the backup file
        timeout (number) : time in seconds for which method waits
                           for the backUp file to generate before it exits.
        remote_enabled (boolean): True if Remote backup is enabled
                                  False - by default
        protocol (str): Transfer protocol for remote backup
                        ['ftp','sftp','tftp','scp']
        hostname (str): Hostname/IP for the remote backup
        username (str): Username for remote backup
        password (str): Password for remote backup
        remove_from_ucsc (boolean): True/False, False - by default
        preserve_pooled_values (boolean): True/False, False - by default

    Example:
        file_dir = "/home/user/backup"\n
        file_name = "config_backup.tgz"\n
        _backup(handle, file_dir=file_dir, file_name=file_name)\n

        _backup(handle, file_dir=file_dir, file_name=file_name,
                remote_enabled=True, protocol='scp',hostname='192.168.1.1',
                username='admin',password='password')\n

    
_backup internal method helps create UcsCentral full-state backup and
download it locally or to remote location.
_backup internal method helps create UcsCentral full - state backup and download it locally or to remote location .
def _backup(handle, file_dir, file_name, timeout=600,
            remote_enabled=False, protocol=None,
            hostname="localhost", username=None, password="",
            remove_from_ucsc=False,
            preserve_pooled_values=False):
    from ..mometa.mgmt.MgmtBackup import MgmtBackup, MgmtBackupConsts
    from ..mometa.top.TopSystem import TopSystem
    backup_type = "full-state"
    if not file_dir:
        raise UcscValidationException("Missing file_dir argument")
    if not file_name:
        raise UcscValidationException("Missing file_name argument")
    top_system = TopSystem()
    if remote_enabled:
        _validate_remote_host_args(protocol, hostname, username, password)
        if (not file_name.endswith('.tgz')):
            raise UcscValidationException(
                "file_name must be .tgz format")
        file_path = os.path.join(file_dir, file_name)
        mgmt_backup = MgmtBackup(
                parent_mo_or_dn=top_system,
                hostname=hostname,
                admin_state=MgmtBackupConsts.ADMIN_STATE_ENABLED,
                proto=protocol,
                type=backup_type,
                remote_file=file_path,
                user=username,
                pwd=password)
    else:
        if not os.path.exists(file_dir):
            os.makedirs(file_dir)
        file_string = platform.node().lower() \
            + datetime.datetime.now().strftime("%Y%m%d%H%M%S")
        file_path = "/" + file_string + "_" + backup_type + "_backup.tgz"
        mgmt_backup = MgmtBackup(
                parent_mo_or_dn=top_system,
                hostname=hostname,
                admin_state=MgmtBackupConsts.ADMIN_STATE_ENABLED,
                proto=MgmtBackupConsts.PROTO_HTTP,
                type=backup_type,
                remote_file=file_path)
    if preserve_pooled_values:
        mgmt_backup.preserve_pooled_values = \
            MgmtBackupConsts.PRESERVE_POOLED_VALUES_YES
    else:
        mgmt_backup.preserve_pooled_values = \
            MgmtBackupConsts.PRESERVE_POOLED_VALUES_NO
    handle.add_mo(mgmt_backup, modify_present=True)
    handle.commit()
    mgmt_backup = handle.query_dn(dn=mgmt_backup.dn)
    admin_state_temp = mgmt_backup.admin_state
    duration = timeout
    poll_interval = 2
    log.debug("Starting Backup ")
    while True:
        mgmt_backup = handle.query_dn(dn=mgmt_backup.dn)
        admin_state_temp = mgmt_backup.admin_state
        if admin_state_temp == MgmtBackupConsts.ADMIN_STATE_DISABLED:
            break
        time.sleep(min(duration, poll_interval))
        duration = max(0, (duration - poll_interval))
        if duration == 0:
            handle.remove_mo(mgmt_backup)
            handle.commit()
            raise UcscOperationError(
                  "Backup UcsCentral", " operation timed out")
    if remote_enabled:
        if mgmt_backup.over_all_status == \
                MgmtBackupConsts.OVER_ALL_STATUS_FAILED:
            log.debug("Backup failed")
            handle.remove_mo(mgmt_backup)
            handle.commit()
            raise UcscOperationError(
                "Backup UcsCentral", "%s" % mgmt_backup.fsm_rmt_inv_err_descr)
    if not remote_enabled:
        file_source = "backupfile" + file_path
        if handle.is_local_download_supported():
            try:
                log.debug("Starting Download ")
                handle.file_download(url_suffix=file_source,
                                     file_dir=file_dir,
                                     file_name=file_name)
            except Exception as err:
                log.debug("Download backup Failed")
                UcscWarning(str(err))
        else:
            log.error("Local download not supported from sdk for this "
                      "version of UcsCentral, skipping it")
    if remove_from_ucsc:
        log.debug("Removing backup from UcsCentral")
        handle.remove_mo(mgmt_backup)
        handle.commit()
def _backup ( handle , file_dir , file_name , timeout = 600 , remote_enabled = False , protocol = None , hostname = "localhost" , username = None , password = "" , remove_from_ucsc = False , preserve_pooled_values = False ) : from . . mometa . mgmt . MgmtBackup import MgmtBackup , MgmtBackupConsts from . . mometa . top . TopSystem import TopSystem backup_type = "full-state" if not file_dir : raise UcscValidationException ( "Missing file_dir argument" ) if not file_name : raise UcscValidationException ( "Missing file_name argument" ) top_system = TopSystem ( ) if remote_enabled : _validate_remote_host_args ( protocol , hostname , username , password ) if ( not file_name . endswith ( '.tgz' ) ) : raise UcscValidationException ( "file_name must be .tgz format" ) file_path = os . path . join ( file_dir , file_name ) mgmt_backup = MgmtBackup ( parent_mo_or_dn = top_system , hostname = hostname , admin_state = MgmtBackupConsts . ADMIN_STATE_ENABLED , proto = protocol , type = backup_type , remote_file = file_path , user = username , pwd = password ) else : if not os . path . exists ( file_dir ) : os . makedirs ( file_dir ) file_string = platform . node ( ) . lower ( ) + datetime . datetime . now ( ) . strftime ( "%Y%m%d%H%M%S" ) file_path = "/" + file_string + "_" + backup_type + "_backup.tgz" mgmt_backup = MgmtBackup ( parent_mo_or_dn = top_system , hostname = hostname , admin_state = MgmtBackupConsts . ADMIN_STATE_ENABLED , proto = MgmtBackupConsts . PROTO_HTTP , type = backup_type , remote_file = file_path ) if preserve_pooled_values : mgmt_backup . preserve_pooled_values = MgmtBackupConsts . PRESERVE_POOLED_VALUES_YES else : mgmt_backup . preserve_pooled_values = MgmtBackupConsts . PRESERVE_POOLED_VALUES_NO handle . add_mo ( mgmt_backup , modify_present = True ) handle . commit ( ) mgmt_backup = handle . query_dn ( dn = mgmt_backup . dn ) admin_state_temp = mgmt_backup . admin_state duration = timeout poll_interval = 2 log . debug ( "Starting Backup " ) while True : mgmt_backup = handle . query_dn ( dn = mgmt_backup . dn ) admin_state_temp = mgmt_backup . admin_state if admin_state_temp == MgmtBackupConsts . ADMIN_STATE_DISABLED : break time . sleep ( min ( duration , poll_interval ) ) duration = max ( 0 , ( duration - poll_interval ) ) if duration == 0 : handle . remove_mo ( mgmt_backup ) handle . commit ( ) raise UcscOperationError ( "Backup UcsCentral" , " operation timed out" ) if remote_enabled : if mgmt_backup . over_all_status == MgmtBackupConsts . OVER_ALL_STATUS_FAILED : log . debug ( "Backup failed" ) handle . remove_mo ( mgmt_backup ) handle . commit ( ) raise UcscOperationError ( "Backup UcsCentral" , "%s" % mgmt_backup . fsm_rmt_inv_err_descr ) if not remote_enabled : file_source = "backupfile" + file_path if handle . is_local_download_supported ( ) : try : log . debug ( "Starting Download " ) handle . file_download ( url_suffix = file_source , file_dir = file_dir , file_name = file_name ) except Exception as err : log . debug ( "Download backup Failed" ) UcscWarning ( str ( err ) ) else : log . error ( "Local download not supported from sdk for this " "version of UcsCentral, skipping it" ) if remove_from_ucsc : log . debug ( "Removing backup from UcsCentral" ) handle . remove_mo ( mgmt_backup ) handle . commit ( )
_backup internal method helps create UcsCentral full-state backup and
download it locally or to remote location.
_backup internal method helps create UcsCentral full - state backup and download it locally or to remote location .
"""
    _backup internal method helps create UcsCentral full-state backup and
    download it locally or to remote location.

    Args:
        handle (UcscHandle): UcsCentral Connection handle
        file_dir (str): directory to download backup file to
        file_name (str): name for the backup file
        timeout (number) : time in seconds for which method waits
                           for the backUp file to generate before it exits.
        remote_enabled (boolean): True if Remote backup is enabled
                                  False - by default
        protocol (str): Transfer protocol for remote backup
                        ['ftp','sftp','tftp','scp']
        hostname (str): Hostname/IP for the remote backup
        username (str): Username for remote backup
        password (str): Password for remote backup
        remove_from_ucsc (boolean): True/False, False - by default
        preserve_pooled_values (boolean): True/False, False - by default

    Example:
        file_dir = "/home/user/backup"\n
        file_name = "config_backup.tgz"\n
        _backup(handle, file_dir=file_dir, file_name=file_name)\n

        _backup(handle, file_dir=file_dir, file_name=file_name,
                remote_enabled=True, protocol='scp',hostname='192.168.1.1',
                username='admin',password='password')\n

    """ # Checking for the backup to compete. # Break condition:- if state id disabled then break # remove backup from UcsCentral
handle
UcsCentral Connection handle
UcsCentral Connection handle
file_dir
directory to download backup file to
directory to download backup file to
file_name
name for the backup file
name for the backup file
timeout
time in seconds for which method waits
for the backUp file to generate before it exits.
time in seconds for which method waits for the backUp file to generate before it exits .
remote_enabled
True if Remote backup is enabled
False - by default
True if Remote backup is enabled False - by default
protocol
Transfer protocol for remote backup
['ftp','sftp','tftp','scp']
Transfer protocol for remote backup [ ' ftp ' ' sftp ' ' tftp ' ' scp ' ]
hostname
Hostname/IP for the remote backup
Hostname / IP for the remote backup
username
Username for remote backup
Username for remote backup
password
Password for remote backup
Password for remote backup
remove_from_ucsc
True/False, False - by default
True / False False - by default
preserve_pooled_values
True/False, False - by default
True / False False - by default
examples


_backup(handle, file_dir=file_dir, file_name=file_name,
remote_enabled=True, protocol='scp',hostname='192.168.1.1',
username='admin',password='password')\n
_backup ( handle file_dir = file_dir file_name = file_name remote_enabled = True protocol = ' scp ' hostname = ' 192 . 168 . 1 . 1 ' username = ' admin ' password = ' password ' ) \ n