From 73d67e29b4967fcbfaf28384468c64147e2d1046 Mon Sep 17 00:00:00 2001 From: Felix Buehler Date: Thu, 6 Dec 2018 23:58:14 +0100 Subject: [PATCH 1/2] Support for Borg create & prune --stats via borgmatic command-line flag (#100) --- borgmatic/borg/create.py | 2 ++ borgmatic/borg/prune.py | 9 ++++++++- borgmatic/commands/borgmatic.py | 12 ++++++++++++ scripts/find-unsupported-borg-options | 1 + tests/integration/commands/test_borgmatic.py | 13 +++++++++++++ 5 files changed, 36 insertions(+), 1 deletion(-) diff --git a/borgmatic/borg/create.py b/borgmatic/borg/create.py index a4463802..818ccac4 100644 --- a/borgmatic/borg/create.py +++ b/borgmatic/borg/create.py @@ -91,6 +91,7 @@ def create_archive( local_path='borg', remote_path=None, progress=False, + stats=False, json=False, ): ''' @@ -139,6 +140,7 @@ def create_archive( + (('--debug', '--show-rc') if logger.isEnabledFor(logging.DEBUG) else ()) + (('--dry-run',) if dry_run else ()) + (('--progress',) if progress else ()) + + (('--stats',) if stats else ()) + (('--json',) if json else ()) ) diff --git a/borgmatic/borg/prune.py b/borgmatic/borg/prune.py index fa1277d5..54e4ba5a 100644 --- a/borgmatic/borg/prune.py +++ b/borgmatic/borg/prune.py @@ -31,7 +31,13 @@ def _make_prune_flags(retention_config): def prune_archives( - dry_run, repository, storage_config, retention_config, local_path='borg', remote_path=None + dry_run, + repository, + storage_config, + retention_config, + local_path='borg', + remote_path=None, + stats=False, ): ''' Given dry-run flag, a local or remote repository path, a storage config dict, and a @@ -51,6 +57,7 @@ def prune_archives( + (('--info',) if logger.getEffectiveLevel() == logging.INFO else ()) + (('--debug', '--list', '--show-rc') if logger.isEnabledFor(logging.DEBUG) else ()) + (('--dry-run',) if dry_run else ()) + + (('--stats',) if stats else ()) ) logger.debug(' '.join(full_command)) diff --git a/borgmatic/commands/borgmatic.py b/borgmatic/commands/borgmatic.py index 6be39333..09b66ae5 100644 --- a/borgmatic/commands/borgmatic.py +++ b/borgmatic/commands/borgmatic.py @@ -107,6 +107,13 @@ def parse_arguments(*arguments): action='store_true', help='Display progress with --create option for each file as it is backed up', ) + parser.add_argument( + '--stats', + dest='stats', + default=False, + action='store_true', + help='Display status with --create or --prune option for each file as it is backed up', + ) parser.add_argument( '--json', dest='json', @@ -152,6 +159,9 @@ def parse_arguments(*arguments): if args.progress and not args.create: raise ValueError('The --progress option can only be used with the --create option') + if args.stats and not (args.create or args.prune): + raise ValueError('The --stats option can only be used with the --create, or --prune options') + if args.json and not (args.create or args.list or args.info): raise ValueError( 'The --json option can only be used with the --create, --list, or --info options' @@ -261,6 +271,7 @@ def _run_commands_on_repository( retention, local_path=local_path, remote_path=remote_path, + stats=args.stats, ) if args.create: logger.info('{}: Creating archive{}'.format(repository, dry_run_label)) @@ -272,6 +283,7 @@ def _run_commands_on_repository( local_path=local_path, remote_path=remote_path, progress=args.progress, + stats=args.stats, ) if args.check and checks.repository_enabled_for_checks(repository, consistency): logger.info('{}: Running consistency checks'.format(repository)) diff --git a/scripts/find-unsupported-borg-options b/scripts/find-unsupported-borg-options index 8b98142a..ee66ed79 100755 --- a/scripts/find-unsupported-borg-options +++ b/scripts/find-unsupported-borg-options @@ -41,6 +41,7 @@ for sub_command in prune create check list info; do | grep -v '^--nobsdflags$' \ | grep -v '^--pattern$' \ | grep -v '^--progress$' \ + | grep -v '^--stats$' \ | grep -v '^--read-special$' \ | grep -v '^--repository-only$' \ | grep -v '^--show-rc$' \ diff --git a/tests/integration/commands/test_borgmatic.py b/tests/integration/commands/test_borgmatic.py index cbbd4a02..82058aa1 100644 --- a/tests/integration/commands/test_borgmatic.py +++ b/tests/integration/commands/test_borgmatic.py @@ -143,6 +143,19 @@ def test_parse_arguments_disallows_progress_without_create(): module.parse_arguments('--progress', '--list') +def test_parse_arguments_with_stats_and_create_flags_does_not_raise(): + module.parse_arguments('--stats', '--create', '--list') + + +def test_parse_arguments_with_stats_and_prune_flags_does_not_raise(): + module.parse_arguments('--stats', '--prune', '--list') + + +def test_parse_arguments_with_stats_flag_but_no_create_or_prune_flag_raises_value_error(): + with pytest.raises(ValueError): + module.parse_arguments('--stats', '--list') + + def test_parse_arguments_allows_json_with_list_or_info(): module.parse_arguments('--list', '--json') module.parse_arguments('--info', '--json') From 7516443a8968a9dc4953596a96042b816e4eece5 Mon Sep 17 00:00:00 2001 From: Felix Buehler Date: Mon, 10 Dec 2018 12:37:24 +0100 Subject: [PATCH 2/2] fix changes requested about stats --- borgmatic/borg/create.py | 3 +-- borgmatic/commands/borgmatic.py | 6 ++++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/borgmatic/borg/create.py b/borgmatic/borg/create.py index 818ccac4..70151e24 100644 --- a/borgmatic/borg/create.py +++ b/borgmatic/borg/create.py @@ -136,11 +136,10 @@ def create_archive( + (('--lock-wait', str(lock_wait)) if lock_wait else ()) + (('--list', '--filter', 'AME-') if logger.isEnabledFor(logging.INFO) else ()) + (('--info',) if logger.getEffectiveLevel() == logging.INFO else ()) - + (('--stats',) if not dry_run and logger.isEnabledFor(logging.INFO) else ()) + + (('--stats',) if not dry_run and (logger.isEnabledFor(logging.INFO) or stats) else ()) + (('--debug', '--show-rc') if logger.isEnabledFor(logging.DEBUG) else ()) + (('--dry-run',) if dry_run else ()) + (('--progress',) if progress else ()) - + (('--stats',) if stats else ()) + (('--json',) if json else ()) ) diff --git a/borgmatic/commands/borgmatic.py b/borgmatic/commands/borgmatic.py index 09b66ae5..e4aab495 100644 --- a/borgmatic/commands/borgmatic.py +++ b/borgmatic/commands/borgmatic.py @@ -112,7 +112,7 @@ def parse_arguments(*arguments): dest='stats', default=False, action='store_true', - help='Display status with --create or --prune option for each file as it is backed up', + help='Display statistics of archive with --create or --prune option', ) parser.add_argument( '--json', @@ -160,7 +160,9 @@ def parse_arguments(*arguments): raise ValueError('The --progress option can only be used with the --create option') if args.stats and not (args.create or args.prune): - raise ValueError('The --stats option can only be used with the --create, or --prune options') + raise ValueError( + 'The --stats option can only be used with the --create, or --prune options' + ) if args.json and not (args.create or args.list or args.info): raise ValueError(