mirror of
https://gitlab.com/crafty-controller/crafty-4.git
synced 2024-08-30 18:23:09 +00:00
Refactor restore to persist backups
This commit is contained in:
parent
e5d18e5f2c
commit
3030712587
@ -187,93 +187,105 @@ 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)
|
backup_location = os.path.join(
|
||||||
if Helpers.validate_traversal(backup_config["backup_location"], zip_name):
|
backup_config["backup_location"], backup_config["backup_id"]
|
||||||
temp_dir = Helpers.unzip_backup_archive(
|
)
|
||||||
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(
|
||||||
svr_obj.server_name,
|
svr_obj.server_name,
|
||||||
temp_dir,
|
temp_dir,
|
||||||
server_data["executable"],
|
server_data["executable"],
|
||||||
"1",
|
"1",
|
||||||
"2",
|
"2",
|
||||||
server_data["server_port"],
|
server_data["server_port"],
|
||||||
server_data["created_by"],
|
server_data["created_by"],
|
||||||
)
|
|
||||||
elif server_data["type"] == "minecraft-bedrock":
|
|
||||||
new_server = self.controller.restore_bedrock_zip_server(
|
|
||||||
svr_obj.server_name,
|
|
||||||
temp_dir,
|
|
||||||
server_data["executable"],
|
|
||||||
server_data["server_port"],
|
|
||||||
server_data["created_by"],
|
|
||||||
)
|
|
||||||
new_server_id = new_server
|
|
||||||
new_server = self.controller.servers.get_server_data(new_server)
|
|
||||||
self.controller.rename_backup_dir(
|
|
||||||
server_id,
|
|
||||||
new_server_id,
|
|
||||||
new_server["server_id"],
|
|
||||||
)
|
)
|
||||||
# preserve current schedules
|
elif server_data["type"] == "minecraft-bedrock":
|
||||||
for schedule in self.controller.management.get_schedules_by_server(
|
new_server = self.controller.restore_bedrock_zip_server(
|
||||||
server_id
|
svr_obj.server_name,
|
||||||
):
|
temp_dir,
|
||||||
job_data = self.controller.management.get_scheduled_task(
|
server_data["executable"],
|
||||||
schedule.schedule_id
|
server_data["server_port"],
|
||||||
)
|
server_data["created_by"],
|
||||||
job_data["server_id"] = new_server_id
|
|
||||||
del job_data["schedule_id"]
|
|
||||||
self.tasks_manager.update_job(schedule.schedule_id, job_data)
|
|
||||||
# preserve execution command
|
|
||||||
new_server_obj = self.controller.servers.get_server_obj(new_server_id)
|
|
||||||
new_server_obj.execution_command = server_data["execution_command"]
|
|
||||||
# reset executable path
|
|
||||||
if svr_obj.path in svr_obj.executable:
|
|
||||||
new_server_obj.executable = str(svr_obj.executable).replace(
|
|
||||||
svr_obj.path, new_server_obj.path
|
|
||||||
)
|
|
||||||
# reset run command path
|
|
||||||
if svr_obj.path in svr_obj.execution_command:
|
|
||||||
new_server_obj.execution_command = str(
|
|
||||||
svr_obj.execution_command
|
|
||||||
).replace(svr_obj.path, new_server_obj.path)
|
|
||||||
# reset log path
|
|
||||||
if svr_obj.path in svr_obj.log_path:
|
|
||||||
new_server_obj.log_path = str(svr_obj.log_path).replace(
|
|
||||||
svr_obj.path, new_server_obj.path
|
|
||||||
)
|
|
||||||
self.controller.servers.update_server(new_server_obj)
|
|
||||||
|
|
||||||
# preserve backup config
|
|
||||||
server_backups = self.controller.management.get_backups_by_server(
|
|
||||||
server_id
|
|
||||||
)
|
)
|
||||||
for backup in server_backups:
|
new_server_id = new_server
|
||||||
del server_backups[backup]["backup_id"]
|
new_server = self.controller.servers.get_server_data(new_server)
|
||||||
server_backups[backup]["server_id"] = new_server_id
|
self.controller.rename_backup_dir(
|
||||||
if str(server_id) in (server_backups[backup]["backup_location"]):
|
server_id,
|
||||||
server_backups[backup]["backup_location"] = str(
|
new_server_id,
|
||||||
server_backups[backup]["backup_location"]
|
new_server["server_id"],
|
||||||
).replace(str(server_id), str(new_server_id))
|
|
||||||
self.controller.management.add_backup_config(server_backups[backup])
|
|
||||||
# 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}"}
|
|
||||||
)
|
)
|
||||||
|
# preserve current schedules
|
||||||
|
for schedule in self.controller.management.get_schedules_by_server(
|
||||||
|
server_id
|
||||||
|
):
|
||||||
|
job_data = self.controller.management.get_scheduled_task(
|
||||||
|
schedule.schedule_id
|
||||||
|
)
|
||||||
|
job_data["server_id"] = new_server_id
|
||||||
|
del job_data["schedule_id"]
|
||||||
|
self.tasks_manager.update_job(schedule.schedule_id, job_data)
|
||||||
|
# preserve execution command
|
||||||
|
new_server_obj = self.controller.servers.get_server_obj(new_server_id)
|
||||||
|
new_server_obj.execution_command = server_data["execution_command"]
|
||||||
|
# reset executable path
|
||||||
|
if svr_obj.path in svr_obj.executable:
|
||||||
|
new_server_obj.executable = str(svr_obj.executable).replace(
|
||||||
|
svr_obj.path, new_server_obj.path
|
||||||
|
)
|
||||||
|
# reset run command path
|
||||||
|
if svr_obj.path in svr_obj.execution_command:
|
||||||
|
new_server_obj.execution_command = str(
|
||||||
|
svr_obj.execution_command
|
||||||
|
).replace(svr_obj.path, new_server_obj.path)
|
||||||
|
# reset log path
|
||||||
|
if svr_obj.path in svr_obj.log_path:
|
||||||
|
new_server_obj.log_path = str(svr_obj.log_path).replace(
|
||||||
|
svr_obj.path, new_server_obj.path
|
||||||
|
)
|
||||||
|
self.controller.servers.update_server(new_server_obj)
|
||||||
|
|
||||||
|
# preserve backup config
|
||||||
|
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))
|
||||||
|
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)
|
||||||
|
|
||||||
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']}",
|
||||||
|
Loading…
Reference in New Issue
Block a user