From 7924f28ccb1448f9c30feab35ec29b230359a953 Mon Sep 17 00:00:00 2001 From: simon Date: Tue, 14 Dec 2021 19:40:46 +0700 Subject: [PATCH] implement backup rotate from config value --- tubearchivist/home/src/index_management.py | 25 ++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/tubearchivist/home/src/index_management.py b/tubearchivist/home/src/index_management.py index ad03eb1..b748ebb 100644 --- a/tubearchivist/home/src/index_management.py +++ b/tubearchivist/home/src/index_management.py @@ -563,6 +563,28 @@ class ElasticBackup: return response.ok + def rotate_backup(self): + """delete old backups if needed""" + rotate = self.config["scheduler"]["run_backup_rotate"] + if not rotate: + return + + all_backup_files = self.get_all_backup_files() + auto = [i for i in all_backup_files if i["reason"] == "auto"] + + if len(auto) <= rotate: + print("no backup files to rotate") + return + + cache_dir = self.config["application"]["cache_dir"] + backup_dir = os.path.join(cache_dir, "backup") + + all_to_delete = auto[rotate:] + for to_delete in all_to_delete: + file_path = os.path.join(backup_dir, to_delete["filename"]) + print(f"remove old backup file: {file_path}") + os.remove(file_path) + def get_available_backups(): """return dict of available backups for settings view""" @@ -586,6 +608,9 @@ def backup_all_indexes(reason): backup_handler.zip_it() + if reason == "auto": + backup_handler.rotate_backup() + def restore_from_backup(filename): """restore indexes from backup file"""