Ejemplo n.º 1
0
def disconnect():
    """
    Disconnects from oVirt/RHEV engine.
    """
    global api

    if api:
        api.disconnect()
        api = None
        logging.debug('Closed connection.')
Ejemplo n.º 2
0
def main(argv):
    usage = "backup.py -c <config.cfg>"
    try:
        opts, args = getopt(argv, "hc:d")
        debug = False
        if not opts:
            print usage
            sys.exit(1)
        for opt, arg in opts:
            if (opt == "-h") or (opt == "--help"):
                print usage
                sys.exit(0)
            elif opt in ("-c"):
                config_file = arg
            elif opt in ("-d"):
                debug = True
    except GetoptError:
        print usage
        sys.exit(1)

    global config
    config = Config(config_file, debug)

    time_start = int(time.time())

    has_errors = False

    # Connect to server
    connect()

    # Test if all VM names are valid
    for vm_from_list in config.get_vm_names():
        if not api.vms.get(vm_from_list):
            print "!!! There are no VM with the following name in your cluster: " + vm_from_list
            api.disconnect()
            sys.exit(1)

    vms_with_failures = list(config.get_vm_names())

    for vm_from_list in config.get_vm_names():
        config.clear_vm_suffix()
        vm_clone_name = vm_from_list + config.get_vm_middle(
        ) + config.get_vm_suffix()

        # Check VM name length limitation
        length = len(vm_clone_name)
        if length > config.get_vm_name_max_length():
            print "!!! VM name with middle and suffix are to long (size: " + str(
                length) + ", allowed " + str(
                    config.get_vm_name_max_length()) + ") !!!"
            Logger.log("VM name: " + vm_clone_name)
            api.disconnect()
            sys.exit(1)

        Logger.log("Start backup for: " + vm_from_list)
        try:
            # Get the VM
            vm = api.vms.get(vm_from_list)

            # Cleanup: Delete the cloned VM
            VMTools.delete_vm(api, config, vm_from_list)

            # Delete old backup snapshots
            VMTools.delete_snapshots(vm, config, vm_from_list)

            # Determine disks to snapshot
            vm_disks = []
            try:
                config_disks = config.get_vm_disks()[vm_from_list]
            except KeyError:
                config_disks = None
            for vm_disk in vm.disks.list():
                if config_disks is None or vm_disk.get_name() in config_disks:
                    vm_disks.append(vm_disk)

            # Create a VM snapshot:
            try:
                Logger.log("Snapshot creation started ...")
                if not config.get_dry_run():
                    vm.snapshots.add(
                        params.Snapshot(
                            description=config.get_snapshot_description(),
                            vm=vm,
                            disks=params.Disks(disk=vm_disks)))
                    VMTools.wait_for_snapshot_operation(vm, config, "creation")
                Logger.log("Snapshot created")
            except Exception as e:
                Logger.log("Can't create snapshot for VM: " + vm_from_list)
                Logger.log("DEBUG: " + str(e))
                has_errors = True
                continue

            # Clone the snapshot into a VM
            snapshots = vm.snapshots.list(
                description=config.get_snapshot_description())
            if not snapshots:
                Logger.log("!!! No snapshot found")
                has_errors = True
                continue
            snapshot = snapshots[0]
            snapshot_param = params.Snapshot(id=snapshot.id)
            snapshots_param = params.Snapshots(snapshot=[snapshot_param],
                                               collapse_snapshots=True)
            if config.get_vm_clone_domain() is not None:
                clone_sd = api.storagedomains.get(
                    name=config.get_vm_clone_domain())
                if not clone_sd:
                    Logger.log(
                        "!!! Unknown storage domain value for vm_clone_domain")
                    has_errors = True
                    continue
                vm_clone_disks = []
                for disk in snapshot.disks.list():
                    vm_clone_disks.append(
                        params.Disk(image_id=disk.get_id(),
                                    storage_domains=params.StorageDomains(
                                        storage_domain=[clone_sd])))
            else:
                vm_clone_disks = snapshot.disks.list()

            Logger.log("Clone into VM started ...")
            if not config.get_dry_run():
                api.vms.add(
                    params.VM(name=vm_clone_name,
                              memory=vm.get_memory(),
                              cluster=api.clusters.get(
                                  config.get_cluster_name()),
                              snapshots=snapshots_param,
                              disks=params.Disks(disk=vm_clone_disks)))
                VMTools.wait_for_vm_operation(api, config, "Cloning",
                                              vm_from_list)
            Logger.log("Cloning finished")

            # Delete backup snapshots
            VMTools.delete_snapshots(vm, config, vm_from_list)

            # Delete old backups
            VMTools.delete_old_backups(api, config, vm_from_list)

            # Export the VM
            try:
                vm_clone = api.vms.get(vm_clone_name)
                Logger.log("Export started ...")
                if not config.get_dry_run():
                    vm_clone.export(
                        params.Action(storage_domain=api.storagedomains.get(
                            config.get_export_domain())))
                    VMTools.wait_for_vm_operation(api, config, "Exporting",
                                                  vm_from_list)
                Logger.log("Exporting finished")
            except Exception as e:
                Logger.log("Can't export cloned VM (" + vm_clone_name +
                           ") to domain: " + config.get_export_domain())
                Logger.log("DEBUG: " + str(e))
                has_errors = True
                continue

            # Delete the VM
            VMTools.delete_vm(api, config, vm_from_list)

            time_end = int(time.time())
            time_diff = (time_end - time_start)
            time_minutes = int(time_diff / 60)
            time_seconds = time_diff % 60

            Logger.log("Duration: " + str(time_minutes) + ":" +
                       str(time_seconds) + " minutes")
            Logger.log("VM exported as " + vm_clone_name)
            Logger.log("Backup done for: " + vm_from_list)
            vms_with_failures.remove(vm_from_list)
        except errors.ConnectionError as e:
            Logger.log("!!! Can't connect to the server" + str(e))
            connect()
            continue
        except errors.RequestError as e:
            Logger.log("!!! Got a RequestError: " + str(e))
            has_errors = True
            continue
        except Exception as e:
            Logger.log("!!! Got unexpected exception: " + str(e))
            api.disconnect()
            sys.exit(1)

    Logger.log("All backups done")

    if vms_with_failures:
        Logger.log("Backup failured for:")
        for i in vms_with_failures:
            Logger.log("  " + i)

    if has_errors:
        Logger.log(
            "Some errors occured during the backup, please check the log file")
        api.disconnect()
        sys.exit(1)

    # Disconnect from the server
    api.disconnect()
