From 8e2154dfbf86539cd7781c92c2fb454b2cfcf0c7 Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Mon, 14 Aug 2017 18:53:39 +1000 Subject: [PATCH] change backup filename - embed date in name to make it easier for people to locate correct backup - move old backups to the trash instead of deleting outright - backups in the old format will not be rotated, and will need to be deleted by the user - if we just deleted them at the start of the rotation it could lead to data loss for users moving back and forth between 2.1 and 2.0 --- aqt/main.py | 40 +++++++++++++++++++--------------------- 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/aqt/main.py b/aqt/main.py index e492c4ca3..19048d84f 100644 --- a/aqt/main.py +++ b/aqt/main.py @@ -344,31 +344,29 @@ the manual for information on how to restore from an automatic backup.")) return dir = self.pm.backupFolder() path = self.pm.collectionPath() - # find existing backups - backups = [] - for file in os.listdir(dir): - m = re.search("backup-(\d+).apkg", file) - if not m: - # unknown file - continue - backups.append((int(m.group(1)), file)) - backups.sort() - # get next num - if not backups: - n = 1 - else: - n = backups[-1][0] + 1 + # do backup - newpath = os.path.join(dir, "backup-%d.apkg" % n) + fname = time.strftime("backup-%Y-%m-%d-%H.%M.%S.apkg", time.localtime(time.time())) + newpath = os.path.join(dir, fname) data = open(path, "rb").read() b = self.BackupThread(newpath, data) b.start() - # remove if over - if len(backups) + 1 > nbacks: - delete = len(backups) + 1 - nbacks - delete = backups[:delete] - for file in delete: - os.unlink(os.path.join(dir, file[1])) + + # find existing backups + backups = [] + for file in os.listdir(dir): + # only look for new-style format + m = re.match("backup-\{4}-.+.apkg", file) + if not m: + continue + backups.append(file) + backups.sort() + + # remove old ones + while len(backups) > nbacks: + fname = backups.pop(0) + path = os.path.join(dir, fname) + send2trash(path) def maybeOptimize(self): # have two weeks passed?