def testDirState(self): dir_state = dirtools.DirState(self.dir, index_cmp=dirtools.filehash) self.shutil.copytree('/test_dirtools', 'test_dirtools2') with self.open('/test_dirtools2/dir1/subdir1/file_subdir1', 'w') as f: f.write("dir state") with self.open('/test_dirtools2/new_file', 'w') as f: f.write("dir state") self.os.remove('/test_dirtools2/file1') self.shutil.rmtree('/test_dirtools2/dir2') dir_state2 = dirtools.DirState(dirtools.Dir('/test_dirtools2'), index_cmp=dirtools.filehash) diff = dir_state2 - dir_state self.assertEqual(diff, {'deleted': ['file1', 'dir2/file_dir2'], 'updated': ['dir1/subdir1/file_subdir1'], 'deleted_dirs': ['dir2'], 'created': ['new_file']}) self.assertEqual(diff, dirtools.compute_diff(dir_state2.state, dir_state.state))
def restore_backup(key, dest, cache_path=None): """ Restore backups given the key to dest using cache_path as source for state and deltas. """ if cache_path is None: cache_path = tempfile.gettempdir() for index, (state_file, previous_state_file, state_dt) in enumerate(get_full_and_incremental(key)): if index == 0: # At index == 0, state is the full archive log.info('Restored full backup ({})'.format(state_dt)) tarfile.open(state_file, 'r:gz').extractall(dest) else: with open(state_file, 'rb') as f: state = json.loads(f.read()) with open(previous_state_file, 'rb') as f: previous_state = json.loads(f.read()) diff = compute_diff(state, previous_state) patch_diff(dest, diff, FileFinder.check_key('created', key, state_dt), FileFinder.check_key('updated', key, state_dt)) log.info('Patched incremental backup ({})'.format(state_dt)) return dest