Beispiel #1
0
def test_extract_last_archive_dry_run_calls_borg_with_last_archive():
    insert_execute_command_output_mock(('borg', 'list', '--short', 'repo'),
                                       result='archive1\narchive2\n')
    insert_execute_command_mock(
        ('borg', 'extract', '--dry-run', 'repo::archive2'))

    module.extract_last_archive_dry_run(repository='repo', lock_wait=None)
Beispiel #2
0
def test_extract_last_archive_dry_run_without_any_archives_should_bail():
    flexmock(sys.stdout).encoding = 'utf-8'
    insert_subprocess_check_output_mock(('borg', 'list', '--short', 'repo'),
                                        result='\n'.encode('utf-8'))
    insert_subprocess_never()

    module.extract_last_archive_dry_run(repository='repo', lock_wait=None)
Beispiel #3
0
def check_archives(verbosity, repository, consistency_config, remote_path=None):
    '''
    Given a verbosity flag, a local or remote repository path, a consistency config dict, and a
    command to run, check the contained Borg archives for consistency.

    If there are no consistency checks to run, skip running them.
    '''
    checks = _parse_checks(consistency_config)
    check_last = consistency_config.get('check_last', None)

    if set(checks).intersection(set(DEFAULT_CHECKS)):
        remote_path_flags = ('--remote-path', remote_path) if remote_path else ()
        verbosity_flags = {
            VERBOSITY_SOME: ('--info',),
            VERBOSITY_LOTS: ('--debug',),
        }.get(verbosity, ())

        full_command = (
            'borg', 'check',
            repository,
        ) + _make_check_flags(checks, check_last) + remote_path_flags + verbosity_flags

        # The check command spews to stdout/stderr even without the verbose flag. Suppress it.
        stdout = None if verbosity_flags else open(os.devnull, 'w')

        logger.debug(' '.join(full_command))
        subprocess.check_call(full_command, stdout=stdout, stderr=subprocess.STDOUT)

    if 'extract' in checks:
        extract.extract_last_archive_dry_run(verbosity, repository, remote_path)
Beispiel #4
0
def check_archives(verbosity, repository, consistency_config, remote_path=None):
    '''
    Given a verbosity flag, a local or remote repository path, a consistency config dict, and a
    command to run, check the contained Borg archives for consistency.

    If there are no consistency checks to run, skip running them.
    '''
    checks = _parse_checks(consistency_config)
    check_last = consistency_config.get('check_last', None)

    if set(checks).intersection(set(DEFAULT_CHECKS)):
        remote_path_flags = ('--remote-path', remote_path) if remote_path else ()
        verbosity_flags = {
            VERBOSITY_SOME: ('--info',),
            VERBOSITY_LOTS: ('--debug',),
        }.get(verbosity, ())

        full_command = (
            'borg', 'check',
            repository,
        ) + _make_check_flags(checks, check_last) + remote_path_flags + verbosity_flags

        # The check command spews to stdout/stderr even without the verbose flag. Suppress it.
        stdout = None if verbosity_flags else open(os.devnull, 'w')

        subprocess.check_call(full_command, stdout=stdout, stderr=subprocess.STDOUT)

    if 'extract' in checks:
        extract.extract_last_archive_dry_run(verbosity, repository, remote_path)
Beispiel #5
0
def test_extract_last_archive_dry_run_should_call_borg_with_last_archive():
    flexmock(sys.stdout).encoding = 'utf-8'
    insert_subprocess_check_output_mock(
        ('borg', 'list', '--short', 'repo'),
        result='archive1\narchive2\n'.encode('utf-8'))
    insert_subprocess_mock(('borg', 'extract', '--dry-run', 'repo::archive2'))

    module.extract_last_archive_dry_run(repository='repo', lock_wait=None)
Beispiel #6
0
def test_extract_last_archive_dry_run_with_log_debug_calls_borg_with_debug_parameter(
):
    insert_execute_command_output_mock(
        ('borg', 'list', '--short', '--debug', '--show-rc', 'repo'),
        result='archive1\narchive2\n')
    insert_execute_command_mock(('borg', 'extract', '--dry-run', '--debug',
                                 '--show-rc', '--list', 'repo::archive2'))
    insert_logging_mock(logging.DEBUG)

    module.extract_last_archive_dry_run(repository='repo', lock_wait=None)
