mirror of
https://gitlab.com/crafty-controller/crafty-4.git
synced 2025-01-18 17:15:13 +01:00
Merge branch 'refactor/backups' into refactor/upload-api
This commit is contained in:
commit
59d835bd9d
@ -200,7 +200,7 @@ class ManagementController:
|
||||
updates["backup_location"] = Helpers.wtol_path(updates["backup_location"])
|
||||
return HelpersManagement.update_backup_config(backup_id, updates)
|
||||
|
||||
def add_backup_config(self, data):
|
||||
def add_backup_config(self, data) -> str:
|
||||
if "backup_location" in data:
|
||||
data["backup_location"] = Helpers.wtol_path(data["backup_location"])
|
||||
return self.management_helper.add_backup_config(data)
|
||||
|
@ -393,9 +393,10 @@ class HelpersManagement:
|
||||
def remove_backup_config(backup_id):
|
||||
Backups.delete().where(Backups.backup_id == backup_id).execute()
|
||||
|
||||
def add_backup_config(self, conf):
|
||||
Backups.create(**conf)
|
||||
def add_backup_config(self, conf) -> str:
|
||||
backup = Backups.create(**conf)
|
||||
logger.debug("Creating new backup record.")
|
||||
return backup.backup_id
|
||||
|
||||
@staticmethod
|
||||
def update_backup_config(backup_id, data):
|
||||
|
@ -187,15 +187,20 @@ class ApiServersServerBackupsBackupIndexHandler(BaseApiHandler):
|
||||
},
|
||||
)
|
||||
|
||||
try:
|
||||
svr_obj = self.controller.servers.get_server_obj(server_id)
|
||||
server_data = self.controller.servers.get_server_data_by_id(server_id)
|
||||
zip_name = data["filename"]
|
||||
# import the server again based on zipfile
|
||||
backup_config = self.controller.management.get_backup_config(backup_id)
|
||||
if Helpers.validate_traversal(backup_config["backup_location"], zip_name):
|
||||
temp_dir = Helpers.unzip_backup_archive(
|
||||
backup_config["backup_location"], zip_name
|
||||
backup_location = os.path.join(
|
||||
backup_config["backup_location"], backup_config["backup_id"]
|
||||
)
|
||||
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":
|
||||
new_server = self.controller.restore_java_zip_server(
|
||||
@ -253,27 +258,34 @@ class ApiServersServerBackupsBackupIndexHandler(BaseApiHandler):
|
||||
self.controller.servers.update_server(new_server_obj)
|
||||
|
||||
# preserve backup config
|
||||
server_backups = self.controller.management.get_backups_by_server(
|
||||
server_id
|
||||
)
|
||||
server_backups = self.controller.management.get_backups_by_server(server_id)
|
||||
for backup in server_backups:
|
||||
old_backup_id = server_backups[backup]["backup_id"]
|
||||
del server_backups[backup]["backup_id"]
|
||||
server_backups[backup]["server_id"] = new_server_id
|
||||
if str(server_id) in (server_backups[backup]["backup_location"]):
|
||||
server_backups[backup]["backup_location"] = str(
|
||||
server_backups[backup]["backup_location"]
|
||||
).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
|
||||
try:
|
||||
self.tasks_manager.remove_all_server_tasks(server_id)
|
||||
except JobLookupError as e:
|
||||
logger.info("No active tasks found for server: {e}")
|
||||
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(
|
||||
auth_data[4]["user_id"],
|
||||
f"Restored server {server_id} backup {data['filename']}",
|
||||
|
Loading…
x
Reference in New Issue
Block a user