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
|
return
|
||||||
dir = self.pm.backupFolder()
|
dir = self.pm.backupFolder()
|
||||||
path = self.pm.collectionPath()
|
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
|
# 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()
|
data = open(path, "rb").read()
|
||||||
b = self.BackupThread(newpath, data)
|
b = self.BackupThread(newpath, data)
|
||||||
b.start()
|
b.start()
|
||||||
# remove if over
|
|
||||||
if len(backups) + 1 > nbacks:
|
# find existing backups
|
||||||
delete = len(backups) + 1 - nbacks
|
backups = []
|
||||||
delete = backups[:delete]
|
for file in os.listdir(dir):
|
||||||
for file in delete:
|
# only look for new-style format
|
||||||
os.unlink(os.path.join(dir, file[1]))
|
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):
|
def maybeOptimize(self):
|
||||||
# have two weeks passed?
|
# have two weeks passed?
|
||||||
|
|
Loading…
Reference in a new issue