restore: execute hooks, filter for volumes and container

This commit is contained in:
Moritz 2024-09-17 17:49:37 +02:00
parent cc59087b8c
commit ac7c5fb50d

View File

@ -110,31 +110,36 @@ def create(retries):
@click.option('snapshot', '--snapshot', '-s', envvar='SNAPSHOT', default='latest')
@click.option('target', '--target', '-t', envvar='TARGET', default='/')
@click.option('noninteractive', '--noninteractive', envvar='NONINTERACTIVE', is_flag=True)
def restore(snapshot, target, noninteractive):
# Todo: recommend to shutdown the container
service_paths = VOLUME_PATH
@click.option('volumes', '--volumes', '-v', envvar='VOLUMES', multiple=True)
@click.option('container', '--container', '-c', envvar='CONTAINER', multiple=True)
def restore(snapshot, target, noninteractive, volumes, container):
app_settings = parse_backup_labels('restore', container)
if SERVICE:
service_paths = service_paths + f'{SERVICE}_*'
app_settings = {SERVICE: app_settings[SERVICE]}
pre_commands, post_commands, backup_paths, apps = get_backup_details(app_settings, volumes)
snapshots = get_snapshots(snapshot_id=snapshot, app=SERVICE)
if not snapshot:
logger.error("No Snapshots with ID {snapshots}")
logger.error("No Snapshots with ID {snapshots} for {apps} found.")
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"You are going to restore Snapshot {snapshot} of {apps} at {target}")
print("The following volume paths will be restored:")
for p in backup_paths:
print(f'\t{p}')
print(f"This snapshot is {delta} old")
print(
f"THIS COMMAND WILL IRREVERSIBLY OVERWRITES {target}{service_paths.removeprefix('/')}")
f"THIS COMMAND WILL IRREVERSIBLY OVERWRITES FILES AT {target}")
prompt = input("Type YES (uppercase) to continue: ")
if prompt != 'YES':
logger.error("Restore aborted")
exit(1)
print(f"Restoring Snapshot {snapshot} of {service_paths} at {target}")
print(f"Restoring Snapshot {snapshot} at {target}")
# TODO: use tags if no snapshot is selected, to use a snapshot including SERVICE
result = restic.restore(snapshot_id=snapshot,
include=service_paths, target_dir=target)
run_commands(pre_commands)
result = restic_restore(snapshot_id=snapshot, include=backup_paths, target_dir=target)
run_commands(post_commands)
logger.debug(result)