filter snapshots by app

This commit is contained in:
Moritz 2024-09-17 17:34:25 +02:00
parent 45af6e8b5e
commit 249772ec03

View File

@ -115,7 +115,7 @@ def restore(snapshot, target, noninteractive):
service_paths = VOLUME_PATH
if SERVICE:
service_paths = service_paths + f'{SERVICE}_*'
snapshots = restic.snapshots(snapshot_id=snapshot)
snapshots = get_snapshots(snapshot_id=snapshot, app=SERVICE)
if not snapshot:
logger.error("No Snapshots with ID {snapshots}")
exit(1)
@ -138,6 +138,22 @@ def restore(snapshot, target, noninteractive):
logger.debug(result)
def get_snapshots(snapshot_id=None, app=None):
if snapshot_id and snapshot_id != 'latest':
snapshots = restic.snapshots(snapshot_id=snapshot_id)
if app and app not in snapshots[0]['tags']:
logger.error(f'Snapshot with ID {snapshot_id} does not contain {app}')
exit(1)
else:
snapshots = restic.snapshots()
if app:
snapshots = list(filter(lambda x: app in x.get('tags'), snapshots))
if snapshot_id == 'latest':
return snapshots[-1:]
else:
return snapshots
def parse_backup_labels(hook_type='backup', selected_container=[]):
client = docker.from_env()
container_by_service = {
@ -321,13 +337,10 @@ def path_exists(path):
@cli.command()
def snapshots():
snapshots = restic.snapshots()
no_snapshots = True
snapshots = get_snapshots(app=SERVICE)
for snap in snapshots:
if not SERVICE or (tags := snap.get('tags')) and SERVICE in tags:
print(snap['time'], snap['id'])
no_snapshots = False
if no_snapshots:
print(snap['time'], snap['id'])
if not snapshots:
err_msg = "No Snapshots found"
if SERVICE:
service_name = SERVICE.replace('_', '.')