def test_execute_hook_with_multiple_commands_invokes_each_command(): subprocess = flexmock(module.subprocess) subprocess.should_receive('check_call').with_args(':', shell=True).once() subprocess.should_receive('check_call').with_args('true', shell=True).once() module.execute_hook([':', 'true'], 'config.yaml', 'pre-backup')
def run_configuration(config_filename, config, args): # pragma: no cover ''' Given a config filename and the corresponding parsed config dict, execute its defined pruning, backups, consistency checks, and/or other actions. ''' (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_environment.initialize(storage) if args.create: hook.execute_hook(hooks.get('before_backup'), config_filename, 'pre-backup') _run_commands( args=args, consistency=consistency, local_path=local_path, location=location, remote_path=remote_path, retention=retention, storage=storage, ) if args.create: 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 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) if args.create: hook.execute_hook(hooks.get('before_backup'), config_filename, 'pre-backup') _run_commands(args, consistency, local_path, location, remote_path, retention, storage) if args.create: 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 run_configuration(config_filename, config, args): # pragma: no cover ''' Given a config filename and the corresponding parsed config dict, execute its defined pruning, backups, consistency checks, and/or other actions. Yield JSON output strings from executing any actions that produce JSON. ''' (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_environment.initialize(storage) if args.create: hook.execute_hook( hooks.get('before_backup'), config_filename, 'pre-backup', args.dry_run ) for repository_path in location['repositories']: yield from run_actions( args=args, location=location, storage=storage, retention=retention, consistency=consistency, local_path=local_path, remote_path=remote_path, repository_path=repository_path, ) if args.create: hook.execute_hook( hooks.get('after_backup'), config_filename, 'post-backup', args.dry_run ) except (OSError, CalledProcessError): hook.execute_hook(hooks.get('on_error'), config_filename, 'on-error', args.dry_run) raise
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: remote_path = location.get('remote_path') 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) if args.prune: logger.info('{}: Pruning archives'.format(repository)) prune.prune_archives(args.verbosity, repository, retention, remote_path=remote_path) if args.create: logger.info('{}: Creating archive'.format(repository)) create.create_archive( args.verbosity, repository, location, storage, ) if args.check: logger.info('{}: Running consistency checks'.format(repository)) check.check_archives(args.verbosity, repository, consistency, 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_execute_hook_with_empty_commands_does_not_raise(): module.execute_hook([], 'config.yaml', 'post-backup')
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_execute_hook_invokes_each_command(): subprocess = flexmock(module.subprocess) subprocess.should_receive('check_call').with_args(':', shell=True).once() module.execute_hook([':'], 'config.yaml', 'pre-backup', dry_run=False)
def test_execute_hook_with_empty_commands_does_not_raise(): module.execute_hook([], 'config.yaml', 'post-backup', dry_run=False)
def test_execute_hook_with_dry_run_skips_commands(): subprocess = flexmock(module.subprocess) subprocess.should_receive('check_call').never() module.execute_hook([':', 'true'], 'config.yaml', 'pre-backup', dry_run=True)