LibrePortal/scripts/backup/engine/kopia_restore.sh
librelad 226ebe1717 backup: give borg and kopia the same ownership mapping as restic
The user-namespace prefix that lets an unprivileged restore put back a file's
original owner was only wired into restic. borg extract and kopia snapshot
restore run as the same backup user with the same lack of CAP_CHOWN, so both
lost <container-uid>:<backup-user> exactly the way restic did — an app whose
data comes back owned by the backup user cannot write it, which is how grafana
kept dying with "attempt to write a readonly database".

borg is quieter about it than restic: it does not print an "ignoring error"
line at all, so there was nothing to notice.

Move the prefix to engine_dispatch.sh as backupUsernsPrefix — it was never
restic-specific — and use it from all three engines.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-28 06:20:31 +01:00

57 lines
1.8 KiB
Bash

#!/bin/bash
kopiaRestoreSnapshot()
{
local idx="$1"
local snapshot_id="$2"
local target_dir="$3"
local include_path="$4" # ignored — kopia restores by snapshot id directly
if [[ -z "$snapshot_id" || -z "$target_dir" ]]; then
isError "kopiaRestoreSnapshot requires snapshot_id and target_dir"
return 1
fi
kopiaEnvExport "$idx" || return 1
runFileOp mkdir -p "$target_dir"
isNotice "Restoring ${snapshot_id:0:12} from $(resticLocationName "$idx")$target_dir"
# Kopia's restore lays down the snapshot's source tree relative to target.
# If include_path was given (we always pass /docker/containers/<app>), we
# nest into that subpath under target so it matches restic's behaviour.
local final_target="$target_dir"
if [[ -n "$include_path" ]]; then
final_target="$target_dir/${include_path#/}"
runFileOp mkdir -p "$final_target"
fi
# Restore runs as the backup user with no CAP_CHOWN; without the namespace
# every <container-uid>:<backup-user> file comes back owned by the backup
# user and the app cannot write its own data. See restic-userns-exec.
local ns_prefix=()
mapfile -t ns_prefix < <(backupUsernsPrefix)
runBackupOp "${ns_prefix[@]}" kopia snapshot restore "$snapshot_id" "$final_target"
local rc=$?
kopiaEnvUnset
return $rc
}
kopiaDumpFile()
{
local idx="$1"
local snapshot_id="$2"
local file_path="$3"
local target_file="$4"
kopiaEnvExport "$idx" || return 1
# `kopia show` writes the file contents from a snapshot to stdout.
if [[ -n "$target_file" ]]; then
runBackupOp kopia show "${snapshot_id}:${file_path}" | runBackupOp tee "$target_file" >/dev/null
else
runBackupOp kopia show "${snapshot_id}:${file_path}"
fi
local rc=$?
kopiaEnvUnset
return $rc
}