def delete(filename, destination=None, profile="default", config=CONFIG_FILE, **kwargs): """Delete a backup. :type filename: str :param filename: stored filename to delete. :type destination: str :param destination: glacier|s3|swift :type profile: str :param profile: Profile name (default by default). :type conf: dict :keyword conf: A dict with a custom configuration. :type conf: dict :keyword conf: Override/set AWS configuration. :rtype: bool :return: True if the file is deleted. """ if not filename: log.error("No file to delete, use -f to specify one.") return backup = Backups.match_filename(filename, destination, profile=profile, config=config) if not backup: log.error("No file matched.") return key_name = backup.stored_filename storage_backend, destination, conf = _get_store_backend( config, destination, profile) session_id = str(uuid.uuid4()) events.before_delete(session_id) log.info("Deleting {0}".format(key_name)) storage_backend.delete(key_name) backup.set_deleted() events.on_delete(session_id, backup) return backup
def delete(filename, destination=None, profile="default", config=CONFIG_FILE, **kwargs): """Delete a backup. :type filename: str :param filename: stored filename to delete. :type destination: str :param destination: glacier|s3|swift :type profile: str :param profile: Profile name (default by default). :type conf: dict :keyword conf: A dict with a custom configuration. :type conf: dict :keyword conf: Override/set AWS configuration. :rtype: bool :return: True if the file is deleted. """ if not filename: log.error("No file to delete, use -f to specify one.") return backup = Backups.match_filename(filename, destination, profile=profile, config=config) if not backup: log.error("No file matched.") return key_name = backup.stored_filename storage_backend, destination, conf = _get_store_backend(config, destination, profile) session_id = str(uuid.uuid4()) events.before_delete(session_id) log.info("Deleting {0}".format(key_name)) storage_backend.delete(key_name) backup.set_deleted() events.on_delete(session_id, backup) return backup
def delete(filename, destination=DEFAULT_DESTINATION, profile="default", **kwargs): """Delete a backup. :type filename: str :param filename: stored filename to delete. :type destination: str :param destination: glacier|s3 :type profile: str :param profile: Profile name (default by default). :type conf: dict :keyword conf: A dict with a custom configuration. :type conf: dict :keyword conf: Override/set AWS configuration. :rtype: bool :return: True if the file is deleted. """ conf = kwargs.get("conf", None) if not filename: log.error("No file to delete, use -f to specify one.") return backup = Backups.match_filename(filename, destination, profile=profile) if not backup: log.error("No file matched.") return key_name = backup.stored_filename storage_backend = _get_store_backend(conf, destination, profile) log.info("Deleting {0}".format(key_name)) storage_backend.delete(key_name) backup.set_deleted() BakSyncer(conf).sync_auto() return True
def sync(self): """Draft for implementing bakthat clients (hosts) backups data synchronization. Synchronize Bakthat sqlite database via a HTTP POST request. Backups are never really deleted from sqlite database, we just update the is_deleted key. It sends the last server sync timestamp along with data updated since last sync. Then the server return backups that have been updated on the server since last sync. On both sides, backups are either created if they don't exists or updated if the incoming version is newer. """ log.debug("Start syncing") self.register() last_sync_ts = Config.get_key("sync_ts", 0) to_insert_in_mongo = [b._data for b in Backups.search(last_updated_gt=last_sync_ts)] data = dict(sync_ts=last_sync_ts, to_insert_in_mongo=to_insert_in_mongo) r_kwargs = self.request_kwargs.copy() log.debug("Initial payload: {0}".format(data)) r_kwargs.update({"data": json.dumps(data)}) r = requests.post(self.get_resource("backups/sync/status"), **r_kwargs) if r.status_code != 200: log.error("An error occured during sync: {0}".format(r.text)) return log.debug("Sync result: {0}".format(r.json())) to_insert_in_bakthat = r.json().get("to_insert_in_bakthat") sync_ts = r.json().get("sync_ts") for newbackup in to_insert_in_bakthat: sqlite_backup = Backups.match_filename(newbackup["stored_filename"], newbackup["backend"]) if sqlite_backup and newbackup["last_updated"] > sqlite_backup.last_updated: log.debug("Upsert {0}".format(newbackup)) Backups.upsert(**newbackup) elif not sqlite_backup: log.debug("Create backup {0}".format(newbackup)) Backups.create(**newbackup) Config.set_key("sync_ts", sync_ts) log.debug("Sync succcesful")
def restore(filename, destination=None, profile="default", config=CONFIG_FILE, **kwargs): """Restore backup in the current working directory. :type filename: str :param filename: File/directory to backup. :type destination: str :param destination: s3|glacier|swift :type profile: str :param profile: Profile name (default by default). :type conf: dict :keyword conf: Override/set AWS configuration. :rtype: bool :return: True if successful. """ storage_backend, destination, conf = _get_store_backend( config, destination, profile) if not filename: log.error("No file to restore, use -f to specify one.") return backup = Backups.match_filename(filename, destination, profile=profile, config=config) if not backup: log.error("No file matched.") return key_name = backup.stored_filename log.info("Restoring " + key_name) # Asking password before actually download to avoid waiting if key_name and backup.is_encrypted(): password = kwargs.get("password") if not password: password = getpass() log.info("Downloading...") download_kwargs = {} if kwargs.get("job_check"): download_kwargs["job_check"] = True log.info("Job Check: " + repr(download_kwargs)) out = storage_backend.download(key_name, **download_kwargs) if kwargs.get("job_check"): log.info("Job Check Request") # If it's a job_check call, we return Glacier job data return out if out and backup.is_encrypted(): log.info("Decrypting...") decrypted_out = tempfile.TemporaryFile() decrypt(out, decrypted_out, password) out = decrypted_out if out and (key_name.endswith(".tgz") or key_name.endswith(".tgz.enc")): log.info("Uncompressing...") out.seek(0) if not backup.metadata.get("KeyValue"): tar = tarfile.open(fileobj=out) tar.extractall() tar.close() else: with closing(GzipFile(fileobj=out, mode="r")) as f: with open(backup.stored_filename, "w") as out: out.write(f.read()) return True elif out: log.info("Backup is not compressed") with open(backup.filename, "w") as restored: out.seek(0) restored.write(out.read()) return True
def restore(filename, destination=None, profile="default", config=CONFIG_FILE, **kwargs): """Restore backup in the current working directory. :type filename: str :param filename: File/directory to backup. :type destination: str :param destination: s3|glacier|swift :type profile: str :param profile: Profile name (default by default). :type conf: dict :keyword conf: Override/set AWS configuration. :rtype: bool :return: True if successful. """ storage_backend, destination, conf = _get_store_backend(config, destination, profile) if not filename: log.error("No file to restore, use -f to specify one.") return backup = Backups.match_filename(filename, destination, profile=profile, config=config) if not backup: log.error("No file matched.") return session_id = str(uuid.uuid4()) events.before_restore(session_id) key_name = backup.stored_filename log.info("Restoring " + key_name) # Asking password before actually download to avoid waiting if key_name and backup.is_encrypted(): password = kwargs.get("password") if not password: password = getpass() log.info("Downloading...") download_kwargs = {} if kwargs.get("job_check"): download_kwargs["job_check"] = True log.info("Job Check: " + repr(download_kwargs)) out = storage_backend.download(key_name, **download_kwargs) if kwargs.get("job_check"): log.info("Job Check Request") # If it's a job_check call, we return Glacier job data return out if out and backup.is_encrypted(): log.info("Decrypting...") decrypted_out = tempfile.TemporaryFile() decrypt(out, decrypted_out, password) out = decrypted_out if out and (key_name.endswith(".tgz") or key_name.endswith(".tgz.enc")): log.info("Uncompressing...") out.seek(0) if not backup.metadata.get("KeyValue"): tar = tarfile.open(fileobj=out) tar.extractall() tar.close() else: with closing(GzipFile(fileobj=out, mode="r")) as f: with open(backup.stored_filename, "w") as out: out.write(f.read()) elif out: log.info("Backup is not compressed") with open(backup.filename, "w") as restored: out.seek(0) restored.write(out.read()) events.on_restore(session_id, backup) return backup
def restore(filename, destination=DEFAULT_DESTINATION, profile="default", **kwargs): """Restore backup in the current working directory. :type filename: str :param filename: File/directory to backup. :type destination: str :param destination: s3|glacier :type profile: str :param profile: Profile name (default by default). :type conf: dict :keyword conf: Override/set AWS configuration. :rtype: bool :return: True if successful. """ conf = kwargs.get("conf", None) storage_backend = _get_store_backend(conf, destination, profile) if not filename: log.error("No file to restore, use -f to specify one.") return backup = Backups.match_filename(filename, destination, profile=profile) if not backup: log.error("No file matched.") return key_name = backup.stored_filename log.info("Restoring " + key_name) # Asking password before actually download to avoid waiting if key_name and key_name.endswith(".enc"): password = kwargs.get("password") if not password: password = getpass() log.info("Downloading...") download_kwargs = {} if kwargs.get("job_check"): download_kwargs["job_check"] = True log.info("Job Check: " + repr(download_kwargs)) out = storage_backend.download(key_name, **download_kwargs) if kwargs.get("job_check"): log.info("Job Check Request") # If it's a job_check call, we return Glacier job data return out if out and key_name.endswith(".enc"): log.info("Decrypting...") decrypted_out = tempfile.TemporaryFile() decrypt(out, decrypted_out, password) out = decrypted_out if out: log.info("Uncompressing...") out.seek(0) tar = tarfile.open(fileobj=out) tar.extractall() tar.close() return True