Refactor restore to persist backups

This commit is contained in:
amcmanu3 2024-06-14 19:31:26 -04:00
parent e5d18e5f2c
commit 3030712587

View File

@ -187,15 +187,20 @@ class ApiServersServerBackupsBackupIndexHandler(BaseApiHandler):
}, },
) )
try:
svr_obj = self.controller.servers.get_server_obj(server_id) svr_obj = self.controller.servers.get_server_obj(server_id)
server_data = self.controller.servers.get_server_data_by_id(server_id) server_data = self.controller.servers.get_server_data_by_id(server_id)
zip_name = data["filename"] zip_name = data["filename"]
# import the server again based on zipfile # import the server again based on zipfile
backup_config = self.controller.management.get_backup_config(backup_id) backup_config = self.controller.management.get_backup_config(backup_id)
if Helpers.validate_traversal(backup_config["backup_location"], zip_name): backup_location = os.path.join(
temp_dir = Helpers.unzip_backup_archive( backup_config["backup_location"], backup_config["backup_id"]
backup_config["backup_location"], zip_name )
if Helpers.validate_traversal(backup_location, zip_name):
try:
temp_dir = Helpers.unzip_backup_archive(backup_location, zip_name)
except (FileNotFoundError, NotADirectoryError) as e:
return self.finish_json(
400, {"status": "error", "error": f"NO BACKUP FOUND {e}"}
) )
if server_data["type"] == "minecraft-java": if server_data["type"] == "minecraft-java":
new_server = self.controller.restore_java_zip_server( new_server = self.controller.restore_java_zip_server(
@ -253,27 +258,34 @@ class ApiServersServerBackupsBackupIndexHandler(BaseApiHandler):
self.controller.servers.update_server(new_server_obj) self.controller.servers.update_server(new_server_obj)
# preserve backup config # preserve backup config
server_backups = self.controller.management.get_backups_by_server( server_backups = self.controller.management.get_backups_by_server(server_id)
server_id
)
for backup in server_backups: for backup in server_backups:
old_backup_id = server_backups[backup]["backup_id"]
del server_backups[backup]["backup_id"] del server_backups[backup]["backup_id"]
server_backups[backup]["server_id"] = new_server_id server_backups[backup]["server_id"] = new_server_id
if str(server_id) in (server_backups[backup]["backup_location"]): if str(server_id) in (server_backups[backup]["backup_location"]):
server_backups[backup]["backup_location"] = str( server_backups[backup]["backup_location"] = str(
server_backups[backup]["backup_location"] server_backups[backup]["backup_location"]
).replace(str(server_id), str(new_server_id)) ).replace(str(server_id), str(new_server_id))
self.controller.management.add_backup_config(server_backups[backup]) new_backup_id = self.controller.management.add_backup_config(
server_backups[backup]
)
os.listdir(server_backups[backup]["backup_location"])
FileHelpers.move_dir(
os.path.join(
server_backups[backup]["backup_location"], old_backup_id
),
os.path.join(
server_backups[backup]["backup_location"], new_backup_id
),
)
# remove old server's tasks # remove old server's tasks
try: try:
self.tasks_manager.remove_all_server_tasks(server_id) self.tasks_manager.remove_all_server_tasks(server_id)
except JobLookupError as e: except JobLookupError as e:
logger.info("No active tasks found for server: {e}") logger.info("No active tasks found for server: {e}")
self.controller.remove_server(server_id, True) self.controller.remove_server(server_id, True)
except (FileNotFoundError, NotADirectoryError) as e:
return self.finish_json(
400, {"status": "error", "error": f"NO BACKUP FOUND {e}"}
)
self.controller.management.add_to_audit_log( self.controller.management.add_to_audit_log(
auth_data[4]["user_id"], auth_data[4]["user_id"],
f"Restored server {server_id} backup {data['filename']}", f"Restored server {server_id} backup {data['filename']}",