Prompt before restore

This commit is contained in:
Moritz 2023-10-03 22:04:15 +02:00
parent 9398e0d83d
commit ab6c06d423

View File

@ -7,6 +7,7 @@ import subprocess
import logging import logging
import docker import docker
import restic import restic
from datetime import datetime, timezone
from restic.errors import ResticFailedError from restic.errors import ResticFailedError
from pathlib import Path from pathlib import Path
from shutil import copyfile, rmtree from shutil import copyfile, rmtree
@ -143,12 +144,27 @@ def backup_volumes(backup_paths, apps, dry_run=False):
@cli.command() @cli.command()
@click.option('snapshot', '--snapshot', '-s', envvar='SNAPSHOT', default='latest') @click.option('snapshot', '--snapshot', '-s', envvar='SNAPSHOT', default='latest')
@click.option('target', '--target', '-t', envvar='TARGET', default='/') @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 # Todo: recommend to shutdown the container
service_paths = VOLUME_PATH service_paths = VOLUME_PATH
if SERVICE: if SERVICE:
service_paths = service_paths + f'{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, result = restic.restore(snapshot_id=snapshot,
include=service_paths, target_dir=target) include=service_paths, target_dir=target)
logging.debug(result) logging.debug(result)