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