LibrePortal/scripts/backup/locations/location_migrate.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

107 lines
3.9 KiB
Bash

#!/bin/bash
# One-shot migration from the legacy single-file layout
# (configs/backup/backup_locations + configs/security/restic/loc_N.pass)
# to the per-location directory layout. Passwords now live as
# CFG_BACKUP_LOC_<idx>_PASSWORD inside the location.config — old .pass
# files get inlined and deleted.
#
# Idempotent — early-outs if the new layout is already present.
backupLocationsMigrate()
{
local new_dir
new_dir=$(backupLocationsDir)
local old_file="$configs_dir/backup/backup_locations"
# Already migrated? Detect by presence of at least one location.config.
if [[ -d "$new_dir" ]]; then
local existing
existing=$(sudo find "$new_dir" -mindepth 2 -maxdepth 2 -name location.config -type f 2>/dev/null | head -1)
if [[ -n "$existing" ]]; then
return 0
fi
fi
# Nothing to migrate AND no fresh seed in dev — skip silently.
if [[ ! -f "$old_file" ]]; then
return 0
fi
isHeader "Migrating backup locations to per-location directories"
local owner
owner=$(backupLocationOwner)
sudo mkdir -p "$new_dir"
sudo chown "$owner":"$owner" "$new_dir"
sudo chmod 0755 "$new_dir"
local indices
indices=$(sudo grep -oE '^CFG_BACKUP_LOC_[0-9]+_' "$old_file" 2>/dev/null | grep -oE '[0-9]+' | sort -nu)
if [[ -z "$indices" ]]; then
isNotice "No locations found in legacy file — archiving without migration"
fi
local idx
for idx in $indices; do
local loc_dir cfg_file
loc_dir=$(backupLocationDir "$idx")
cfg_file=$(backupLocationConfig "$idx")
sudo mkdir -p "$loc_dir"
sudo chown "$owner":"$owner" "$loc_dir"
sudo chmod 0700 "$loc_dir"
local old_pass="/docker/configs/security/restic/loc_${idx}.pass"
local pass_value=""
if [[ -f "$old_pass" ]]; then
pass_value=$(sudo cat "$old_pass" | tr -d '\n\r')
fi
[[ -z "$pass_value" ]] && pass_value="RANDOMIZEDPASSWORD1"
{
echo "# Backup location $idx — migrated $(date -Iseconds) from $old_file."
echo "# Edit via the Locations page on /backup, or directly here."
echo "CFG_BACKUP_LOC_${idx}_PASSWORD=${pass_value} # Repository Password - Used to encrypt/decrypt snapshots — back up offline!"
sudo grep "^CFG_BACKUP_LOC_${idx}_" "$old_file" | grep -v "^CFG_BACKUP_LOC_${idx}_PASSWORD="
} | sudo tee "$cfg_file" >/dev/null
sudo chown "$owner":"$owner" "$cfg_file"
sudo chmod 0640 "$cfg_file"
if [[ -f "$old_pass" ]]; then
sudo rm -f "$old_pass"
isNotice "Inlined password for location $idx into $cfg_file (old .pass removed)"
elif declare -f replacePlainPasswords >/dev/null 2>&1; then
replacePlainPasswords "$cfg_file"
fi
local old_kopia="/docker/configs/security/restic/kopia/loc_${idx}.config"
local new_kopia
new_kopia=$(backupLocationKopiaConfig "$idx")
if [[ -f "$old_kopia" ]]; then
sudo mv "$old_kopia" "$new_kopia"
sudo chown "$owner":"$owner" "$new_kopia"
sudo chmod 0600 "$new_kopia"
isNotice "Moved kopia state for location $idx$new_kopia"
fi
local old_ssh="/docker/configs/security/restic/ssh/loc_${idx}.key"
local new_ssh
new_ssh=$(backupLocationSshKey "$idx")
if [[ -f "$old_ssh" ]]; then
sudo mv "$old_ssh" "$new_ssh"
sudo chown "$owner":"$owner" "$new_ssh"
sudo chmod 0600 "$new_ssh"
isNotice "Moved SSH key for location $idx$new_ssh"
fi
isSuccessful "Location $idx migrated"
done
sudo mv "$old_file" "${old_file}.migrated"
isSuccessful "Migration complete — old file archived as ${old_file}.migrated"
# Re-source the new per-location configs so CFG vars reflect new state.
sourceBackupLocations
}