From f7c73f7ebade8b0a8014460f80de197326c2a45a Mon Sep 17 00:00:00 2001 From: simon Date: Tue, 14 Dec 2021 19:05:58 +0700 Subject: [PATCH] change backup and restore to tag files --- tubearchivist/home/src/frontend.py | 6 +-- tubearchivist/home/src/index_management.py | 44 ++++++++++++++++++---- tubearchivist/home/tasks.py | 4 +- tubearchivist/home/views.py | 3 ++ 4 files changed, 45 insertions(+), 12 deletions(-) diff --git a/tubearchivist/home/src/frontend.py b/tubearchivist/home/src/frontend.py index 1c8b0c2..fc6c5ae 100644 --- a/tubearchivist/home/src/frontend.py +++ b/tubearchivist/home/src/frontend.py @@ -268,11 +268,11 @@ class PostData: run_backup.delay("manual") return {"success": True} - @staticmethod - def _db_restore(): + def _db_restore(self): """restore es zip from settings page""" print("restoring index from backup zip") - run_restore_backup.delay() + filename = self.exec_val + run_restore_backup.delay(filename) return {"success": True} @staticmethod diff --git a/tubearchivist/home/src/index_management.py b/tubearchivist/home/src/index_management.py index e15b064..ad03eb1 100644 --- a/tubearchivist/home/src/index_management.py +++ b/tubearchivist/home/src/index_management.py @@ -492,8 +492,8 @@ class ElasticBackup: if not request.ok: print(request.text) - def unpack_zip_backup(self): - """extract backup zip and return filelist""" + def get_all_backup_files(self): + """build all available backup files for view""" cache_dir = self.config["application"]["cache_dir"] backup_dir = os.path.join(cache_dir, "backup") backup_files = os.listdir(backup_dir) @@ -503,9 +503,32 @@ class ElasticBackup: for i in all_backup_files if i.startswith("ta_") and i.endswith(".zip") ] - all_available_backups.sort() - newest_backup = all_available_backups[-1] - file_path = os.path.join(backup_dir, newest_backup) + all_available_backups.sort(reverse=True) + + backup_dicts = [] + for backup_file in all_available_backups: + file_split = backup_file.split("-") + if len(file_split) == 2: + timestamp = file_split[1].strip(".zip") + reason = False + elif len(file_split) == 3: + timestamp = file_split[1] + reason = file_split[2].strip(".zip") + + to_add = { + "filename": backup_file, + "timestamp": timestamp, + "reason": reason, + } + backup_dicts.append(to_add) + + return backup_dicts + + def unpack_zip_backup(self, filename): + """extract backup zip and return filelist""" + cache_dir = self.config["application"]["cache_dir"] + backup_dir = os.path.join(cache_dir, "backup") + file_path = os.path.join(backup_dir, filename) with zipfile.ZipFile(file_path, "r") as z: zip_content = z.namelist() @@ -541,6 +564,13 @@ class ElasticBackup: return response.ok +def get_available_backups(): + """return dict of available backups for settings view""" + backup_handler = ElasticBackup(INDEX_CONFIG, reason=False) + all_backup_files = backup_handler.get_all_backup_files() + return all_backup_files + + def backup_all_indexes(reason): """backup all es indexes to disk""" backup_handler = ElasticBackup(INDEX_CONFIG, reason) @@ -557,13 +587,13 @@ def backup_all_indexes(reason): backup_handler.zip_it() -def restore_from_backup(): +def restore_from_backup(filename): """restore indexes from backup file""" # delete index_check(force_restore=True) # recreate backup_handler = ElasticBackup(INDEX_CONFIG, reason=False) - zip_content = backup_handler.unpack_zip_backup() + zip_content = backup_handler.unpack_zip_backup(filename) backup_handler.restore_json_files(zip_content) diff --git a/tubearchivist/home/tasks.py b/tubearchivist/home/tasks.py index 3fb61d9..a8a2743 100644 --- a/tubearchivist/home/tasks.py +++ b/tubearchivist/home/tasks.py @@ -169,9 +169,9 @@ def run_backup(reason="auto"): @shared_task -def run_restore_backup(): +def run_restore_backup(filename): """called from settings page, dump backup to zip file""" - restore_from_backup() + restore_from_backup(filename) print("index restore finished") diff --git a/tubearchivist/home/views.py b/tubearchivist/home/views.py index cdc4a82..7248bbb 100644 --- a/tubearchivist/home/views.py +++ b/tubearchivist/home/views.py @@ -31,6 +31,7 @@ from home.src.config import AppConfig, ScheduleBuilder from home.src.frontend import PostData from home.src.helper import RedisArchivist, UrlListParser from home.src.index import YoutubePlaylist +from home.src.index_management import get_available_backups from home.src.searching import Pagination, SearchHandler from home.tasks import extrac_dl, subscribe_to @@ -898,6 +899,7 @@ class SettingsView(View): config_handler = AppConfig(request.user.id) colors = config_handler.colors + available_backups = get_available_backups() user_form = UserSettingsForm() app_form = ApplicationSettingsForm() scheduler_form = SchedulerSettingsForm() @@ -906,6 +908,7 @@ class SettingsView(View): "title": "Settings", "config": config_handler.config, "colors": colors, + "available_backups": available_backups, "user_form": user_form, "app_form": app_form, "scheduler_form": scheduler_form,