mirror of
https://gitlab.com/crafty-controller/crafty-4.git
synced 2024-08-30 18:23:09 +00:00
Add function for removing old malformed backups
This commit is contained in:
parent
ff9abe4359
commit
4a58759183
@ -13,6 +13,13 @@ from app.classes.shared.file_helpers import FileHelpers
|
|||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
def is_valid_backup(backup, all_servers):
|
||||||
|
try:
|
||||||
|
return str(backup.server_id) in all_servers
|
||||||
|
except (TypeError, peewee.DoesNotExist):
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
def migrate(migrator: Migrator, database, **kwargs):
|
def migrate(migrator: Migrator, database, **kwargs):
|
||||||
"""
|
"""
|
||||||
Write your migrations here.
|
Write your migrations here.
|
||||||
@ -150,9 +157,17 @@ def migrate(migrator: Migrator, database, **kwargs):
|
|||||||
migrator.create_table(NewSchedules)
|
migrator.create_table(NewSchedules)
|
||||||
|
|
||||||
migrator.run()
|
migrator.run()
|
||||||
|
all_servers = [
|
||||||
|
row.server_id for row in Servers.select(Servers.server_id).distinct()
|
||||||
|
]
|
||||||
|
all_backups = Backups.select()
|
||||||
|
Console.info("Cleaning up orphan backups for all servers")
|
||||||
|
valid_backups = [
|
||||||
|
backup for backup in all_backups if is_valid_backup(backup, all_servers)
|
||||||
|
]
|
||||||
# Copy data from the existing backups table to the new one
|
# Copy data from the existing backups table to the new one
|
||||||
for backup in Backups.select():
|
for backup in valid_backups:
|
||||||
|
Console.info(f"Trying to get server for backup migration {backup.server_id}")
|
||||||
# Fetch the related server entry from the Servers table
|
# Fetch the related server entry from the Servers table
|
||||||
server = Servers.get(Servers.server_id == backup.server_id)
|
server = Servers.get(Servers.server_id == backup.server_id)
|
||||||
Console.info(f"Migrations: Migrating backup for server {server.server_name}")
|
Console.info(f"Migrations: Migrating backup for server {server.server_name}")
|
||||||
@ -172,13 +187,28 @@ def migrate(migrator: Migrator, database, **kwargs):
|
|||||||
default=True,
|
default=True,
|
||||||
enabled=True,
|
enabled=True,
|
||||||
)
|
)
|
||||||
|
Console.info(
|
||||||
|
f"New backup table created for {server.server_name} with id {new_backup.backup_id}"
|
||||||
|
)
|
||||||
Helpers.ensure_dir_exists(
|
Helpers.ensure_dir_exists(
|
||||||
os.path.join(server.backup_path, new_backup.backup_id)
|
os.path.join(server.backup_path, new_backup.backup_id)
|
||||||
)
|
)
|
||||||
FileHelpers.move_dir(
|
try:
|
||||||
os.path.join(server.backup_path),
|
Console.info(
|
||||||
os.path.join(server.backup_path, new_backup.backup_id),
|
f"Moving old backups to new backup dir for {server.server_name}"
|
||||||
)
|
)
|
||||||
|
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),
|
||||||
|
)
|
||||||
|
except FileNotFoundError as why:
|
||||||
|
logger.error(
|
||||||
|
f"Could not move backup {file} for {server.server_name} to new location with error {why}"
|
||||||
|
)
|
||||||
|
|
||||||
Console.debug("Migrations: Dropping old backup table")
|
Console.debug("Migrations: Dropping old backup table")
|
||||||
# Drop the existing backups table
|
# Drop the existing backups table
|
||||||
|
Loading…
Reference in New Issue
Block a user