def get_start_date(date): # start_date format "%Y-%m-%dT%H:%M:%S" now = datetime.datetime.now() start_date = now + datetime.timedelta(0, 2, 0) if (utils.date_to_timestamp(date) > utils.date_to_timestamp( now.isoformat().split('.')[0])): start_date = datetime.datetime.strptime( date, "%Y-%m-%dT%H:%M:%S") return start_date
def get_start_date(date): # start_date format "%Y-%m-%dT%H:%M:%S" now = datetime.datetime.now() start_date = now + datetime.timedelta(0, 2, 0) if (utils.date_to_timestamp(date) > utils.date_to_timestamp(now.isoformat().split('.')[0])): start_date = datetime.datetime.strptime( date, "%Y-%m-%dT%H:%M:%S") return start_date
def execute_method(self): conf = self.conf logging.info('[*] Executing FS restore...') restore_timestamp = None restore_abs_path = conf.restore_abs_path if conf.restore_from_date: restore_timestamp = utils.date_to_timestamp(conf.restore_from_date) if conf.backup_media == 'fs': backup = self.storage.find_one(conf.hostname_backup_name, restore_timestamp) self.engine.restore(backup, restore_abs_path, conf.overwrite) return {} res = restore.RestoreOs(conf.client_manager, conf.container) if conf.backup_media == 'nova': res.restore_nova(conf.nova_inst_id, restore_timestamp) elif conf.backup_media == 'cinder': res.restore_cinder_by_glance(conf.cinder_vol_id, restore_timestamp) elif conf.backup_media == 'cindernative': res.restore_cinder(conf.cindernative_vol_id, restore_timestamp) else: raise Exception("unknown backup type: %s" % conf.backup_media) return {}
def restore_cinder(self, volume_id, restore_from_timestamp): """ Restoring cinder backup using :param volume_id: :param restore_from_timestamp: :return: """ cinder = self.client_manager.get_cinder() search_opts = { 'volume_id': volume_id, 'status': 'available', } backups = cinder.backups.list(search_opts=search_opts) backups_filter = ([ x for x in backups if utils.date_to_timestamp( x.created_at.split('.')[0]) >= restore_from_timestamp ]) if not backups_filter: logging.warning("no available backups for cinder volume," "restore newest backup") backup = max(backups, key=lambda x: x.created_at) cinder.restores.restore(backup_id=backup.id) else: backup = min(backups_filter, key=lambda x: x.created_at) cinder.restores.restore(backup_id=backup.id)
def get_backups_from_timestamp(backups, restore_from_timestamp): for backup in backups: backup_created_date = backup.created_at.split('.')[0] backup_created_timestamp = ( utils.date_to_timestamp(backup_created_date)) if backup_created_timestamp >= restore_from_timestamp: yield backup
def execute(self): conf = self.conf LOG.info('Executing FS restore...') restore_timestamp = None restore_abs_path = conf.restore_abs_path if conf.restore_from_date: restore_timestamp = utils.date_to_timestamp(conf.restore_from_date) if conf.backup_media == 'fs': self.engine.restore( hostname_backup_name=self.conf.hostname_backup_name, restore_path=restore_abs_path, overwrite=conf.overwrite, recent_to_date=restore_timestamp) try: if conf.consistency_checksum: backup_checksum = conf.consistency_checksum restore_checksum = checksum.CheckSum(restore_abs_path, ignorelinks=True) if restore_checksum.compare(backup_checksum): LOG.info('Consistency check success.') else: raise ConsistencyCheckException( "Backup Consistency Check failed: backup checksum " "({0}) and restore checksum ({1}) did not match.". format(backup_checksum, restore_checksum.checksum)) except OSError as e: raise ConsistencyCheckException( "Backup Consistency Check failed: could not checksum file" " {0} ({1})".format(e.filename, e.strerror)) return {} res = restore.RestoreOs(conf.client_manager, conf.container, self.storage) if conf.backup_media == 'nova': LOG.info("Restoring nova backup. Instance ID: {0}, timestamp: {1}". format(conf.nova_inst_id, restore_timestamp)) nova_network = None if conf.nova_restore_network: nova_network = conf.nova_restore_network res.restore_nova(conf.nova_inst_id, restore_timestamp, nova_network) elif conf.backup_media == 'cinder': LOG.info("Restoring cinder backup from glance. Volume ID: {0}, " "timestamp: {1}".format(conf.cinder_vol_id, restore_timestamp)) res.restore_cinder_by_glance(conf.cinder_vol_id, restore_timestamp) elif conf.backup_media == 'cindernative': LOG.info("Restoring cinder native backup. Volume ID {0}, Backup ID" " {1}, timestamp: {2}".format(conf.cindernative_vol_id, conf.cindernative_backup_id, restore_timestamp)) res.restore_cinder(conf.cindernative_vol_id, conf.cindernative_backup_id, restore_timestamp) else: raise Exception("unknown backup type: %s" % conf.backup_media) return {}
def execute(self): # remove backups by freezer admin action backup_media = self.conf.backup_media if backup_media == 'cindernative': admin_os = admin.AdminOs(self.conf.client_manager) admin_os.del_off_limit_fullbackup(self.conf.cindernative_vol_id, self.conf.fullbackup_rotation) return {} if self.conf.remove_from_date: timestamp = utils.date_to_timestamp(self.conf.remove_from_date) else: timestamp = datetime.datetime.now() - \ datetime.timedelta(days=float(self.conf.remove_older_than)) timestamp = int(time.mktime(timestamp.timetuple())) if self.conf.backup_media == 'cinder': if self.conf.cinder_vol_id: old_backups = self.get_cinder_old_backups( timestamp, self.conf.cinder_vol_id) self.remove_backup_dirs(old_backups, self.conf.cinder_vol_id) return {} else: for instance_id in self.cinder_vol_ids: old_backups = self.get_cinder_old_backups( timestamp, instance_id) self.remove_backup_dirs(old_backups, instance_id) return {} hostname_backup_name_set = set() if self.conf.backup_media == 'nova': if self.conf.project_id: instance_ids = self.engine.get_nova_tenant( self.conf.project_id) for instance_id in instance_ids: hostname_backup_name = os.path.join( self.conf.hostname_backup_name, instance_id) hostname_backup_name_set.add(hostname_backup_name) elif self.conf.nova_inst_id: hostname_backup_name = os.path.join( self.conf.hostname_backup_name, self.conf.nova_inst_id) hostname_backup_name_set.add(hostname_backup_name) else: for instance_id in self.nova_instance_ids: hostname_backup_name = os.path.join( self.conf.hostname_backup_name, instance_id) hostname_backup_name_set.add(hostname_backup_name) else: hostname_backup_name_set.add(self.conf.hostname_backup_name) for backup_name in hostname_backup_name_set: self.storage.remove_older_than(self.engine, timestamp, backup_name) return {}
def execute_method(self): if self.conf.remove_from_date: timestamp = utils.date_to_timestamp(self.conf.remove_from_date) else: timestamp = datetime.datetime.now() - \ datetime.timedelta(days=self.conf.remove_older_than) timestamp = int(time.mktime(timestamp.timetuple())) self.storage.remove_older_than(timestamp, self.conf.hostname_backup_name) return {}
def execute(self): if self.conf.remove_from_date: timestamp = utils.date_to_timestamp(self.conf.remove_from_date) else: timestamp = datetime.datetime.now() - \ datetime.timedelta(days=self.conf.remove_older_than) timestamp = int(time.mktime(timestamp.timetuple())) self.storage.remove_older_than(self.engine, timestamp, self.conf.hostname_backup_name) return {}
def execute_method(self): conf = self.conf logging.info('[*] Executing FS restore...') restore_timestamp = None restore_abs_path = conf.restore_abs_path if conf.restore_from_date: restore_timestamp = utils.date_to_timestamp(conf.restore_from_date) if conf.backup_media == 'fs': backup = self.storage.find_one(conf.hostname_backup_name, restore_timestamp) self.engine.restore(backup, restore_abs_path, conf.overwrite) try: if conf.consistency_checksum: backup_checksum = conf.consistency_checksum restore_checksum = CheckSum(restore_abs_path, ignorelinks=True) if restore_checksum.compare(backup_checksum): logging.info('[*] Consistency check success.') else: raise ConsistencyCheckException( "Backup Consistency Check failed: backup checksum " "({0}) and restore checksum ({1}) did not match.". format(backup_checksum, restore_checksum.checksum)) except OSError as e: raise ConsistencyCheckException( "Backup Consistency Check failed: could not checksum file" " {0} ({1})".format(e.filename, e.strerror)) return {} res = restore.RestoreOs(conf.client_manager, conf.container) if conf.backup_media == 'nova': res.restore_nova(conf.nova_inst_id, restore_timestamp) elif conf.backup_media == 'cinder': res.restore_cinder_by_glance(conf.cinder_vol_id, restore_timestamp) elif conf.backup_media == 'cindernative': res.restore_cinder(conf.cindernative_vol_id, restore_timestamp) else: raise Exception("unknown backup type: %s" % conf.backup_media) return {}
def execute_method(self): conf = self.conf logging.info('[*] Executing FS restore...') restore_timestamp = None restore_abs_path = conf.restore_abs_path if conf.restore_from_date: restore_timestamp = utils.date_to_timestamp(conf.restore_from_date) if conf.backup_media == 'fs': backup = self.storage.find_one(conf.hostname_backup_name, restore_timestamp) self.engine.restore(backup, restore_abs_path, conf.overwrite) try: if conf.consistency_checksum: backup_checksum = conf.consistency_checksum restore_checksum = CheckSum(restore_abs_path, ignorelinks=True) if restore_checksum.compare(backup_checksum): logging.info('[*] Consistency check success.') else: raise ConsistencyCheckException( "Backup Consistency Check failed: backup checksum " "({0}) and restore checksum ({1}) did not match.". format(backup_checksum, restore_checksum.checksum)) except OSError as e: raise ConsistencyCheckException( "Backup Consistency Check failed: could not checksum file" " {0} ({1})".format(e.filename, e.strerror)) return {} res = restore.RestoreOs(conf.client_manager, conf.container) if conf.backup_media == 'nova': res.restore_nova(conf.nova_inst_id, restore_timestamp) elif conf.backup_media == 'cinder': res.restore_cinder_by_glance(conf.cinder_vol_id, restore_timestamp) elif conf.backup_media == 'cindernative': res.restore_cinder(conf.cindernative_vol_id, restore_timestamp) else: raise Exception("unknown backup type: %s" % conf.backup_media) return {}
def restore_cinder(self, volume_id, restore_from_timestamp): """ Restoring cinder backup using :param volume_id: :param restore_from_timestamp: :return: """ cinder = self.client_manager.get_cinder() search_opts = {'volume_id': volume_id, 'status': 'available', } backups = cinder.backups.list(search_opts=search_opts) backups_filter = ([x for x in backups if utils.date_to_timestamp( x.created_at.split('.')[0]) >= restore_from_timestamp]) if not backups_filter: logging.warning("no available backups for cinder volume," "restore newest backup") backup = max(backups, key=lambda x: x.created_at) cinder.restores.restore(backup_id=backup.id) else: backup = min(backups_filter, key=lambda x: x.created_at) cinder.restores.restore(backup_id=backup.id)
def get_end_date(start, end): # start end format "%Y-%m-%dT%H:%M:%S" end_date = datetime.datetime.strptime(end, "%Y-%m-%dT%H:%M:%S") if (utils.date_to_timestamp(start) > utils.date_to_timestamp(end)): end_date = None return end_date
def test_date_to_timestamp(self): # ensure that timestamp is check with appropriate timezone offset assert (1417649003+time.timezone) == \ utils.date_to_timestamp("2014-12-03T23:23:23")
def execute(self): conf = self.conf LOG.info('Executing Restore...') restore_timestamp = None restore_abs_path = conf.restore_abs_path if conf.restore_from_date: restore_timestamp = utils.date_to_timestamp(conf.restore_from_date) if conf.backup_media == 'fs': self.engine.restore( hostname_backup_name=self.conf.hostname_backup_name, restore_resource=restore_abs_path, overwrite=conf.overwrite, recent_to_date=restore_timestamp, backup_media=conf.mode) try: if conf.consistency_checksum: backup_checksum = conf.consistency_checksum restore_checksum = checksum.CheckSum(restore_abs_path, ignorelinks=True) if restore_checksum.compare(backup_checksum): LOG.info('Consistency check success.') else: raise ConsistencyCheckException( "Backup Consistency Check failed: backup checksum " "({0}) and restore checksum ({1}) did not match.". format(backup_checksum, restore_checksum.checksum)) except OSError as e: raise ConsistencyCheckException( "Backup Consistency Check failed: could not checksum file" " {0} ({1})".format(e.filename, e.strerror)) return {} res = restore.RestoreOs(conf.client_manager, conf.container, self.storage) if conf.backup_media == 'nova': if self.conf.project_id: return self.engine.restore_nova_tenant( project_id=self.conf.project_id, hostname_backup_name=self.conf.hostname_backup_name, overwrite=conf.overwrite, recent_to_date=restore_timestamp) elif conf.nova_inst_id: LOG.info("Restoring nova backup. Instance ID: {0}, " "timestamp: {1} network-id {2}".format( conf.nova_inst_id, restore_timestamp, conf.nova_restore_network)) hostname_backup_name = os.path.join( self.conf.hostname_backup_name, self.conf.nova_inst_id) self.engine.restore(hostname_backup_name=hostname_backup_name, restore_resource=conf.nova_inst_id, overwrite=conf.overwrite, recent_to_date=restore_timestamp, backup_media=conf.mode) else: for instance_id in self.nova_instance_ids: LOG.info("Restoring nova backup. Instance ID: {0}, " "timestamp: {1} network-id {2}".format( instance_id, restore_timestamp, conf.nova_restore_network)) hostname_backup_name = os.path.join( self.conf.hostname_backup_name, instance_id) self.engine.restore( hostname_backup_name=hostname_backup_name, restore_resource=instance_id, overwrite=conf.overwrite, recent_to_date=restore_timestamp, backup_media=conf.mode) elif conf.backup_media == 'glance': if self.conf.project_id: return self.engine.restore_glance_tenant( project_id=self.conf.project_id, hostname_backup_name=self.conf.hostname_backup_name, overwrite=conf.overwrite, recent_to_date=restore_timestamp) elif conf.glance_image_id: LOG.info("Restoring glance backup. Image ID: {0}, " "timestamp: {1} ".format(conf.glance_image_id, restore_timestamp)) hostname_backup_name = os.path.join( self.conf.hostname_backup_name, self.conf.glance_image_id) self.engine.restore(hostname_backup_name=hostname_backup_name, restore_resource=conf.glance_image_id, overwrite=conf.overwrite, recent_to_date=restore_timestamp, backup_media=conf.mode) else: for image_id in self.glance_image_ids: LOG.info("Restoring glance backup. Image ID: {0}, " "timestamp: {1}".format(image_id, restore_timestamp)) hostname_backup_name = os.path.join( self.conf.hostname_backup_name, image_id) self.engine.restore( hostname_backup_name=hostname_backup_name, restore_resource=image_id, overwrite=conf.overwrite, recent_to_date=restore_timestamp, backup_media=conf.mode) elif conf.backup_media == 'cinder': if conf.cinder_vol_id: LOG.info("Restoring cinder backup from glance. " "Volume ID: {0}, timestamp: {1}".format( conf.cinder_vol_id, restore_timestamp)) res.restore_cinder_by_glance(conf.cinder_vol_id, restore_timestamp) else: for instance_id in self.cinder_vol_ids: LOG.info("Restoring cinder backup from glance. " "Volume ID: {0}, timestamp: {1}".format( instance_id, restore_timestamp)) res.restore_cinder_by_glance(instance_id, restore_timestamp) elif conf.backup_media == 'cindernative': LOG.info("Restoring cinder native backup. Volume ID {0}, Backup ID" " {1}, timestamp: {2}".format(conf.cindernative_vol_id, conf.cindernative_backup_id, restore_timestamp)) res.restore_cinder(conf.cindernative_vol_id, conf.cindernative_backup_id, restore_timestamp) elif conf.backup_media == 'cinderbrick': LOG.info("Restoring cinder backup using os-brick. Volume ID {0}, " "timestamp: {1}".format(conf.cinderbrick_vol_id, restore_timestamp)) self.engine.restore( hostname_backup_name=self.conf.hostname_backup_name, restore_resource=conf.cinderbrick_vol_id, overwrite=conf.overwrite, recent_to_date=restore_timestamp, backup_media=conf.mode) else: raise Exception("unknown backup type: %s" % conf.backup_media) return {}
def test_date_to_timestamp(self): # ensure that timestamp is check with appropriate timezone offset assert (1417649003+time.timezone) == \ utils.date_to_timestamp("2014-12-03T23:23:23")
def get_end_date(start, end): # start end format "%Y-%m-%dT%H:%M:%S" end_date = datetime.datetime.strptime(end, "%Y-%m-%dT%H:%M:%S") if (utils.date_to_timestamp(start) > utils.date_to_timestamp(end)): end_date = None return end_date