self.closed = True
raise
- def extractall(self, path=".", members=None):
+ def extractall(self, path=".", members=None, filter=None):
"""Extract all members from the archive to the current working
directory and set owner, modification time and permissions on
directories afterwards. `path' specifies a different directory
if self.volume_number > 0 and tarinfo.ismultivol():
continue
+ if filter and not filter(tarinfo.path):
+ continue
+
if tarinfo.isdir():
# Extract directories with a safe mode.
directories.append(tarinfo)
'/test/test2'
])
+ def test_restore_tar_basic_filtering(self):
+ '''
+ Creates a backup, and then filter when doing the tar based restore.
+ '''
+ deltatar = DeltaTar(mode=self.MODE, password=self.PASSWORD,
+ logger=self.consoleLogger)
+
+ # create first backup
+ deltatar.create_full_backup(
+ source_path="source_dir",
+ backup_path="backup_dir")
+
+ assert os.path.exists("backup_dir")
+ shutil.rmtree("source_dir")
+
+ deltatar.included_files = ["/test", "/small"]
+ deltatar.excluded_files = ["/test/huge"]
+
+ tar_filename = deltatar.volume_name_func('backup_dir', True, 0)
+ tar_path = os.path.join("backup_dir", tar_filename)
+
+ deltatar.restore_backup(target_path="source_dir",
+ backup_tar_path=tar_path)
+
+ assert os.path.exists("./source_dir/small")
+ assert os.path.exists("./source_dir/test")
+ assert os.path.exists("./source_dir/test/huge2")
+ assert os.path.exists("./source_dir/test/test2")
+
+ assert not os.path.exists("./source_dir/test/huge")
+ assert not os.path.exists("./source_dir/big")
+
+ def test_restore_tar_filter_func(self):
+ '''
+ Creates a backup, and then filter when doing the tar based restore,
+ using the filter function.
+ '''
+ visited_paths = []
+ def filter_func(visited_paths, path):
+ if path not in visited_paths:
+ visited_paths.append(path)
+ return True
+
+ filter_func = partial(filter_func, visited_paths)
+ deltatar = DeltaTar(mode=self.MODE, password=self.PASSWORD,
+ logger=self.consoleLogger)
+
+ # create first backup
+ deltatar.create_full_backup(
+ source_path="source_dir",
+ backup_path="backup_dir")
+
+ assert os.path.exists("backup_dir")
+ shutil.rmtree("source_dir")
+
+ index_filename = deltatar.index_name_func(True)
+ index_path = os.path.join("backup_dir", index_filename)
+
+ deltatar.included_files = ["/test", "/small"]
+ deltatar.excluded_files = ["/test/huge"]
+ deltatar.filter_func = filter_func
+
+ tar_filename = deltatar.volume_name_func('backup_dir', True, 0)
+ tar_path = os.path.join("backup_dir", tar_filename)
+
+ deltatar.restore_backup(target_path="source_dir",
+ backup_tar_path=tar_path)
+ assert set(visited_paths) == set([
+ '/small',
+ '/test',
+ '/test/huge2',
+ '/test/test2'
+ ])
+
+
class DeltaTar2Test(DeltaTarTest):
'''