Exemple #1
0
    def execute_for_each(self, session, host):
        all_vms = session.xenapi.VM.get_all_records()
        prog = not self.args.no_progress

        storage = Storage(self.config['storage_root'],
                          self.config.get('storage_retain', 3),
                          self.config.get('compress_level', 1))
        batch = self.args.batch
        with RuntimeLock(BACKUP_LOCK, 'Another backup is running!'):
            for vm_id in all_vms:
                vm = all_vms[vm_id]
                if not vm['is_control_domain'] and  \
                vm['other_config'].get(AUTOBACKUP_KEY) and \
                (not batch or batch == vm['other_config'].get(AUTOBACKUP_BATCH)):
                    vm_name = vm['name_label']
                    if prog:
                        print "Host %s - VM %s" % (host['name'], vm_name)
                    try:
                        self.backup(session,
                                    vm_id,
                                    vm_name,
                                    host,
                                    storage,
                                    self.args.shutdown,
                                    show_progress=prog)
                    except Exception, e:
                        log.error('Error while backing up VM %s on host %s',
                                  vm_name, host['name'])
Exemple #2
0
 def backup(self, session, vm_id, vm_name, host, storage, force_shutdown=False, show_progress=False):
     uuid=session.xenapi.VM.get_uuid(vm_id)  # @ReservedAssignment
     state = session.xenapi.VM.get_power_state(vm_id)
     restore_actions=[]
     try:
         
         if state =='Paused':
             log.error('Cannot backup VM is state Paused')
             return
         elif state =='Suspended':
             log.warn('Backing up machine in suspended stated')
         elif state =='Running':
             if not force_shutdown:
                 try:
                     tt_id=session.xenapi.VM.snapshot(vm_id,'Temp.backup of %s'% vm_name )
                 except XenAPI.Failure, f:
                     if f.details[0]== 'SR_OPERATION_NOT_SUPPORTED':
                         raise CommandError('Cannot create snapshot of vm %s (try backup in halted state, or detach disks, that do not support snapshots)'% vm_name)
                     else:
                         raise
                 uuid=session.xenapi.VM.get_uuid(tt_id)
                 restore_actions.append(lambda: uninstall_VM(session, tt_id))
                 log.debug('Made snapshot of %s to uuid: %s', vm_name, uuid)
             else:
                 log.debug('Shutting down VM')
                 session.xenapi.VM.clean_shutdown(vm_id)
                 restore_actions.append(lambda: session.xenapi.VM.start(vm_id, False, False))
     
         sid=session._session
         rack=storage.get_rack_for(vm_name)
         log.info('Starting backup for VM %s (uuid=%s) on server %s', vm_name, uuid, host['name'])
         progress=None
         with Client(host['url']) as c:
             resp=c.get('/export', {'session_id':sid, 'uuid': uuid})
             task_id=resp.getheader('task-id')
             restore_actions.append(lambda: cancel_task(session,task_id))
             if show_progress:
                 log.debug("Starting progress monitor")
                 progress=ProgressMonitor(session, task_id)
                 progress.start()
             s=rack.create_slot()
             writer=s.get_writer()
             bufsize=1048576
             while True:
                 data=resp.read(bufsize)
                 if not data: break
                 writer.write(data)
             s.close() # close only if finished
         if progress:
             progress.stop()
         rack.shrink()
Exemple #3
0
 def execute_for_each(self, session, host):
     all_vms = session.xenapi.VM.get_all_records()
     prog=not self.args.no_progress
     
     storage=Storage(self.config['storage_root'], self.config.get('storage_retain', 3),
                     self.config.get('compress_level', 1))
     batch=self.args.batch
     with RuntimeLock(BACKUP_LOCK, 'Another backup is running!'):
         for vm_id in all_vms:
             vm = all_vms[vm_id]
             if not vm['is_control_domain'] and  \
             vm['other_config'].get(AUTOBACKUP_KEY) and \
             (not batch or batch == vm['other_config'].get(AUTOBACKUP_BATCH)):
                 vm_name=vm['name_label']
                 if prog:
                     print "Host %s - VM %s" % (host['name'], vm_name)
                 try:
                     self.backup(session, vm_id, vm_name, host, storage, self.args.shutdown, show_progress=prog)
                 except Exception, e:
                     log.error('Error while backing up VM %s on host %s', vm_name, host['name'])