Ejemplo n.º 3
0
def main(argv):
    try:
        opts, args = getopt(argv, "hac:d")
        debug = False
        all_vms = False
        if not opts:
            usage()
        for opt, arg in opts:
            if (opt == "-h") or (opt == "--help"):
                usage()
            elif opt in ("-c"):
                config_file = arg
            elif opt in ("-d"):
                debug = True
            elif opt in ("-a"):
                all_vms = True
    except GetoptError:
        usage()
    
    global config    
    config = Config(config_file, debug)
    
    time_start = int(time.time())

    has_errors = False
    
    # Connect to server
    connect()

    # Add all VM's to the config file
    if all_vms:
        vms=api.vms.list(max=400)
        vmlist.get_vm_list(vms,config_file)
        config = Config(config_file, debug)

    # Test if all VM names are valid
    for vm_from_list in config.get_vm_names():
        if not api.vms.get(vm_from_list):
            print "!!! There are no VM with the following name in your cluster: " + vm_from_list
            api.disconnect()
            sys.exit(1)

    vms_with_failures = list(config.get_vm_names())
    
    for vm_from_list in config.get_vm_names():
        config.clear_vm_suffix()
        vm_clone_name = vm_from_list + config.get_vm_middle() + config.get_vm_suffix()

        # Check VM name length limitation
        length = len(vm_clone_name)
        if length > config.get_vm_name_max_length():
            print "!!! VM name with middle and suffix are to long (size: " + str(length) + ", allowed " + str(config.get_vm_name_max_length()) + ") !!!"
            Logger.log("VM name: " + vm_clone_name)
            api.disconnect()
            sys.exit(1)
    
        Logger.log("Start backup for: " + vm_from_list)
        try:
            # Get the VM
            vm = api.vms.get(vm_from_list)

            # Cleanup: Delete the cloned VM
            VMTools.delete_vm(api, config, vm_from_list)
        
            # Delete old backup snapshots
            VMTools.delete_snapshots(vm, config, vm_from_list)

            # Check free space on the storage
            VMTools.check_free_space(api, config, vm)
        
            # Create a VM snapshot:
            try:
                Logger.log("Snapshot creation started ...")
                if not config.get_dry_run():
                    vm.snapshots.add(params.Snapshot(description=config.get_snapshot_description(), vm=vm))
                    VMTools.wait_for_snapshot_operation(vm, config, "creation")
                Logger.log("Snapshot created")
            except Exception as e:
                Logger.log("Can't create snapshot for VM: " + vm_from_list)
                Logger.log("DEBUG: " + str(e))
                has_errors = True
                continue
        
            # Clone the snapshot into a VM
            snapshots = vm.snapshots.list(description=config.get_snapshot_description())
            if not snapshots:
                Logger.log("!!! No snapshot found")
                has_errors = True
                continue
            snapshot_param = params.Snapshot(id=snapshots[0].id)
            snapshots_param = params.Snapshots(snapshot=[snapshot_param])
            Logger.log("Clone into VM started ...")
            if not config.get_dry_run():
                api.vms.add(params.VM(name=vm_clone_name, memory=vm.get_memory(), cluster=api.clusters.get(config.get_cluster_name()), snapshots=snapshots_param))    
                VMTools.wait_for_vm_operation(api, config, "Cloning", vm_from_list)
            Logger.log("Cloning finished")
        
            # Delete backup snapshots
            VMTools.delete_snapshots(vm, config, vm_from_list)
        
            # Delete old backups
            VMTools.delete_old_backups(api, config, vm_from_list)
        
            # Export the VM
            try:
                vm_clone = api.vms.get(vm_clone_name)
                Logger.log("Export started ...")
                if not config.get_dry_run():
                    vm_clone.export(params.Action(storage_domain=api.storagedomains.get(config.get_export_domain())))
                    VMTools.wait_for_vm_operation(api, config, "Exporting", vm_from_list)
                Logger.log("Exporting finished")
            except Exception as e:
                Logger.log("Can't export cloned VM (" + vm_clone_name + ") to domain: " + config.get_export_domain())
                Logger.log("DEBUG: " + str(e))
                has_errors = True
                continue
            
            # Delete the VM
            VMTools.delete_vm(api, config, vm_from_list)
        
            time_end = int(time.time())
            time_diff = (time_end - time_start)
            time_minutes = int(time_diff / 60)
            time_seconds = time_diff % 60
        
            Logger.log("Duration: " + str(time_minutes) + ":" + str(time_seconds) + " minutes")
            Logger.log("VM exported as " + vm_clone_name)
            Logger.log("Backup done for: " + vm_from_list)
            vms_with_failures.remove(vm_from_list)
        except errors.ConnectionError as e:
            Logger.log("!!! Can't connect to the server" + str(e))
            connect()
            continue
        except errors.RequestError as e:
            Logger.log("!!! Got a RequestError: " + str(e))
            has_errors = True
            continue
        except  Exception as e:
            Logger.log("!!! Got unexpected exception: " + str(e))
            api.disconnect()
            sys.exit(1)

    Logger.log("All backups done")

    if vms_with_failures:
        Logger.log("Backup failured for:")
        for i in vms_with_failures:
            Logger.log("  " + i)
   
    if has_errors:
        Logger.log("Some errors occured during the backup, please check the log file")
        api.disconnect()
        sys.exit(1)
 
    # Disconnect from the server
    api.disconnect()
