refactor: move restore function
This commit is contained in:
parent
241fe3ce92
commit
f7207cdf36
63
backupbot.py
63
backupbot.py
@ -106,6 +106,38 @@ def create(retries):
|
|||||||
run_commands(post_commands)
|
run_commands(post_commands)
|
||||||
|
|
||||||
|
|
||||||
|
@cli.command()
|
||||||
|
@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
|
||||||
|
if SERVICE:
|
||||||
|
service_paths = service_paths + f'{SERVICE}_*'
|
||||||
|
snapshots = restic.snapshots(snapshot_id=snapshot)
|
||||||
|
if not snapshot:
|
||||||
|
logger.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':
|
||||||
|
logger.error("Restore aborted")
|
||||||
|
exit(1)
|
||||||
|
print(f"Restoring Snapshot {snapshot} of {service_paths} 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)
|
||||||
|
logger.debug(result)
|
||||||
|
|
||||||
|
|
||||||
def parse_backup_labels():
|
def parse_backup_labels():
|
||||||
client = docker.from_env()
|
client = docker.from_env()
|
||||||
container_by_service = {
|
container_by_service = {
|
||||||
@ -282,37 +314,6 @@ def path_exists(path):
|
|||||||
logger.error(f'{path} does not exist')
|
logger.error(f'{path} does not exist')
|
||||||
return path.exists()
|
return path.exists()
|
||||||
|
|
||||||
@cli.command()
|
|
||||||
@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
|
|
||||||
if SERVICE:
|
|
||||||
service_paths = service_paths + f'{SERVICE}_*'
|
|
||||||
snapshots = restic.snapshots(snapshot_id=snapshot)
|
|
||||||
if not snapshot:
|
|
||||||
logger.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':
|
|
||||||
logger.error("Restore aborted")
|
|
||||||
exit(1)
|
|
||||||
print(f"Restoring Snapshot {snapshot} of {service_paths} 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)
|
|
||||||
logger.debug(result)
|
|
||||||
|
|
||||||
|
|
||||||
@cli.command()
|
@cli.command()
|
||||||
def snapshots():
|
def snapshots():
|
||||||
|
Loading…
x
Reference in New Issue
Block a user