diff --git a/backupbot.py b/backupbot.py index 30ce01b..a15d848 100755 --- a/backupbot.py +++ b/backupbot.py @@ -7,6 +7,7 @@ import subprocess import logging import docker import restic +from datetime import datetime, timezone from restic.errors import ResticFailedError from pathlib import Path from shutil import copyfile, rmtree @@ -143,12 +144,27 @@ def backup_volumes(backup_paths, apps, dry_run=False): @cli.command() @click.option('snapshot', '--snapshot', '-s', envvar='SNAPSHOT', default='latest') @click.option('target', '--target', '-t', envvar='TARGET', default='/') -def restore(snapshot, target): +@click.option('noninteractive', '--noninteractive', envvar='NONINTERACTIVE', default=False) +def restore(snapshot, target, noninteractive): # Todo: recommend to shutdown the container service_paths = VOLUME_PATH if SERVICE: service_paths = service_paths + f'{SERVICE}_*' - print(f"restoring Snapshot {snapshot} of {service_paths} at {target}") + snapshots = restic.snapshots(snapshot_id=snapshot) + if not snapshot: + logging.error("No Snapshots with ID {snapshots}") + exit(1) + if not noninteractive: + snapshot_date = datetime.fromisoformat(snapshots[0]['time']) + delta = datetime.now(tz=timezone.utc) - snapshot_date + print(f"You are going to restore Snapshot {snapshot} of {service_paths} at {target}") + print(f"This snapshot is {delta} old") + print(f"THIS COMMAND WILL IRREVERSIBLY OVERWRITES {target}{service_paths.removeprefix('/')}") + prompt = input("Type YES (uppercase) to continue: ") + if prompt != 'YES': + logging.error("Restore aborted") + exit(1) + print(f"Restoring Snapshot {snapshot} of {service_paths} at {target}") result = restic.restore(snapshot_id=snapshot, include=service_paths, target_dir=target) logging.debug(result)