#!/usr/bin/env bash # obcon-hf — 실 운영 데이터를 per-device 균등 샘플링해서 seed/03_data.sql 재생성. # # 기존 03_data.sql 은 "전체 최근 5000행" 이라 활동 많은 몇몇 장치에 쏠려 있어서 다른 # 장치 클릭 시 그래프 비어 보임. 이 스크립트는 각 장치별로 최근 N행씩 추출해서 # 모든 장치가 화면에 데이터를 가지도록 한다. # # Usage: # cd /home/gpuadmin/cream/obcon-hf # bash scripts/dump_real_data.sh # 기본: 장치당 50행 # ROWS_PER_DEVICE=100 bash scripts/dump_real_data.sh set -euo pipefail HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" SEED="${HERE}/seed/03_data.sql" SRC_HOST="${SRC_DB_HOST:-127.0.0.1}" SRC_PORT="${SRC_DB_PORT:-3306}" SRC_USER="${SRC_DB_USER:-scada}" SRC_PASS="${SRC_DB_PASS:-scada}" SRC_NAME="${SRC_DB_NAME:-obcondb}" N="${ROWS_PER_DEVICE:-50}" MYSQL_FLAGS=( -h "$SRC_HOST" -P "$SRC_PORT" -u "$SRC_USER" "-p${SRC_PASS}" -D "$SRC_NAME" ) DUMP_FLAGS=( --no-create-info --skip-comments --skip-add-locks --skip-lock-tables --insert-ignore --set-charset --default-character-set=utf8mb4 --single-transaction ) echo "[dump] target: $SEED (장치당 $N 행)" echo "" # ---- A. 메타 테이블 풀 덤프 ------------------------------------------------ echo "[dump] A. 메타 풀 덤프 (sites/branches/devices/pipelines/alarms/...)" mysqldump -h "$SRC_HOST" -P "$SRC_PORT" -u "$SRC_USER" "-p${SRC_PASS}" \ "${DUMP_FLAGS[@]}" \ "$SRC_NAME" \ sites branches devices pipelines alarms settings mapLines reports jobManagers \ 2>/dev/null > "$SEED" echo " → $(wc -c < "$SEED") bytes" echo "" # ---- B. devicedata: 장치당 최근 N 행 ---------------------------------------- for t in devicedata1s devicedata2s devicedataAs; do DEVS=$(mysql "${MYSQL_FLAGS[@]}" -N -e "SELECT DISTINCT deviceKey FROM $t" 2>/dev/null) COUNT=0 for d in $DEVS; do mysqldump -h "$SRC_HOST" -P "$SRC_PORT" -u "$SRC_USER" "-p${SRC_PASS}" \ "${DUMP_FLAGS[@]}" \ --where="deviceKey='$d' ORDER BY statusDatetime DESC LIMIT $N" \ "$SRC_NAME" "$t" 2>/dev/null \ | grep -E "^INSERT" >> "$SEED" COUNT=$((COUNT+1)) done echo "[dump] B. $t : $COUNT 장치 × 최대 $N 행 (누적 $(wc -c < "$SEED") bytes)" done echo "" echo "[dump] 완료. seed/03_data.sql:" ls -lh "$SEED" echo "" echo "각 테이블별 inserted row 추정:" for t in devicedata1s devicedata2s devicedataAs; do n=$(grep "INSERT IGNORE INTO \`$t\`" "$SEED" | grep -oE "\)," | wc -l) echo " $t : ~$n rows" done