Beispiel #7
0
def check_archives(
    repository,
    storage_config,
    consistency_config,
    local_path='borg',
    remote_path=None,
    progress=None,
    repair=None,
    only_checks=None,
):
    '''
    Given a local or remote repository path, a storage config dict, a consistency config dict,
    local/remote commands to run, whether to include progress information, whether to attempt a
    repair, and an optional list of checks to use instead of configured checks, check the contained
    Borg archives for consistency.

    If there are no consistency checks to run, skip running them.
    '''
    checks = _parse_checks(consistency_config, only_checks)
    check_last = consistency_config.get('check_last', None)
    lock_wait = None
    extra_borg_options = storage_config.get('extra_borg_options',
                                            {}).get('check', '')

    if set(checks).intersection(set(DEFAULT_CHECKS + ('data', ))):
        lock_wait = storage_config.get('lock_wait', None)

        verbosity_flags = ()
        if logger.isEnabledFor(logging.INFO):
            verbosity_flags = ('--info', )
        if logger.isEnabledFor(logging.DEBUG):
            verbosity_flags = ('--debug', '--show-rc')

        prefix = consistency_config.get('prefix', DEFAULT_PREFIX)

        full_command = (
            (local_path, 'check') + (('--repair', ) if repair else ()) +
            _make_check_flags(checks, check_last, prefix) +
            (('--remote-path', remote_path) if remote_path else
             ()) + (('--lock-wait', str(lock_wait)) if lock_wait else
                    ()) + verbosity_flags + (('--progress', ) if progress else
                                             ()) +
            (tuple(extra_borg_options.split(' ')) if extra_borg_options else
             ()) + (repository, ))

        # The Borg repair option trigger an interactive prompt, which won't work when output is
        # captured. And progress messes with the terminal directly.
        if repair or progress:
            execute_command(full_command, output_file=DO_NOT_CAPTURE)
        else:
            execute_command(full_command)

    if 'extract' in checks:
        extract.extract_last_archive_dry_run(repository, lock_wait, local_path,
                                             remote_path)
Beispiel #8
0
def test_extract_last_archive_dry_run_with_log_debug_should_call_borg_with_debug_parameter(
):
    flexmock(sys.stdout).encoding = 'utf-8'
    insert_subprocess_check_output_mock(
        ('borg', 'list', '--short', 'repo', '--debug', '--show-rc'),
        result='archive1\narchive2\n'.encode('utf-8'),
    )
    insert_subprocess_mock(('borg', 'extract', '--dry-run', 'repo::archive2',
                            '--debug', '--show-rc', '--list'))
    insert_logging_mock(logging.DEBUG)

    module.extract_last_archive_dry_run(repository='repo', lock_wait=None)
Beispiel #9
0
def test_extract_last_archive_dry_run_calls_borg_with_remote_path_parameters():
    flexmock(sys.stdout).encoding = 'utf-8'
    insert_subprocess_check_output_mock(
        ('borg', 'list', '--short', 'repo', '--remote-path', 'borg1'),
        result='archive1\narchive2\n'.encode('utf-8'),
    )
    insert_subprocess_mock(('borg', 'extract', '--dry-run', 'repo::archive2',
                            '--remote-path', 'borg1'))

    module.extract_last_archive_dry_run(repository='repo',
                                        lock_wait=None,
                                        remote_path='borg1')
Beispiel #10
0
def test_extract_last_archive_dry_run_with_verbosity_lots_should_call_borg_with_debug_parameter(
):
    flexmock(sys.stdout).encoding = 'utf-8'
    insert_subprocess_check_output_mock(
        ('borg', 'list', '--short', 'repo', '--debug'),
        result='archive1\narchive2\n'.encode('utf-8'),
    )
    insert_subprocess_mock(('borg', 'extract', '--dry-run', 'repo::archive2',
                            '--debug', '--list'), )

    module.extract_last_archive_dry_run(
        verbosity=VERBOSITY_LOTS,
        repository='repo',
    )
