- teardown_all.sh: replace `yes |` pipeline with `< <(yes)` process
substitution to avoid SIGPIPE (exit 141) false failures under pipefail
- phase6_teardown.sh: extract push mirror `.id` instead of `.remote_name`
to match the DELETE /push_mirrors/{id} API contract
- phase5_migrate_pipelines.sh: expand sed regex from `[a-z_]*` to
`[a-z_.]*` to handle nested GitHub contexts like
`github.event.pull_request.number`
- lib/common.sh: render_template now requires explicit variable list to
prevent envsubst from eating Nginx variables ($host, $proxy_add_...)
- backup scripts: remove MacBook relay, use direct Unraid↔Fedora SCP;
fix dump path to write to /data/ (mounted volume) instead of /tmp/
(container-only); add unzip -t integrity verification
- preflight.sh: add --skip-port-checks flag for resuming with
--start-from (ports already bound by earlier phases)
- run_all.sh: update run_step to pass extra args; use --skip-port-checks
when --start-from > 1
- post-checks (phase4/7/9): wrap API calls in helper functions with
>/dev/null redirection instead of passing -o /dev/null as API data
- phase8: replace GitHub archiving with [MIRROR] description marking
and disable wiki/projects/Pages (archived repos reject push mirrors)
- restore_to_primary.sh: add require_vars for Fedora SSH variables
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
193 lines
7.9 KiB
Bash
Executable File
193 lines
7.9 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
set -euo pipefail
|
|
|
|
# =============================================================================
|
|
# backup/restore_to_primary.sh — Restore a Gitea backup to Unraid primary
|
|
# DESTRUCTIVE: Replaces all Gitea data on Unraid with the backup contents.
|
|
#
|
|
# Usage: ./backup/restore_to_primary.sh --archive <path>
|
|
# <path> can be:
|
|
# - A path on Fedora (e.g. /mnt/nvme/gitea-backups/gitea-dump-*.zip)
|
|
# - A local path on this machine
|
|
#
|
|
# Steps:
|
|
# 1. Stop Gitea container
|
|
# 2. Back up current data as safety net (data.pre-restore)
|
|
# 3. Extract archive to Gitea data directory
|
|
# 4. Restart Gitea container
|
|
# 5. Wait for Gitea to be ready
|
|
# 6. Verify admin login
|
|
# 7. Regenerate API token (old token from dump may be stale)
|
|
# =============================================================================
|
|
|
|
SCRIPT_DIR="$(cd "$(dirname "$0")/.." && pwd)"
|
|
source "${SCRIPT_DIR}/lib/common.sh"
|
|
|
|
load_env
|
|
require_vars UNRAID_IP UNRAID_SSH_USER UNRAID_GITEA_DATA_PATH \
|
|
GITEA_INTERNAL_URL GITEA_ADMIN_USER GITEA_ADMIN_PASSWORD
|
|
|
|
DATA_PATH="$UNRAID_GITEA_DATA_PATH"
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Parse arguments
|
|
# ---------------------------------------------------------------------------
|
|
ARCHIVE_PATH=""
|
|
|
|
while [[ $# -gt 0 ]]; do
|
|
case "$1" in
|
|
--archive)
|
|
if [[ $# -lt 2 ]]; then log_error "--archive requires a path"; exit 1; fi
|
|
ARCHIVE_PATH="$2"; shift 2 ;;
|
|
--help|-h)
|
|
echo "Usage: $(basename "$0") --archive <path-to-gitea-dump.zip>"
|
|
exit 0 ;;
|
|
*) log_error "Unknown argument: $1"; exit 1 ;;
|
|
esac
|
|
done
|
|
|
|
if [[ -z "$ARCHIVE_PATH" ]]; then
|
|
log_error "Missing required --archive <path>"
|
|
echo "Usage: $(basename "$0") --archive <path-to-gitea-dump.zip>"
|
|
exit 1
|
|
fi
|
|
|
|
log_warn "=== Gitea Restore to Primary ==="
|
|
log_warn "This will REPLACE all Gitea data on Unraid with the backup."
|
|
|
|
printf 'Continue? This is IRREVERSIBLE (current data will be backed up first). [y/N] '
|
|
read -r confirm
|
|
if [[ ! "$confirm" =~ ^[Yy]$ ]]; then
|
|
log_info "Restore cancelled"
|
|
exit 0
|
|
fi
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Step 1: Transfer archive to Unraid /tmp if needed
|
|
# If the archive is a local file, SCP it directly. If it's on Fedora,
|
|
# SCP directly from Fedora to Unraid (no MacBook relay).
|
|
# ---------------------------------------------------------------------------
|
|
log_step 1 "Preparing archive..."
|
|
ARCHIVE_NAME=$(basename "$ARCHIVE_PATH")
|
|
UNRAID_ARCHIVE="/tmp/${ARCHIVE_NAME}"
|
|
|
|
if [[ -f "$ARCHIVE_PATH" ]]; then
|
|
# Local file — SCP to Unraid
|
|
log_info "Uploading local archive to Unraid..."
|
|
scp_to UNRAID "$ARCHIVE_PATH" "$UNRAID_ARCHIVE"
|
|
else
|
|
# Assume path is on Fedora — SCP directly from Fedora to Unraid
|
|
require_vars FEDORA_IP FEDORA_SSH_USER UNRAID_IP UNRAID_SSH_USER
|
|
log_info "Transferring archive from Fedora to Unraid..."
|
|
UNRAID_PORT="${UNRAID_SSH_PORT:-22}"
|
|
ssh_exec FEDORA "scp -o ConnectTimeout=10 -o StrictHostKeyChecking=accept-new \
|
|
-o BatchMode=yes -P '${UNRAID_PORT}' \
|
|
'${ARCHIVE_PATH}' '${UNRAID_SSH_USER}@${UNRAID_IP}:${UNRAID_ARCHIVE}'"
|
|
fi
|
|
log_success "Archive ready on Unraid: ${UNRAID_ARCHIVE}"
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Step 2: Stop Gitea container
|
|
# ---------------------------------------------------------------------------
|
|
log_step 2 "Stopping Gitea container..."
|
|
ssh_exec UNRAID "cd '${DATA_PATH}' && docker compose down 2>/dev/null || docker-compose down" || true
|
|
log_success "Gitea container stopped"
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Step 3: Back up current data as safety net
|
|
# Rename data/ → data.pre-restore/ so we can roll back if needed.
|
|
# If a pre-restore backup already exists, append a timestamp.
|
|
# ---------------------------------------------------------------------------
|
|
log_step 3 "Backing up current data..."
|
|
if ssh_exec UNRAID "test -d '${DATA_PATH}/data'" 2>/dev/null; then
|
|
BACKUP_SUFFIX="pre-restore-$(date +%Y%m%d-%H%M%S)"
|
|
ssh_exec UNRAID "mv '${DATA_PATH}/data' '${DATA_PATH}/data.${BACKUP_SUFFIX}'"
|
|
log_success "Current data backed up to data.${BACKUP_SUFFIX}"
|
|
else
|
|
log_info "No existing data directory to back up"
|
|
fi
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Step 4: Extract archive
|
|
# gitea dump creates a zip with: gitea-db.sql (or gitea.db), repos/, app.ini
|
|
# We extract to a temp dir, then move files to the correct locations.
|
|
# ---------------------------------------------------------------------------
|
|
log_step 4 "Extracting archive..."
|
|
ssh_exec UNRAID "mkdir -p '${DATA_PATH}/data' && cd '${DATA_PATH}' && unzip -o '${UNRAID_ARCHIVE}' -d restore-tmp"
|
|
|
|
# Move restored files into place
|
|
# The dump structure varies by Gitea version — handle common layouts
|
|
ssh_exec UNRAID "
|
|
cd '${DATA_PATH}/restore-tmp'
|
|
# Move repos if present
|
|
if [ -d repos ]; then mv repos '${DATA_PATH}/data/'; fi
|
|
# Move gitea.db (SQLite database)
|
|
if [ -f gitea.db ]; then mv gitea.db '${DATA_PATH}/data/'; fi
|
|
if [ -f gitea-db.sql ]; then mv gitea-db.sql '${DATA_PATH}/data/'; fi
|
|
# Move config if present
|
|
if [ -f app.ini ]; then
|
|
mkdir -p '${DATA_PATH}/config'
|
|
mv app.ini '${DATA_PATH}/config/'
|
|
fi
|
|
# Move any remaining data files
|
|
if [ -d data ]; then cp -r data/* '${DATA_PATH}/data/' 2>/dev/null || true; fi
|
|
# Clean up temp
|
|
cd '${DATA_PATH}' && rm -rf restore-tmp
|
|
"
|
|
log_success "Archive extracted and files placed"
|
|
|
|
# Clean up archive from Unraid /tmp
|
|
ssh_exec UNRAID "rm -f '${UNRAID_ARCHIVE}'"
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Step 5: Restart Gitea container
|
|
# ---------------------------------------------------------------------------
|
|
log_step 5 "Starting Gitea container..."
|
|
ssh_exec UNRAID "cd '${DATA_PATH}' && docker compose up -d 2>/dev/null || docker-compose up -d"
|
|
log_success "Gitea container started"
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Step 6: Wait for Gitea to be ready
|
|
# ---------------------------------------------------------------------------
|
|
log_step 6 "Waiting for Gitea to be ready..."
|
|
wait_for_http "${GITEA_INTERNAL_URL}/api/v1/version" 120
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Step 7: Verify admin login
|
|
# ---------------------------------------------------------------------------
|
|
log_step 7 "Verifying admin login..."
|
|
if curl -sf -u "${GITEA_ADMIN_USER}:${GITEA_ADMIN_PASSWORD}" "${GITEA_INTERNAL_URL}/api/v1/user" -o /dev/null 2>/dev/null; then
|
|
log_success "Admin login verified"
|
|
else
|
|
log_error "Admin login failed — check credentials or backup integrity"
|
|
exit 1
|
|
fi
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Step 8: Regenerate API token
|
|
# Old tokens from the dump may conflict or be stale. Generate a fresh one.
|
|
# ---------------------------------------------------------------------------
|
|
log_step 8 "Regenerating API token..."
|
|
TOKEN_RESPONSE=$(curl -sf -u "${GITEA_ADMIN_USER}:${GITEA_ADMIN_PASSWORD}" \
|
|
-X POST \
|
|
-H "Content-Type: application/json" \
|
|
-d '{"name":"migration-token-restored","scopes":["all"]}' \
|
|
"${GITEA_INTERNAL_URL}/api/v1/users/${GITEA_ADMIN_USER}/tokens")
|
|
|
|
NEW_TOKEN=$(printf '%s' "$TOKEN_RESPONSE" | jq -r '.sha1')
|
|
|
|
if [[ -z "$NEW_TOKEN" ]] || [[ "$NEW_TOKEN" == "null" ]]; then
|
|
log_warn "Could not generate new API token — may need manual regeneration"
|
|
else
|
|
save_env_var "GITEA_ADMIN_TOKEN" "$NEW_TOKEN"
|
|
log_success "New API token generated and saved to .env"
|
|
fi
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Summary
|
|
# ---------------------------------------------------------------------------
|
|
printf '\n'
|
|
log_success "Restore complete — Gitea is running with restored data"
|
|
log_info "Pre-restore data preserved at: ${DATA_PATH}/data.${BACKUP_SUFFIX:-unknown}"
|
|
log_info "Verify all repos and users are accessible before deleting the pre-restore backup."
|