Files
starRiverProperty/scripts/test-env/prepare-db.sh
T
反编译工作区 25db029859 feat: 租户访客策略 SQL、访客邀约验证包、component-org 与发布脚本
- docs/sql: organization_* 与 tenant_* 访客楼层策略脚本
- docs/testing: 访客邀约页初始化验证、pack 脚本与 README(忽略 dist/__pycache__)
- maven-ninca-common-component-organization: CpImageStoreServiceImpl、starter、run-verify、releases 脚本与 javap 审计 JSON
- docs/superpowers: component-org 生产问题修复计划
- scripts/test-env/prepare-db.sh 更新

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-06 22:00:16 +08:00

86 lines
3.2 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
# prepare-db.sh — 恢复 11 个数据库 SQL 备份到 MySQL
source "$(dirname "${BASH_SOURCE[0]}")/config/env.sh"
log_info "Phase 2: Database preparation"
log_info "Target: $MYSQL_HOST:$MYSQL_PORT (user: $MYSQL_USER)"
MYSQL_CMD="mysql -h $MYSQL_HOST -P $MYSQL_PORT -u $MYSQL_USER -p${MYSQL_PASS}"
# Check MySQL connectivity
if ! $MYSQL_CMD -e "SELECT 1" &>/dev/null; then
log_error "Cannot connect to MySQL at $MYSQL_HOST:$MYSQL_PORT"
exit 1
fi
log_ok "MySQL connection OK"
# DB name → backup file mapping
declare -A DB_MAP=(
["$DB_ELEVATOR"]="12_2026_04_23_17_28_33.sql.gz"
["$DB_ALARM"]="alarm_deploy_2026_04_23_17_28_33.sql.gz"
["$DB_MANAGER"]="cwos_manager_2026_04_23_17_28_33.sql.gz"
["$DB_PORTAL"]="cwos_portal_2026_04_23_17_28_33.sql.gz"
["$DB_COMMON"]="ninca_common_2026_04_23_17_28_33.sql.gz"
["$DB_COMPONENT_ORG"]="component-organization_2026_04_23_17_28_33.sql.gz"
["$DB_ODS"]="ods_2026_04_23_17_28_33.sql.gz"
["$DB_THIRDPARTY"]="cloudwalk_device_thirdparty_2026_04_23_17_28_33.sql.gz"
["$DB_G"]="g_2026_04_23_17_28_33.sql.gz"
["$DB_P"]="p_2026_04_23_17_28_33.sql.gz"
)
# Check if DB exists and has tables — skip restore if already populated
db_has_tables() {
local db="$1"
local count
count=$($MYSQL_CMD -N -e "SELECT COUNT(*) FROM information_schema.tables WHERE table_schema='$db'" 2>/dev/null || echo "0")
[[ "$count" -gt 0 ]]
}
for db_name in "${!DB_MAP[@]}"; do
backup_file="${DB_MAP[$db_name]}"
backup_path="$DATA_BACKUP/$backup_file"
if [[ ! -f "$backup_path" ]]; then
log_warn "Backup not found: $backup_path — skipping $db_name"
continue
fi
# Idempotency: skip if DB already has data
if db_has_tables "$db_name"; then
log_ok " $db_name already exists ($(du -h "$backup_path" | cut -f1) backup skipped)"
continue
fi
log_info "Restoring $db_name from $backup_file ($(du -h "$backup_path" | cut -f1))..."
# Create DB if not exists
$MYSQL_CMD -e "CREATE DATABASE IF NOT EXISTS \`$db_name\` DEFAULT CHARACTER SET utf8mb4;"
# Import (strip multi-line GTID_PURGED to avoid MySQL error 1840)
zcat "$backup_path" | sed '/SET @@GLOBAL.GTID_PURGED=/,/;/d' | $MYSQL_CMD "$db_name" 2>&1 | tail -1
if [[ ${PIPESTATUS[0]} -eq 0 ]]; then
log_ok " $db_name restored successfully"
else
log_error " $db_name restore FAILED"
fi
done
# Elevator app also needs second backup (34_*.sql.gz) — skip if DB already populated
if [[ -f "$DATA_BACKUP/34_2026_04_23_17_28_33.sql.gz" ]]; then
if db_has_tables "$DB_ELEVATOR"; then
log_ok " $DB_ELEVATOR partition 34 skipped (DB already populated)"
else
log_info "Restoring elevator DB partition 34..."
zcat "$DATA_BACKUP/34_2026_04_23_17_28_33.sql.gz" | sed '/SET @@GLOBAL.GTID_PURGED=/,/;/d' | $MYSQL_CMD "$DB_ELEVATOR"
log_ok " $DB_ELEVATOR partition 34 restored"
fi
fi
# 电梯库策略表:DROP + 按最终结构重建(清空历史策略数据;表结构含 org_id / uk_org_building
log_info "Applying elevator tenant_visitor_floor_policy DDL (DROP + CREATE)..."
$MYSQL_CMD "$DB_ELEVATOR" < "$REPO_ROOT/docs/sql/tenant_visitor_floor_policy.sql"
log_ok " tenant_visitor_floor_policy reset (see docs/sql/tenant_visitor_floor_policy.sql)"
log_info "Database preparation complete"