From 8dd82f4111b1baf6cfa0e1f8a97cb1abe0ca08d8 Mon Sep 17 00:00:00 2001 From: Andrew Date: Fri, 7 Jun 2024 14:08:05 -0400 Subject: [PATCH] Set backups to live in a directory with the backup id --- app/classes/shared/server.py | 14 ++++++++++---- app/classes/web/panel_handler.py | 2 +- app/migrations/20240308_multi-backup.py | 13 ++++++++++++- 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/app/classes/shared/server.py b/app/classes/shared/server.py index fc43143e..8ebe4450 100644 --- a/app/classes/shared/server.py +++ b/app/classes/shared/server.py @@ -1150,6 +1150,9 @@ class ServerInstance: ) time.sleep(3) conf = HelpersManagement.get_backup_config(backup_id) + conf["backup_location"] = os.path.join( + conf["backup_location"], conf["backup_id"] + ) backup_location = conf["backup_location"] if not backup_location: Console.critical("No backup path found. Canceling") @@ -1201,10 +1204,10 @@ class ServerInstance: ) while ( - len(self.list_backups(backup_location)) > conf["max_backups"] + len(self.list_backups(conf)) > conf["max_backups"] and conf["max_backups"] > 0 ): - backup_list = self.list_backups(conf["backup_location"]) + backup_list = self.list_backups(conf) oldfile = backup_list[0] oldfile_path = f"{backup_location}/{oldfile['path']}" logger.info(f"Removing old backup '{oldfile['path']}'") @@ -1292,12 +1295,15 @@ class ServerInstance: alert = True self.last_backup_failed = alert - def list_backups(self, backup_location): - if not backup_location: + def list_backups(self, backup_config: dict) -> list: + if not backup_config: logger.info( f"Error putting backup file list for server with ID: {self.server_id}" ) return [] + backup_location = os.path.join( + backup_config["backup_location"], backup_config["backup_id"] + ) if not Helpers.check_path_exists( Helpers.get_os_understandable_path(backup_location) ): diff --git a/app/classes/web/panel_handler.py b/app/classes/web/panel_handler.py index b34f8462..26274a12 100644 --- a/app/classes/web/panel_handler.py +++ b/app/classes/web/panel_handler.py @@ -1289,7 +1289,7 @@ class PanelHandler(BaseHandler): self.controller.servers.refresh_server_settings(server_id) try: page_data["backup_list"] = server.list_backups( - page_data["backup_config"]["backup_location"] + page_data["backup_config"] ) except: page_data["backup_list"] = [] diff --git a/app/migrations/20240308_multi-backup.py b/app/migrations/20240308_multi-backup.py index 78f891d1..5b139c28 100644 --- a/app/migrations/20240308_multi-backup.py +++ b/app/migrations/20240308_multi-backup.py @@ -1,3 +1,4 @@ +import os import datetime import uuid import peewee @@ -8,6 +9,7 @@ from app.classes.models.management import Backups, Schedules from app.classes.shared.helpers import Helpers from app.classes.shared.console import Console from app.classes.shared.migration import Migrator, MigrateHistory +from app.classes.shared.file_helpers import FileHelpers logger = logging.getLogger(__name__) @@ -116,7 +118,7 @@ def migrate(migrator: Migrator, database, **kwargs): Console.info(f"Migrations: Migrating backup for server {server.server_name}") # Create a new backup entry with data from the # old backup entry and related server - NewBackups.create( + new_backup = NewBackups.create( backup_name=f"{server.server_name} Backup", # Set backup_location equal to backup_path backup_location=server.backup_path, @@ -130,6 +132,15 @@ def migrate(migrator: Migrator, database, **kwargs): default=True, enabled=True, ) + Helpers.ensure_dir_exists( + os.path.join(server.backup_path, new_backup.backup_id) + ) + for file in os.listdir(server.backup_path): + if not os.path.isdir(os.path.join(os.path.join(server.backup_path, file))): + FileHelpers.move_file( + os.path.join(server.backup_path, file), + os.path.join(server.backup_path, new_backup.backup_id, file), + ) Console.debug("Migrations: Dropping old backup table") # Drop the existing backups table