From b7a5dca8038f3e253011dec1df37608e5253955b Mon Sep 17 00:00:00 2001 From: azivner Date: Fri, 6 Oct 2017 20:39:00 -0400 Subject: [PATCH] cleanup of old backup files --- TODO | 4 ++++ src/backup.py | 28 ++++++++++++++++++++++++++-- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/TODO b/TODO index fdab14966..23364815e 100644 --- a/TODO +++ b/TODO @@ -6,6 +6,10 @@ New features: deterministic to allow lookup by cipher text - db upgrade / migration - db backup into directory + - basic impl done + - configurable backup time interval + - delete old backups + - configurable backup deletion - might do the same thing with alt-j and alt-l - ctrl-b nad linkem by mohlo byt goto do notu - potencialne nova navigace back - forward diff --git a/src/backup.py b/src/backup.py index 7ac14c31d..4d7c8baab 100644 --- a/src/backup.py +++ b/src/backup.py @@ -1,9 +1,11 @@ from datetime import datetime import utils -from sql import getOption, setOption +from sql import getOption, setOption, commit import config_provider from shutil import copyfile +import os +import re def backup(): now = utils.nowTimestamp() @@ -19,4 +21,26 @@ def backup(): copyfile(document_path, backup_directory + "/" + "backup-" + date_str + ".db") - setOption('last_backup_date', now) \ No newline at end of file + setOption('last_backup_date', now) + commit() + + cleanup_old_backups() + + +def cleanup_old_backups(): + now = datetime.utcnow() + config = config_provider.getConfig() + backup_directory = config['Backup']['backupDirectory'] + + for file in os.listdir(backup_directory): + match = re.search(r"backup-([0-9 -:]+)\.db", file) + + if match: + date_str = match.group(1) + + date = datetime.strptime(date_str, "%Y-%m-%d %H:%M") + + if (now - date).days > 30: + print("Removing old backup - " + file) + + os.remove(backup_directory + "/" + file) \ No newline at end of file