Последняя активность 4 hours ago

A python script to search through multiple (or all) snapshots of restic for a specified path

adridoesthings's Avatar adridoesthings ревизий этого фрагмента 4 hours ago. К ревизии

1 file changed, 57 insertions

restic-search.py(файл создан)

@@ -0,0 +1,57 @@
1 + #!/usr/bin/env python3
2 + import sys
3 + from argparse import ArgumentParser
4 + from json import loads
5 + from pathlib import Path
6 + from subprocess import PIPE, Popen
7 + from sys import stderr
8 + from typing import TypedDict
9 +
10 + parser = ArgumentParser(prog="restic-search")
11 + parser.add_argument("search", type=str, nargs="+")
12 + parser.add_argument(
13 + "-n", "--last", type=int, help="Only search through the n latest snapshots"
14 + )
15 +
16 +
17 + class Snapshot(TypedDict):
18 + id: str
19 +
20 +
21 + def restic_snapshots() -> list[Snapshot]:
22 + ps = Popen(["restic", "snapshots", "--json"], stdout=PIPE)
23 + stdout, _ = ps.communicate()
24 + if ps.returncode != 0:
25 + raise RuntimeError(
26 + f"Error while running restic snapshots: returncode {ps.returncode}"
27 + )
28 + j = loads(stdout.decode("utf-8"))
29 + return j
30 +
31 +
32 + def restic_ls(snapshot_id: str) -> list[str]:
33 + ps = Popen(["restic", "ls", snapshot_id], stdout=PIPE)
34 + stdout, _ = ps.communicate()
35 + if ps.returncode != 0:
36 + raise RuntimeError(f"Error while running restic ls: returncode {ps.returncode}")
37 + return stdout.decode("utf-8").split("\n")
38 +
39 +
40 + if __name__ == "__main__":
41 + args = parser.parse_args()
42 + stderr.write("Listing snapshots...\n")
43 + snapshots = restic_snapshots()
44 + if args.last:
45 + snapshots = snapshots[-args.last :]
46 + results = []
47 + for snapshot in snapshots:
48 + stderr.write(f"Listing files in snapshot {snapshot['id']}...\n")
49 + files = restic_ls(snapshot["id"])
50 + for file in files:
51 + path = Path(file)
52 + for search in args.search:
53 + if path.match(search):
54 + results.append((search, file, snapshot["id"]))
55 +
56 + for result in results:
57 + print(f"{result[0]}\t{result[1]}\t{result[2]}")
Новее Позже