refactor: move backup/restore worker scripts to extras/ as source files
Replace embedded heredocs in the three backup service installers with cp from versioned source files in extras/: extras/backup_kopia.sh — Kopia worker (was inline in services/backup.sh) extras/backup_borg.sh — Borg worker (was inline in services/borg-backup.sh) extras/backup_gaming.sh — gaming saves worker (was inline in services/gaming-backup.sh) extras/restore_kopia.sh — unified Kopia restore (multi-dest + single-dest) extras/restore_borg.sh — unified Borg restore with destination picker Each installer now does `cp extras/<script>.sh $DIR/<script>.sh` instead of writing the script inline. Workers and restore scripts are now readable in the repo rather than buried in heredocs. Restore scripts are installed flat into the service directory root: ~/docker/backup/backup_kopia.sh ~/docker/backup/restore_kopia.sh ~/docker/borg-backup/backup_borg.sh ~/docker/borg-backup/restore_borg.sh ~/docker/gaming-backup/backup_gaming.sh ~/docker/gaming-backup/restore_kopia.sh The new restore scripts handle destination selection internally, so a single script replaces the old per-destination restore/<dest>/ layout. Also fixes `local` used outside a function in restore_kopia.sh and restore_borg.sh (destination picker loop), and removes the now-superseded extras/restore_kopia_backup.sh and extras/restore_borg_backup.sh. https://claude.ai/code/session_019XgsQ13XKm4Zj3cNsDNwHj
This commit is contained in:
+21
-183
@@ -11,9 +11,9 @@
|
||||
# Vorta GUI, SSH remote repos out of the box, widely packaged.
|
||||
#
|
||||
# Creates: ~/docker/borg-backup/
|
||||
# backup.conf settings + per-dest repo/passphrase (chmod 600)
|
||||
# borg-backup.sh worker (run directly or via systemd timer)
|
||||
# restore/<dest>/ restore_borg_backup.sh + backup.conf per destination
|
||||
# backup.conf settings + per-dest repo/passphrase (chmod 600)
|
||||
# backup_borg.sh worker (run directly or via systemd timer)
|
||||
# restore_borg.sh interactive restore helper
|
||||
|
||||
register_service borg-backup backup "Encrypted backup of all Docker services via Borg"
|
||||
|
||||
@@ -22,9 +22,8 @@ install_borg_backup() {
|
||||
|
||||
local DIR="$DOCKER_DIR/borg-backup"
|
||||
local CONF_FILE="$DIR/backup.conf"
|
||||
local WORKER="$DIR/borg-backup.sh"
|
||||
local RESTORE_DIR="$DIR/restore"
|
||||
local RESTORE_SRC="${HERE:-}/extras/restore_borg_backup.sh"
|
||||
local WORKER="$DIR/backup_borg.sh"
|
||||
local RESTORE="$DIR/restore_borg.sh"
|
||||
local SVC_NAME="post-install-borg-backup"
|
||||
|
||||
echo ""
|
||||
@@ -193,7 +192,7 @@ install_borg_backup() {
|
||||
KEEP_MONTHLY="${KEEP_MONTHLY:-3}"
|
||||
|
||||
# ── 7. Create dirs + init Borg repos ─────────────────────────────────────
|
||||
mkdir -p "$DIR" "$RESTORE_DIR"
|
||||
mkdir -p "$DIR"
|
||||
ensure_docker_dir_ownership "$DIR"
|
||||
|
||||
local repo pw
|
||||
@@ -232,7 +231,7 @@ install_borg_backup() {
|
||||
echo "# ── backup.conf ────────────────────────────────────────────────────────────"
|
||||
echo "# Generated $(date '+%F %T'). Safe to hand-edit."
|
||||
echo "# Worker : sudo $WORKER"
|
||||
echo "# Restore: sudo $RESTORE_DIR/<dest>/restore_borg_backup.sh"
|
||||
echo "# Restore: sudo $RESTORE"
|
||||
echo ""
|
||||
echo "BORG=\"$BORG_BIN\""
|
||||
echo ""
|
||||
@@ -268,182 +267,23 @@ install_borg_backup() {
|
||||
chmod 600 "$CONF_FILE"
|
||||
log_success "backup.conf written (chmod 600)"
|
||||
|
||||
# ── 9. Generate worker script ─────────────────────────────────────────────
|
||||
log_info "Writing worker $WORKER ..."
|
||||
cat > "$WORKER" << 'WORKEREOF'
|
||||
#!/bin/bash
|
||||
# Generated by the borg-backup installer.
|
||||
# Backs up full ~/docker/<service>/ directories via Borg.
|
||||
# Minecraft instances: flush to disk (save-all) then archive — no downtime.
|
||||
# All other services: stop → archive → restart for consistency.
|
||||
#
|
||||
# sudo ./borg-backup.sh run a full backup cycle
|
||||
# sudo ./borg-backup.sh list list all archives in all repos
|
||||
# sudo ./borg-backup.sh info show repo info for all destinations
|
||||
#
|
||||
# Reads backup.conf from the same directory.
|
||||
set -uo pipefail
|
||||
|
||||
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
CONF="${BACKUP_CONF:-$HERE/backup.conf}"
|
||||
[ -f "$CONF" ] || { echo "Config not found: $CONF (re-run the borg-backup service)"; exit 1; }
|
||||
# shellcheck source=/dev/null
|
||||
source "$CONF"
|
||||
|
||||
ACTUAL_USER="${SUDO_USER:-${USER:-$(id -un)}}"
|
||||
ACTUAL_HOME="$(getent passwd "$ACTUAL_USER" 2>/dev/null | cut -d: -f6 || echo "/home/$ACTUAL_USER")"
|
||||
DOCKER_DIR="$ACTUAL_HOME/docker"
|
||||
|
||||
log() { echo "[$(date '+%F %T')] $*"; }
|
||||
|
||||
repo_for() {
|
||||
local var="DEST_${1}_REPO"; echo "${!var:-}"
|
||||
}
|
||||
pass_for() {
|
||||
local var="DEST_${1}_PASSPHRASE"; echo "${!var:-}"
|
||||
}
|
||||
dest_for_svc() {
|
||||
local var="SVC_${1//-/_}"; echo "${!var:-${DEST_DEFAULT:-default}}"
|
||||
}
|
||||
|
||||
b_for() {
|
||||
local dest="$1"; shift
|
||||
local repo; repo="$(repo_for "$dest")"
|
||||
local pass; pass="$(pass_for "$dest")"
|
||||
[ -n "$repo" ] || { log "Unknown destination: $dest"; return 1; }
|
||||
BORG_PASSPHRASE="$pass" BORG_REPO="$repo" "$BORG" "$@"
|
||||
}
|
||||
|
||||
is_minecraft() { [ -f "${1}Dockerfile" ] && grep -qs itzg "${1}Dockerfile"; }
|
||||
|
||||
case "${1:-run}" in
|
||||
list)
|
||||
for dest in ${DEST_NAMES:-default}; do
|
||||
echo ""; echo "── dest: $dest ($(repo_for "$dest")) ──"
|
||||
b_for "$dest" list 2>/dev/null | sort -r | head -30 || true
|
||||
done
|
||||
exit 0 ;;
|
||||
info)
|
||||
for dest in ${DEST_NAMES:-default}; do
|
||||
echo ""; echo "── dest: $dest ──"
|
||||
b_for "$dest" info 2>/dev/null || true
|
||||
done
|
||||
exit 0 ;;
|
||||
esac
|
||||
|
||||
log "===== Borg backup starting ====="
|
||||
rc=0
|
||||
TS="$(date +%Y-%m-%dT%H-%M-%S)"
|
||||
|
||||
for svc_dir in "$DOCKER_DIR"/*/; do
|
||||
[ -f "${svc_dir}docker-compose.yml" ] || continue
|
||||
svc="$(basename "$svc_dir")"
|
||||
[[ "$svc" == "borg-backup" || "$svc" == "backup" || "$svc" == "gaming-backup" ]] && continue
|
||||
|
||||
dest="$(dest_for_svc "$svc")"
|
||||
repo="$(repo_for "$dest")"
|
||||
[ -n "$repo" ] || { log "SKIP $svc — dest '$dest' not configured"; continue; }
|
||||
|
||||
ARCHIVE="${svc}-${TS}"
|
||||
|
||||
if is_minecraft "$svc_dir"; then
|
||||
# ── Minecraft: flush world to disk, archive without stopping ─────────
|
||||
if docker ps --format '{{.Names}}' 2>/dev/null | grep -qx "$svc"; then
|
||||
log "Flushing Minecraft world '$svc' (save-all, no downtime)..."
|
||||
docker exec "$svc" mc-send-to-console save-all flush 2>/dev/null \
|
||||
|| docker exec "$svc" rcon-cli save-all 2>/dev/null || true
|
||||
sleep 5
|
||||
fi
|
||||
log "Archiving $svc → $dest::$ARCHIVE ..."
|
||||
if b_for "$dest" create \
|
||||
--compression=zstd,6 \
|
||||
--exclude-caches \
|
||||
--stats \
|
||||
"::$ARCHIVE" "$svc_dir" 2>&1 | while IFS= read -r line; do log " $line"; done; then
|
||||
log "OK $svc (Minecraft, no downtime)"
|
||||
else
|
||||
log "WARNING: archive failed for $svc"; rc=1
|
||||
fi
|
||||
else
|
||||
# ── All other services: stop → archive → restart ──────────────────
|
||||
STOPPED=false
|
||||
if docker ps --format '{{.Names}}' 2>/dev/null | grep -qx "$svc"; then
|
||||
log "Stopping $svc..."
|
||||
docker compose -f "${svc_dir}docker-compose.yml" down 2>/dev/null \
|
||||
|| docker stop "$svc" 2>/dev/null \
|
||||
|| log "WARNING: could not stop $svc — archiving live (consistency not guaranteed)"
|
||||
STOPPED=true
|
||||
fi
|
||||
|
||||
log "Archiving $svc → $dest::$ARCHIVE ..."
|
||||
if b_for "$dest" create \
|
||||
--compression=zstd,6 \
|
||||
--exclude-caches \
|
||||
--stats \
|
||||
"::$ARCHIVE" "$svc_dir" 2>&1 | while IFS= read -r line; do log " $line"; done; then
|
||||
log "OK $svc"
|
||||
else
|
||||
log "WARNING: archive failed for $svc"; rc=1
|
||||
fi
|
||||
|
||||
if [ "$STOPPED" = true ]; then
|
||||
log "Starting $svc..."
|
||||
docker compose -f "${svc_dir}docker-compose.yml" up -d 2>/dev/null \
|
||||
|| log "WARNING: could not restart $svc — run: docker compose -f ${svc_dir}docker-compose.yml up -d"
|
||||
fi
|
||||
fi
|
||||
|
||||
# Prune old archives for this service in its destination repo
|
||||
log "Pruning old archives for $svc in '$dest'..."
|
||||
b_for "$dest" prune \
|
||||
--keep-daily="${KEEP_DAILY:-7}" \
|
||||
--keep-weekly="${KEEP_WEEKLY:-4}" \
|
||||
--keep-monthly="${KEEP_MONTHLY:-3}" \
|
||||
--glob-archives="${svc}-*" \
|
||||
--list 2>/dev/null \
|
||||
|| log "WARNING: prune failed for $svc (non-fatal)"
|
||||
done
|
||||
|
||||
# Compact each repo to reclaim space freed by pruning
|
||||
for dest in ${DEST_NAMES:-default}; do
|
||||
repo="$(repo_for "$dest")"
|
||||
[ -n "$repo" ] || continue
|
||||
log "Compacting repo '$dest'..."
|
||||
b_for "$dest" compact 2>/dev/null || true
|
||||
done
|
||||
|
||||
if [ "$rc" -eq 0 ]; then
|
||||
log "===== Borg backup complete ====="
|
||||
else
|
||||
log "===== Borg backup finished WITH WARNINGS (see above) ====="
|
||||
fi
|
||||
exit "$rc"
|
||||
WORKEREOF
|
||||
# ── 9. Install worker script ──────────────────────────────────────────────
|
||||
log_info "Installing worker $WORKER ..."
|
||||
cp "${HERE:-}/extras/backup_borg.sh" "$WORKER"
|
||||
chmod +x "$WORKER"
|
||||
chown root:root "$WORKER" 2>/dev/null || true
|
||||
log_success "borg-backup.sh written"
|
||||
log_success "backup_borg.sh installed"
|
||||
|
||||
# ── 10. Restore scripts (one per destination) ─────────────────────────────
|
||||
# ── 10. Install restore script ────────────────────────────────────────────
|
||||
local RESTORE_SRC="${HERE:-}/extras/restore_borg.sh"
|
||||
if [ -f "$RESTORE_SRC" ]; then
|
||||
for dn in "${DEST_NAMES_ARR[@]}"; do
|
||||
local dest_rdir="$RESTORE_DIR/$dn"
|
||||
mkdir -p "$dest_rdir"
|
||||
cp "$RESTORE_SRC" "$dest_rdir/restore_borg_backup.sh"
|
||||
chmod +x "$dest_rdir/restore_borg_backup.sh"
|
||||
{
|
||||
echo "# backup.conf for borg-backup destination '$dn'"
|
||||
echo "# Read by restore_borg_backup.sh in this directory."
|
||||
echo "BORG=\"$BORG_BIN\""
|
||||
echo "BORG_REPO=\"${DEST_REPOS[$dn]}\""
|
||||
printf "BORG_PASSPHRASE='%s'\n" "${DEST_PASSWORDS[$dn]}"
|
||||
} > "$dest_rdir/backup.conf"
|
||||
chown root:root "$dest_rdir/backup.conf" 2>/dev/null || true
|
||||
chmod 600 "$dest_rdir/backup.conf"
|
||||
log_success "restore/$dn/ ready"
|
||||
done
|
||||
cp "$RESTORE_SRC" "$RESTORE"
|
||||
chmod +x "$RESTORE"
|
||||
chown root:root "$RESTORE" 2>/dev/null || true
|
||||
log_success "restore_borg.sh installed"
|
||||
else
|
||||
log_warning "extras/restore_borg_backup.sh not found — restore scripts not installed"
|
||||
log_warning "Copy it manually: cp extras/restore_borg_backup.sh $RESTORE_DIR/<dest>/"
|
||||
log_warning "extras/restore_borg.sh not found — restore script not installed"
|
||||
log_warning "Copy it manually: cp extras/restore_borg.sh $RESTORE"
|
||||
fi
|
||||
|
||||
# ── 11. Systemd timer ─────────────────────────────────────────────────────
|
||||
@@ -525,10 +365,8 @@ SVCEOF
|
||||
echo " sudo $WORKER info repo stats"
|
||||
echo ""
|
||||
echo " Restore:"
|
||||
for dn in "${DEST_NAMES_ARR[@]}"; do
|
||||
echo " sudo $RESTORE_DIR/$dn/restore_borg_backup.sh"
|
||||
echo " sudo $RESTORE_DIR/$dn/restore_borg_backup.sh --list"
|
||||
done
|
||||
echo " sudo $RESTORE"
|
||||
echo " sudo $RESTORE --list"
|
||||
echo ""
|
||||
log_warning "IMPORTANT — back up your Borg key and passphrase now."
|
||||
echo " The key is stored in the repo itself (repokey-blake2 encryption)."
|
||||
|
||||
Reference in New Issue
Block a user