Ejemplo n.º 4
0
def main(argv):
    usage = "backup.py -c <config.cfg> -l <vm_list>"
    try:
        opts, args = getopt(argv, "hc:l:d")
        debug = False
        if not opts:
            print usage
            sys.exit(1)
        for opt, arg in opts:
            if (opt == "-h") or (opt == "--help"):
                print usage
                sys.exit(0)
            elif opt in ("-c"):
                config_file = arg
            elif opt in ("-l"):
                list_file = arg
            elif opt in ("-d"):
                debug = True
    except GetoptError:
        print usage
        sys.exit(1)
        
    config = Config(config_file, debug)

    # Load vm list
    with open(list_file) as f:
        vm_list = [line.rstrip('\n') for line in f]
    
    time_start = int(time.time())
    
    # Connect to server
    api = ovirtsdk.api.API(
        url=config.get_server(),
        username=config.get_username(),
        password=config.get_password(),
        insecure=True,
        debug=False
    )

    # Queue for VMs to be backed up
    queue = Queue.Queue()
    for vm in vm_list:
        # Skip comment lines
        if vm[0] == "#":
            continue

        if api.vms.get(vm): # Check VM exists
            queue.put(vm)
        else:
            log(vm, "Skipping - does not exist")

    # Create workers
    worker_count = 2
    for i in range(worker_count):
        t = BackupWorker(queue, api, config)
        t.setDaemon(True)
        t.start()

    # Wait for queue to be emptied
    while threading.active_count() > 1:
        time.sleep(1)

    print "All backups done"
    
    # Disconnect from the server
    api.disconnect()
Ejemplo n.º 5
0
newVm.initialization.configuration = params.Configuration()
newVm.initialization.configuration.set_type("ovf")
newVm.initialization.configuration.set_data(configuration_data)
my_vm = api.vms.add(newVm)


DOMAIN_NAME = 'sdffds'
MB=1024*1024
INTERFACE='virtio'
FORMAT='qcow'

#Create a disk to restore the data to and attach it to that VM
storage_domain = api.storagedomains.get(DOMAIN_NAME)

#Find the VM that has access to the backup:
vm_backup_access = api.vms.get(VM_THAT_PERFORM_BACKUP)

