def InternalBackup(source_local, dest_local, src_dir, dest_dir, current_time=None, eas=None, acls=None): """Backup src to dest internally This is like rdiff_backup but instead of running a separate rdiff-backup script, use the separate *.py files. This way the script doesn't have to be rebuild constantly, and stacktraces have correct line/file references. """ Globals.current_time = current_time Globals.security_level = "override" Globals.set("no_compression_regexp_string", os.fsencode(actions.DEFAULT_NOT_COMPRESSED_REGEXP)) cmdpairs = _internal_get_cmd_pairs(source_local, dest_local, src_dir, dest_dir) Security.initialize("backup", cmdpairs) rpin, rpout = list(map(SetConnections.cmdpair2rp, cmdpairs)) for attr in ('eas_active', 'eas_write', 'eas_conn'): SetConnections.UpdateGlobal(attr, eas) for attr in ('acls_active', 'acls_write', 'acls_conn'): SetConnections.UpdateGlobal(attr, acls) Main._misc_setup([rpin, rpout]) Main._action_backup(rpin, rpout) Main._cleanup()
def BackupRestoreSeries(source_local, dest_local, list_of_dirnames, compare_hardlinks = 1, dest_dirname = "testfiles/output", restore_dirname = "testfiles/rest_out", compare_backups = 1, compare_eas = 0, compare_acls = 0, compare_ownership = 0): """Test backing up/restoring of a series of directories The dirnames correspond to a single directory at different times. After each backup, the dest dir will be compared. After the whole set, each of the earlier directories will be recovered to the restore_dirname and compared. """ Globals.set('preserve_hardlinks', compare_hardlinks) time = 10000 dest_rp = rpath.RPath(Globals.local_connection, dest_dirname) restore_rp = rpath.RPath(Globals.local_connection, restore_dirname) Myrm(dest_dirname) for dirname in list_of_dirnames: src_rp = rpath.RPath(Globals.local_connection, dirname) reset_hardlink_dicts() _reset_connections(src_rp, dest_rp) InternalBackup(source_local, dest_local, dirname, dest_dirname, time, eas = compare_eas, acls = compare_acls) time += 10000 _reset_connections(src_rp, dest_rp) if compare_backups: assert CompareRecursive(src_rp, dest_rp, compare_hardlinks, compare_eas = compare_eas, compare_acls = compare_acls, compare_ownership = compare_ownership) time = 10000 for dirname in list_of_dirnames[:-1]: reset_hardlink_dicts() Myrm(restore_dirname) InternalRestore(dest_local, source_local, dest_dirname, restore_dirname, time, eas = compare_eas, acls = compare_acls) src_rp = rpath.RPath(Globals.local_connection, dirname) assert CompareRecursive(src_rp, restore_rp, compare_eas = compare_eas, compare_acls = compare_acls, compare_ownership = compare_ownership) # Restore should default back to newest time older than it # with a backup then. if time == 20000: time = 21000 time += 10000
def set_select(cls, rp, tuplelist, *filelists): """ Initialize select object using tuplelist Note that each list in filelists must each be passed as separate arguments, so each is recognized as a file by the connection. Otherwise we will get an error because a list containing files can't be pickled. Also, cls._select needs to be cached so get_diffs below can retrieve the necessary rps. """ sel = selection.Select(rp) sel.parse_selection_args(tuplelist, filelists) sel_iter = sel.set_iter() cache_size = Globals.pipeline_max_length * 3 # to and from+leeway cls._select = rorpiter.CacheIndexable(sel_iter, cache_size) Globals.set('select_mirror', sel_iter)
def MirrorTest(source_local, dest_local, list_of_dirnames, compare_hardlinks = 1, dest_dirname = "testfiles/output"): """Mirror each of list_of_dirnames, and compare after each""" Globals.set('preserve_hardlinks', compare_hardlinks) dest_rp = rpath.RPath(Globals.local_connection, dest_dirname) old_force_val = Main.force Main.force = 1 Myrm(dest_dirname) for dirname in list_of_dirnames: src_rp = rpath.RPath(Globals.local_connection, dirname) reset_hardlink_dicts() _reset_connections(src_rp, dest_rp) InternalMirror(source_local, dest_local, dirname, dest_dirname) _reset_connections(src_rp, dest_rp) assert CompareRecursive(src_rp, dest_rp, compare_hardlinks) Main.force = old_force_val
def MirrorTest(source_local, dest_local, list_of_dirnames, compare_hardlinks=1, dest_dirname=abs_output_dir): """Mirror each of list_of_dirnames, and compare after each""" Globals.set('preserve_hardlinks', compare_hardlinks) Globals.set("no_compression_regexp_string", os.fsencode(actions.DEFAULT_NOT_COMPRESSED_REGEXP)) dest_rp = rpath.RPath(Globals.local_connection, dest_dirname) Myrm(dest_dirname) for dirname in list_of_dirnames: src_rp = rpath.RPath(Globals.local_connection, dirname) reset_hardlink_dicts() _reset_connections(src_rp, dest_rp) InternalMirror(source_local, dest_local, dirname, dest_dirname, force=True) _reset_connections(src_rp, dest_rp) assert compare_recursive(src_rp, dest_rp, compare_hardlinks)
def InternalRestore(mirror_local, dest_local, mirror_dir, dest_dir, time, eas=None, acls=None): """Restore mirror_dir to dest_dir at given time This will automatically find the increments.XXX.dir representing the time specified. The mirror_dir and dest_dir are relative to the testing directory and will be modified for remote trials. """ Main._force = 1 Main._restore_root_set = 0 Globals.security_level = "override" Globals.set("no_compression_regexp_string", os.fsencode(actions.DEFAULT_NOT_COMPRESSED_REGEXP)) cmdpairs = _internal_get_cmd_pairs(mirror_local, dest_local, mirror_dir, dest_dir) Security.initialize("restore", cmdpairs) mirror_rp, dest_rp = list(map(SetConnections.cmdpair2rp, cmdpairs)) for attr in ('eas_active', 'eas_write', 'eas_conn'): SetConnections.UpdateGlobal(attr, eas) for attr in ('acls_active', 'acls_write', 'acls_conn'): SetConnections.UpdateGlobal(attr, acls) Main._misc_setup([mirror_rp, dest_rp]) inc = get_increment_rp(mirror_rp, time) if inc: Main._restore_timestr = None Main._action_restore(get_increment_rp(mirror_rp, time), dest_rp) else: # use alternate syntax Main._restore_timestr = str(time) Main._action_restore(mirror_rp, dest_rp) Main._cleanup()
def _parse_cmdlineoptions_compat200(arglist): # noqa: C901 """ Parse argument list and set global preferences, compatibility function between old and new way of parsing parameters. """ if arglist.action in ('backup', 'restore'): Globals.set("acls_active", arglist.acls) Globals.set("win_acls_active", arglist.acls) Globals.set("carbonfile_active", arglist.carbonfile) Globals.set("compare_inode", arglist.compare_inode) Globals.set("eas_active", arglist.eas) Globals.set("preserve_hardlinks", arglist.hard_links) Globals.set("resource_forks_active", arglist.resource_forks) Globals.set("never_drop_acls", arglist.never_drop_acls) if arglist.action in ('backup', 'regress', 'restore'): Globals.set("compression", arglist.compression) Globals.set("no_compression_regexp_string", os.fsencode(arglist.not_compressed_regexp)) else: Globals.set("no_compression_regexp_string", os.fsencode(actions.DEFAULT_NOT_COMPRESSED_REGEXP)) if arglist.action in ('server'): Globals.server = True if arglist.action in ('backup'): Globals.set("file_statistics", arglist.file_statistics) Globals.set("print_statistics", arglist.print_statistics) if arglist.action in ('regress'): Globals.set("allow_duplicate_timestamps", arglist.allow_duplicate_timestamps) Globals.set("null_separator", arglist.null_separator) Globals.set("use_compatible_timestamps", arglist.use_compatible_timestamps) Globals.set("do_fsync", arglist.fsync) if arglist.current_time is not None: Globals.set_integer('current_time', arglist.current_time) if arglist.chars_to_quote is not None: Globals.set('chars_to_quote', os.fsencode(arglist.chars_to_quote)) if arglist.api_version is not None: # FIXME Globals.set_api_version(arglist.api_version)
def setUp(self): Globals.set('isbackup_writer', 1) MakeOutputDir()
import unittest import os from commontest import old_test_dir, abs_test_dir, abs_output_dir, Myrm, \ abs_restore_dir, re_init_rpath_dir, compare_recursive, \ BackupRestoreSeries, rdiff_backup, RBBin, xcopytree, os_system from rdiff_backup import Globals, rpath """Root tests - contain tests which need to be run as root. Some of the quoting here may not work with csh (works on bash). Also, if you aren't me, check out the 'user' global variable. """ Globals.set('change_source_perms', None) Globals.counter = 0 assert os.getuid() == 0, "Run this test as root!" # we need a normal user for our test, we use environment variables to use # the right one, either through SUDO_* variables set by sudo, or set # explicitly by the tester. userid = int(os.getenv('RDIFF_TEST_UID', os.getenv('SUDO_UID'))) user = os.getenv('RDIFF_TEST_USER', os.getenv('SUDO_USER')) assert userid, "Unable to assess ID of non-root user to be used for tests" assert user, "Unable to assess name of non-root user to be used for tests" class BaseRootTest(unittest.TestCase): def _run_cmd(self, cmd): print("Running: ", cmd) rc = os_system(cmd)
import unittest, os from commontest import * from rdiff_backup import Globals, SetConnections, log, rpath, backup, fs_abilities """Regression tests This one must be run in the rdiff-backup directory, as it requres chdir-wrapper, the various rdiff-backup files, and the directory testfiles """ Globals.set("change_source_perms", 1) Globals.counter = 0 Globals.security_level = "override" log.Log.setverbosity(5) def get_local_rp(extension): return rpath.RPath(Globals.local_connection, "testfiles/" + extension) class Local: """This is just a place to put increments relative to the local connection""" inc1rp = get_local_rp("increment1") inc2rp = get_local_rp("increment2") inc3rp = get_local_rp("increment3") inc4rp = get_local_rp("increment4")
import unittest, os from commontest import * from rdiff_backup import Globals, log """Root tests - contain tests which need to be run as root. Some of the quoting here may not work with csh (works on bash). Also, if you aren't me, check out the 'user' global variable. """ Globals.set('change_source_perms', None) Globals.counter = 0 verbosity = 5 log.Log.setverbosity(verbosity) user = '******' # Non-root user to su to userid = 500 # id of user above assert os.getuid() == 0, "Run this test as root!" def Run(cmd): print "Running: ", cmd assert not os.system(cmd) class RootTest(unittest.TestCase): dirlist1 = ["testfiles/root", "testfiles/various_file_types", "testfiles/increment4"] dirlist2 = ["testfiles/increment4", "testfiles/root", "testfiles/increment1"] def testLocal1(self): BackupRestoreSeries(1, 1, self.dirlist1, compare_ownership = 1) def testLocal2(self):
def _parse_cmdlineoptions_compat200(arglist): # noqa: C901 """ Parse argument list and set global preferences, compatibility function between old and new way of parsing parameters. """ global _args, _action def normalize_path(path): """Used below to normalize the security paths before setting""" return rpath.RPath(Globals.local_connection, path).normalize().path if arglist.action in ('backup', 'restore'): Globals.set("acls_active", arglist.acls) Globals.set("win_acls_active", arglist.acls) Globals.set("carbonfile_active", arglist.carbonfile) Globals.set("compare_inode", arglist.compare_inode) Globals.set("eas_active", arglist.eas) Globals.set("preserve_hardlinks", arglist.hard_links) Globals.set("resource_forks_active", arglist.resource_forks) Globals.set("never_drop_acls", arglist.never_drop_acls) if arglist.action in ('backup', 'regress', 'restore'): Globals.set("compression", arglist.compression) Globals.set("no_compression_regexp_string", os.fsencode(arglist.not_compressed_regexp)) else: Globals.set("no_compression_regexp_string", os.fsencode(actions.DEFAULT_NOT_COMPRESSED_REGEXP)) if arglist.action in ('server'): Globals.server = True if arglist.action in ('backup'): Globals.set("file_statistics", arglist.file_statistics) Globals.set("print_statistics", arglist.print_statistics) if arglist.action in ('regress'): Globals.set("allow_duplicate_timestamps", arglist.allow_duplicate_timestamps) Globals.set("null_separator", arglist.null_separator) Globals.set("use_compatible_timestamps", arglist.use_compatible_timestamps) Globals.set("do_fsync", arglist.fsync) if arglist.current_time is not None: Globals.set_integer('current_time', arglist.current_time) if arglist.chars_to_quote is not None: Globals.set('chars_to_quote', os.fsencode(arglist.chars_to_quote)) if arglist.remote_tempdir is not None: Globals.remote_tempdir = os.fsencode(arglist.remote_tempdir) if arglist.restrict_path is not None: Globals.restrict_path = normalize_path(arglist.restrict_path) if arglist.restrict_mode == "read-write": Globals.security_level = "all" else: Globals.security_level = arglist.restrict_mode if arglist.api_version is not None: # FIXME Globals.set_api_version(arglist.api_version) if arglist.terminal_verbosity is not None: log.Log.setterm_verbosity(arglist.terminal_verbosity) log.Log.setverbosity(arglist.verbosity) if arglist.tempdir is not None: if not os.path.isdir(arglist.tempdir): log.Log.FatalError( "Temporary directory '{dir}' doesn't exist.".format( dir=arglist.tempdir)) # At least until Python 3.10, the module tempfile doesn't work properly, # especially under Windows, if tempdir is stored as bytes. # See https://github.com/python/cpython/pull/20442 tempfile.tempdir = arglist.tempdir if arglist.action in ('info', 'server'): _args = [] else: _args = arglist.locations
import unittest, os from commontest import * from rdiff_backup import Globals, SetConnections, log, rpath, backup, fs_abilities """Regression tests This one must be run in the rdiff-backup directory, as it requres chdir-wrapper, the various rdiff-backup files, and the directory testfiles """ Globals.set('change_source_perms', 1) Globals.counter = 0 Globals.security_level = "override" log.Log.setverbosity(5) def get_local_rp(extension): return rpath.RPath(Globals.local_connection, "testfiles/" + extension) class Local: """This is just a place to put increments relative to the local connection""" inc1rp = get_local_rp('increment1') inc2rp = get_local_rp('increment2') inc3rp = get_local_rp('increment3') inc4rp = get_local_rp('increment4') rpout = get_local_rp('output') rpout_inc = get_local_rp('output_inc')