[svn.haxx.se] · SVN Dev · SVN Users · SVN Org · TSVN Dev · TSVN Users · Subclipse Dev · Subclipse Users · this month's index

[PATCH] hot_backup.py backup removal bug.

From: Michael <mixtim_at_ispwest.com>
Date: 2003-02-22 17:41:00 CET

I couldn't find this in the mail archive (was sent on 2/14) so I'm
sending it again.

Michael

--- hot-backup.py.in.orig Fri Feb 14 02:03:08 2003
+++ hot-backup.py.in Fri Feb 14 02:03:12 2003
@@ -22,7 +22,7 @@
 
 ######################################################################
 
-import sys, os, shutil, string
+import sys, os, shutil, string, re
 
 ######################################################################
 # Global Settings
@@ -78,15 +78,47 @@
 
 backup_subdir = os.path.join(backup_dir, repo + "-" + youngest)
 
-# If there is already a backup of this revision, append an increment
-# to the path. (We still need to do a backup, because the repos might
-# have changed despite no new revision having been created.)
-try_count = 1
-while os.path.exists(backup_subdir):
- backup_subdir = os.path.join(backup_dir, repo + "-"
- + youngest + "-" + `try_count`)
- try_count += 1
-
+# We pass in filenames so there is never a case where they are equal.
+def comparator(a, b):
+ regexp = re.compile("-(?P<revision>[0-9]+)(-(?P<increment>[0-9]+))?$")
+ matcha = regexp.search(a)
+ matchb = regexp.search(b)
+ reva = int(matcha.groupdict()['revision'])
+ revb = int(matchb.groupdict()['revision'])
+ if (reva < revb):
+ return -1
+ elif (reva > revb):
+ return 1
+ else:
+ inca = matcha.groupdict()['increment']
+ incb = matchb.groupdict()['increment']
+ if not inca:
+ return -1
+ elif not incb:
+ return 1;
+ elif (int(inca) < int(incb)):
+ return -1
+ else:
+ return 1
+
+# If there is already a backup of this revision, then append the
+# next highest increment to the path. We still need to do a backup
+# because the repository might have changed despite no new revision
+# having been created. We find the highest increment and add one
+# rather than start from 1 and increment because the starting
+# increments may have already been removed due to num_backups.
+
+regexp = re.compile("^" + repo + "-" + youngest + "(-(?P<increment>[0-9]+))?$")
+directory_list = os.listdir(backup_dir)
+young_list = filter(lambda x: regexp.search(x), directory_list)
+if young_list:
+ young_list.sort(comparator)
+ increment = regexp.search(young_list.pop()).groupdict()['increment']
+ if increment:
+ backup_subdir = os.path.join(backup_dir, repo + "-" + youngest + "-"
+ + str(int(increment) + 1))
+ else:
+ backup_subdir = os.path.join(backup_dir, repo + "-" + youngest + "-1")
 
 print "Backing up repository to '" + backup_subdir + "'..."
 shutil.copytree(repo_dir, backup_subdir)
@@ -168,23 +200,17 @@
 os.unlink(lockpath)
 print "Lock removed. Cleanup complete."
 
-# Step 8: finally, remove the repository back that's NUM_BACKUPS older
-# than the one we just created. If there are multiple versions of
-# this repos backup, remove them too.
-
-kill_rev = int(youngest) - num_backups
-old_backup_subdir = os.path.join(backup_dir, repo + "-" + `kill_rev`)
-if os.path.exists(old_backup_subdir):
+# Step 8: finally, remove all repository backups other than
+# the last NUM_BACKUPS.
+
+regexp = re.compile("^" + repo + "-[0-9]+")
+directory_list = os.listdir(backup_dir)
+old_list = filter(lambda x: regexp.search(x), directory_list)
+old_list.sort(comparator)
+del old_list[max(0,len(old_list)-num_backups):]
+for item in old_list:
+ old_backup_subdir = os.path.join(backup_dir, item)
   print "Removing old backup: " + old_backup_subdir
   shutil.rmtree(old_backup_subdir)
- try_count = 1
- old_backup_subdir = os.path.join(backup_dir, repo + "-" + `kill_rev`
- + "-" + `try_count`)
- while os.path.exists(old_backup_subdir):
- print "Removing old backup: " + old_backup_subdir
- shutil.rmtree(old_backup_subdir)
- try_count += 1
- old_backup_subdir = os.path.join(backup_dir, repo + "-" + `kill_rev`
- + "-" + `try_count`)
 

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@subversion.tigris.org
For additional commands, e-mail: dev-help@subversion.tigris.org
Received on Sat Feb 22 17:41:23 2003

This is an archived mail posted to the Subversion Dev mailing list.