#Create a new disk and attach it to the VM with access to the backed up data.
target_storage=params.StorageDomains(storage_domain={storage_domain})
created_disk=vm_backup_access.disks.add(params.Disk(storage_domains=target_storage, interface='virtio', format='cow', provisioned_size=1024*MB))

while vm_backup_access.disks.get(id=created_disk.get_id()).get_status() == 'locked':
    sleep(1)

#Restore the data to the disk/disks.

#Detach the disk from the VM with the access to the backup and attach it to the restored VM (see in the backup flow)

# Bye:
api.disconnect()
Ejemplo n.º 6
0
def main(argv):
    p = create_argparser()
    opts = p.parse_args(argv)
    config_arguments = arguments_to_dict(opts)

    global config
    with opts.config_file:
        config = Config(opts.config_file, opts.debug, config_arguments)
    initialize_logger(
        config.get_logger_fmt(),
        config.get_logger_file_path(),
        opts.debug,
    )

    time_start = int(time.time())

    has_errors = False

    # Connect to server
    connect()

    # Add all VM's to the config file
    if opts.all_vms:
        vms = api.vms.list(max=400)
        config.set_vm_names([vm.name for vm in vms])
        # Update config file
        if opts.config_file.name != "<stdin>":
            config.write_update(opts.config_file.name)
    # Add VM's with the tag to the vm list
    if opts.vm_tag:
        vms = api.vms.list(max=400, query="tag=" + opts.vm_tag)
        config.set_vm_names([vm.name for vm in vms])
        # Update config file
        if opts.config_file.name != "<stdin>":
            config.write_update(opts.config_file.name)

    # Test if data center is valid
    if api.datacenters.get(config.get_datacenter_name()) is None:
        logger.error("!!! Check the datacenter_name in the config")
        api.disconnect()
        sys.exit(1)
    # Test if config export_domain is valid
    if api.storagedomains.get(config.get_export_domain()) is None:
        logger.error("!!! Check the export_domain in the config")
        api.disconnect()
        sys.exit(1)

    # Test if config cluster_name is valid
    if api.clusters.get(config.get_cluster_name()) is None:
        logger.error("!!! Check the cluster_name in the config")
        api.disconnect()
        sys.exit(1)

    # Test if config storage_domain is valid
    if api.storagedomains.get(config.get_storage_domain()) is None:
        logger.error("!!! Check the storage_domain in the config")
        api.disconnect()
        sys.exit(1)

    # Test if all VM names are valid
    for vm_from_list in config.get_vm_names():
        if not api.vms.get(vm_from_list):
            logger.error(
                "!!! There are no VM with the following name in your cluster: %s",
                vm_from_list)
            api.disconnect()
            sys.exit(1)

    # Test if config vm_middle is valid
    if not config.get_vm_middle():
        logger.error("!!! It's not valid to leave vm_middle empty")
        api.disconnect()
        sys.exit(1)

    vms_with_failures = list(config.get_vm_names())

    for vm_from_list in config.get_vm_names():
        config.clear_vm_suffix()
        vm_clone_name = vm_from_list + config.get_vm_middle(
        ) + config.get_vm_suffix()

        # Check VM name length limitation
        length = len(vm_clone_name)
        if length > config.get_vm_name_max_length():
            logger.error(
                "!!! VM name with middle and suffix are to long (size: %s, allowed %s) !!!",
                length, config.get_vm_name_max_length())
            logger.info("VM name: %s", vm_clone_name)
            api.disconnect()
            sys.exit(1)

        logger.info("Start backup for: %s", vm_from_list)
        try:
            VMTools.check_storage_domain_status(api,
                                                config.get_datacenter_name(),
                                                config.get_export_domain())
            # Cleanup: Delete the cloned VM
            VMTools.delete_vm(api, config, vm_from_list)

            # Get the VM
            vm = api.vms.get(vm_from_list)
            if vm is None:
                logger.warn(
                    "The VM (%s) doesn't exist anymore, skipping backup ...",
                    vm_from_list)
                continue

            # Delete old backup snapshots
            VMTools.delete_snapshots(vm, config, vm_from_list)

            # Check free space on the storage
            VMTools.check_free_space(api, config, vm)

            # Create a VM snapshot:
            try:
                logger.info("Snapshot creation started ...")
                if not config.get_dry_run():
                    vm.snapshots.add(
                        params.Snapshot(
                            description=config.get_snapshot_description(),
                            vm=vm,
                            persist_memorystate=config.get_persist_memorystate(
                            ),
                        ))
                    VMTools.wait_for_snapshot_operation(vm, config, "creation")
                logger.info("Snapshot created")
            except Exception as e:
                logger.info("Can't create snapshot for VM: %s", vm_from_list)
                logger.info("DEBUG: %s", e)
                has_errors = True
                continue
            # Workaround for some SDK problems see issue #17
            time.sleep(10)

            # Clone the snapshot into a VM
            snapshots = vm.snapshots.list(
                description=config.get_snapshot_description())
            if not snapshots:
                logger.error("!!! No snapshot found !!!")
                has_errors = True
                continue
            snapshot_param = params.Snapshot(id=snapshots[0].id)
            snapshots_param = params.Snapshots(snapshot=[snapshot_param])
            logger.info("Clone into VM (%s) started ..." % vm_clone_name)
            if not config.get_dry_run():
                api.vms.add(
                    params.VM(name=vm_clone_name,
                              memory=vm.get_memory(),
                              cluster=api.clusters.get(
                                  config.get_cluster_name()),
                              snapshots=snapshots_param))
                VMTools.wait_for_vm_operation(api, config, "Cloning",
                                              vm_from_list)
            logger.info("Cloning finished")

            # Delete backup snapshots
            VMTools.delete_snapshots(vm, config, vm_from_list)

            # Delete old backups
            if (config.get_backup_keep_count()):
                VMTools.delete_old_backups(api, config, vm_from_list)
            if (config.get_backup_keep_count_by_number()):
                VMTools.delete_old_backups_by_number(api, config, vm_from_list)

            # Export the VM
            try:
                vm_clone = api.vms.get(vm_clone_name)
                logger.info("Export of VM (%s) started ..." % vm_clone_name)
                if not config.get_dry_run():
                    vm_clone.export(
                        params.Action(storage_domain=api.storagedomains.get(
                            config.get_export_domain())))
                    VMTools.wait_for_vm_operation(api, config, "Exporting",
                                                  vm_from_list)
                logger.info("Exporting finished")
            except Exception as e:
                logger.info("Can't export cloned VM (%s) to domain: %s",
                            vm_clone_name, config.get_export_domain())
                logger.info("DEBUG: %s", e)
                has_errors = True
                continue

            # Delete the VM
            VMTools.delete_vm(api, config, vm_from_list)

            time_end = int(time.time())
            time_diff = (time_end - time_start)
            time_minutes = int(time_diff / 60)
            time_seconds = time_diff % 60

            logger.info("Duration: %s:%s minutes", time_minutes, time_seconds)
            logger.info("VM exported as %s", vm_clone_name)
            logger.info("Backup done for: %s", vm_from_list)
            vms_with_failures.remove(vm_from_list)
        except errors.ConnectionError as e:
            logger.error("!!! Can't connect to the server %s", e)
            connect()
            continue
        except errors.RequestError as e:
            logger.error("!!! Got a RequestError: %s", e)
            has_errors = True
            continue
        except Exception as e:
            logger.error("!!! Got unexpected exception: %s", e)
            api.disconnect()
            sys.exit(1)

    logger.info("All backups done")

    if vms_with_failures:
        logger.info("Backup failured for:")
        for i in vms_with_failures:
            logger.info("  %s", i)

    if has_errors:
        logger.info(
            "Some errors occured during the backup, please check the log file")
        api.disconnect()
        sys.exit(1)

    # Disconnect from the server
    api.disconnect()
