Refactor backup config methods to add/update

This commit is contained in:
amcmanu3 2024-04-20 17:30:37 -04:00
parent 5fe696e269
commit b898595371
5 changed files with 47 additions and 58 deletions

View File

@ -185,18 +185,22 @@ class ManagementController:
def get_backups_by_server(server_id, model=False):
return HelpersManagement.get_backups_by_server(server_id, model)
def set_backup_config(
@staticmethod
def update_backup_config(backup_id, updates):
return HelpersManagement.update_backup_config(backup_id, updates)
def add_backup_config(
self,
server_id: int,
backup_path: str = None,
max_backups: int = None,
backup_path: str = "",
max_backups: int = 0,
excluded_dirs: list = None,
compress: bool = False,
shutdown: bool = False,
before: str = "",
after: str = "",
):
return self.management_helper.set_backup_config(
return self.management_helper.add_backup_config(
server_id,
backup_path,
max_backups,

View File

@ -385,15 +385,14 @@ class HelpersManagement:
# Backups Methods
# **********************************************************************************
@staticmethod
def get_backup_config(server_id):
def get_backup_config(backup_id):
try:
row = (
Backups.select().where(Backups.server_id == server_id).join(Servers)[0]
)
row = Backups.select().where(Backups.backup_id == backup_id)
conf = {
"backup_path": row.backup_location,
"excluded_dirs": row.excluded_dirs,
"max_backups": row.max_backups,
"backup_location": row.backup_location,
"server_id": row.server_id_id,
"compress": row.compress,
"shutdown": row.shutdown,
@ -401,16 +400,7 @@ class HelpersManagement:
"after": row.after,
}
except IndexError:
conf = {
"backup_path": None,
"excluded_dirs": None,
"max_backups": 0,
"server_id": server_id,
"compress": False,
"shutdown": False,
"before": "",
"after": "",
}
return None
return conf
@staticmethod
@ -440,7 +430,31 @@ class HelpersManagement:
def remove_backup_config(server_id):
Backups.delete().where(Backups.server_id == server_id).execute()
def set_backup_config(
def add_backup_config(
self,
server_id: str,
backup_path: str = "",
max_backups: int = 0,
excluded_dirs: list = None,
compress: bool = False,
shutdown: bool = False,
before: str = "",
after: str = "",
):
conf = {
"excluded_dirs": excluded_dirs,
"max_backups": max_backups,
"server_id": server_id,
"backup_location": backup_path,
"compress": compress,
"shutdown": shutdown,
"before": before,
"after": after,
}
Backups.create(**conf)
logger.debug("Creating new backup record.")
def update_backup_config(
self,
server_id: int,
backup_path: str = None,
@ -503,8 +517,8 @@ class HelpersManagement:
logger.debug("Creating new backup record.")
@staticmethod
def get_excluded_backup_dirs(server_id: int):
excluded_dirs = HelpersManagement.get_backup_config(server_id)["excluded_dirs"]
def get_excluded_backup_dirs(backup_id: int):
excluded_dirs = HelpersManagement.get_backup_config(backup_id)["excluded_dirs"]
if excluded_dirs is not None and excluded_dirs != "":
dir_list = excluded_dirs.split(",")
else:
@ -516,7 +530,7 @@ class HelpersManagement:
if dir_to_add not in dir_list:
dir_list.append(dir_to_add)
excluded_dirs = ",".join(dir_list)
self.set_backup_config(server_id=server_id, excluded_dirs=excluded_dirs)
self.update_backup_config(server_id=server_id, excluded_dirs=excluded_dirs)
else:
logger.debug(
f"Not adding {dir_to_add} to excluded directories - "
@ -528,7 +542,7 @@ class HelpersManagement:
if dir_to_del in dir_list:
dir_list.remove(dir_to_del)
excluded_dirs = ",".join(dir_list)
self.set_backup_config(server_id=server_id, excluded_dirs=excluded_dirs)
self.update_backup_config(server_id=server_id, excluded_dirs=excluded_dirs)
else:
logger.debug(
f"Not removing {dir_to_del} from excluded directories - "

View File

@ -552,7 +552,6 @@ class Controller:
name=data["name"],
server_uuid=server_fs_uuid,
server_dir=new_server_path,
backup_path=backup_path,
server_command=server_command,
server_file=server_file,
server_log_file=log_location,
@ -562,7 +561,7 @@ class Controller:
server_host=monitoring_host,
server_type=monitoring_type,
)
self.management.set_backup_config(
self.management.add_backup_config(
new_server_id,
backup_path,
)
@ -905,7 +904,6 @@ class Controller:
name: str,
server_uuid: str,
server_dir: str,
backup_path: str,
server_command: str,
server_file: str,
server_log_file: str,
@ -920,7 +918,6 @@ class Controller:
name,
server_uuid,
server_dir,
backup_path,
server_command,
server_file,
server_log_file,

View File

@ -189,7 +189,7 @@ class ApiServersServerBackupsBackupIndexHandler(BaseApiHandler):
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.set_backup_config(
self.controller.management.add_backup_config(
new_server_id,
new_server_obj.backup_path,
backup_config["max_backups"],

View File

@ -52,7 +52,7 @@ class ApiServersServerBackupsIndexHandler(BaseApiHandler):
return self.finish_json(400, {"status": "error", "error": "NOT_AUTHORIZED"})
self.finish_json(200, self.controller.management.get_backup_config(server_id))
def patch(self, server_id: str):
def patch(self, backup_id: str):
auth_data = self.authenticate_user()
if not auth_data:
return
@ -78,7 +78,8 @@ class ApiServersServerBackupsIndexHandler(BaseApiHandler):
"error_data": str(e),
},
)
backup_conf = self.controller.management.get_backup_config(backup_id)
server_id = backup_conf["server_id"]
if server_id not in [str(x["server_id"]) for x in auth_data[0]]:
# if the user doesn't have access to the server, return an error
return self.finish_json(400, {"status": "error", "error": "NOT_AUTHORIZED"})
@ -92,32 +93,5 @@ class ApiServersServerBackupsIndexHandler(BaseApiHandler):
# if the user doesn't have Schedule permission, return an error
return self.finish_json(400, {"status": "error", "error": "NOT_AUTHORIZED"})
self.controller.management.set_backup_config(
server_id,
data.get(
"backup_path",
self.controller.management.get_backup_config(server_id)["backup_path"],
),
data.get(
"max_backups",
self.controller.management.get_backup_config(server_id)["max_backups"],
),
data.get("exclusions"),
data.get(
"compress",
self.controller.management.get_backup_config(server_id)["compress"],
),
data.get(
"shutdown",
self.controller.management.get_backup_config(server_id)["shutdown"],
),
data.get(
"backup_before",
self.controller.management.get_backup_config(server_id)["before"],
),
data.get(
"backup_after",
self.controller.management.get_backup_config(server_id)["after"],
),
)
self.controller.management.update_backup_config(server_id, data)
return self.finish_json(200, {"status": "ok"})