LibrePortal/scripts/backup/engine/kopia_snapshots.sh
librelad 875a60f90f LibrePortal v0.1.0 — initial release
A free, open, self-hosted app platform (GNU AGPLv3): one-click app deploys,
Traefik reverse proxy with automatic SSL, rootless Docker support, gluetun
VPN routing, and a web dashboard to manage it all.

Free & open forever to self-host; optional paid hosted services fund it.
See PROMISE.md.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

Signed-off-by: librelad <librelad@digitalangels.vip>
2026-05-21 20:37:54 +01:00

70 lines
1.7 KiB
Bash

#!/bin/bash
# Reshape `kopia snapshot list --json` to match the restic-style {short_id,
# time, hostname, tags, paths} schema the frontend consumes.
kopiaSnapshotsJson()
{
local idx="$1"
local app_filter="$2"
local host_filter="$3"
kopiaEnvExport "$idx" || return 1
local args=(snapshot list --all --json)
local raw
raw=$(sudo -E -u "$docker_install_user" kopia "${args[@]}" 2>/dev/null)
local rc=$?
kopiaEnvUnset
[[ $rc -ne 0 || -z "$raw" ]] && { echo "[]"; return $rc; }
local jq_filter='[.[] | {
id: .id,
short_id: (.id[0:8]),
time: .startTime,
hostname: .source.host,
tags: ((.tags // []) | map(sub(":"; "="))),
paths: [.source.path]
}]'
if [[ -n "$app_filter" ]]; then
jq_filter='[.[] | select(any(.tags[]?; . == "app:'"$app_filter"'"))] | '"$jq_filter"
fi
if [[ -n "$host_filter" ]]; then
jq_filter='[.[] | select(.source.host == "'"$host_filter"'")] | '"$jq_filter"
fi
if command -v jq >/dev/null 2>&1; then
echo "$raw" | jq -c "$jq_filter"
else
echo "$raw"
fi
}
kopiaSnapshotLatestId()
{
local idx="$1"
local app_name="$2"
local host="${3:-$CFG_INSTALL_NAME}"
local json
json=$(kopiaSnapshotsJson "$idx" "$app_name" "$host")
if command -v jq >/dev/null 2>&1; then
echo "$json" | jq -r 'sort_by(.time) | last | .id // empty'
else
echo "$json" | grep -oE '"id":\s*"[^"]+"' | tail -1 | cut -d'"' -f4
fi
}
kopiaSnapshotListFiles()
{
local idx="$1"
local snapshot_id="$2"
kopiaEnvExport "$idx" || return 1
sudo -E -u "$docker_install_user" kopia snapshot list "$snapshot_id" --json 2>/dev/null
local rc=$?
kopiaEnvUnset
return $rc
}