infra(BIT-24694): add PBS DB extraction script and prod dump import support
- tools/proxmox/extract_db_from_pbs.sh: restores a PBS backup to an air-gapped temp CT (no net0), dumps MariaDB to a .sql.gz, then destroys the temp CT. Handles root no-pass, moneyflix.env creds, and --list mode. - tools/proxmox/install_moneyflix_stack.sh: adds PROD_DB_DUMP env var; when set to a .sql.gz dump from PBS extraction, imports production data instead of applying the empty lab schema. Strips DEFINER clauses for clean import under a different user. Paperclip-Issue: BIT-24694 Paperclip-Run: fda0935a-f525-46a5-81a0-6d63da4188cf
This commit is contained in:
1 parent
e570468610
commit
da978374a1
2 files changed
+194
-3
No files matched your search
Executable
+169
@@ -0,0 +1,169 @@
|
||||
#!/usr/bin/env bash
|
||||
# Safely extract the MoneyFlix MariaDB database from a PBS backup (BIT-24694).
|
||||
#
|
||||
# Safety contract:
|
||||
# - Restored CT gets NO network interface (air-gapped). Do not add --net0.
|
||||
# - CT is stopped and destroyed immediately after the dump is copied out.
|
||||
# - Never run with internet access; this backup is potentially compromised.
|
||||
#
|
||||
# Run as root on the Proxmox hypervisor (node09).
|
||||
#
|
||||
# Usage:
|
||||
# PBS_STORAGE=backupsafodamesmo \
|
||||
# BACKUP_VOLID=backupsafodamesmo:backup/ct/VMID/SNAPSHOT \
|
||||
# bash tools/proxmox/extract_db_from_pbs.sh
|
||||
#
|
||||
# If you don't know BACKUP_VOLID yet, run without it to list available backups:
|
||||
# PBS_STORAGE=backupsafodamesmo bash tools/proxmox/extract_db_from_pbs.sh --list
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
PBS_STORAGE="${PBS_STORAGE:-backupsafodamesmo}"
|
||||
BACKUP_VOLID="${BACKUP_VOLID:-}" # e.g. backupsafodamesmo:backup/ct/237/2026-06-07T00:00:00Z
|
||||
TEMP_VMID="${TEMP_VMID:-998}" # temp CT VMID — must be free on this node
|
||||
TEMP_STORAGE="${TEMP_STORAGE:-local-lvm}" # local storage for temp rootfs
|
||||
TEMP_ROOTFS_GB="${TEMP_ROOTFS_GB:-20}"
|
||||
DB_NAME="${DB_NAME:-}" # leave blank to dump ALL databases
|
||||
DUMP_OUT="${DUMP_OUT:-/tmp/moneyflix_prod_db_$(date +%Y%m%d_%H%M%S).sql.gz}"
|
||||
WAIT_BOOT_SECONDS="${WAIT_BOOT_SECONDS:-30}"
|
||||
|
||||
###############################################################################
|
||||
# Helpers
|
||||
###############################################################################
|
||||
|
||||
die() { echo "ERROR: $*" >&2; exit 1; }
|
||||
info() { echo "[extract_db_from_pbs] $*"; }
|
||||
|
||||
cleanup_ct() {
|
||||
local vmid="$1"
|
||||
info "Stopping temp CT $vmid..."
|
||||
pct stop "$vmid" --skiplock 2>/dev/null || true
|
||||
sleep 3
|
||||
info "Destroying temp CT $vmid..."
|
||||
pct destroy "$vmid" --purge 2>/dev/null || true
|
||||
info "Temp CT $vmid removed."
|
||||
}
|
||||
|
||||
###############################################################################
|
||||
# List mode
|
||||
###############################################################################
|
||||
|
||||
if [[ "${1:-}" == "--list" ]]; then
|
||||
info "Backups available in storage '$PBS_STORAGE':"
|
||||
pvesh get "/nodes/$(hostname)/storage/${PBS_STORAGE}/content" --content backup 2>/dev/null \
|
||||
|| pvesh get "/nodes/$(hostname -s)/storage/${PBS_STORAGE}/content" --content backup
|
||||
echo ""
|
||||
echo "Re-run with BACKUP_VOLID=<volid> to restore and extract."
|
||||
exit 0
|
||||
fi
|
||||
|
||||
###############################################################################
|
||||
# Pre-flight checks
|
||||
###############################################################################
|
||||
|
||||
[[ "$(id -u)" -eq 0 ]] || die "Must run as root on the Proxmox hypervisor."
|
||||
command -v pct >/dev/null || die "'pct' not found — run this on the Proxmox node, not inside a CT."
|
||||
command -v pvesh >/dev/null || die "'pvesh' not found — run this on the Proxmox node."
|
||||
|
||||
if [[ -z "$BACKUP_VOLID" ]]; then
|
||||
info "BACKUP_VOLID not set. Available backups in '$PBS_STORAGE':"
|
||||
pvesh get "/nodes/$(hostname -s)/storage/${PBS_STORAGE}/content" --content backup 2>/dev/null || true
|
||||
echo ""
|
||||
die "Set BACKUP_VOLID=<volid from the list above> and re-run."
|
||||
fi
|
||||
|
||||
# Ensure temp VMID is free
|
||||
if pct status "$TEMP_VMID" &>/dev/null; then
|
||||
die "VMID $TEMP_VMID already exists. Choose a free VMID with TEMP_VMID=<free-id>."
|
||||
fi
|
||||
|
||||
info "PBS storage: $PBS_STORAGE"
|
||||
info "Backup volid: $BACKUP_VOLID"
|
||||
info "Temp VMID: $TEMP_VMID (air-gapped — no net0)"
|
||||
info "Dump output: $DUMP_OUT"
|
||||
echo ""
|
||||
|
||||
###############################################################################
|
||||
# Restore backup to air-gapped temp CT (no --net0)
|
||||
###############################################################################
|
||||
|
||||
info "Restoring backup to CT $TEMP_VMID (no network)..."
|
||||
pct restore "$TEMP_VMID" "$BACKUP_VOLID" \
|
||||
--storage "$TEMP_STORAGE" \
|
||||
--rootfs "${TEMP_STORAGE}:${TEMP_ROOTFS_GB}" \
|
||||
--unprivileged 1 \
|
||||
--start 0
|
||||
|
||||
info "Restore complete. Starting CT $TEMP_VMID (no internet)..."
|
||||
pct start "$TEMP_VMID"
|
||||
|
||||
info "Waiting ${WAIT_BOOT_SECONDS}s for services to start inside CT..."
|
||||
sleep "$WAIT_BOOT_SECONDS"
|
||||
|
||||
###############################################################################
|
||||
# Dump the database
|
||||
###############################################################################
|
||||
|
||||
DUMP_ARGS="--single-transaction --quick --default-character-set=utf8mb4"
|
||||
if [[ -n "$DB_NAME" ]]; then
|
||||
DUMP_CMD="mysqldump $DUMP_ARGS $DB_NAME"
|
||||
info "Dumping database '$DB_NAME' from CT $TEMP_VMID..."
|
||||
else
|
||||
DUMP_CMD="mysqldump $DUMP_ARGS --all-databases"
|
||||
info "Dumping ALL databases from CT $TEMP_VMID..."
|
||||
fi
|
||||
|
||||
# Try unix-socket (root with no password), fall back to common creds
|
||||
if pct exec "$TEMP_VMID" -- bash -c "mysql -u root -e 'SELECT 1;'" 2>/dev/null; then
|
||||
info "MariaDB/MySQL accessible as root (no password). Dumping..."
|
||||
pct exec "$TEMP_VMID" -- bash -c "$DUMP_CMD" | gzip > "$DUMP_OUT"
|
||||
elif pct exec "$TEMP_VMID" -- bash -c "test -f /etc/moneyflix/moneyflix.env && source /etc/moneyflix/moneyflix.env && mysql -u \"\$DB_USER\" -p\"\$DB_PASS\" -e 'SELECT 1;'" 2>/dev/null; then
|
||||
info "MariaDB accessible via /etc/moneyflix/moneyflix.env credentials. Dumping..."
|
||||
pct exec "$TEMP_VMID" -- bash -c "
|
||||
source /etc/moneyflix/moneyflix.env
|
||||
$DUMP_CMD -u \"\$DB_USER\" -p\"\$DB_PASS\"
|
||||
" | gzip > "$DUMP_OUT"
|
||||
else
|
||||
# Last resort: try to find .env and extract credentials
|
||||
info "Trying to locate .env or MySQL credentials inside CT..."
|
||||
DB_CREDS=$(pct exec "$TEMP_VMID" -- bash -c "
|
||||
for f in /opt/moneyflix/.env /var/www/.env /data/wwwroot/*/env.php /root/.my.cnf; do
|
||||
[[ -f \"\$f\" ]] && cat \"\$f\" && echo '---found:'\"\$f\" && break
|
||||
done
|
||||
" 2>/dev/null || true)
|
||||
echo "$DB_CREDS"
|
||||
cleanup_ct "$TEMP_VMID"
|
||||
die "Could not authenticate to MariaDB inside the CT. Check the output above for credential hints, then re-run with DB_NAME=<name> and the correct credentials."
|
||||
fi
|
||||
|
||||
###############################################################################
|
||||
# Verify dump and clean up
|
||||
###############################################################################
|
||||
|
||||
if [[ ! -s "$DUMP_OUT" ]]; then
|
||||
cleanup_ct "$TEMP_VMID"
|
||||
die "Dump file is empty: $DUMP_OUT"
|
||||
fi
|
||||
|
||||
DUMP_SIZE=$(du -sh "$DUMP_OUT" | cut -f1)
|
||||
info "Dump written: $DUMP_OUT ($DUMP_SIZE)"
|
||||
|
||||
cleanup_ct "$TEMP_VMID"
|
||||
|
||||
echo ""
|
||||
echo "=== Database extraction complete ==="
|
||||
echo ""
|
||||
echo " Dump file: $DUMP_OUT"
|
||||
echo " Size: $DUMP_SIZE"
|
||||
echo ""
|
||||
echo "Next steps (run on the new MoneyFlix LXC):"
|
||||
echo " 1. Copy dump to the new CT:"
|
||||
echo " pct push <new-ct-vmid> $DUMP_OUT /tmp/moneyflix_prod.sql.gz"
|
||||
echo " 2. Install the stack with the dump:"
|
||||
echo " PROD_DB_DUMP=/tmp/moneyflix_prod.sql.gz \\"
|
||||
echo " bash /opt/moneyflix/tools/proxmox/install_moneyflix_stack.sh"
|
||||
echo " 3. Run smoke:"
|
||||
echo " bash /opt/moneyflix/tools/proxmox/smoke_moneyflix_prod.sh"
|
||||
echo ""
|
||||
echo " Alternatively: import manually inside the new CT:"
|
||||
echo " zcat /tmp/moneyflix_prod.sql.gz | mariadb -u \$DB_USER -p\$DB_PASS \$DB_NAME"
|
||||
@@ -11,6 +11,9 @@ GITEA_REPO="${GITEA_REPO:-https://git.bitreport.pt/BitReport.pt/MoneyFlix.git}"
|
||||
GITEA_BRANCH="${GITEA_BRANCH:-master}"
|
||||
CRM_PORT="${CRM_PORT:-8080}"
|
||||
REDIRECTOR_PORT="${REDIRECTOR_PORT:-3000}"
|
||||
# Optional: path to a gzipped mysqldump from PBS extraction (see tools/proxmox/extract_db_from_pbs.sh).
|
||||
# If set, the production dump is imported instead of applying the empty lab schema.
|
||||
PROD_DB_DUMP="${PROD_DB_DUMP:-}"
|
||||
|
||||
# --- Validate credentials file exists and is non-empty ---
|
||||
if [[ ! -f "$ENV_FILE" ]]; then
|
||||
@@ -119,13 +122,32 @@ APP_TIMEZONE=Europe/Lisbon
|
||||
EOF
|
||||
chmod 640 "$APP_DIR/.env"
|
||||
|
||||
# Apply DB schema if not already applied
|
||||
# Apply DB schema or import production dump
|
||||
if [[ -n "$PROD_DB_DUMP" ]]; then
|
||||
if [[ ! -f "$PROD_DB_DUMP" ]]; then
|
||||
echo "ERROR: PROD_DB_DUMP set but file not found: $PROD_DB_DUMP" >&2
|
||||
exit 1
|
||||
fi
|
||||
echo " Importing production DB dump: $PROD_DB_DUMP"
|
||||
# Strip DEFINER clauses so the dump imports cleanly under a different user
|
||||
if [[ "$PROD_DB_DUMP" == *.gz ]]; then
|
||||
zcat "$PROD_DB_DUMP" \
|
||||
| sed 's|/\*![0-9]* DEFINER=[^ ]* \*/||g' \
|
||||
| mariadb -u "${DB_USER}" -p"${DB_PASS}" "${DB_NAME}"
|
||||
else
|
||||
sed 's|/\*![0-9]* DEFINER=[^ ]* \*/||g' "$PROD_DB_DUMP" \
|
||||
| mariadb -u "${DB_USER}" -p"${DB_PASS}" "${DB_NAME}"
|
||||
fi
|
||||
echo " Production DB imported."
|
||||
else
|
||||
# Apply empty schema when no production dump is available
|
||||
if ! mariadb -u "${DB_USER}" -p"${DB_PASS}" "${DB_NAME}" -e "SHOW TABLES LIKE 'redirects';" | grep -q redirects; then
|
||||
if [[ -f "$APP_DIR/tools/lab/schema.sql" ]]; then
|
||||
mariadb -u "${DB_USER}" -p"${DB_PASS}" "${DB_NAME}" < "$APP_DIR/tools/lab/schema.sql"
|
||||
echo " DB schema applied."
|
||||
echo " DB schema applied (empty database)."
|
||||
else
|
||||
echo " WARNING: schema.sql not found — apply manually."
|
||||
echo " WARNING: schema.sql not found — apply DB schema manually."
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
|
||||
|
||||
Reference in new issue
Block a user