From 760688a22426437d68adb6cb92b2c635f1f8d609 Mon Sep 17 00:00:00 2001
From: Brian Warner <warner@allmydata.com>
Date: Thu, 12 Mar 2009 16:04:27 -0700
Subject: [PATCH] consolidate: create multiple numbered backups of the original
 Archives directory, not just the first time

---
 src/allmydata/scripts/consolidate.py   | 12 ++++++++----
 src/allmydata/test/test_consolidate.py |  5 ++++-
 2 files changed, 12 insertions(+), 5 deletions(-)

diff --git a/src/allmydata/scripts/consolidate.py b/src/allmydata/scripts/consolidate.py
index 5fccff3d..eea872c7 100644
--- a/src/allmydata/scripts/consolidate.py
+++ b/src/allmydata/scripts/consolidate.py
@@ -89,10 +89,14 @@ class Consolidator:
         if not systems:
             self.msg("No systems under /Backups, nothing to consolidate")
             return 0
-        if not os.path.exists(self.backupfile):
-            f = open(self.backupfile, "wb")
-            pickle.dump(backup_data, f)
-            f.close()
+        backupfile = self.backupfile
+        counter = 0
+        while os.path.exists(backupfile):
+            backupfile = self.backupfile + "." + str(counter)
+            counter += 1
+        f = open(backupfile, "wb")
+        pickle.dump(backup_data, f)
+        f.close()
 
         for name, archives_dircap in sorted(systems.items()):
             self.do_system(name, archives_dircap)
diff --git a/src/allmydata/test/test_consolidate.py b/src/allmydata/test/test_consolidate.py
index acc0f49f..6d7b6919 100644
--- a/src/allmydata/test/test_consolidate.py
+++ b/src/allmydata/test/test_consolidate.py
@@ -179,7 +179,7 @@ class Consolidate(GridTestMixin, CLITestMixin, unittest.TestCase):
                                  "7 dirs created, 2 used as-is, 13 reused")
             self.failUnless(os.path.exists(dbfile))
             self.failUnless(os.path.exists(backupfile))
-            backup = pickle.load(open(backupfile, "rb"))
+            self.first_backup = backup = pickle.load(open(backupfile, "rb"))
             self.failUnless(u"fluxx" in backup["systems"])
             self.failUnless(u"fluxx" in backup["archives"])
             adata = backup["archives"]["fluxx"]
@@ -198,6 +198,9 @@ class Consolidate(GridTestMixin, CLITestMixin, unittest.TestCase):
             self.failUnlessEqual(last.strip(),
                                  "system done, "
                                  "0 dirs created, 0 used as-is, 0 reused")
+            backup = pickle.load(open(backupfile, "rb"))
+            self.failUnlessEqual(backup, self.first_backup)
+            self.failUnless(os.path.exists(backupfile + ".0"))
         d.addCallback(_check_consolidate_output2)
 
         d.addCallback(lambda ignored: self.build_manifest(self.nodes["Archives"]))
-- 
2.45.2