mirror of
https://gitlab.com/crafty-controller/crafty-4.git
synced 2025-01-18 17:15:13 +01:00
Restore backup/clone
This commit is contained in:
parent
a46129f20c
commit
217435283b
@ -146,8 +146,7 @@ class ServersController(metaclass=Singleton):
|
|||||||
PermissionsServers.delete_roles_permissions(role_id, role_data["servers"])
|
PermissionsServers.delete_roles_permissions(role_id, role_data["servers"])
|
||||||
# Remove roles from server
|
# Remove roles from server
|
||||||
PermissionsServers.remove_roles_of_server(server_id)
|
PermissionsServers.remove_roles_of_server(server_id)
|
||||||
# Remove backup configs tied to server
|
self.management_helper.remove_all_server_backups(server_id)
|
||||||
self.management_helper.remove_backup_config(server_id)
|
|
||||||
# Finally remove server
|
# Finally remove server
|
||||||
self.servers_helper.remove_server(server_id)
|
self.servers_helper.remove_server(server_id)
|
||||||
|
|
||||||
|
@ -104,11 +104,11 @@ class Schedules(BaseModel):
|
|||||||
# Backups Class
|
# Backups Class
|
||||||
# **********************************************************************************
|
# **********************************************************************************
|
||||||
class Backups(BaseModel):
|
class Backups(BaseModel):
|
||||||
backup_id = CharField(primary_key=True, default=Helpers.create_uuid())
|
backup_id = CharField(primary_key=True, default=Helpers.create_uuid)
|
||||||
backup_name = CharField(default="New Backup")
|
backup_name = CharField(default="New Backup")
|
||||||
backup_location = CharField(default="")
|
backup_location = CharField(default="")
|
||||||
excluded_dirs = CharField(null=True)
|
excluded_dirs = CharField(null=True)
|
||||||
max_backups = IntegerField()
|
max_backups = IntegerField(default=0)
|
||||||
server_id = ForeignKeyField(Servers, backref="backups_server")
|
server_id = ForeignKeyField(Servers, backref="backups_server")
|
||||||
compress = BooleanField(default=False)
|
compress = BooleanField(default=False)
|
||||||
shutdown = BooleanField(default=False)
|
shutdown = BooleanField(default=False)
|
||||||
@ -385,6 +385,10 @@ class HelpersManagement:
|
|||||||
return model_to_dict(backup_model)
|
return model_to_dict(backup_model)
|
||||||
raise IndexError
|
raise IndexError
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def remove_all_server_backups(server_id):
|
||||||
|
Backups.delete().where(Backups.server_id == server_id).execute()
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def remove_backup_config(backup_id):
|
def remove_backup_config(backup_id):
|
||||||
Backups.delete().where(Backups.backup_id == backup_id).execute()
|
Backups.delete().where(Backups.backup_id == backup_id).execute()
|
||||||
|
@ -874,16 +874,13 @@ class Controller:
|
|||||||
# **********************************************************************************
|
# **********************************************************************************
|
||||||
|
|
||||||
def rename_backup_dir(self, old_server_id, new_server_id, new_uuid):
|
def rename_backup_dir(self, old_server_id, new_server_id, new_uuid):
|
||||||
server_data = self.servers.get_server_data_by_id(old_server_id)
|
|
||||||
server_obj = self.servers.get_server_obj(new_server_id)
|
server_obj = self.servers.get_server_obj(new_server_id)
|
||||||
old_bu_path = server_data["backup_path"]
|
|
||||||
ServerPermsController.backup_role_swap(old_server_id, new_server_id)
|
ServerPermsController.backup_role_swap(old_server_id, new_server_id)
|
||||||
backup_path = old_bu_path
|
backup_path = os.path.join(self.helper.backup_path, old_server_id)
|
||||||
backup_path = Path(backup_path)
|
backup_path = Path(backup_path)
|
||||||
backup_path_components = list(backup_path.parts)
|
backup_path_components = list(backup_path.parts)
|
||||||
backup_path_components[-1] = new_uuid
|
backup_path_components[-1] = new_uuid
|
||||||
new_bu_path = pathlib.PurePath(os.path.join(*backup_path_components))
|
new_bu_path = pathlib.PurePath(os.path.join(*backup_path_components))
|
||||||
server_obj.backup_path = new_bu_path
|
|
||||||
default_backup_dir = os.path.join(self.helper.backup_path, new_uuid)
|
default_backup_dir = os.path.join(self.helper.backup_path, new_uuid)
|
||||||
try:
|
try:
|
||||||
os.rmdir(default_backup_dir)
|
os.rmdir(default_backup_dir)
|
||||||
@ -975,16 +972,16 @@ class Controller:
|
|||||||
f"Unable to delete server files for server with ID: "
|
f"Unable to delete server files for server with ID: "
|
||||||
f"{server_id} with error logged: {e}"
|
f"{server_id} with error logged: {e}"
|
||||||
)
|
)
|
||||||
if Helpers.check_path_exists(
|
backup_configs = HelpersManagement.get_backups_by_server(
|
||||||
self.servers.get_server_data_by_id(server_id)["backup_path"]
|
server_id, True
|
||||||
):
|
)
|
||||||
FileHelpers.del_dirs(
|
for config in backup_configs:
|
||||||
Helpers.get_os_understandable_path(
|
if Helpers.check_path_exists(config.backup_location):
|
||||||
self.servers.get_server_data_by_id(server_id)[
|
FileHelpers.del_dirs(
|
||||||
"backup_path"
|
Helpers.get_os_understandable_path(
|
||||||
]
|
config.backup_location
|
||||||
|
)
|
||||||
)
|
)
|
||||||
)
|
|
||||||
|
|
||||||
# Cleanup scheduled tasks
|
# Cleanup scheduled tasks
|
||||||
try:
|
try:
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
|
import json
|
||||||
from app.classes.models.server_permissions import EnumPermissionsServer
|
from app.classes.models.server_permissions import EnumPermissionsServer
|
||||||
from app.classes.models.servers import Servers
|
from app.classes.models.servers import Servers
|
||||||
from app.classes.shared.file_helpers import FileHelpers
|
from app.classes.shared.file_helpers import FileHelpers
|
||||||
@ -82,6 +83,20 @@ class ApiServersServerActionHandler(BaseApiHandler):
|
|||||||
new_server_id = self.helper.create_uuid()
|
new_server_id = self.helper.create_uuid()
|
||||||
new_server_path = os.path.join(self.helper.servers_dir, new_server_id)
|
new_server_path = os.path.join(self.helper.servers_dir, new_server_id)
|
||||||
new_backup_path = os.path.join(self.helper.backup_path, new_server_id)
|
new_backup_path = os.path.join(self.helper.backup_path, new_server_id)
|
||||||
|
backup_data = {
|
||||||
|
"backup_name": f"{new_server_name} Backup",
|
||||||
|
"backup_location": new_backup_path,
|
||||||
|
"excluded_dirs": "",
|
||||||
|
"max_backups": 0,
|
||||||
|
"server_id": new_server_id,
|
||||||
|
"compress": False,
|
||||||
|
"shutdown": False,
|
||||||
|
"before": "",
|
||||||
|
"after": "",
|
||||||
|
"default": True,
|
||||||
|
"status": json.dumps({"status": "Standby", "message": ""}),
|
||||||
|
"enabled": True,
|
||||||
|
}
|
||||||
new_server_command = str(server_data.get("execution_command")).replace(
|
new_server_command = str(server_data.get("execution_command")).replace(
|
||||||
server_id, new_server_id
|
server_id, new_server_id
|
||||||
)
|
)
|
||||||
@ -102,6 +117,8 @@ class ApiServersServerActionHandler(BaseApiHandler):
|
|||||||
server_data.get("type"),
|
server_data.get("type"),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
self.controller.management.add_backup_config(backup_data)
|
||||||
|
|
||||||
self.controller.management.add_to_audit_log(
|
self.controller.management.add_to_audit_log(
|
||||||
user_id,
|
user_id,
|
||||||
f"is cloning server {server_id} named {server_data.get('server_name')}",
|
f"is cloning server {server_id} named {server_data.get('server_name')}",
|
||||||
|
@ -159,7 +159,7 @@ class ApiServersServerBackupsBackupIndexHandler(BaseApiHandler):
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
backup_config = self.controller.management.get_backup_config(backup_id)
|
backup_config = self.controller.management.get_backup_config(backup_id)
|
||||||
if backup_config["server_id"] != server_id:
|
if backup_config["server_id"]["server_id"] != server_id:
|
||||||
return self.finish_json(
|
return self.finish_json(
|
||||||
400,
|
400,
|
||||||
{
|
{
|
||||||
@ -192,9 +192,11 @@ class ApiServersServerBackupsBackupIndexHandler(BaseApiHandler):
|
|||||||
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_path = svr_obj.backup_path
|
backup_config = self.controller.management.get_backup_config(backup_id)
|
||||||
if Helpers.validate_traversal(backup_path, zip_name):
|
if Helpers.validate_traversal(backup_config["backup_location"], zip_name):
|
||||||
temp_dir = Helpers.unzip_backup_archive(backup_path, zip_name)
|
temp_dir = Helpers.unzip_backup_archive(
|
||||||
|
backup_config["backup_location"], zip_name
|
||||||
|
)
|
||||||
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,
|
||||||
@ -216,7 +218,9 @@ class ApiServersServerBackupsBackupIndexHandler(BaseApiHandler):
|
|||||||
new_server_id = new_server
|
new_server_id = new_server
|
||||||
new_server = self.controller.servers.get_server_data(new_server)
|
new_server = self.controller.servers.get_server_data(new_server)
|
||||||
self.controller.rename_backup_dir(
|
self.controller.rename_backup_dir(
|
||||||
server_id, new_server_id, new_server["server_id"]
|
server_id,
|
||||||
|
new_server_id,
|
||||||
|
new_server["server_id"],
|
||||||
)
|
)
|
||||||
# preserve current schedules
|
# preserve current schedules
|
||||||
for schedule in self.controller.management.get_schedules_by_server(
|
for schedule in self.controller.management.get_schedules_by_server(
|
||||||
@ -249,20 +253,17 @@ 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
|
||||||
excluded_dirs = []
|
server_backups = self.controller.management.get_backups_by_server(
|
||||||
server_obj = self.controller.servers.get_server_obj(server_id)
|
|
||||||
loop_backup_path = self.helper.wtol_path(server_obj.path)
|
|
||||||
for item in self.controller.management.get_excluded_backup_dirs(
|
|
||||||
server_id
|
server_id
|
||||||
):
|
|
||||||
item_path = self.helper.wtol_path(item)
|
|
||||||
bu_path = os.path.relpath(item_path, loop_backup_path)
|
|
||||||
bu_path = os.path.join(new_server_obj.path, bu_path)
|
|
||||||
excluded_dirs.append(bu_path)
|
|
||||||
self.controller.management.add_backup_config(
|
|
||||||
new_server_id,
|
|
||||||
new_server_obj.backup_path,
|
|
||||||
)
|
)
|
||||||
|
for backup in server_backups:
|
||||||
|
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])
|
||||||
# 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)
|
||||||
|
@ -43,7 +43,7 @@
|
|||||||
<div class="col-md-6 col-sm-12">
|
<div class="col-md-6 col-sm-12">
|
||||||
<br>
|
<br>
|
||||||
<br>
|
<br>
|
||||||
<div id="{{data['backup_config']['backup_id']}}_status" class="progress"
|
<div id="{{data['backup_config'].get('backup_id', None)}}_status" class="progress"
|
||||||
style="height: 15px; display: none;">
|
style="height: 15px; display: none;">
|
||||||
</div>
|
</div>
|
||||||
{% if data['backing_up'] %}
|
{% if data['backing_up'] %}
|
||||||
@ -61,7 +61,7 @@
|
|||||||
<form id="backup-form" class="forms-sample">
|
<form id="backup-form" class="forms-sample">
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label for="backup_name">{{ translate('serverBackups', 'name', data['lang']) }}
|
<label for="backup_name">{{ translate('serverBackups', 'name', data['lang']) }}
|
||||||
{% if data["backup_config"]["default"] %}
|
{% if data["backup_config"].get("default", None) %}
|
||||||
<span class="badge-pill badge-outline-warning">{{ translate('serverBackups', 'default',
|
<span class="badge-pill badge-outline-warning">{{ translate('serverBackups', 'default',
|
||||||
data['lang']) }}</span><small><button class="badge-pill badge-outline-info backup-explain"
|
data['lang']) }}</span><small><button class="badge-pill badge-outline-info backup-explain"
|
||||||
data-explain="{{ translate('serverBackups', 'defaultExplain', data['lang'])}}"><i
|
data-explain="{{ translate('serverBackups', 'defaultExplain', data['lang'])}}"><i
|
||||||
|
Loading…
x
Reference in New Issue
Block a user