фикс импорта
This commit is contained in:
+22
-53
@@ -5,10 +5,6 @@ set -euo pipefail
|
|||||||
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
||||||
ENV_FILE="${ENV_FILE:-$ROOT_DIR/.env}"
|
ENV_FILE="${ENV_FILE:-$ROOT_DIR/.env}"
|
||||||
DUMP_FILE=""
|
DUMP_FILE=""
|
||||||
TARGET_HOST="127.0.0.1"
|
|
||||||
TARGET_PORT="3306"
|
|
||||||
TARGET_USER="root"
|
|
||||||
TARGET_PASSWORD=""
|
|
||||||
DRY_RUN=0
|
DRY_RUN=0
|
||||||
FORCE=0
|
FORCE=0
|
||||||
|
|
||||||
@@ -19,48 +15,37 @@ if [[ -f "$ENV_FILE" ]]; then
|
|||||||
set +a
|
set +a
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Use password from .env if not overridden
|
|
||||||
TARGET_PASSWORD="${TARGET_PASSWORD:-${DOCKER_DB_ROOT_PASSWORD:-}}"
|
|
||||||
|
|
||||||
usage() {
|
usage() {
|
||||||
cat <<EOF
|
cat <<EOF
|
||||||
Usage: $(basename "$0") --from FILE [options]
|
Usage: $(basename "$0") --from FILE [--dry-run] [--force]
|
||||||
|
|
||||||
Restores a full database dump (produced by db-export.sh) into a MySQL instance.
|
Restores a full database dump (produced by db-export.sh) into the
|
||||||
Works with both the local Docker container and a remote MySQL server.
|
ac-database Docker container. The container must be running.
|
||||||
|
|
||||||
Options:
|
Options:
|
||||||
--from FILE Dump file to restore (.sql or .sql.gz) (required)
|
--from FILE Dump file to restore (.sql or .sql.gz) (required)
|
||||||
--host HOST Target MySQL host (default: 127.0.0.1)
|
|
||||||
--port PORT Target MySQL port (default: 3306)
|
|
||||||
--user USER Target MySQL user (default: root)
|
|
||||||
--password PASS Target MySQL password (default: from .env DOCKER_DB_ROOT_PASSWORD)
|
|
||||||
--dry-run Show what would happen without executing
|
--dry-run Show what would happen without executing
|
||||||
--force Skip the confirmation prompt
|
--force Skip the confirmation prompt
|
||||||
-h, --help Show this help
|
-h, --help Show this help
|
||||||
|
|
||||||
Examples:
|
Examples:
|
||||||
# Restore into local Docker container (new server, container must be running)
|
|
||||||
$(basename "$0") --from backups/2025-01-01_12-00-00.sql.gz
|
$(basename "$0") --from backups/2025-01-01_12-00-00.sql.gz
|
||||||
|
$(basename "$0") --from /mnt/backup/dump.sql.gz --force
|
||||||
# Restore into a remote MySQL server
|
|
||||||
$(basename "$0") --from backups/dump.sql.gz \\
|
|
||||||
--host db.example.com --port 3306 \\
|
|
||||||
--user root --password secret
|
|
||||||
EOF
|
EOF
|
||||||
}
|
}
|
||||||
|
|
||||||
log() { printf '[db-import] %s\n' "$*"; }
|
log() { printf '[db-import] %s\n' "$*"; }
|
||||||
die() { printf 'ERROR: %s\n' "$*" >&2; exit 1; }
|
die() { printf 'ERROR: %s\n' "$*" >&2; exit 1; }
|
||||||
|
|
||||||
mysql_cmd() {
|
require_running_db() {
|
||||||
mysql \
|
if ! docker compose ps --status running --services 2>/dev/null | grep -qx 'ac-database'; then
|
||||||
--host="$TARGET_HOST" \
|
die "ac-database is not running. Start it with: docker compose up -d ac-database"
|
||||||
--port="$TARGET_PORT" \
|
fi
|
||||||
--user="$TARGET_USER" \
|
}
|
||||||
--password="$TARGET_PASSWORD" \
|
|
||||||
--connect-timeout=10 \
|
mysql_exec() {
|
||||||
"$@"
|
docker compose exec -T ac-database bash -lc \
|
||||||
|
"mysql -uroot -p\"\$MYSQL_ROOT_PASSWORD\""
|
||||||
}
|
}
|
||||||
|
|
||||||
# ── argument parsing ──────────────────────────────────────────────────────────
|
# ── argument parsing ──────────────────────────────────────────────────────────
|
||||||
@@ -68,10 +53,6 @@ mysql_cmd() {
|
|||||||
while (($#)); do
|
while (($#)); do
|
||||||
case "$1" in
|
case "$1" in
|
||||||
--from) DUMP_FILE="$2"; shift ;;
|
--from) DUMP_FILE="$2"; shift ;;
|
||||||
--host) TARGET_HOST="$2"; shift ;;
|
|
||||||
--port) TARGET_PORT="$2"; shift ;;
|
|
||||||
--user) TARGET_USER="$2"; shift ;;
|
|
||||||
--password) TARGET_PASSWORD="$2"; shift ;;
|
|
||||||
--dry-run) DRY_RUN=1 ;;
|
--dry-run) DRY_RUN=1 ;;
|
||||||
--force) FORCE=1 ;;
|
--force) FORCE=1 ;;
|
||||||
-h|--help) usage; exit 0 ;;
|
-h|--help) usage; exit 0 ;;
|
||||||
@@ -84,11 +65,6 @@ done
|
|||||||
|
|
||||||
[[ -z "$DUMP_FILE" ]] && { usage >&2; die "--from FILE is required"; }
|
[[ -z "$DUMP_FILE" ]] && { usage >&2; die "--from FILE is required"; }
|
||||||
[[ -f "$DUMP_FILE" ]] || die "file not found: $DUMP_FILE"
|
[[ -f "$DUMP_FILE" ]] || die "file not found: $DUMP_FILE"
|
||||||
[[ -z "$TARGET_PASSWORD" ]] && die "no password set — use --password or set DOCKER_DB_ROOT_PASSWORD in .env"
|
|
||||||
|
|
||||||
if ! command -v mysql >/dev/null 2>&1; then
|
|
||||||
die "'mysql' client not found. Install it: apt install mysql-client or brew install mysql-client"
|
|
||||||
fi
|
|
||||||
|
|
||||||
cd "$ROOT_DIR"
|
cd "$ROOT_DIR"
|
||||||
|
|
||||||
@@ -97,9 +73,8 @@ cd "$ROOT_DIR"
|
|||||||
file_size="$(du -sh "$DUMP_FILE" | cut -f1)"
|
file_size="$(du -sh "$DUMP_FILE" | cut -f1)"
|
||||||
|
|
||||||
printf '\nMigration plan:\n'
|
printf '\nMigration plan:\n'
|
||||||
printf ' Source file : %s (%s)\n' "$DUMP_FILE" "$file_size"
|
printf ' Source : %s (%s)\n' "$DUMP_FILE" "$file_size"
|
||||||
printf ' Target host : %s:%s\n' "$TARGET_HOST" "$TARGET_PORT"
|
printf ' Target : ac-database container\n\n'
|
||||||
printf ' Target user : %s\n\n' "$TARGET_USER"
|
|
||||||
|
|
||||||
if [[ "$DRY_RUN" -eq 1 ]]; then
|
if [[ "$DRY_RUN" -eq 1 ]]; then
|
||||||
log "dry-run mode — nothing imported"
|
log "dry-run mode — nothing imported"
|
||||||
@@ -112,27 +87,21 @@ if [[ "$FORCE" -eq 0 ]]; then
|
|||||||
[[ "$confirm" =~ ^[Yy]$ ]] || { log "aborted"; exit 0; }
|
[[ "$confirm" =~ ^[Yy]$ ]] || { log "aborted"; exit 0; }
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# ── connectivity check ────────────────────────────────────────────────────────
|
|
||||||
|
|
||||||
log "checking connection to ${TARGET_HOST}:${TARGET_PORT}..."
|
|
||||||
if ! mysql_cmd --execute="SELECT 1;" >/dev/null 2>&1; then
|
|
||||||
die "cannot connect to MySQL at ${TARGET_HOST}:${TARGET_PORT} as ${TARGET_USER}"
|
|
||||||
fi
|
|
||||||
log "connection OK"
|
|
||||||
|
|
||||||
# ── import ────────────────────────────────────────────────────────────────────
|
# ── import ────────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
log "restoring $DUMP_FILE → ${TARGET_HOST}:${TARGET_PORT}..."
|
require_running_db
|
||||||
|
|
||||||
|
log "restoring $DUMP_FILE into ac-database..."
|
||||||
log "this may take several minutes..."
|
log "this may take several minutes..."
|
||||||
|
|
||||||
if [[ "$DUMP_FILE" == *.gz ]]; then
|
if [[ "$DUMP_FILE" == *.gz ]]; then
|
||||||
zcat "$DUMP_FILE" | mysql_cmd
|
zcat "$DUMP_FILE" | mysql_exec
|
||||||
else
|
else
|
||||||
mysql_cmd < "$DUMP_FILE"
|
mysql_exec < "$DUMP_FILE"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
log "import complete"
|
log "import complete"
|
||||||
log ""
|
log ""
|
||||||
log "next steps on the new server:"
|
log "next steps:"
|
||||||
log " 1. Update realmlist: bash scripts/sync-realmlist.sh"
|
log " 1. Update realmlist: bash sync-realmlist.sh"
|
||||||
log " 2. Start the server: bash scripts/prod-deploy.sh --no-build"
|
log " 2. Start the server: bash scripts/prod-deploy.sh --no-build"
|
||||||
|
|||||||
Reference in New Issue
Block a user