#!/usr/bin/env python3 from pathlib import Path from typing import Iterable, Set import click import docker import restic from docker.models.containers import Container from rich import print from rich.console import Console console = Console() client = docker.from_env() def stop_containers(names: Iterable[str]) -> Set[Container]: names = set(names) stopped_containers = set() for con in client.containers.list(): if con.name in names: stopped_containers.add(con) try: con.stop() except: print('Error') break else: print(f'Stopped [{con.short_id}] {con.name}') return stopped_containers def start_containers(containers: Iterable[Container]): for con in containers: try: con.start() except: print(f'Error starting: [{con.short_id}] {con.name}') else: print(f'Started: [{con.short_id}] {con.name}') @click.command() @click.argument('src', type=click.Path(exists=True, file_okay=False, readable=True, resolve_path=True, path_type=Path)) @click.option('-ds', '--docker-stop', type=click.STRING, help='Comma-delimited list of the container names to stop before running the backup') def main(src: Path, docker_stop: str = None): print(type(src).__name__, src) if docker_stop is not None: cons = stop_containers(docker_stop.split(',')) else: cons = set() with console.status(f'Running backup'): backup_result = restic.backup( paths=[src], dry_run=True ) print(backup_result) print(f'{len(restic.snapshots())} snapshots found in the repo') start_containers(cons) if __name__ == '__main__': main()