def setUp(self): super(TestBackup, self).setUp() self.backup_opt = commons.BackupOpt1() self.bakup_os = backup.BackupOs(self.backup_opt.client_manager, self.backup_opt.container, self.backup_opt.storage) self.client_manager = self.backup_opt.client_manager self.storage = self.backup_opt.storage
def backup(self, app_mode): """ :type app_mode: freezer.mode.mode.Mode :return: """ backup_media = self.conf.backup_media time_stamp = utils.DateTime.now().timestamp self.conf.time_stamp = time_stamp if backup_media == 'fs': LOG.info('Path to backup: {0}'.format(self.conf.path_to_backup)) app_mode.prepare() snapshot_taken = snapshot.snapshot_create(self.conf) if snapshot_taken: app_mode.release() try: filepath = '.' chdir_path = os.path.expanduser( os.path.normpath(self.conf.path_to_backup.strip())) if not os.path.exists(chdir_path): msg = 'Path to backup does not exist {0}'.format( chdir_path) LOG.critical(msg) raise IOError(msg) if not os.path.isdir(chdir_path): filepath = os.path.basename(chdir_path) chdir_path = os.path.dirname(chdir_path) os.chdir(chdir_path) # Checksum for Backup Consistency if self.conf.consistency_check: ignorelinks = (self.conf.dereference_symlink is None or self.conf.dereference_symlink == 'hard') consistency_checksum = checksum.CheckSum( filepath, ignorelinks=ignorelinks).compute() LOG.info('Computed checksum for consistency {0}'.format( consistency_checksum)) self.conf.consistency_checksum = consistency_checksum return self.engine.backup( backup_resource=filepath, hostname_backup_name=self.conf.hostname_backup_name, no_incremental=self.conf.no_incremental, max_level=self.conf.max_level, always_level=self.conf.always_level, restart_always_level=self.conf.restart_always_level) finally: # whether an error occurred or not, remove the snapshot anyway app_mode.release() if snapshot_taken: snapshot.snapshot_remove(self.conf, self.conf.shadow, self.conf.windows_volume) backup_os = backup.BackupOs(self.conf.client_manager, self.conf.container, self.storage) if backup_media == 'nova': if self.conf.project_id: return self.engine.backup_nova_tenant( project_id=self.conf.project_id, hostname_backup_name=self.conf.hostname_backup_name, no_incremental=self.conf.no_incremental, max_level=self.conf.max_level, always_level=self.conf.always_level, restart_always_level=self.conf.restart_always_level) elif self.conf.nova_inst_id: LOG.info('Executing nova backup. Instance ID: {0}'.format( self.conf.nova_inst_id)) hostname_backup_name = os.path.join( self.conf.hostname_backup_name, self.conf.nova_inst_id) return self.engine.backup( backup_resource=self.conf.nova_inst_id, hostname_backup_name=hostname_backup_name, no_incremental=self.conf.no_incremental, max_level=self.conf.max_level, always_level=self.conf.always_level, restart_always_level=self.conf.restart_always_level) else: executor = futures.ThreadPoolExecutor( max_workers=len(self.nova_instance_ids)) futures_list = [] for instance_id in self.nova_instance_ids: hostname_backup_name = os.path.join( self.conf.hostname_backup_name, instance_id) futures_list.append( executor.submit( self.engine.backup( backup_resource=instance_id, hostname_backup_name=hostname_backup_name, no_incremental=self.conf.no_incremental, max_level=self.conf.max_level, always_level=self.conf.always_level, restart_always_level=self.conf. restart_always_level))) futures.wait(futures_list, CONF.timeout) elif backup_media == 'cindernative': LOG.info('Executing cinder native backup. Volume ID: {0}, ' 'incremental: {1}'.format(self.conf.cindernative_vol_id, self.conf.incremental)) backup_os.backup_cinder(self.conf.cindernative_vol_id, name=self.conf.backup_name, incremental=self.conf.incremental) elif backup_media == 'cinder': if self.conf.cinder_vol_id: LOG.info('Executing cinder snapshot. Volume ID: {0}'.format( self.conf.cinder_vol_id)) backup_os.backup_cinder_by_glance(self.conf.cinder_vol_id) else: executor = futures.ThreadPoolExecutor( max_workers=len(self.cinder_vol_ids)) futures_list = [] for instance_id in self.cinder_vol_ids: LOG.info('Executing cinder snapshot. Volume ID:' ' {0}'.format(instance_id)) futures_list.append( executor.submit( backup_os.backup_cinder_by_glance(instance_id))) futures.wait(futures_list, CONF.timeout) elif backup_media == 'cinderbrick': LOG.info('Executing cinder volume backup using os-brick. ' 'Volume ID: {0}'.format(self.conf.cinderbrick_vol_id)) return self.engine.backup( backup_resource=self.conf.cinderbrick_vol_id, hostname_backup_name=self.conf.hostname_backup_name, no_incremental=self.conf.no_incremental, max_level=self.conf.max_level, always_level=self.conf.always_level, restart_always_level=self.conf.restart_always_level) else: raise Exception('unknown parameter backup_media %s' % backup_media) return None
def backup(self, app_mode): """ :type app_mode: freezer.mode.mode.Mode :return: """ backup_media = self.conf.backup_media time_stamp = utils.DateTime.now().timestamp self.conf.time_stamp = time_stamp if backup_media == 'fs': app_mode.prepare() snapshot_taken = snapshot.snapshot_create(self.conf) if snapshot_taken: app_mode.release() try: filepath = '.' chdir_path = os.path.expanduser( os.path.normpath(self.conf.path_to_backup.strip())) if not os.path.isdir(chdir_path): filepath = os.path.basename(chdir_path) chdir_path = os.path.dirname(chdir_path) os.chdir(chdir_path) # Checksum for Backup Consistency if self.conf.consistency_check: ignorelinks = (self.conf.dereference_symlink == 'none' or self.conf.dereference_symlink == 'hard') consistency_checksum = CheckSum( filepath, ignorelinks=ignorelinks).compute() logging.info( '[*] Computed checksum for consistency {0}'.format( consistency_checksum)) self.conf.consistency_checksum = consistency_checksum hostname_backup_name = self.conf.hostname_backup_name backup_instance = self.storage.create_backup( hostname_backup_name, self.conf.no_incremental, self.conf.max_level, self.conf.always_level, self.conf.restart_always_level, time_stamp=time_stamp) self.engine.backup(filepath, backup_instance) return backup_instance finally: # whether an error occurred or not, remove the snapshot anyway app_mode.release() if snapshot_taken: snapshot.snapshot_remove(self.conf, self.conf.shadow, self.conf.windows_volume) backup_os = backup.BackupOs(self.conf.client_manager, self.conf.container, self.storage) if backup_media == 'nova': logging.info('[*] Executing nova backup') backup_os.backup_nova(self.conf.nova_inst_id) elif backup_media == 'cindernative': logging.info('[*] Executing cinder backup') backup_os.backup_cinder(self.conf.cindernative_vol_id) elif backup_media == 'cinder': logging.info('[*] Executing cinder snapshot') backup_os.backup_cinder_by_glance(self.conf.cinder_vol_id) else: raise Exception('unknown parameter backup_media %s' % backup_media) return None