From 9f3328781b1c6b8ea26645098328f2c8404465ab Mon Sep 17 00:00:00 2001 From: Dan Helfman Date: Wed, 6 Sep 2023 23:13:40 -0700 Subject: [PATCH] When "archive_name_format" is not set, filter archives using the default archive name format (#753). --- NEWS | 2 ++ borgmatic/borg/create.py | 5 +---- borgmatic/borg/flags.py | 16 +++++++++------- tests/unit/borg/test_flags.py | 11 ++++++++--- 4 files changed, 20 insertions(+), 14 deletions(-) diff --git a/NEWS b/NEWS index b52cfeae..3c89e467 100644 --- a/NEWS +++ b/NEWS @@ -2,6 +2,8 @@ * #743: Add a monitoring hook for sending backup status and logs to to Grafana Loki. See the documentation for more information: https://torsion.org/borgmatic/docs/how-to/monitor-your-backups/#loki-hook + * #753: When "archive_name_format" is not set, filter archives using the default archive name + format. * Update documentation to recommend installing/upgrading borgmatic with pipx instead of pip. See the documentation for more information: https://torsion.org/borgmatic/docs/how-to/set-up-backups/#installation diff --git a/borgmatic/borg/create.py b/borgmatic/borg/create.py index cb61d0ee..b09eac8f 100644 --- a/borgmatic/borg/create.py +++ b/borgmatic/borg/create.py @@ -215,9 +215,6 @@ def make_list_filter_flags(local_borg_version, dry_run): return f'{base_flags}-' -DEFAULT_ARCHIVE_NAME_FORMAT = '{hostname}-{now:%Y-%m-%dT%H:%M:%S.%f}' # noqa: FS003 - - def collect_borgmatic_source_directories(borgmatic_source_directory): ''' Return a list of borgmatic-specific source directories used for state like database backups. @@ -388,7 +385,7 @@ def create_archive( lock_wait = config.get('lock_wait', None) list_filter_flags = make_list_filter_flags(local_borg_version, dry_run) files_cache = config.get('files_cache') - archive_name_format = config.get('archive_name_format', DEFAULT_ARCHIVE_NAME_FORMAT) + archive_name_format = config.get('archive_name_format', flags.DEFAULT_ARCHIVE_NAME_FORMAT) extra_borg_options = config.get('extra_borg_options', {}).get('create', '') if feature.available(feature.Feature.ATIME, local_borg_version): diff --git a/borgmatic/borg/flags.py b/borgmatic/borg/flags.py index dc7e84d6..bcfadfa4 100644 --- a/borgmatic/borg/flags.py +++ b/borgmatic/borg/flags.py @@ -59,12 +59,15 @@ def make_repository_archive_flags(repository_path, archive, local_borg_version): ) +DEFAULT_ARCHIVE_NAME_FORMAT = '{hostname}-{now:%Y-%m-%dT%H:%M:%S.%f}' # noqa: FS003 + + def make_match_archives_flags(match_archives, archive_name_format, local_borg_version): ''' Return match archives flags based on the given match archives value, if any. If it isn't set, - return match archives flags to match archives created with the given archive name format, if - any. This is done by replacing certain archive name format placeholders for ephemeral data (like - "{now}") with globs. + return match archives flags to match archives created with the given (or default) archive name + format. This is done by replacing certain archive name format placeholders for ephemeral data + (like "{now}") with globs. ''' if match_archives: if feature.available(feature.Feature.MATCH_ARCHIVES, local_borg_version): @@ -72,10 +75,9 @@ def make_match_archives_flags(match_archives, archive_name_format, local_borg_ve else: return ('--glob-archives', re.sub(r'^sh:', '', match_archives)) - if not archive_name_format: - return () - - derived_match_archives = re.sub(r'\{(now|utcnow|pid)([:%\w\.-]*)\}', '*', archive_name_format) + derived_match_archives = re.sub( + r'\{(now|utcnow|pid)([:%\w\.-]*)\}', '*', archive_name_format or DEFAULT_ARCHIVE_NAME_FORMAT + ) if derived_match_archives == '*': return () diff --git a/tests/unit/borg/test_flags.py b/tests/unit/borg/test_flags.py index 804dd8f1..ba6cf2ea 100644 --- a/tests/unit/borg/test_flags.py +++ b/tests/unit/borg/test_flags.py @@ -88,8 +88,8 @@ def test_make_repository_archive_flags_with_borg_features_joins_repository_and_a @pytest.mark.parametrize( 'match_archives,archive_name_format,feature_available,expected_result', ( - (None, None, True, ()), - (None, '', True, ()), + (None, None, True, ('--match-archives', 'sh:{hostname}-*')), # noqa: FS003 + (None, '', True, ('--match-archives', 'sh:{hostname}-*')), # noqa: FS003 ( 're:foo-.*', '{hostname}-{now}', # noqa: FS003 @@ -145,7 +145,12 @@ def test_make_repository_archive_flags_with_borg_features_joins_repository_and_a True, (), ), - (None, '{utcnow}-docs-{user}', False, ('--glob-archives', '*-docs-{user}')), # noqa: FS003 + ( + None, + '{utcnow}-docs-{user}', # noqa: FS003 + False, + ('--glob-archives', '*-docs-{user}'), # noqa: FS003 + ), ), ) def test_make_match_archives_flags_makes_flags_with_globs(