def test_patch(self): """Test combining 3 iters of metadata rorps""" self.make_temp() xcopytree(os.path.join(old_test_dir, b"various_file_types"), tempdir.path, content=True) rp1 = tempdir.append('regular_file') rp2 = tempdir.append('subdir') rp3 = rp2.append('subdir_file') rp4 = tempdir.append('test') rp1new = tempdir.append('regular_file') rp1new.chmod(0) zero = rpath.RORPath(('test', )) current = [rp1, rp2, rp3] diff1 = [rp1, rp4] diff2 = [rp1new, rp2, zero] Globals.rbdir = tempdir output = meta_mgr.PatchDiffMan()._iterate_patched_attr( [iter(current), iter(diff1), iter(diff2)]) out1 = next(output) self.assertIs(out1, rp1new) out2 = next(output) self.assertIs(out2, rp2) out3 = next(output) self.assertIs(out3, rp3) self.assertRaises(StopIteration, output.__next__)
def make_root_dirs(self): """Make directory createable only by root""" rp = rpath.RPath(Globals.local_connection, os.path.join(abs_test_dir, b"root_out1")) re_init_rpath_dir(rp) rp1 = rp.append("1") rp1.touch() rp2 = rp.append("2") rp2.touch() rp2.chown(1, 1) rp3 = rp.append("3") rp3.touch() rp3.chown(2, 2) rp4 = rp.append("dev") rp4.makedev('c', 4, 28) sp = rpath.RPath(Globals.local_connection, os.path.join(abs_test_dir, b"root_out2")) if sp.lstat(): Myrm(sp.path) xcopytree(rp.path, sp.path) rp2 = sp.append("2") rp2.chown(2, 2) rp3 = sp.append("3") rp3.chown(1, 1) self.assertFalse(compare_recursive(rp, sp, compare_ownership=1)) return rp, sp
def testWindowsMode(self): """Test backup with quoting enabled We need to delete from the increment? directories long file names, because quoting adds too many extra letters. """ def delete_long(base_rp, length=100): """Delete filenames longer than length given""" for rp in selection.Select(base_rp).get_select_iter(): if len(rp.dirsplit()[1]) > length: rp.delete() if not Local.wininc2.lstat() or not Local.wininc3.lstat(): xcopytree(b"testfiles/increment2", b"testfiles/win-increment2") xcopytree(b"testfiles/increment3", b"testfiles/win-increment3") delete_long(Local.wininc2) delete_long(Local.wininc3) old_schema = self.rb_schema self.rb_schema = old_schema + b" --override-chars-to-quote '^a-z0-9_ -.' " self.set_connections(None, None, None, None) self.delete_tmpdirs() # Back up increment2, this contains a file with colons self.exec_rb(20000, b'testfiles/win-increment2', b'testfiles/output') self.rb_schema = old_schema # Quoting setting should now be saved time.sleep(1) # Back up increment3 self.exec_rb(30000, b'testfiles/win-increment3', b'testfiles/output') # Now check to make sure no ":" in output directory popen_fp = os.popen(b"find testfiles/output -name '*:*' | wc") wc_output = popen_fp.read() popen_fp.close() self.assertEqual(wc_output.split(), [b"0", b"0", b"0"]) # Start restore of increment 2 Globals.chars_to_quote = b'^a-z0-9_ -.' inc_paths = self.getinc_paths(b"increments.", b"testfiles/output/rdiff-backup-data", 1) Globals.chars_to_quote = None self.assertEqual(len(inc_paths), 1) self.exec_rb(None, inc_paths[0], b'testfiles/restoretarget2') self.assertTrue( compare_recursive(Local.wininc2, Local.rpout2, compare_hardlinks=0)) # Restore increment 3 again, using different syntax self.rb_schema = old_schema + b'-r 30000 ' self.exec_rb(None, b'testfiles/output', b'testfiles/restoretarget3') self.assertTrue( compare_recursive(Local.wininc3, Local.rpout3, compare_hardlinks=0)) self.rb_schema = old_schema
def testRemoveOlderThanRemote(self): """Test --remove-older-than remotely""" Myrm(Local.rpout.path) xcopytree(Local.backup3rp.path, Local.rpout.path) rdiff_backup(False, True, Local.rpout.path, None, extra_options=b"--remove-older-than 20000") rbdir = Local.rpout.append("rdiff-backup-data") for inc in self.get_all_increments(rbdir): self.assertGreaterEqual(inc.getinctime(), 20000)
def testRemoveOlderThan2(self): """Test --remove-older-than, but '1B'. Uses restoretest3""" Myrm(Local.rpout.path) xcopytree(Local.backup3rp.path, Local.rpout.path) rdiff_backup(True, True, Local.rpout.path, None, extra_options=b"--remove-older-than 1B --force") rbdir = Local.rpout.append("rdiff-backup-data") for inc in self.get_all_increments(rbdir): self.assertGreaterEqual(inc.getinctime(), 30000)
def testRemoveOlderThanCurrent(self): """Make sure --remove-older-than doesn't delete current incs""" Myrm(Local.rpout.path) xcopytree(Local.backup3rp.path, Local.rpout.path) rdiff_backup(True, True, Local.rpout.path, None, extra_options=b"--remove-older-than now --force") rbdir = Local.rpout.append("rdiff-backup-data") has_cur_mirror, has_metadata = 0, 0 for inc in self.get_all_increments(rbdir): if inc.getincbase().index[-1] == b'current_mirror': has_cur_mirror = 1 elif inc.getincbase().index[-1] == b'mirror_metadata': has_metadata = 1 self.assertTrue(has_cur_mirror) self.assertTrue(has_metadata)
def make_dir(self): self.delete_tmpdirs() rp1 = Local.get_tgt_local_rp('final_deleted1') if rp1.lstat(): Myrm(rp1.path) rp1.mkdir() rp1_1 = rp1.append('regfile') rp1_1.touch() rp1_2 = rp1.append('dir') rp1_2.mkdir() rp1_2_1 = rp1_2.append('regfile2') rp1_2_1.write_string('foo') rp2 = Local.get_tgt_local_rp('final_deleted2') if rp2.lstat(): Myrm(rp2.path) xcopytree(rp1.path, rp2.path) rp2_2_1 = rp2.append('dir', 'regfile2') self.assertTrue(rp2_2_1.lstat()) rp2_2_1.delete() rp2_2_1.touch() return rp1, rp1_2, rp2
def testInnerRestore(self): """Restore part of a dir, see if hard links preserved""" MakeOutputDir() output = rpath.RPath(Globals.local_connection, abs_output_dir) hlout1_dir = os.path.join(abs_test_dir, b"out_hardlink1") hlout2_dir = os.path.join(abs_test_dir, b"out_hardlink2") # Now set up directories out_hardlink1 and out_hardlink2 hlout1 = rpath.RPath(Globals.local_connection, hlout1_dir) if hlout1.lstat(): hlout1.delete() hlout1.mkdir() hlout1_sub = hlout1.append("subdir") hlout1_sub.mkdir() hl1_1 = hlout1_sub.append("hardlink1") hl1_2 = hlout1_sub.append("hardlink2") hl1_3 = hlout1_sub.append("hardlink3") hl1_4 = hlout1_sub.append("hardlink4") # 1 and 2 are hard linked, as are 3 and 4 hl1_1.touch() hl1_2.hardlink(hl1_1.path) hl1_3.touch() hl1_4.hardlink(hl1_3.path) hlout2 = rpath.RPath(Globals.local_connection, hlout2_dir) if hlout2.lstat(): hlout2.delete() xcopytree(hlout1_dir, hlout2_dir) hlout2_sub = hlout2.append("subdir") hl2_1 = hlout2_sub.append("hardlink1") hl2_2 = hlout2_sub.append("hardlink2") hl2_3 = hlout2_sub.append("hardlink3") hl2_4 = hlout2_sub.append("hardlink4") # Now 2 and 3 are hard linked, also 1 and 4 rpath.copy_with_attribs(hl1_1, hl2_1) rpath.copy_with_attribs(hl1_2, hl2_2) hl2_3.delete() hl2_3.hardlink(hl2_2.path) hl2_4.delete() hl2_4.hardlink(hl2_1.path) rpath.copy_attribs(hlout1_sub, hlout2_sub) # Now try backing up twice, making sure hard links are preserved InternalBackup(1, 1, hlout1.path, output.path) out_subdir = output.append("subdir") self.assertEqual( out_subdir.append("hardlink1").getinode(), out_subdir.append("hardlink2").getinode()) self.assertEqual( out_subdir.append("hardlink3").getinode(), out_subdir.append("hardlink4").getinode()) self.assertNotEqual( out_subdir.append("hardlink1").getinode(), out_subdir.append("hardlink3").getinode()) time.sleep(1) InternalBackup(1, 1, hlout2.path, output.path) out_subdir.setdata() self.assertEqual( out_subdir.append("hardlink1").getinode(), out_subdir.append("hardlink4").getinode()) self.assertEqual( out_subdir.append("hardlink2").getinode(), out_subdir.append("hardlink3").getinode()) self.assertNotEqual( out_subdir.append("hardlink1").getinode(), out_subdir.append("hardlink2").getinode()) # Now try restoring, still checking hard links. sub_dir = os.path.join(abs_output_dir, b"subdir") out2_dir = os.path.join(abs_test_dir, b"out2") out2 = rpath.RPath(Globals.local_connection, out2_dir) hlout1 = out2.append("hardlink1") hlout2 = out2.append("hardlink2") hlout3 = out2.append("hardlink3") hlout4 = out2.append("hardlink4") if out2.lstat(): out2.delete() InternalRestore(1, 1, sub_dir, out2_dir, 1) out2.setdata() for rp in [hlout1, hlout2, hlout3, hlout4]: rp.setdata() self.assertEqual(hlout1.getinode(), hlout2.getinode()) self.assertEqual(hlout3.getinode(), hlout4.getinode()) self.assertNotEqual(hlout1.getinode(), hlout3.getinode()) if out2.lstat(): out2.delete() InternalRestore(1, 1, sub_dir, out2_dir, int(time.time())) out2.setdata() for rp in [hlout1, hlout2, hlout3, hlout4]: rp.setdata() self.assertEqual(hlout1.getinode(), hlout4.getinode()) self.assertEqual(hlout2.getinode(), hlout3.getinode()) self.assertNotEqual(hlout1.getinode(), hlout2.getinode())
def copy_thrice(input, output): """Copy input directory to output directory three times""" xcopytree(input, output) xcopytree(input, os.path.join(output, b"killtesta")) xcopytree(input, os.path.join(output, b"killtestb"))