LibrePortal/scripts/backup/engine/kopia_restore.sh
librelad 7acfdabbac refactor(de-sudo): backup subsystem data ops via runFileOp/runFileWrite
The backup engine already drops to the backup user (sudo -E -u
$docker_install_user) and backupLocationOwner == $docker_install_user, which is
exactly what runFileOp/runFileWrite resolve to in both modes. So convert the
raw-sudo data ops (mkdir/chmod/rm/find/cat/grep/mv/chown/tee on backup repos,
location configs, keys, manifests) to runFileOp/runFileWrite — creating files
as the owner directly, no root chown. backup_verify creates its scratch as the
backup user (runFileOp mktemp) instead of chown-after. Binary installs
(kopia tar/install, borg dnf) -> runSystem. The 44 sudo -u engine drops stay
(already least-privilege; the scoped sudoers will grant them).

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Signed-off-by: librelad <librelad@digitalangels.vip>
2026-05-24 17:01:05 +01:00

51 lines
1.6 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
sudo -E -u "$docker_install_user" 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
sudo -E -u "$docker_install_user" kopia show "${snapshot_id}:${file_path}" | sudo tee "$target_file" >/dev/null
else
sudo -E -u "$docker_install_user" kopia show "${snapshot_id}:${file_path}"
fi
local rc=$?
kopiaEnvUnset
return $rc
}