mirror of
https://github.com/ankitects/anki.git
synced 2025-09-25 01:06:35 -04:00
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
This commit is contained in:
parent
44c764b895
commit
8e2154dfbf
1 changed files with 19 additions and 21 deletions
40
aqt/main.py
40
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?
|
||||
|
|
Loading…
Reference in a new issue