def execute_restore(self, context, backup_id, restore_location): try: LOG.debug("Cleaning out restore location: %s", restore_location) utils.execute_with_timeout("sudo", "chmod", "-R", "0777", restore_location) utils.clean_out(restore_location) LOG.debug("Finding backup %s to restore", backup_id) backup = DBBackup.find_by(id=backup_id) LOG.debug("Getting Restore Runner of type %s", backup.backup_type) restore_runner = self._get_restore_runner(backup.backup_type) LOG.debug("Getting Storage Strategy") storage_strategy = get_storage_strategy(CONF.storage_strategy, CONF.storage_namespace)(context) LOG.debug("Preparing storage to download stream.") download_stream = storage_strategy.load(context, backup.location, restore_runner.is_zipped) with restore_runner(restore_stream=download_stream, restore_location=restore_location) as runner: LOG.debug("Restoring instance from backup %s to %s", backup_id, restore_location) content_size = runner.restore() LOG.info("Restore from backup %s completed successfully to %s", backup_id, restore_location) LOG.info("Restore size: %s", content_size) utils.execute_with_timeout("sudo", "chown", "-R", "mysql", restore_location) except Exception as e: LOG.error(e) LOG.error("Error restoring backup %s", backup_id) raise else: LOG.info("Restored Backup %s", backup_id)
def execute_backup(self, context, backup_id, runner=RUNNER): LOG.debug("Searching for backup instance %s", backup_id) backup = DBBackup.find_by(id=backup_id) LOG.info("Setting task state to %s for instance %s", BackupState.NEW, backup.instance_id) backup.state = BackupState.NEW backup.save() LOG.info("Running backup %s", backup_id) user = ADMIN_USER_NAME password = get_auth_password() swiftStorage = get_storage_strategy( CONF.storage_strategy, CONF.storage_namespace)(context) backup.state = BackupState.BUILDING backup.save() try: with runner(filename=backup_id, user=user, password=password)\ as bkup: LOG.info("Starting Backup %s", backup_id) success, note, checksum, location = swiftStorage.save( BACKUP_CONTAINER, bkup) LOG.info("Backup %s completed status: %s", backup_id, success) LOG.info("Backup %s file size: %s", backup_id, bkup.content_length) LOG.info('Backup %s file checksum: %s', backup_id, checksum) LOG.info('Backup %s location: %s', backup_id, location) if not success: raise BackupError(backup.note) except Exception as e: LOG.error(e) LOG.error("Error saving %s Backup", backup_id) backup.state = BackupState.FAILED backup.save() raise else: LOG.info("Saving %s Backup Info to model", backup_id) backup.state = BackupState.COMPLETED backup.checksum = checksum backup.location = location backup.note = note backup.backup_type = bkup.backup_type backup.save()
def execute_backup(self, context, backup_id, runner=RUNNER): LOG.debug("Searching for backup instance %s", backup_id) backup = DBBackup.find_by(id=backup_id) LOG.info("Setting task state to %s for instance %s", BackupState.NEW, backup.instance_id) backup.state = BackupState.NEW backup.save() LOG.info("Running backup %s", backup_id) user = ADMIN_USER_NAME password = get_auth_password() swiftStorage = get_storage_strategy(CONF.storage_strategy, CONF.storage_namespace)(context) backup.state = BackupState.BUILDING backup.save() try: with runner(filename=backup_id, user=user, password=password)\ as bkup: LOG.info("Starting Backup %s", backup_id) success, note, checksum, location = swiftStorage.save( BACKUP_CONTAINER, bkup) LOG.info("Backup %s completed status: %s", backup_id, success) LOG.info("Backup %s file size: %s", backup_id, bkup.content_length) LOG.info('Backup %s file checksum: %s', backup_id, checksum) LOG.info('Backup %s location: %s', backup_id, location) if not success: raise BackupError(backup.note) except Exception as e: LOG.error(e) LOG.error("Error saving %s Backup", backup_id) backup.state = BackupState.FAILED backup.save() raise else: LOG.info("Saving %s Backup Info to model", backup_id) backup.state = BackupState.COMPLETED backup.checksum = checksum backup.location = location backup.note = note backup.backup_type = bkup.backup_type backup.save()
def execute_backup(self, context, backup_id, runner=RUNNER): LOG.debug("Searching for backup instance %s", backup_id) backup = DBBackup.find_by(id=backup_id) LOG.info("Setting task state to %s for instance %s", BackupState.NEW, backup.instance_id) backup.state(BackupState.NEW) backup.save() LOG.info("Running backup %s", backup_id) user = ADMIN_USER_NAME password = get_auth_password() connection = create_swift_client(context) with runner(filename=backup_id, user=user, password=password) as bkup: LOG.info("Starting Backup %s", backup_id) connection.put_container(BACKUP_CONTAINER) while not bkup.end_of_file: segment = bkup.segment etag = connection.put_object(BACKUP_CONTAINER, segment, bkup) # Check each segment MD5 hash against swift etag # Raise an error and mark backup as failed if etag != bkup.schecksum.hexdigest(): print etag, bkup.schecksum.hexdigest() backup.state(BackupState.FAILED) backup.note = "Error sending data to cloud files!" backup.save() raise BackupError(backup.note) checksum = bkup.checksum.hexdigest() url = connection.url location = "%s/%s/%s" % (url, BACKUP_CONTAINER, bkup.manifest) LOG.info("Backup %s file size: %s", backup_id, bkup.content_length) LOG.info('Backup %s file checksum: %s', backup_id, checksum) LOG.info('Backup %s location: %s', location) # Create the manifest file headers = {'X-Object-Manifest': bkup.prefix} connection.put_object(BACKUP_CONTAINER, bkup.manifest, contents='', headers=headers) LOG.info("Saving %s Backup Info", backup_id) backup.state(BackupState.COMPLETED) backup.checksum = checksum backup.location = location backup.backup_type = bkup.backup_type backup.save()
def execute_restore(self, context, backup_id, restore_location): try: LOG.debug("Cleaning out restore location: %s", restore_location) utils.execute_with_timeout("sudo", "chmod", "-R", "0777", restore_location) utils.clean_out(restore_location) LOG.debug("Finding backup %s to restore", backup_id) backup = DBBackup.find_by(id=backup_id) LOG.debug("Getting Restore Runner of type %s", backup.backup_type) restore_runner = self._get_restore_runner(backup.backup_type) LOG.debug("Getting Storage Strategy") storage_strategy = get_storage_strategy( CONF.storage_strategy, CONF.storage_namespace)(context) LOG.debug("Preparing storage to download stream.") download_stream = storage_strategy.load(context, backup.location, restore_runner.is_zipped) with restore_runner(restore_stream=download_stream, restore_location=restore_location) as runner: LOG.debug("Restoring instance from backup %s to %s", backup_id, restore_location) content_size = runner.restore() LOG.info("Restore from backup %s completed successfully to %s", backup_id, restore_location) LOG.info("Restore size: %s", content_size) utils.execute_with_timeout("sudo", "chown", "-R", "mysql", restore_location) except Exception as e: LOG.error(e) LOG.error("Error restoring backup %s", backup_id) raise else: LOG.info("Restored Backup %s", backup_id)