Ejemplo n.º 7
0
def main(argv):
    try:
        opts, args = getopt(argv, "hac:d")
        debug = False
        all_vms = False
        if not opts:
            usage()
        for opt, arg in opts:
            if (opt == "-h") or (opt == "--help"):
                usage()
            elif opt in ("-c"):
                config_file = arg
            elif opt in ("-d"):
                debug = True
            elif opt in ("-a"):
                all_vms = True
    except GetoptError:
        usage()

    global config
    config = Config(config_file, debug)

    time_start = int(time.time())

    has_errors = False

    # Connect to server
    connect()

    # Add all VM's to the config file
    if all_vms:
        vms = api.vms.list(max=400)
        vmlist.get_vm_list(vms, config_file)
        config = Config(config_file, debug)

    # Test if config export_domain is valid
    if api.storagedomains.get(config.get_export_domain()) is None:
        print "!!! Check the export_domain in the config"
        api.disconnect()
        sys.exit(1)

    # Test if config cluster_name is valid
    if api.clusters.get(config.get_cluster_name()) is None:
        print "!!! Check the cluster_name in the config"
        api.disconnect()
        sys.exit(1)

    # Test if config storage_domain is valid
    if api.storagedomains.get(config.get_storage_domain()) is None:
        print "!!! Check the storage_domain in the config"
        api.disconnect()
        sys.exit(1)

    # Test if all VM names are valid
    for vm_from_list in config.get_vm_names():
        if not api.vms.get(vm_from_list):
            print "!!! There are no VM with the following name in your cluster: " + vm_from_list
            api.disconnect()
            sys.exit(1)

    # Test if config vm_middle is valid
    if not config.get_vm_middle():
        print "!!! It's not valid to leave vm_middle empty"
        api.disconnect()
        sys.exit(1)

    vms_with_failures = list(config.get_vm_names())

    for vm_from_list in config.get_vm_names():
        config.clear_vm_suffix()
        vm_clone_name = vm_from_list + config.get_vm_middle(
        ) + config.get_vm_suffix()

        # Check VM name length limitation
        length = len(vm_clone_name)
        if length > config.get_vm_name_max_length():
            print "!!! VM name with middle and suffix are to long (size: " + str(
                length) + ", allowed " + str(
                    config.get_vm_name_max_length()) + ") !!!"
            Logger.log("VM name: " + vm_clone_name)
            api.disconnect()
            sys.exit(1)

        Logger.log("Start backup for: " + vm_from_list)
        try:
            # Get the VM
            vm = api.vms.get(vm_from_list)

            # Cleanup: Delete the cloned VM
            VMTools.delete_vm(api, config, vm_from_list)

            # Delete old backup snapshots
            VMTools.delete_snapshots(vm, config, vm_from_list)

            # Check free space on the storage
            VMTools.check_free_space(api, config, vm)

            # Create a VM snapshot:
            try:
                Logger.log("Snapshot creation started ...")
                if not config.get_dry_run():
                    vm.snapshots.add(
                        params.Snapshot(
                            description=config.get_snapshot_description(),
                            vm=vm))
                    VMTools.wait_for_snapshot_operation(vm, config, "creation")
                Logger.log("Snapshot created")
            except Exception as e:
                Logger.log("Can't create snapshot for VM: " + vm_from_list)
                Logger.log("DEBUG: " + str(e))
                has_errors = True
                continue
            # Workaround for some SDK problems see issue #17
            time.sleep(10)

            # Clone the snapshot into a VM
            snapshots = vm.snapshots.list(
                description=config.get_snapshot_description())
            if not snapshots:
                Logger.log("!!! No snapshot found")
                has_errors = True
                continue
            snapshot_param = params.Snapshot(id=snapshots[0].id)
            snapshots_param = params.Snapshots(snapshot=[snapshot_param])
            Logger.log("Clone into VM started ...")
            if not config.get_dry_run():
                api.vms.add(
                    params.VM(name=vm_clone_name,
                              memory=vm.get_memory(),
                              cluster=api.clusters.get(
                                  config.get_cluster_name()),
                              snapshots=snapshots_param))
                VMTools.wait_for_vm_operation(api, config, "Cloning",
                                              vm_from_list)
            Logger.log("Cloning finished")

            # Delete backup snapshots
            VMTools.delete_snapshots(vm, config, vm_from_list)

            # Delete old backups
            VMTools.delete_old_backups(api, config, vm_from_list)

            # Export the VM
            try:
                vm_clone = api.vms.get(vm_clone_name)
                Logger.log("Export started ...")
                if not config.get_dry_run():
                    vm_clone.export(
                        params.Action(storage_domain=api.storagedomains.get(
                            config.get_export_domain())))
                    VMTools.wait_for_vm_operation(api, config, "Exporting",
                                                  vm_from_list)
                Logger.log("Exporting finished")
            except Exception as e:
                Logger.log("Can't export cloned VM (" + vm_clone_name +
                           ") to domain: " + config.get_export_domain())
                Logger.log("DEBUG: " + str(e))
                has_errors = True
                continue

            # Delete the VM
            VMTools.delete_vm(api, config, vm_from_list)

            time_end = int(time.time())
            time_diff = (time_end - time_start)
            time_minutes = int(time_diff / 60)
            time_seconds = time_diff % 60

            Logger.log("Duration: " + str(time_minutes) + ":" +
                       str(time_seconds) + " minutes")
            Logger.log("VM exported as " + vm_clone_name)
            Logger.log("Backup done for: " + vm_from_list)
            vms_with_failures.remove(vm_from_list)
        except errors.ConnectionError as e:
            Logger.log("!!! Can't connect to the server" + str(e))
            connect()
            continue
        except errors.RequestError as e:
            Logger.log("!!! Got a RequestError: " + str(e))
            has_errors = True
            continue
        except Exception as e:
            Logger.log("!!! Got unexpected exception: " + str(e))
            api.disconnect()
            sys.exit(1)

    Logger.log("All backups done")

    if vms_with_failures:
        Logger.log("Backup failured for:")
        for i in vms_with_failures:
            Logger.log("  " + i)

    if has_errors:
        Logger.log(
            "Some errors occured during the backup, please check the log file")
        api.disconnect()
        sys.exit(1)

    # Disconnect from the server
    api.disconnect()
