2. excluded_files
3. filter_func (which must return whether the file is accepted or not)
'''
+
if len(source_path) > 0:
path = path[len(source_path):]
password=self.password,
new_volume_handler=new_volume_handler)
os.chdir(target_path)
+ # TODO: filtering here
tarobj.extractall()
os.chdir(cwd)
tarobj.close()
'restored: %s, line %d' % (op_type, l_no))
continue
- # TODO: filter by j.get('path', '')
+ # filtering paths
+ op_path = j.get('path', '')
+ if not self.filter_path(op_path, '.'):
+ continue
vol_no = j.get('volume', -1)
if not isinstance(vol_no, int) or vol_no < 0:
offset = j.get('offset', -1)
if tarobj:
member = tarobj.next()
- if member.path != j['path']:
+ if member.path != op_path:
# force a seek and reopen
tarobj.close()
tarobj = None
deltatar.restore_backup(target_path="source_dir",
backup_tar_path=tar_path)
- assert visited_paths == [
+ assert set(visited_paths) == set([
'/small',
'/test',
'/test/huge2',
'/test/test2'
- ]
+ ])
def test_create_filter_out_func(self):
'''
deltatar.restore_backup(target_path="source_dir",
backup_tar_path=tar_path)
- assert visited_paths == [
+ assert set(visited_paths) == set([
'/small',
'/test'
- ]
+ ])
# check that effectively no file was backed up
assert not os.path.exists("./source_dir/small")
assert not os.path.exists("./source_dir/big")
assert not os.path.exists("./source_dir/test")
+ def test_restore_index_basic_filtering(self):
+ '''
+ Creates a backup, and then filter when doing the index 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")
+
+ 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.restore_backup(target_path="source_dir",
+ backup_indexes_paths=[index_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_index_filter_func(self):
+ '''
+ Creates a backup, and then filter when doing the index 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
+ deltatar.restore_backup(target_path="source_dir",
+ backup_indexes_paths=[index_path])
+
+ assert set(visited_paths) == set([
+ '/small',
+ '/test',
+ '/test/huge2',
+ '/test/test2'
+ ])
+
+
class DeltaTar2Test(DeltaTarTest):
'''
Same as DeltaTar but with specific ":" mode