restic-search.py
· 1.7 KiB · Python
Bruto
#!/usr/bin/env python3
import sys
from argparse import ArgumentParser
from json import loads
from pathlib import Path
from subprocess import PIPE, Popen
from sys import stderr
from typing import TypedDict
parser = ArgumentParser(prog="restic-search")
parser.add_argument("search", type=str, nargs="+")
parser.add_argument(
"-n", "--last", type=int, help="Only search through the n latest snapshots"
)
class Snapshot(TypedDict):
id: str
def restic_snapshots() -> list[Snapshot]:
ps = Popen(["restic", "snapshots", "--json"], stdout=PIPE)
stdout, _ = ps.communicate()
if ps.returncode != 0:
raise RuntimeError(
f"Error while running restic snapshots: returncode {ps.returncode}"
)
j = loads(stdout.decode("utf-8"))
return j
def restic_ls(snapshot_id: str) -> list[str]:
ps = Popen(["restic", "ls", snapshot_id], stdout=PIPE)
stdout, _ = ps.communicate()
if ps.returncode != 0:
raise RuntimeError(f"Error while running restic ls: returncode {ps.returncode}")
return stdout.decode("utf-8").split("\n")
if __name__ == "__main__":
args = parser.parse_args()
stderr.write("Listing snapshots...\n")
snapshots = restic_snapshots()
if args.last:
snapshots = snapshots[-args.last :]
results = []
for snapshot in snapshots:
stderr.write(f"Listing files in snapshot {snapshot['id']}...\n")
files = restic_ls(snapshot["id"])
for file in files:
path = Path(file)
for search in args.search:
if path.match(search):
results.append((search, file, snapshot["id"]))
for result in results:
print(f"{result[0]}\t{result[1]}\t{result[2]}")
| 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]}") |
| 58 |