From 155a86ab06b31688d758c22ceffc46ace7684eb7 Mon Sep 17 00:00:00 2001 From: Nick Leeman Date: Fri, 7 Oct 2022 23:23:57 +0200 Subject: [PATCH] fixed backup --- services/utils.py | 22 ++++++++++++++++------ views/backup_menu.py | 20 +++++--------------- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/services/utils.py b/services/utils.py index 84d8dd5..86f2760 100644 --- a/services/utils.py +++ b/services/utils.py @@ -1,5 +1,6 @@ import os +from zipfile import ZipFile from models.database import Database @@ -9,13 +10,22 @@ class Utils: os.system('cls' if os.name == 'nt' else 'clear') @staticmethod - def export_db(file): - with open(f"./{file}", 'w') as f: + def export_db(): + with open(f"./backup.sql", 'w') as f: for line in Database.connection.iterdump(): f.write('%s\n' % line) + + with ZipFile("backup.zip", "w") as zip: + zip.write("backup.sql") + + os.remove("./backup.sql") @staticmethod - def import_db(file): - with open(f"./{file}", 'r') as f: - str = f.read() - Database.connection.executescript(str) \ No newline at end of file + def import_db(): + with ZipFile("backup.zip", 'r') as zip: + zip.extractall("./") + with open(f"./backup.sql", 'r') as f: + str = f.read() + Database.connection.executescript(str) + + os.remove("./backup.sql") \ No newline at end of file diff --git a/views/backup_menu.py b/views/backup_menu.py index 19d4ae1..3270762 100644 --- a/views/backup_menu.py +++ b/views/backup_menu.py @@ -10,10 +10,8 @@ from ui.selection_menu import SelectionMenu class BackupMenu: @staticmethod def create_backup(): - input_menu = InputMenu("Export Backup File").add_option("FILE", "Filename", "STR", "backup.sql", 1, 50, None).do_input() - backup_name = input_menu.get_value("FILE") - print(f"Exporting database to file: {backup_name}") - Utils.export_db(backup_name) + print(f"Exporting database to file: backup.zip") + Utils.export_db() input(f"Exported database. Press any key to return.") @staticmethod @@ -26,17 +24,9 @@ class BackupMenu: if option == False: return - input_menu = InputMenu("Import Backup File").add_option("FILE", "Filename", "STR", "backup.sql", 1, 50, None).do_input() - backup_name = input_menu.get_value("FILE") - - if not path.exists(f"./{backup_name}"): - input(f"Backup file not found. Press any key to return.") - return - print("Cleared current database.") Database.delete_tables() - print(f"Importing database from file: {backup_name}") - Utils.import_db(backup_name) - input(f"Imported database. Press any key to return.") - \ No newline at end of file + print(f"Importing database from file: backup.zip") + Utils.import_db() + input(f"Imported database. Press any key to return.") \ No newline at end of file