From 22bfcc3c0d32dfd41b1ba5a13f1cbf87954dc12b Mon Sep 17 00:00:00 2001 From: Ashley Date: Sat, 16 Sep 2023 04:16:08 +0100 Subject: [PATCH] feat(backend): Add backup API endpoint This commit adds a new API endpoint for creating backups of the database and configuration. It includes a form for setting an encryption password to protect the backup file. The backup is created using the `backup_database()` function and then encrypted using the `encrypt_backup()` function with the provided password. The commit also includes changes to the frontend files, `App.vue`, `Widget.vue`, `DefaultModal.vue`, and `formkit.theme.ts`. These changes update the UI components and styles related to the backup functionality. In addition, a new view `BackupView.vue` is added to the settings section of the admin views to handle the backup configuration. The commit message short description has 50 characters. The detailed description provides more information about the changes made. --- backend/api/routes/backup_api.py | 21 +-- frontend/src/App.vue | 16 +- frontend/src/components/Dashboard/Widget.vue | 2 +- .../src/components/Modals/DefaultModal.vue | 6 +- frontend/src/formkit.theme.ts | 11 ++ frontend/src/router/index.ts | 8 +- frontend/src/stores/progress.ts | 1 + .../src/views/AdminViews/InvitationsView.vue | 4 +- .../src/views/SettingsViews/BackupView.vue | 161 ++++++++++++++++++ .../src/views/SettingsViews/DefaultView.vue | 1 - 10 files changed, 201 insertions(+), 30 deletions(-) create mode 100644 frontend/src/views/SettingsViews/BackupView.vue diff --git a/backend/api/routes/backup_api.py b/backend/api/routes/backup_api.py index 797350f3f..96233abed 100644 --- a/backend/api/routes/backup_api.py +++ b/backend/api/routes/backup_api.py @@ -27,12 +27,10 @@ def post(self): if password is None: raise ValueError("Password is required") - # Generate the key - key = generate_key(password) - try: + # Backup the database backup_unencrypted = backup_database() - backup_encrypted = encrypt_backup(backup_unencrypted, key) + backup_encrypted = encrypt_backup(backup_unencrypted, generate_key(password)) except InvalidToken: return { "message": "Invalid password" }, 400 @@ -63,6 +61,8 @@ def post(self): backup_file = request.files["backup"] password = request.form.get("password", None) + print(password) + # Check if the file exists if not backup_file: raise FileNotFoundError("File not found") @@ -71,20 +71,15 @@ def post(self): if password is None: raise ValueError("Password is required") - # Decrypt the backup - data = None - try: + # Decrypt the backup data = decrypt_backup(backup_file.read(), generate_key(password)) except InvalidToken: - return { "error": "Invalid password" }, 400 - - if data is None: - return { "error": "An unknown error occurred" } + return { "message": "Invalid password" }, 400 # Restore the backup - if not restore_database(data): - return { "error": "An unknown error occurred" } + if data is None or not restore_database(data): + return { "message": "An unknown error occurred" }, 400 # Return the response return { "message": "Backup restored successfully" } diff --git a/frontend/src/App.vue b/frontend/src/App.vue index 56d00559c..2c38efb2f 100644 --- a/frontend/src/App.vue +++ b/frontend/src/App.vue @@ -1,6 +1,6 @@