implement backup rotate from config value

This commit is contained in:
simon 2021-12-14 19:40:46 +07:00
parent ff2de43752
commit 7924f28ccb
No known key found for this signature in database
GPG Key ID: 2C15AA5E89985DD4

View File

@ -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"""