Skip to content

Commit

Permalink
implement --match-archives user:USERNAME host:HOSTNAME
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasWaldmann committed Sep 26, 2024
1 parent d30fa6f commit b8b0514
Showing 1 changed file with 29 additions and 4 deletions.
33 changes: 29 additions & 4 deletions src/borg/manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class NoManifestError(Error):
exit_mcode = 26


ArchiveInfo = namedtuple("ArchiveInfo", "name id ts tags", defaults=[()])
ArchiveInfo = namedtuple("ArchiveInfo", "name id ts tags host user", defaults=[(), None, None])

# timestamp is a replacement for ts, archive is an alias for name (see SortBySpec)
AI_HUMAN_SORT_KEYS = ["timestamp", "archive"] + list(ArchiveInfo._fields)
Expand Down Expand Up @@ -129,6 +129,8 @@ def _get_archive_meta(self, id: bytes) -> dict:
time="1970-01-01T00:00:00.000000",
# new:
exists=False, # we have the pointer, but the repo does not have an archive item
username="",
hostname="",
tags=(),
)
else:
Expand Down Expand Up @@ -161,7 +163,14 @@ def _infos(self):

def _info_tuples(self):
for info in self._infos():
yield ArchiveInfo(name=info["name"], id=info["id"], ts=parse_timestamp(info["time"]), tags=info["tags"])
yield ArchiveInfo(
name=info["name"],
id=info["id"],
ts=parse_timestamp(info["time"]),
tags=info["tags"],
user=info["username"],
host=info["hostname"],
)

def _matching_info_tuples(self, match, match_end):
archive_infos = self._info_tuples()
Expand All @@ -176,6 +185,12 @@ def _matching_info_tuples(self, match, match_end):
wanted_tags = match.removeprefix("tags:")
wanted_tags = [tag for tag in wanted_tags.split(",") if tag] # remove empty tags
archive_infos = [x for x in archive_infos if set(x.tags) >= set(wanted_tags)]
elif match.startswith("user:"):
wanted_user = match.removeprefix("user:")
archive_infos = [x for x in archive_infos if x.user == wanted_user]
elif match.startswith("host:"):
wanted_host = match.removeprefix("host:")
archive_infos = [x for x in archive_infos if x.host == wanted_host]
else: # do a match on the name
regex = get_regex_from_pattern(match)
regex = re.compile(regex + match_end)
Expand Down Expand Up @@ -233,7 +248,12 @@ def _lookup_name(self, name, raw=False):
if not raw:
ts = parse_timestamp(archive_info["time"])
return ArchiveInfo(
name=archive_info["name"], id=archive_info["id"], ts=ts, tags=archive_info["tags"]
name=archive_info["name"],
id=archive_info["id"],
ts=ts,
tags=archive_info["tags"],
user=archive_info["username"],
host=archive_info["hostname"],
)
else:
return archive_info
Expand Down Expand Up @@ -267,7 +287,12 @@ def get_by_id(self, id, raw=False):
if not raw:
ts = parse_timestamp(archive_info["time"])
archive_info = ArchiveInfo(
name=archive_info["name"], id=archive_info["id"], ts=ts, tags=archive_info["tags"]
name=archive_info["name"],
id=archive_info["id"],
ts=ts,
tags=archive_info["tags"],
user=archive_info["username"],
host=archive_info["hostname"],
)
return archive_info
else:
Expand Down

0 comments on commit b8b0514

Please sign in to comment.