From 6ad1c8e73b067ee45bfc0d7bd0424dc86fb42fca Mon Sep 17 00:00:00 2001 From: simon Date: Mon, 13 Dec 2021 21:20:48 +0700 Subject: [PATCH] add reason to backup as file name tag --- tubearchivist/home/src/frontend.py | 2 +- tubearchivist/home/src/index_management.py | 13 +++++++------ tubearchivist/home/tasks.py | 4 ++-- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/tubearchivist/home/src/frontend.py b/tubearchivist/home/src/frontend.py index 40c6806..1c8b0c2 100644 --- a/tubearchivist/home/src/frontend.py +++ b/tubearchivist/home/src/frontend.py @@ -265,7 +265,7 @@ class PostData: def _db_backup(): """backup es to zip from settings page""" print("backing up database") - run_backup.delay() + run_backup.delay("manual") return {"success": True} @staticmethod diff --git a/tubearchivist/home/src/index_management.py b/tubearchivist/home/src/index_management.py index 67e0048..e15b064 100644 --- a/tubearchivist/home/src/index_management.py +++ b/tubearchivist/home/src/index_management.py @@ -362,9 +362,10 @@ class ElasticIndex: class ElasticBackup: """dump index to nd-json files for later bulk import""" - def __init__(self, index_config): + def __init__(self, index_config, reason): self.config = AppConfig().config self.index_config = index_config + self.reason = reason self.timestamp = datetime.now().strftime("%Y%m%d") self.backup_files = [] @@ -456,7 +457,7 @@ class ElasticBackup: def zip_it(self): """pack it up into single zip file""" cache_dir = self.config["application"]["cache_dir"] - file_name = f"ta_backup-{self.timestamp}.zip" + file_name = f"ta_backup-{self.timestamp}-{self.reason}.zip" backup_folder = os.path.join(cache_dir, "backup") backup_file = os.path.join(backup_folder, file_name) @@ -540,9 +541,9 @@ class ElasticBackup: return response.ok -def backup_all_indexes(): +def backup_all_indexes(reason): """backup all es indexes to disk""" - backup_handler = ElasticBackup(INDEX_CONFIG) + backup_handler = ElasticBackup(INDEX_CONFIG, reason) for index in backup_handler.index_config: index_name = index["index_name"] @@ -561,7 +562,7 @@ def restore_from_backup(): # delete index_check(force_restore=True) # recreate - backup_handler = ElasticBackup(INDEX_CONFIG) + backup_handler = ElasticBackup(INDEX_CONFIG, reason=False) zip_content = backup_handler.unpack_zip_backup() backup_handler.restore_json_files(zip_content) @@ -594,7 +595,7 @@ def index_check(force_restore=False): # make backup before rebuild if not backed_up: print("running backup first") - backup_all_indexes() + backup_all_indexes(reason="update") backed_up = True print(f"applying new mappings to index ta_{index_name}...") diff --git a/tubearchivist/home/tasks.py b/tubearchivist/home/tasks.py index 9c8a77c..3fb61d9 100644 --- a/tubearchivist/home/tasks.py +++ b/tubearchivist/home/tasks.py @@ -162,9 +162,9 @@ def run_manual_import(): @shared_task(name="run_backup") -def run_backup(): +def run_backup(reason="auto"): """called from settings page, dump backup to zip file""" - backup_all_indexes() + backup_all_indexes(reason) print("backup finished")