Beispiel #11
0
def check_archives(verbosity,
                   repository,
                   storage_config,
                   consistency_config,
                   local_path='borg',
                   remote_path=None):
    '''
    Given a verbosity flag, a local or remote repository path, a storage config dict, a consistency
    config dict, and a local/remote commands to run, check the contained Borg archives for
    consistency.

    If there are no consistency checks to run, skip running them.
    '''
    checks = _parse_checks(consistency_config)
    check_last = consistency_config.get('check_last', None)
    lock_wait = None

    if set(checks).intersection(set(DEFAULT_CHECKS)):
        remote_path_flags = ('--remote-path',
                             remote_path) if remote_path else ()
        lock_wait = storage_config.get('lock_wait', None)
        lock_wait_flags = ('--lock-wait', str(lock_wait)) if lock_wait else ()
        verbosity_flags = {
            VERBOSITY_SOME: ('--info', ),
            VERBOSITY_LOTS: ('--debug', ),
        }.get(verbosity, ())

        prefix = consistency_config.get('prefix', '{hostname}-')
        prefix_flags = ('--prefix', prefix) if prefix else ()

        full_command = (
            local_path,
            'check',
            repository,
        ) + _make_check_flags(
            checks, check_last
        ) + prefix_flags + remote_path_flags + lock_wait_flags + verbosity_flags

        # The check command spews to stdout/stderr even without the verbose flag. Suppress it.
        stdout = None if verbosity_flags else open(os.devnull, 'w')

        logger.debug(' '.join(full_command))
        subprocess.check_call(full_command,
                              stdout=stdout,
                              stderr=subprocess.STDOUT)

    if 'extract' in checks:
        extract.extract_last_archive_dry_run(verbosity, repository, lock_wait,
                                             local_path, remote_path)
Beispiel #12
0
def check_archives(
    repository,
    storage_config,
    consistency_config,
    local_path='borg',
    remote_path=None,
    only_checks=None,
):
    '''
    Given a local or remote repository path, a storage config dict, a consistency config dict,
    local/remote commands to run, and an optional list of checks to use instead of configured
    checks, check the contained Borg archives for consistency.

    If there are no consistency checks to run, skip running them.
    '''
    checks = _parse_checks(consistency_config, only_checks)
    check_last = consistency_config.get('check_last', None)
    lock_wait = None

    if set(checks).intersection(set(DEFAULT_CHECKS + ('data', ))):
        remote_path_flags = ('--remote-path',
                             remote_path) if remote_path else ()
        lock_wait = storage_config.get('lock_wait', None)
        lock_wait_flags = ('--lock-wait', str(lock_wait)) if lock_wait else ()

        verbosity_flags = ()
        if logger.isEnabledFor(logging.INFO):
            verbosity_flags = ('--info', )
        if logger.isEnabledFor(logging.DEBUG):
            verbosity_flags = ('--debug', '--show-rc')

        prefix = consistency_config.get('prefix', DEFAULT_PREFIX)

        full_command = ((local_path, 'check') +
                        _make_check_flags(checks, check_last, prefix) +
                        remote_path_flags + lock_wait_flags + verbosity_flags +
                        (repository, ))

        execute_command(full_command)

    if 'extract' in checks:
        extract.extract_last_archive_dry_run(repository, lock_wait, local_path,
                                             remote_path)
Beispiel #13
0
def check_archives(
    repository, storage_config, consistency_config, local_path='borg', remote_path=None
):
    '''
    Given a local or remote repository path, a storage config dict, a consistency config dict,
    and a local/remote commands to run, check the contained Borg archives for consistency.

    If there are no consistency checks to run, skip running them.
    '''
    checks = _parse_checks(consistency_config)
    check_last = consistency_config.get('check_last', None)
    lock_wait = None

    if set(checks).intersection(set(DEFAULT_CHECKS)):
        remote_path_flags = ('--remote-path', remote_path) if remote_path else ()
        lock_wait = storage_config.get('lock_wait', None)
        lock_wait_flags = ('--lock-wait', str(lock_wait)) if lock_wait else ()

        verbosity_flags = ()
        if logger.isEnabledFor(logging.INFO):
            verbosity_flags = ('--info',)
        if logger.isEnabledFor(logging.DEBUG):
            verbosity_flags = ('--debug', '--show-rc')

        prefix = consistency_config.get('prefix')

        full_command = (
            (local_path, 'check', repository)
            + _make_check_flags(checks, check_last, prefix)
            + remote_path_flags
            + lock_wait_flags
            + verbosity_flags
        )

        # The check command spews to stdout/stderr even without the verbose flag. Suppress it.
        stdout = None if verbosity_flags else open(os.devnull, 'w')

        logger.debug(' '.join(full_command))
        subprocess.check_call(full_command, stdout=stdout, stderr=subprocess.STDOUT)

    if 'extract' in checks:
        extract.extract_last_archive_dry_run(repository, lock_wait, local_path, remote_path)
Beispiel #14
0
def test_extract_last_archive_dry_run_without_any_archives_should_not_raise():
    insert_execute_command_output_mock(('borg', 'list', '--short', 'repo'),
                                       result='\n')

    module.extract_last_archive_dry_run(repository='repo', lock_wait=None)