Files
gitea-migration/backup/backup_primary.sh
2026-02-26 15:32:34 -06:00

98 lines
4.3 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
# =============================================================================
# backup/backup_primary.sh — Create a full Gitea backup (dump) and store on Fedora
# Depends on: Phase 1 complete (Gitea running on Unraid)
#
# What's in the dump:
# - SQLite database (users, tokens, SSH keys, OAuth, webhooks, org/team, issues)
# - All git repositories
# - app.ini config
#
# Steps:
# 1. Run `gitea dump` inside the container to create a zip archive
# 2. SCP the dump from Unraid to Fedora (offsite storage)
# 3. Clean up the dump from Unraid /tmp
# 4. Prune old backups beyond retention count
# 5. Print backup summary
# =============================================================================
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 \
FEDORA_IP FEDORA_SSH_USER \
BACKUP_STORAGE_PATH BACKUP_RETENTION_COUNT
log_info "=== Gitea Primary Backup ==="
# ---------------------------------------------------------------------------
# Step 1: Run gitea dump inside the container
# The -u git flag is important — gitea dump must run as the git user who
# owns the repository files. The dump is created in /tmp inside the container
# which maps to /tmp on the host via the default Docker tmpfs mount.
# ---------------------------------------------------------------------------
TIMESTAMP=$(date +%Y%m%d-%H%M%S)
DUMP_FILENAME="gitea-dump-${TIMESTAMP}.zip"
DUMP_REMOTE_PATH="/tmp/${DUMP_FILENAME}"
log_info "Creating Gitea dump on Unraid..."
ssh_exec UNRAID "docker exec -u git gitea gitea dump \
-c /data/gitea/conf/app.ini \
-f '${DUMP_REMOTE_PATH}'"
log_success "Dump created: ${DUMP_FILENAME}"
# ---------------------------------------------------------------------------
# Step 2: Create backup storage directory on Fedora and transfer dump
# The dump goes from Unraid → local machine → Fedora because direct
# Unraid→Fedora SCP may not have SSH keys set up. Using the MacBook as
# a relay is more reliable with our existing SSH config.
# ---------------------------------------------------------------------------
log_info "Transferring dump to Fedora backup storage..."
ssh_exec FEDORA "mkdir -p '${BACKUP_STORAGE_PATH}'"
# SCP from Unraid to local temp, then to Fedora
LOCAL_TMP=$(mktemp -d)
scp_to_local() {
local ip_var="UNRAID_IP" user_var="UNRAID_SSH_USER" port_var="UNRAID_SSH_PORT"
local ip="${!ip_var:-}" user="${!user_var:-}" port="${!port_var:-22}"
scp -o ConnectTimeout=10 -o BatchMode=yes -P "$port" \
"${user}@${ip}:${DUMP_REMOTE_PATH}" "${LOCAL_TMP}/${DUMP_FILENAME}"
}
scp_to_local
scp_to FEDORA "${LOCAL_TMP}/${DUMP_FILENAME}" "${BACKUP_STORAGE_PATH}/${DUMP_FILENAME}"
rm -rf "$LOCAL_TMP"
log_success "Dump transferred to Fedora: ${BACKUP_STORAGE_PATH}/${DUMP_FILENAME}"
# ---------------------------------------------------------------------------
# Step 3: Clean up dump from Unraid /tmp
# No reason to keep the dump on Unraid — it's on Fedora now.
# ---------------------------------------------------------------------------
ssh_exec UNRAID "rm -f '${DUMP_REMOTE_PATH}'"
log_info "Cleaned up dump from Unraid"
# ---------------------------------------------------------------------------
# Step 4: Prune old backups beyond retention count
# Lists all gitea-dump-*.zip files sorted by time (newest first), then
# removes everything beyond BACKUP_RETENTION_COUNT.
# ---------------------------------------------------------------------------
log_info "Pruning old backups (keeping ${BACKUP_RETENTION_COUNT})..."
ssh_exec FEDORA "cd '${BACKUP_STORAGE_PATH}' && ls -t gitea-dump-*.zip 2>/dev/null | tail -n +\$((${BACKUP_RETENTION_COUNT}+1)) | xargs -r rm -f"
REMAINING=$(ssh_exec FEDORA "ls -1 '${BACKUP_STORAGE_PATH}'/gitea-dump-*.zip 2>/dev/null | wc -l" | xargs)
log_info "Backups remaining: ${REMAINING}"
# ---------------------------------------------------------------------------
# Step 5: Summary
# ---------------------------------------------------------------------------
DUMP_SIZE=$(ssh_exec FEDORA "du -h '${BACKUP_STORAGE_PATH}/${DUMP_FILENAME}'" | awk '{print $1}')
printf '\n'
log_success "Backup complete"
log_info " File: ${DUMP_FILENAME}"
log_info " Size: ${DUMP_SIZE}"
log_info " Path: ${BACKUP_STORAGE_PATH}/${DUMP_FILENAME} (on Fedora)"
log_info " Total backups: ${REMAINING}"