Merge branch 'refactor/backups' into refactor/upload-api

This commit is contained in:
Andrew 2024-06-07 14:08:23 -04:00
commit 44ab4b8b75
3 changed files with 23 additions and 6 deletions

View File

@ -1150,6 +1150,9 @@ class ServerInstance:
) )
time.sleep(3) time.sleep(3)
conf = HelpersManagement.get_backup_config(backup_id) conf = HelpersManagement.get_backup_config(backup_id)
conf["backup_location"] = os.path.join(
conf["backup_location"], conf["backup_id"]
)
backup_location = conf["backup_location"] backup_location = conf["backup_location"]
if not backup_location: if not backup_location:
Console.critical("No backup path found. Canceling") Console.critical("No backup path found. Canceling")
@ -1201,10 +1204,10 @@ class ServerInstance:
) )
while ( while (
len(self.list_backups(backup_location)) > conf["max_backups"] len(self.list_backups(conf)) > conf["max_backups"]
and conf["max_backups"] > 0 and conf["max_backups"] > 0
): ):
backup_list = self.list_backups(conf["backup_location"]) backup_list = self.list_backups(conf)
oldfile = backup_list[0] oldfile = backup_list[0]
oldfile_path = f"{backup_location}/{oldfile['path']}" oldfile_path = f"{backup_location}/{oldfile['path']}"
logger.info(f"Removing old backup '{oldfile['path']}'") logger.info(f"Removing old backup '{oldfile['path']}'")
@ -1292,12 +1295,15 @@ class ServerInstance:
alert = True alert = True
self.last_backup_failed = alert self.last_backup_failed = alert
def list_backups(self, backup_location): def list_backups(self, backup_config: dict) -> list:
if not backup_location: if not backup_config:
logger.info( logger.info(
f"Error putting backup file list for server with ID: {self.server_id}" f"Error putting backup file list for server with ID: {self.server_id}"
) )
return [] return []
backup_location = os.path.join(
backup_config["backup_location"], backup_config["backup_id"]
)
if not Helpers.check_path_exists( if not Helpers.check_path_exists(
Helpers.get_os_understandable_path(backup_location) Helpers.get_os_understandable_path(backup_location)
): ):

View File

@ -1289,7 +1289,7 @@ class PanelHandler(BaseHandler):
self.controller.servers.refresh_server_settings(server_id) self.controller.servers.refresh_server_settings(server_id)
try: try:
page_data["backup_list"] = server.list_backups( page_data["backup_list"] = server.list_backups(
page_data["backup_config"]["backup_location"] page_data["backup_config"]
) )
except: except:
page_data["backup_list"] = [] page_data["backup_list"] = []

View File

@ -1,3 +1,4 @@
import os
import datetime import datetime
import uuid import uuid
import peewee 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.helpers import Helpers
from app.classes.shared.console import Console from app.classes.shared.console import Console
from app.classes.shared.migration import Migrator, MigrateHistory from app.classes.shared.migration import Migrator, MigrateHistory
from app.classes.shared.file_helpers import FileHelpers
logger = logging.getLogger(__name__) 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}") Console.info(f"Migrations: Migrating backup for server {server.server_name}")
# Create a new backup entry with data from the # Create a new backup entry with data from the
# old backup entry and related server # old backup entry and related server
NewBackups.create( new_backup = NewBackups.create(
backup_name=f"{server.server_name} Backup", backup_name=f"{server.server_name} Backup",
# Set backup_location equal to backup_path # Set backup_location equal to backup_path
backup_location=server.backup_path, backup_location=server.backup_path,
@ -130,6 +132,15 @@ def migrate(migrator: Migrator, database, **kwargs):
default=True, default=True,
enabled=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") Console.debug("Migrations: Dropping old backup table")
# Drop the existing backups table # Drop the existing backups table