Exemple #4
0
            _resp = c.put('/import', slot.get_reader(), slot.size_uncompressed,
                          params)

            log.info(
                'Finished import of VM %s',
                vm_name,
            )

        if progress:
            progress.join(300)
            if progress.is_alive():
                log.warn('Task did not finished')
            else:
                if progress.error:
                    msg = 'Import failed: %s' % progress.result
                    log.error(msg)
                    print >> sys.stderr, msg
                else:
                    res = progress.result
                    m = re.search(r'<value>(OpaqueRef:[^<]+)</value>', res)
                    if m:
                        vm_id = m.group(1)
                        try:

                            vm_uuid = session.xenapi.VM.get_uuid(vm_id)
                            msg = 'VM imported as uuid=%s' % vm_uuid
                            log.info(msg)
                            print msg
                        except XenAPI.Failure, f:
                            log.error(
                                'Cannot get imported VM uuid, error: %s' %
Exemple #5
0
    def backup(self,
               session,
               vm_id,
               vm_name,
               host,
               storage,
               force_shutdown=False,
               show_progress=False,
               compress_on_server=False,
               insecure=False):
        uuid = session.xenapi.VM.get_uuid(vm_id)  # @ReservedAssignment
        vm_uuid = uuid
        state = session.xenapi.VM.get_power_state(vm_id)
        restore_actions = []
        try:

            if state == 'Paused':
                log.error('Cannot backup VM is state Paused')
                return
            elif state == 'Suspended':
                log.warn('Backing up machine in suspended stated')
            elif state == 'Running':
                if not force_shutdown:
                    try:
                        tt_id = session.xenapi.VM.snapshot(
                            vm_id, SNAPSHOT_PREFIX + vm_name)
                    except XenAPI.Failure, f:
                        if f.details[0] == 'SR_OPERATION_NOT_SUPPORTED':
                            raise CommandError(
                                'Cannot create snapshot of vm %s (try backup in halted state, or detach disks, that do not support snapshots)'
                                % vm_name)
                        else:
                            raise
                    uuid = session.xenapi.VM.get_uuid(tt_id)
                    restore_actions.append(
                        lambda: uninstall_VM(session, tt_id))
                    log.debug('Made snapshot of %s(%s) to snapshot uuid: %s',
                              vm_name, vm_uuid, uuid)
                else:
                    log.debug('Shutting down VM')
                    session.xenapi.VM.clean_shutdown(vm_id)

                    def restart_vm():
                        session.xenapi.VM.start(vm_id, False, False)
                        log.debug('Started VM')

                    restore_actions.append(restart_vm)

            sid = session._session
            rack = storage.get_rack_for(vm_name, vm_uuid)
            log.info('Starting backup for VM %s (uuid=%s) on server %s',
                     vm_name, vm_uuid, host['name'])
            progress = None
            with Client(unsecure(host['url'], insecure)) as c:
                params = {'session_id': sid, 'uuid': uuid}
                if compress_on_server:
                    params['use_compression'] = "true"
                resp = c.get('/export', params)
                task_id = resp.getheader('task-id')
                restore_actions.append(lambda: cancel_task(session, task_id))
                if show_progress:
                    log.debug("Starting progress monitor")
                    progress = ProgressMonitor(session, task_id)
                    progress.start()
                s = rack.create_slot()
                writer = s.get_writer()
                bufsize = 65536  # 64k is enough - around this size got best performance
                while True:
                    data = resp.read(bufsize)
                    if not data: break
                    writer.write(data)
                s.close()  # close only if finished
            if progress:
                progress.join(10)
                if progress.is_alive():
                    log.warn('Task did not finished')
                else:
                    if progress.error:
                        msg = 'Export failed: %s' % progress.result
                        log.error(msg)
                        print >> sys.stderr, msg
                progress.stop()
            rack.shrink()
Exemple #6
0
                writer=s.get_writer()
                bufsize=1048576
                while True:
                    data=resp.read(bufsize)
                    if not data: break
                    writer.write(data)
                s.close() # close only if finished
            if progress:
                progress.stop()
            rack.shrink()
        finally:
            try:
                for a in reversed(restore_actions):
                    a()
            except Exception, e:
                log.error('Restore action after backup failed, this may leave some temporary snapshots or machine is halted. Error: %s' % e)
        log.info('Finished backup for VM %s (uuid=%s) on server %s', vm_name, uuid, host['name']) 
        


class BackupOneCommand(CommandForOneHost, BackupOne):
    name="backup"
    description="Backups one VM"
    def execute_for_one(self, session, host):
        vm_name=self.args.vm
        show_progress=not self.args.no_progress
        force_shutdown=self.args.shutdown
        
        ids=session.xenapi.VM.get_by_name_label(vm_name)
        log.debug('Found this VMs %s', ids)
        if not ids:
Exemple #7
0
                    log.warn('Task did not finished')
                else:
                    if progress.error:
                        msg = 'Export failed: %s' % progress.result
                        log.error(msg)
                        print >> sys.stderr, msg
                progress.stop()
            rack.shrink()
        finally:
            try:
                for a in reversed(restore_actions):
                    a()
            except Exception, e:
                traceback.print_exc()
                log.error(
                    'Restore action %s after backup failed, this may leave some temporary snapshots or machine is halted. Error: %s %s',
                    str(a), type(e), str(e))
        log.info('Finished backup for VM %s (uuid=%s) on server %s', vm_name,
                 uuid, host['name'])


class BackupOneCommand(CommandForOneVM, BackupOne):
    name = "backup"
    description = "Backups one VM"

    def execute_for_one(self, session, host):
        vm_name = self.args.vm
        show_progress = not self.args.no_progress
        force_shutdown = self.args.shutdown

        vm_id = self.find_vm(session, host)
Exemple #8
0
 def backup(self, session, vm_id, vm_name, host, storage, force_shutdown=False, show_progress=False,
            compress_on_server=False, insecure=False):
     uuid=session.xenapi.VM.get_uuid(vm_id)  # @ReservedAssignment
     vm_uuid=uuid
     state = session.xenapi.VM.get_power_state(vm_id)
     restore_actions=[]
     try:
         
         if state =='Paused':
             log.error('Cannot backup VM is state Paused')
             return
         elif state =='Suspended':
             log.warn('Backing up machine in suspended stated')
         elif state =='Running':
             if not force_shutdown:
                 try:
                     tt_id=session.xenapi.VM.snapshot(vm_id,SNAPSHOT_PREFIX+ vm_name )
                 except XenAPI.Failure, f:
                     if f.details[0]== 'SR_OPERATION_NOT_SUPPORTED':
                         raise CommandError('Cannot create snapshot of vm %s (try backup in halted state, or detach disks, that do not support snapshots)'% vm_name)
                     else:
                         raise
                 uuid=session.xenapi.VM.get_uuid(tt_id)
                 restore_actions.append(lambda: uninstall_VM(session, tt_id))
                 log.debug('Made snapshot of %s(%s) to snapshot uuid: %s', vm_name, vm_uuid, uuid)
             else:
                 log.debug('Shutting down VM')
                 session.xenapi.VM.clean_shutdown(vm_id)
                 def restart_vm():
                     session.xenapi.VM.start(vm_id, False, False)
                     log.debug('Started VM')
                 restore_actions.append(restart_vm)
     
         sid=session._session
         rack=storage.get_rack_for(vm_name, vm_uuid)
         log.info('Starting backup for VM %s (uuid=%s) on server %s', vm_name, vm_uuid, host['name'])
         progress=None
         with Client(unsecure(host['url'],insecure)) as c:
             params={'session_id':sid, 'uuid': uuid}
             if compress_on_server:
                 params['use_compression']="true"
             resp=c.get('/export', params)
             task_id=resp.getheader('task-id')
             restore_actions.append(lambda: cancel_task(session,task_id))
             if show_progress:
                 log.debug("Starting progress monitor")
                 progress=ProgressMonitor(session, task_id)
                 progress.start()
             s=rack.create_slot()
             writer=s.get_writer()
             bufsize=65536 # 64k is enough - around this size got best performance
             while True:
                 data=resp.read(bufsize)
                 if not data: break
                 writer.write(data)
             s.close() # close only if finished
         if progress:
             progress.join(10)
             if progress.is_alive():
                 log.warn('Task did not finished')
             else:
                 if progress.error:
                     msg='Export failed: %s'%progress.result
                     log.error(msg)
                     print >>sys.stderr, msg
             progress.stop()
         rack.shrink()
Exemple #9
0
                if progress.is_alive():
                    log.warn('Task did not finished')
                else:
                    if progress.error:
                        msg='Export failed: %s'%progress.result
                        log.error(msg)
                        print >>sys.stderr, msg
                progress.stop()
            rack.shrink()
        finally:
            try:
                for a in reversed(restore_actions):
                    a()
            except Exception, e:
                traceback.print_exc()
                log.error('Restore action %s after backup failed, this may leave some temporary snapshots or machine is halted. Error: %s %s', str(a), type(e), str(e))
        log.info('Finished backup for VM %s (uuid=%s) on server %s', vm_name, uuid, host['name']) 
        


class BackupOneCommand(CommandForOneVM, BackupOne):
    name="backup"
    description="Backups one VM"
    def execute_for_one(self, session, host):
        vm_name=self.args.vm
        show_progress=not self.args.no_progress
        force_shutdown=self.args.shutdown
        
        vm_id=self.find_vm(session, host)
        storage=Storage(self.config['storage_root'], self.config.get('storage_retain', 3),
                        self.config.get('compress_level', 1), self.config.get('compress', 'client'))
Exemple #10
0
    def backup(self,
               session,
               vm_id,
               vm_name,
               host,
               storage,
               force_shutdown=False,
               show_progress=False):
        uuid = session.xenapi.VM.get_uuid(vm_id)  # @ReservedAssignment
        state = session.xenapi.VM.get_power_state(vm_id)
        restore_actions = []
        try:

            if state == 'Paused':
                log.error('Cannot backup VM is state Paused')
                return
            elif state == 'Suspended':
                log.warn('Backing up machine in suspended stated')
            elif state == 'Running':
                if not force_shutdown:
                    try:
                        tt_id = session.xenapi.VM.snapshot(
                            vm_id, 'Temp.backup of %s' % vm_name)
                    except XenAPI.Failure, f:
                        if f.details[0] == 'SR_OPERATION_NOT_SUPPORTED':
                            raise CommandError(
                                'Cannot create snapshot of vm %s (try backup in halted state, or detach disks, that do not support snapshots)'
                                % vm_name)
                        else:
                            raise
                    uuid = session.xenapi.VM.get_uuid(tt_id)
                    restore_actions.append(
                        lambda: uninstall_VM(session, tt_id))
                    log.debug('Made snapshot of %s to uuid: %s', vm_name, uuid)
                else:
                    log.debug('Shutting down VM')
                    session.xenapi.VM.clean_shutdown(vm_id)
                    restore_actions.append(
                        lambda: session.xenapi.VM.start(vm_id, False, False))

            sid = session._session
            rack = storage.get_rack_for(vm_name)
            log.info('Starting backup for VM %s (uuid=%s) on server %s',
                     vm_name, uuid, host['name'])
            progress = None
            with Client(host['url']) as c:
                resp = c.get('/export', {'session_id': sid, 'uuid': uuid})
                task_id = resp.getheader('task-id')
                restore_actions.append(lambda: cancel_task(session, task_id))
                if show_progress:
                    log.debug("Starting progress monitor")
                    progress = ProgressMonitor(session, task_id)
                    progress.start()
                s = rack.create_slot()
                writer = s.get_writer()
                bufsize = 1048576
                while True:
                    data = resp.read(bufsize)
                    if not data: break
                    writer.write(data)
                s.close()  # close only if finished
            if progress:
                progress.stop()
            rack.shrink()
Exemple #11
0
                bufsize = 1048576
                while True:
                    data = resp.read(bufsize)
                    if not data: break
                    writer.write(data)
                s.close()  # close only if finished
            if progress:
                progress.stop()
            rack.shrink()
        finally:
            try:
                for a in reversed(restore_actions):
                    a()
            except Exception, e:
                log.error(
                    'Restore action after backup failed, this may leave some temporary snapshots or machine is halted. Error: %s'
                    % e)
        log.info('Finished backup for VM %s (uuid=%s) on server %s', vm_name,
                 uuid, host['name'])


class BackupOneCommand(CommandForOneHost, BackupOne):
    name = "backup"
    description = "Backups one VM"

    def execute_for_one(self, session, host):
        vm_name = self.args.vm
        show_progress = not self.args.no_progress
        force_shutdown = self.args.shutdown

        ids = session.xenapi.VM.get_by_name_label(vm_name)