initial super simple implementation and unit test of restore_backup
authorEduardo Robles Elvira <edulix@wadobo.com>
Fri, 26 Jul 2013 17:10:44 +0000 (19:10 +0200)
committerEduardo Robles Elvira <edulix@wadobo.com>
Fri, 26 Jul 2013 17:10:44 +0000 (19:10 +0200)
deltatar/deltatar.py
testing/test_deltatar.py

index c2f60f5..4a7403d 100644 (file)
@@ -324,9 +324,12 @@ class DeltaTar(object):
                               max_volume_size=max_volume_size,
                               new_volume_handler=new_volume_handler)
 
-        for i in self._recursive_walk_dir(source_path):
+        cwd = os.getcwd()
+        os.chdir(source_path)
+        for i in self._recursive_walk_dir('.'):
             tarobj.add(i)
 
+        os.chdir(cwd)
         tarobj.close()
 
 
@@ -364,4 +367,39 @@ class DeltaTar(object):
           using any file index. If it's a multivol tarfile, volume_name_func
           will be called.
         '''
-        pass
+        if not isinstance(target_path, basestring):
+            raise Exception('Target path must be a string')
+
+        if not isinstance(backup_tar_path, basestring):
+            raise Exception('Backup tar path must be a string')
+
+        if not os.path.exists(backup_tar_path) or not os.path.isfile(backup_tar_path):
+            raise Exception('Source path "%s" does not exist or is not a '\
+                            'directory' % backup_tar_path)
+
+        if not os.access(backup_tar_path, os.R_OK):
+            raise Exception('Source path "%s" is not readable' % backup_tar_path)
+
+        # try to create backup path if needed
+        if not os.path.exists(target_path):
+            os.makedirs(target_path)
+
+        def new_volume_handler(deltarobj, tarobj, base_name, volume_number):
+            '''
+            Handles the new volumes
+            '''
+            volume_path = deltarobj.volume_name_func(True, volume_number)
+            tarobj.open_volume(volume_path)
+        new_volume_handler = partial(new_volume_handler, self)
+
+        tarobj = tarfile.TarFile.open(backup_tar_path,
+                              mode=self.mode,
+                              format=tarfile.GNU_FORMAT,
+                              concat_compression='#gz' in self.mode,
+                              password=self.password,
+                              new_volume_handler=new_volume_handler)
+        cwd = os.getcwd()
+        os.chdir(target_path)
+        tarobj.extractall()
+        os.chdir(cwd)
+        tarobj.close()
index 3bcf659..b088c93 100644 (file)
@@ -17,6 +17,7 @@
 # Author: Eduardo Robles Elvira <edulix@wadobo.com>
 
 import os
+import shutil
 import logging
 
 from deltatar.deltatar import DeltaTar
@@ -36,6 +37,7 @@ class DeltaTarTest(BaseTest):
         '''
         os.makedirs('source_dir/test/test2')
         self.hash = dict()
+        self.hash["source_dir/test/test2"] = ''
         self.hash["source_dir/big"]  = self.create_file("source_dir/big", 50000)
         self.hash["source_dir/small"]  = self.create_file("source_dir/small", 100)
         self.hash["source_dir/test/huge"]  = self.create_file("source_dir/test/huge", 700000)
@@ -60,4 +62,17 @@ class DeltaTarTest(BaseTest):
             source_path="source_dir",
             backup_path="backup_dir")
 
-        assert os.path.exists("backup_dir")
\ No newline at end of file
+        assert os.path.exists("backup_dir")
+        shutil.rmtree("source_dir")
+
+        tar_filename = deltatar.volume_name_func('backup_dir', True, 0)
+        tar_path = os.path.join("backup_dir", tar_filename)
+
+        deltatar.mode = 'r'
+        deltatar.restore_backup(target_path="source_dir",
+                                backup_tar_path=tar_path)
+
+        for key, value in self.hash.iteritems():
+            assert os.path.exists(key)
+            if value:
+                assert value == self.md5sum(key)
\ No newline at end of file