diff --git a/scripts/sync-prod-to-local.sh b/scripts/sync-prod-to-local.sh new file mode 100755 index 0000000..406896c --- /dev/null +++ b/scripts/sync-prod-to-local.sh @@ -0,0 +1,106 @@ +#!/usr/bin/env bash +# 把**生产供应商库**数据拉到本地,并**自动完成本地重接线**(2026-10-19 起,拉库即做) +# +# 为什么要有"重接线"这一步:生产库里的对接客户端配置是**生产值**(client_id / secret / +# webhook_secret / webhook_url 指向 supplier.xdjchuanhui.com)。直接覆盖到本地后: +# · 本地主系统推送会发去**生产**(本地收不到) +# · 本地轮询会用**错的 client**(invalid_client) +# · 验签 secret 不匹配(401) +# 本脚本在恢复数据后立刻把这三处改回本地,并自检 token + 拉单。 +# +# 用法: +# bash scripts/sync-prod-to-local.sh # 只同步供应商库 + 重接线 +# bash scripts/sync-prod-to-local.sh --with-main # 同时同步主系统库(canteen_delivery) +set -euo pipefail +WITH_MAIN=0 +[ "${1:-}" = "--with-main" ] && WITH_MAIN=1 + +ROOT=/home/ubuntu/harness/suppler +KEY=$ROOT/deploy-keys/id_rsa_tencent +PROD=root@129.211.180.208 +SSH="ssh -F /dev/null -i $KEY -o StrictHostKeyChecking=no" +REPO=$ROOT/SupplierSystem +export LD_LIBRARY_PATH=/home/ubuntu/harness/.pgsql/usr/lib/x86_64-linux-gnu +PG=/home/ubuntu/harness/.pgsql/usr/lib/postgresql/16/bin +SUP_PW=uarTUiYR3BNsnhYalZS1 +MAIN_PW=canteen123 +TS=$(date +%Y%m%d-%H%M%S) + +echo "== 1. 导出生产供应商库 ==" +$SSH $PROD 'su - postgres -c "pg_dump -Fc supplier_delivery"' > "$ROOT/.prod_supplier.dump" +echo " $(ls -lh $ROOT/.prod_supplier.dump | awk '{print $5}')" + +if [ "$WITH_MAIN" = "1" ]; then + echo "== 1b. 导出生产主系统库 ==" + $SSH $PROD 'su - postgres -c "pg_dump -Fc canteen_delivery"' > "$ROOT/.prod_main.dump" + echo " $(ls -lh $ROOT/.prod_main.dump | awk '{print $5}')" +fi + +echo "== 2. 恢复本地(先断开连接) ==" +for DB in supplier_delivery; do + PGPASSWORD=$SUP_PW $PG/psql -h 127.0.0.1 -U supplier -d postgres -c \ + "select pg_terminate_backend(pid) from pg_stat_activity where datname='$DB' and pid<>pg_backend_pid()" >/dev/null + PGPASSWORD=$SUP_PW $PG/pg_restore -h 127.0.0.1 -U supplier -d $DB \ + --clean --if-exists --no-owner --no-privileges "$ROOT/.prod_supplier.dump" +done +if [ "$WITH_MAIN" = "1" ]; then + PGPASSWORD=$MAIN_PW $PG/psql -h 127.0.0.1 -U canteen -d postgres -c \ + "select pg_terminate_backend(pid) from pg_stat_activity where datname='canteen_delivery' and pid<>pg_backend_pid()" >/dev/null + PGPASSWORD=$MAIN_PW $PG/pg_restore -h 127.0.0.1 -U canteen -d canteen_delivery \ + --clean --if-exists --no-owner --no-privileges "$ROOT/.prod_main.dump" +fi + +echo "== 3. 自动重接线(拉库即做) ==" +# ① 本地主系统库:webhook 指回本地供应商 +PGPASSWORD=$MAIN_PW $PG/psql -h 127.0.0.1 -U canteen -d canteen_delivery -c \ + "update public.supplier_integration_clients set webhook_url='http://127.0.0.1:3022/api/integration/main-system/webhook', updated_at=now() where status=1" >/dev/null +CID=$(PGPASSWORD=$MAIN_PW $PG/psql -h 127.0.0.1 -U canteen -d canteen_delivery -tAc "select client_id from public.supplier_integration_clients where status=1 limit 1") +WSEC=$(PGPASSWORD=$MAIN_PW $PG/psql -h 127.0.0.1 -U canteen -d canteen_delivery -tAc "select webhook_secret from public.supplier_integration_clients where status=1 limit 1") +# ② 生产该 client 的 secret(不回显) +SEC=$(cat <<'SH' | $SSH $PROD 'bash -s' 2>/dev/null | tail -1 +awk -F= '$1=="MAIN_SYSTEM_INTEGRATION_CLIENT_SECRET"{print substr($0,index($0,"=")+1)}' /opt/supplier-system/server/.env +SH +) +# ③ 写回本地 .env +ENV=$REPO/server/.env +cp "$ENV" "/tmp/env.before-rewire.$TS" +python3 - "$ENV" "$CID" "$WSEC" "$SEC" <<'PY' +import io,sys +env,cid,wsec,sec=[x.strip() for x in sys.argv[1:5]] +lines=io.open(env,encoding='utf-8').read().splitlines() +def setk(key,val): + global lines + out=[];done=False + for l in lines: + if l.startswith(key+'='): out.append(f'{key}={val}');done=True + else: out.append(l) + if not done: out.append(f'{key}={val}') + lines=out +setk('MAIN_SYSTEM_INTEGRATION_CLIENT_ID',cid) +setk('MAIN_SYSTEM_WEBHOOK_SECRET',wsec) +if len(sec)==43: setk('MAIN_SYSTEM_INTEGRATION_CLIENT_SECRET',sec) +io.open(env,'w',encoding='utf-8').write('\n'.join(lines)+'\n') +print(f' .env 已对齐 client_id={cid} webhook_secret_len={len(wsec)} client_secret_len={len(sec)}') +PY + +echo "== 4. 重启本地 API + 自检 ==" +for pid in $(pgrep -f "tsx.*src/index.ts" 2>/dev/null || true); do kill "$pid" 2>/dev/null || true; done +sleep 2 +(cd "$REPO" && nohup npm run dev:server > "$ROOT/.team/api-sync.log" 2>&1 &) +for i in $(seq 1 30); do + code=$(curl -s -o /dev/null -w '%{http_code}' -m 3 http://127.0.0.1:3022/api/health || true) + [ "$code" = "200" ] && break; sleep 2 +done +echo " api_health=$(curl -s -o /dev/null -w '%{http_code}' -m 3 http://127.0.0.1:3022/api/health)" +(cd "$REPO/server" && node --env-file=.env -e ' +const { getMainSystemToken } = require("./dist/services/mainSystem/token"); +const { importMainSystemOrders } = require("./dist/services/mainSystemOrderImport"); +(async () => { + const t = await getMainSystemToken("SUP21822638"); + console.log(" token:", t.kind, t.kind === "ok" ? "ttl=" + Math.round((t.expiresAt-Date.now())/1000) + "s" : (t.reason || "")); + const r = await importMainSystemOrders({ branchCode: "BR005", userId: null }); + console.log(" import:", JSON.stringify(r).slice(0, 200)); + process.exit(0); +})().catch((e) => { console.error(" ERR", e && e.message); process.exit(1); }); +' 2>&1 | tail -3) +echo "完成。若 token 不是 ok:先确认本地主系统 API 在 127.0.0.1:3001 运行。"