Ejemplo n.º 8
0
def main(argv):
    usage = "backup.py -c <config.cfg>"
    try:
        opts, args = getopt(argv, "hc:d")
        debug = False
        if not opts:
            print usage
            sys.exit(1)
        for opt, arg in opts:
            if (opt == "-h") or (opt == "--help"):
                print usage
                sys.exit(0)
            elif opt in ("-c"):
                config_file = arg
            elif opt in ("-d"):
                debug = True
    except GetoptError:
        print usage
        sys.exit(1)

    config = Config(config_file, debug)

    time_start = int(time.time())

    # Connect to server
    api = ovirtsdk.api.API(
        url=config.get_server(),
        username=config.get_username(),
        password=config.get_password(),
        insecure=True,
        debug=False,
    )

    # Test if all VM names are valid
    for vm_from_list in config.get_vm_names():
        if not api.vms.get(vm_from_list):
            print "!!! There are no VM with the following name in your cluster: " + vm_from_list
            sys.exit(1)

    for vm_from_list in config.get_vm_names():

        print "Start backup for: " + vm_from_list

        # Get the VM
        vm = api.vms.get(vm_from_list)

        # Cleanup: Delete the cloned VM
        VMTools.delete_vm(api, config, vm_from_list)

        # Delete old backup snapshots
        VMTools.delete_snapshots(vm, config, vm_from_list)

        # Create a VM snapshot:
        try:
            print "Snapshot creation started ..."
            if not config.get_dry_run():
                vm.snapshots.add(params.Snapshot(description=config.get_snapshot_description(), vm=vm))
                VMTools.wait_for_snapshot_operation(vm, config, "creation")
            print "Snapshot created"
        except Exception as e:
            print "Can't create snapshot for VM: " + vm_from_list
            print "DEBUG: " + str(e)
            sys.exit(1)

        # Clone the snapshot into a VM
        snapshots = vm.snapshots.list(description=config.get_snapshot_description())
        if not snapshots:
            print "!!! No snapshot found"
            sys.exit(1)
        snapshot_param = params.Snapshot(id=snapshots[0].id)
        snapshots_param = params.Snapshots(snapshot=[snapshot_param])
        print "Clone into VM started ..."
        if not config.get_dry_run():
            api.vms.add(
                params.VM(
                    name=vm_from_list + config.get_vm_middle() + config.get_vm_suffix(),
                    memory=vm.get_memory(),
                    cluster=api.clusters.get(config.get_cluster_name()),
                    snapshots=snapshots_param,
                )
            )
            VMTools.wait_for_vm_operation(api, config, "Cloning", vm_from_list)
        print "Cloning finished"

        # Delete backup snapshots
        VMTools.delete_snapshots(vm, config, vm_from_list)

        # Delete old backups
        VMTools.delete_old_backups(api, config, vm_from_list)

        # Export the VM
        try:
            vm_clone = api.vms.get(vm_from_list + config.get_vm_middle() + config.get_vm_suffix())
            print "Export started ..."
            if not config.get_dry_run():
                vm_clone.export(params.Action(storage_domain=api.storagedomains.get(config.get_export_domain())))
                VMTools.wait_for_vm_operation(api, config, "Exporting", vm_from_list)
            print "Exporting finished"
        except Exception as e:
            print "Can't export cloned VM (" + vm_from_list + config.get_vm_middle() + config.get_vm_suffix() + ") to domain: " + config.get_export_domain()
            print "DEBUG: " + str(e)
            sys.exit(1)

        # Delete the VM
        VMTools.delete_vm(api, config, vm_from_list)

        time_end = int(time.time())
        time_diff = time_end - time_start
        time_minutes = int(time_diff / 60)
        time_seconds = time_diff % 60

        print "Duration: " + str(time_minutes) + ":" + str(time_seconds) + " minutes"
        print "VM exported as " + vm_from_list + config.get_vm_middle() + config.get_vm_suffix()
        print "Backup done for: " + vm_from_list
    print "All backups done"

    # Disconnect from the server
    api.disconnect()
