LibrePortal/scripts/backup/engine/kopia_install.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.5 KiB
Bash

#!/bin/bash
kopiaInstall()
{
if command -v kopia >/dev/null 2>&1; then
local v
v=$(kopia --version 2>/dev/null | head -1)
isNotice "Kopia already installed ($v)"
return 0
fi
isHeader "Installing Kopia backup engine"
local arch
arch=$(uname -m)
case "$arch" in
x86_64) arch=x64 ;;
aarch64) arch=arm64 ;;
armv7l) arch=arm ;;
*) isError "Unsupported architecture for Kopia: $arch"; return 1 ;;
esac
local version url tmp
version=$(curl -sL https://api.github.com/repos/kopia/kopia/releases/latest 2>/dev/null \
| grep -oE '"tag_name":\s*"v[0-9.]+"' | head -1 | grep -oE '[0-9.]+')
if [[ -z "$version" ]]; then
isError "Could not look up the latest Kopia release. Install manually (https://kopia.io/) and re-run init."
return 1
fi
url="https://github.com/kopia/kopia/releases/download/v${version}/kopia-${version}-linux-${arch}.tar.gz"
tmp=$(mktemp -d)
if ! curl -sL "$url" -o "$tmp/kopia.tgz"; then
rm -rf "$tmp"
isError "Failed to download Kopia v${version} for ${arch}"
return 1
fi
runSystem tar xzf "$tmp/kopia.tgz" -C "$tmp"
local bin
bin=$(find "$tmp" -name kopia -type f -executable | head -1)
if [[ -z "$bin" ]]; then
rm -rf "$tmp"
isError "Kopia binary not found in archive"
return 1
fi
runSystem install -m 0755 "$bin" /usr/local/bin/kopia
rm -rf "$tmp"
checkSuccess "Kopia v${version} installed to /usr/local/bin/kopia"
}