def test_list_archives_with_archive_calls_borg_with_archive_parameter(): storage_config = {} flexmock(module).should_receive('execute_command').with_args( ('borg', 'list', 'repo::archive'), capture_output=False ) module.list_archives(repository='repo', storage_config=storage_config, archive='archive')
def test_list_archives_with_remote_path_calls_borg_with_remote_path_parameters( ): insert_subprocess_mock(LIST_COMMAND + ('--remote-path', 'borg1')) module.list_archives(repository='repo', storage_config={}, remote_path='borg1')
def test_list_archives_with_archive_calls_borg_with_archive_parameter(): storage_config = {} insert_subprocess_mock(('borg', 'list', 'repo::archive')) module.list_archives(repository='repo', storage_config=storage_config, archive='archive')
def test_list_archives_calls_borg_with_parameters(): insert_subprocess_mock(LIST_COMMAND) module.list_archives( repository='repo', storage_config={}, )
def test_list_archives_with_log_debug_calls_borg_with_debug_parameter(): flexmock(module).should_receive('execute_command').with_args( LIST_COMMAND + ('--debug', '--show-rc'), capture_output=False ) insert_logging_mock(logging.DEBUG) module.list_archives(repository='repo', storage_config={})
def test_list_archives_with_log_info_calls_borg_with_info_parameter(): flexmock(module).should_receive('execute_command').with_args( LIST_COMMAND + ('--info',), capture_output=False ) insert_logging_mock(logging.INFO) module.list_archives(repository='repo', storage_config={})
def test_list_archives_with_lock_wait_calls_borg_with_lock_wait_parameters(): storage_config = {'lock_wait': 5} flexmock(module).should_receive('execute_command').with_args( LIST_COMMAND + ('--lock-wait', '5'), capture_output=False ) module.list_archives(repository='repo', storage_config=storage_config)
def test_list_archives_calls_borg_with_parameters(): flexmock(module).should_receive('execute_command').with_args( LIST_COMMAND, output_log_level=logging.WARNING) module.list_archives(repository='repo', storage_config={}, list_arguments=flexmock(archive=None, json=False))
def test_list_archives_with_log_info_calls_borg_with_info_parameter(): insert_subprocess_mock(LIST_COMMAND + ('--info', )) insert_logging_mock(logging.INFO) module.list_archives( repository='repo', storage_config={}, )
def test_list_archives_with_log_debug_and_json_suppresses_most_borg_output(): flexmock(module).should_receive('execute_command').with_args( LIST_COMMAND + ('--json', ), output_log_level=None) insert_logging_mock(logging.DEBUG) module.list_archives(repository='repo', storage_config={}, list_arguments=flexmock(archive=None, json=True))
def test_list_archives_with_verbosity_some_calls_borg_with_info_parameter(): insert_subprocess_mock(LIST_COMMAND + ('--info',)) module.list_archives( repository='repo', storage_config={}, verbosity=VERBOSITY_SOME, )
def test_list_archives_with_json_calls_borg_with_json_parameter(): insert_subprocess_mock(LIST_COMMAND + ('--json', )) module.list_archives( repository='repo', storage_config={}, json=True, )
def test_list_archives_with_log_debug_calls_borg_with_debug_parameter(): insert_subprocess_mock(LIST_COMMAND + ('--debug', '--show-rc')) insert_logging_mock(logging.DEBUG) module.list_archives( repository='repo', storage_config={}, )
def test_list_archives_with_lock_wait_calls_borg_with_lock_wait_parameters(): storage_config = {'lock_wait': 5} insert_subprocess_mock(LIST_COMMAND + ('--lock-wait', '5')) module.list_archives( repository='repo', storage_config=storage_config, )
def test_list_archives_with_verbosity_lots_calls_borg_with_debug_parameter(): insert_subprocess_mock(LIST_COMMAND + ('--debug',)) module.list_archives( repository='repo', storage_config={}, verbosity=VERBOSITY_LOTS, )
def test_list_archives_with_local_path_calls_borg_via_local_path(): insert_subprocess_mock(('borg1', ) + LIST_COMMAND[1:]) module.list_archives( repository='repo', storage_config={}, local_path='borg1', )
def test_list_archives_with_log_info_and_json_suppresses_most_borg_output(): flexmock(module).should_receive('execute_command').with_args( ('borg', 'list', '--json', 'repo'), output_log_level=None ) insert_logging_mock(logging.INFO) module.list_archives( repository='repo', storage_config={}, list_arguments=flexmock(archive=None, json=True) )
def test_list_archives_with_log_debug_calls_borg_with_debug_parameter(): flexmock(module).should_receive('execute_command').with_args( ('borg', 'list', '--debug', '--show-rc', 'repo'), output_log_level=logging.WARNING ) insert_logging_mock(logging.DEBUG) module.list_archives( repository='repo', storage_config={}, list_arguments=flexmock(archive=None, json=False) )
def test_list_archives_calls_borg_with_parameters(): flexmock(module).should_receive('execute_command').with_args( ('borg', 'list', 'repo'), output_log_level=logging.WARNING, borg_local_path='borg' ) module.list_archives( repository='repo', storage_config={}, list_arguments=flexmock(archive=None, paths=None, json=False, successful=False), )
def test_list_archives_with_lock_wait_calls_borg_with_lock_wait_parameters(): storage_config = {'lock_wait': 5} flexmock(module).should_receive('execute_command').with_args( LIST_COMMAND + ('--lock-wait', '5'), output_log_level=logging.WARNING) module.list_archives( repository='repo', storage_config=storage_config, list_arguments=flexmock(archive=None, json=False), )
def test_list_archives_with_archive_calls_borg_with_archive_parameter(): storage_config = {} flexmock(module).should_receive('execute_command').with_args( ('borg', 'list', 'repo::archive'), output_log_level=logging.WARNING) module.list_archives( repository='repo', storage_config=storage_config, list_arguments=flexmock(archive='archive', json=False), )
def test_list_archives_with_local_path_calls_borg_via_local_path(): flexmock(module).should_receive('execute_command').with_args( ('borg1', ) + LIST_COMMAND[1:], output_log_level=logging.WARNING) module.list_archives( repository='repo', storage_config={}, list_arguments=flexmock(archive=None, json=False), local_path='borg1', )
def test_list_archives_with_short_calls_borg_with_short_parameter(): flexmock(module).should_receive('execute_command').with_args( ('borg', 'list', '--short', 'repo'), output_log_level=logging.WARNING ).and_return('[]') module.list_archives( repository='repo', storage_config={}, list_arguments=flexmock(archive=None, json=False, successful=False, short=True), )
def test_list_archives_with_log_debug_and_json_suppresses_most_borg_output(): flexmock(module).should_receive('execute_command').with_args( ('borg', 'list', '--json', 'repo'), output_log_level=None, borg_local_path='borg' ) insert_logging_mock(logging.DEBUG) module.list_archives( repository='repo', storage_config={}, list_arguments=flexmock(archive=None, paths=None, json=True, successful=False), )
def test_list_archives_with_log_info_calls_borg_with_info_parameter(): flexmock(module).should_receive('execute_command').with_args( ('borg', 'list', '--info', 'repo'), output_log_level=logging.WARNING ) insert_logging_mock(logging.INFO) module.list_archives( repository='repo', storage_config={}, list_arguments=flexmock(archive=None, json=False, successful=False), )
def test_list_archives_with_successful_calls_borg_to_exclude_checkpoints(): flexmock(module).should_receive('execute_command').with_args( ('borg', 'list', '--glob-archives', module.BORG_EXCLUDE_CHECKPOINTS_GLOB, 'repo'), output_log_level=logging.WARNING, ).and_return('[]') module.list_archives( repository='repo', storage_config={}, list_arguments=flexmock(archive=None, json=False, successful=True), )
def test_list_archives_passes_through_arguments_to_borg(argument_name): flexmock(module).should_receive('execute_command').with_args( ('borg', 'list', '--' + argument_name.replace('_', '-'), 'value', 'repo'), output_log_level=logging.WARNING, ).and_return('[]') module.list_archives( repository='repo', storage_config={}, list_arguments=flexmock(archive=None, json=False, **{argument_name: 'value'}), )
def test_list_archives_with_remote_path_calls_borg_with_remote_path_parameters(): flexmock(module).should_receive('execute_command').with_args( ('borg', 'list', '--remote-path', 'borg1', 'repo'), output_log_level=logging.WARNING ) module.list_archives( repository='repo', storage_config={}, list_arguments=flexmock(archive=None, json=False), remote_path='borg1', )
def test_list_archives_with_lock_wait_calls_borg_with_lock_wait_parameters(): storage_config = {'lock_wait': 5} flexmock(module).should_receive('execute_command').with_args( ('borg', 'list', '--lock-wait', '5', 'repo'), output_log_level=logging.WARNING, error_on_warnings=False, ) module.list_archives( repository='repo', storage_config=storage_config, list_arguments=flexmock(archive=None, paths=None, json=False, successful=False), )
def test_list_archives_with_path_calls_borg_with_path_parameter(): storage_config = {} flexmock(module).should_receive('execute_command').with_args( ('borg', 'list', 'repo::archive', 'var/lib'), output_log_level=logging.WARNING, error_on_warnings=False, ) module.list_archives( repository='repo', storage_config=storage_config, list_arguments=flexmock(archive='archive', paths=['var/lib'], json=False, successful=False), )
def test_list_archives_with_json_calls_borg_with_json_parameter(): flexmock(module).should_receive('execute_command').with_args( LIST_COMMAND + ('--json',), capture_output=True ).and_return('[]') json_output = module.list_archives(repository='repo', storage_config={}, json=True) assert json_output == '[]'
def _run_commands_on_repository(*, args, consistency, json_results, local_path, location, remote_path, retention, storage, unexpanded_repository): # pragma: no cover repository = os.path.expanduser(unexpanded_repository) dry_run_label = ' (dry run; not making any changes)' if args.dry_run else '' if args.prune: logger.info('{}: Pruning archives{}'.format(repository, dry_run_label)) borg_prune.prune_archives( args.dry_run, repository, storage, retention, local_path=local_path, remote_path=remote_path, ) if args.create: logger.info('{}: Creating archive{}'.format(repository, dry_run_label)) borg_create.create_archive( args.dry_run, repository, location, storage, local_path=local_path, remote_path=remote_path, ) if args.check and checks.repository_enabled_for_checks( repository, consistency): logger.info('{}: Running consistency checks'.format(repository)) borg_check.check_archives(repository, storage, consistency, local_path=local_path, remote_path=remote_path) if args.list: logger.info('{}: Listing archives'.format(repository)) output = borg_list.list_archives(repository, storage, local_path=local_path, remote_path=remote_path, json=args.json) if args.json: json_results.append(json.loads(output)) else: sys.stdout.write(output) if args.info: logger.info( '{}: Displaying summary info for archives'.format(repository)) output = borg_info.display_archives_info(repository, storage, local_path=local_path, remote_path=remote_path, json=args.json) if args.json: json_results.append(json.loads(output)) else: sys.stdout.write(output)
def test_list_archives_with_json_calls_borg_with_json_parameter(): flexmock(module).should_receive('execute_command').with_args( ('borg', 'list', '--json', 'repo'), output_log_level=None ).and_return('[]') json_output = module.list_archives( repository='repo', storage_config={}, list_arguments=flexmock(archive=None, json=True) ) assert json_output == '[]'
def run_actions( *, args, location, storage, retention, consistency, local_path, remote_path, repository_path ): # pragma: no cover ''' Given parsed command-line arguments as an argparse.ArgumentParser instance, several different configuration dicts, local and remote paths to Borg, and a repository name, run all actions from the command-line arguments on the given repository. Yield JSON output strings from executing any actions that produce JSON. ''' repository = os.path.expanduser(repository_path) dry_run_label = ' (dry run; not making any changes)' if args.dry_run else '' if args.init: logger.info('{}: Initializing repository'.format(repository)) borg_init.initialize_repository( repository, args.encryption_mode, args.append_only, args.storage_quota, local_path=local_path, remote_path=remote_path, ) if args.prune: logger.info('{}: Pruning archives{}'.format(repository, dry_run_label)) borg_prune.prune_archives( args.dry_run, repository, storage, retention, local_path=local_path, remote_path=remote_path, stats=args.stats, ) if args.create: logger.info('{}: Creating archive{}'.format(repository, dry_run_label)) json_output = borg_create.create_archive( args.dry_run, repository, location, storage, local_path=local_path, remote_path=remote_path, progress=args.progress, stats=args.stats, json=args.json, ) if json_output: yield json.loads(json_output) if args.check and checks.repository_enabled_for_checks(repository, consistency): logger.info('{}: Running consistency checks'.format(repository)) borg_check.check_archives( repository, storage, consistency, local_path=local_path, remote_path=remote_path ) if args.extract: if args.repository is None or repository == args.repository: logger.info('{}: Extracting archive {}'.format(repository, args.archive)) borg_extract.extract_archive( args.dry_run, repository, args.archive, args.restore_paths, location, storage, local_path=local_path, remote_path=remote_path, progress=args.progress, ) if args.list: if args.repository is None or repository == args.repository: logger.info('{}: Listing archives'.format(repository)) json_output = borg_list.list_archives( repository, storage, args.archive, local_path=local_path, remote_path=remote_path, json=args.json, ) if json_output: yield json.loads(json_output) if args.info: logger.info('{}: Displaying summary info for archives'.format(repository)) json_output = borg_info.display_archives_info( repository, storage, local_path=local_path, remote_path=remote_path, json=args.json ) if json_output: yield json.loads(json_output)
def run_actions(*, arguments, location, storage, retention, consistency, local_path, remote_path, repository_path): # pragma: no cover ''' Given parsed command-line arguments as an argparse.ArgumentParser instance, several different configuration dicts, local and remote paths to Borg, and a repository name, run all actions from the command-line arguments on the given repository. Yield JSON output strings from executing any actions that produce JSON. ''' repository = os.path.expanduser(repository_path) global_arguments = arguments['global'] dry_run_label = ' (dry run; not making any changes)' if global_arguments.dry_run else '' if 'init' in arguments: logger.info('{}: Initializing repository'.format(repository)) borg_init.initialize_repository( repository, arguments['init'].encryption_mode, arguments['init'].append_only, arguments['init'].storage_quota, local_path=local_path, remote_path=remote_path, ) if 'prune' in arguments: logger.info('{}: Pruning archives{}'.format(repository, dry_run_label)) borg_prune.prune_archives( global_arguments.dry_run, repository, storage, retention, local_path=local_path, remote_path=remote_path, stats=arguments['prune'].stats, ) if 'create' in arguments: logger.info('{}: Creating archive{}'.format(repository, dry_run_label)) json_output = borg_create.create_archive( global_arguments.dry_run, repository, location, storage, local_path=local_path, remote_path=remote_path, progress=arguments['create'].progress, stats=arguments['create'].stats, json=arguments['create'].json, ) if json_output: yield json.loads(json_output) if 'check' in arguments and checks.repository_enabled_for_checks( repository, consistency): logger.info('{}: Running consistency checks'.format(repository)) borg_check.check_archives( repository, storage, consistency, local_path=local_path, remote_path=remote_path, only_checks=arguments['check'].only, ) if 'extract' in arguments: if arguments['extract'].repository is None or repository == arguments[ 'extract'].repository: logger.info('{}: Extracting archive {}'.format( repository, arguments['extract'].archive)) borg_extract.extract_archive( global_arguments.dry_run, repository, arguments['extract'].archive, arguments['extract'].restore_paths, location, storage, local_path=local_path, remote_path=remote_path, progress=arguments['extract'].progress, ) if 'list' in arguments: if arguments['list'].repository is None or repository == arguments[ 'list'].repository: logger.info('{}: Listing archives'.format(repository)) json_output = borg_list.list_archives( repository, storage, list_arguments=arguments['list'], local_path=local_path, remote_path=remote_path, ) if json_output: yield json.loads(json_output) if 'info' in arguments: if arguments['info'].repository is None or repository == arguments[ 'info'].repository: logger.info( '{}: Displaying summary info for archives'.format(repository)) json_output = borg_info.display_archives_info( repository, storage, info_arguments=arguments['info'], local_path=local_path, remote_path=remote_path, ) if json_output: yield json.loads(json_output)
def run_configuration(config_filename, args): # pragma: no cover ''' Parse a single configuration file, and execute its defined pruning, backups, and/or consistency checks. ''' logger.info('{}: Parsing configuration file'.format(config_filename)) config = validate.parse_configuration(config_filename, validate.schema_filename()) (location, storage, retention, consistency, hooks) = (config.get(section_name, {}) for section_name in ('location', 'storage', 'retention', 'consistency', 'hooks')) try: local_path = location.get('local_path', 'borg') remote_path = location.get('remote_path') borg_create.initialize_environment(storage) hook.execute_hook(hooks.get('before_backup'), config_filename, 'pre-backup') for unexpanded_repository in location['repositories']: repository = os.path.expanduser(unexpanded_repository) dry_run_label = ' (dry run; not making any changes)' if args.dry_run else '' if args.prune: logger.info('{}: Pruning archives{}'.format( repository, dry_run_label)) borg_prune.prune_archives( args.verbosity, args.dry_run, repository, storage, retention, local_path=local_path, remote_path=remote_path, ) if args.create: logger.info('{}: Creating archive{}'.format( repository, dry_run_label)) borg_create.create_archive( args.verbosity, args.dry_run, repository, location, storage, local_path=local_path, remote_path=remote_path, ) if args.check: logger.info( '{}: Running consistency checks'.format(repository)) borg_check.check_archives( args.verbosity, repository, storage, consistency, local_path=local_path, remote_path=remote_path, ) if args.list: logger.info('{}: Listing archives'.format(repository)) borg_list.list_archives( args.verbosity, repository, storage, local_path=local_path, remote_path=remote_path, ) if args.info: logger.info('{}: Displaying summary info for archives'.format( repository)) borg_info.display_archives_info( args.verbosity, repository, storage, local_path=local_path, remote_path=remote_path, ) hook.execute_hook(hooks.get('after_backup'), config_filename, 'post-backup') except (OSError, CalledProcessError): hook.execute_hook(hooks.get('on_error'), config_filename, 'on-error') raise
def test_list_archives_with_local_path_calls_borg_via_local_path(): flexmock(module).should_receive('execute_command').with_args( ('borg1',) + LIST_COMMAND[1:], capture_output=False ) module.list_archives(repository='repo', storage_config={}, local_path='borg1')
def test_list_archives_with_remote_path_calls_borg_with_remote_path_parameters(): flexmock(module).should_receive('execute_command').with_args( LIST_COMMAND + ('--remote-path', 'borg1'), capture_output=False ) module.list_archives(repository='repo', storage_config={}, remote_path='borg1')