Ejemplo n.º 9
0
def main(args):
    retcode = 0
    if (args.config):
        # Read config file
        config = ConfigParser.ConfigParser()
        config.read(args.config)
        server = config.get('connection', 'server')
        username = config.get('connection', 'user_name')
        password = config.get('connection', 'password')
    if not server or not username or not password:
        logger.error("Server credentials not provided")
        sys.exit("Server credentials not provided")

    time_start = int(time.time())
    # Connect to server
    try:
        connect(server, username, password)
        logger.debug("connected to server: " + server)
    except Exception as e:
        logger.error("Error:" + str(e))
        sys.exit(1)

    wait_timeout = config.get('snapshot', 'wait_timeout')
    vms=api.vms.list(max=100)

    vms_to_commit = []
    for vm_ in vms:
        try:
            # Get the VM
            vm = api.vms.get(vm_.name)
            if vm.status.state == 'up' and vm.name != 'HostedEngine':
                logger.info("Adding snapshot for: " + vm_.name )
                snapshot = vm.snapshots.add(params.Snapshot(description=_SNAPSHOT_NAME))
                logger.debug("snapshot: " + snapshot.get_id())
                vms_to_commit.append({'vm': vm, 'snapshot': snapshot})
                logger.debug("Added snapshot for vm: " + vm_.name)
        except Exception as e:
            logger.error("Error:" + str(e))

    diskimgs_to_del = []
    for vm_to_commit in vms_to_commit:
        try:
            # Get the VM
            vmcached = vm_to_commit['vm']
            vm = api.vms.get(vmcached.name)
            snapshotid = vm_to_commit['snapshot'].get_id()
            logger.debug("Refreshed vm object for: " + vm.name + ":" + snapshotid)
            while True:
                snapshot = vm.snapshots.get(id=snapshotid)
                if snapshot is not None:
                    if snapshot.get_snapshot_status() == 'ok':
                        logger.info("Snapshot created for VM :" + vm.name)
                        # get the overlay image id to delete at slave
                        overlaydisks = vm.disks.list()
                        for disk in overlaydisks:
                            logger.debug("DISK:" + disk.get_id())
                            dskImage = disk.get_image_id()
                            logger.debug("DISK IMAGE:" + dskImage)
                            diskimgs_to_del.append(dskImage)
                        break
                    else:
                        logger.debug ("Waiting for snapshot creation.. status: " + snapshot.get_snapshot_status())
                        time.sleep(10)
                else:
                    logger.error ("Snapshot not retrieved for vm: " + vm.name)
                    break

        except Exception as e:
            logger.error("Error:" + str(e))

    # call geo-rep scheduler
    cmd = _georepScheduleCmd + [args.mastervol, args.slave, args.slavevol, "--interval", str(args.interval),
                                 "--timeout", str(args.timeout)]
    ret, out, err = execCmd(cmd)
    # Post schedule successful exit - block commit all VMs
    if ret != 0:
        logger.error("Error:" + str(out) + ":" + '.'.join(err))
        retcode = 1
    else:
        # delete overlay images from slave
        with glustermount(args.slave, args.slavevol) as mnt:
            # find overlay image path (returns .lease and .meta files too)
            for diskImg in diskimgs_to_del:
                imgPaths = findImgPaths(diskImg, mnt)
                for imgPath in imgPaths:
                    logger.debug("IMG PATH:" + imgPath)
                    os.remove(imgPath)

    for vm_to_commit in vms_to_commit:
        # Block commit VMs
        try:
            vm = vm_to_commit['vm']
            logger.debug("Deleting snapshot for: " + vm.name)
            snapshot = vm_to_commit['snapshot']
            # live merge
            snapshot.delete()
            # wait for snapshot deletion to complete
            wait_for_snapshot_deletion(vm, snapshot.get_id(), wait_timeout)
            logger.debug("Deleted snapshot {0} for vm {1}".format(snapshot.get_name(), vm.name))
        except Exception as e:
            logger.error("Error:" + str(e))

    time_end = int(time.time())
    time_diff = (time_end - time_start)
    time_minutes = int(time_diff / 60)
    time_seconds = time_diff % 60

    logger.info("Duration of run: " + str(time_minutes) + ":" + str(time_seconds) + " minutes")

    add_event(retcode, str(time_minutes) + ":" + str(time_seconds))
    # Disconnect from the server
    api.disconnect()
    sys.exit(retcode)
Ejemplo n.º 10
0
DOMAIN_NAME = 'sdffds'
MB = 1024 * 1024
INTERFACE = 'virtio'
FORMAT = 'qcow'

#Create a disk to restore the data to and attach it to that VM
storage_domain = api.storagedomains.get(DOMAIN_NAME)

#Find the VM that has access to the backup:
vm_backup_access = api.vms.get(VM_THAT_PERFORM_BACKUP)

#Create a new disk and attach it to the VM with access to the backed up data.
target_storage = params.StorageDomains(storage_domain={storage_domain})
created_disk = vm_backup_access.disks.add(
    params.Disk(storage_domains=target_storage,
                interface='virtio',
                format='cow',
                provisioned_size=1024 * MB))

while vm_backup_access.disks.get(
        id=created_disk.get_id()).get_status() == 'locked':
    sleep(1)

#Restore the data to the disk/disks.

#Detach the disk from the VM with the access to the backup and attach it to the restored VM (see in the backup flow)

# Bye:
api.disconnect()