Files
ewoooc/scripts/pg_backup.sh
ogt 0099543c05
Some checks failed
CD Pipeline / deploy (push) Failing after 5m18s
fix(security): 全域健檢 — 40 項安全/Bug/品質修復
🔴 Critical
- auto_heal_service: 補 import re + sqlalchemy.text + 修正 orchestrator 變數名
  + autoheal_playbook→playbooks 表名 + _alert_and_store cooldown 修復
- aider_heal_executor: shell injection 改 shell=False + list 參數
- docker-compose: DISABLE_LOGIN 改 env var + 移除密碼 fallback + POSTGRES_HOST 修正
- app.py: /api/backup /api/run_task 等 6 個管理 API 加 @login_required
- config.py + pg_sync + e2e_test: 移除 wooo_pg_2026 hardcoded 密碼 fallback
- pg_backup.sh: 移除 TELEGRAM_TOKEN= 中間變數,直接用 $TELEGRAM_BOT_TOKEN
- migration 014: trigger_pattern→match_pattern + 補 error_type NOT NULL 欄位

🟡 High
- telegram_bot_service: str(e) 改通用訊息 + session try/finally + 移除 pa:/pr: 舊 callback
- run_scheduler: ElephantAlpha thread 死亡監控 + 自動重啟 + Telegram 告警
  + agent_context 03:30 TTL 定時清理任務
- openclaw_learning_service: build_rag_context 兩路徑加 .limit(200)
- hooks: commit-quality + momo-prod-guard 空 catch 改 stderr+exit(1)
- scripts/code_review: auto_yes 預設改 false
- db_backup_service: PGPASSWORD 透過 env dict 傳遞

📦 Migrations
- 013_autoheal: 修正建表順序 playbooks→incidents(外鍵前向引用)
- 018_add_missing_indexes: heal_logs/incidents 外鍵索引 + cleanup_expired_agent_context()

🟢 Infrastructure
- requirements.txt: 加版本下界 Flask>=2.3 SQLAlchemy>=1.4 等
- cd.yaml: 新增 run_scheduler.py + run_telegram_bot.py 監聽路徑
- .gitignore: insert_playbook_local.py 加入忽略

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-22 01:12:23 +08:00

72 lines
2.8 KiB
Bash
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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
# EwoooC PostgreSQL 備份腳本 (Host-Level)
# 執行環境192.168.0.188 host每日 02:00 cron 觸發
# pg_dump 在 momo-db container 內執行
set -euo pipefail
BACKUP_DIR="/home/ollama/momo_backups"
DB_CONTAINER="momo-db"
DB_USER="momo"
DB_NAME="momo_analytics"
DB_PASS="${POSTGRES_PASSWORD}"
KEEP_DAYS=7
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
FILENAME="momo_analytics_${TIMESTAMP}.sql.gz"
FILEPATH="${BACKUP_DIR}/${FILENAME}"
LOG_FILE="${BACKUP_DIR}/backup.log"
TG_CHAT="${TELEGRAM_ADMIN_CHAT_ID:-5619078117}"
log() { echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" | tee -a "$LOG_FILE"; }
send_tg() {
curl -s -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage" \
-d "chat_id=${TG_CHAT}&text=$1&parse_mode=HTML" > /dev/null 2>&1 || true
}
mkdir -p "$BACKUP_DIR"
log "===== EwoooC DB Backup 開始 ====="
START=$(date +%s)
# 執行 pg_dump在 momo-db container 內,透過 docker exec
if PGPASSWORD="$DB_PASS" docker exec -e PGPASSWORD="$DB_PASS" \
"$DB_CONTAINER" pg_dump -U "$DB_USER" -d "$DB_NAME" --no-password | \
gzip > "$FILEPATH"; then
END=$(date +%s)
DURATION=$((END - START))
SIZE=$(du -h "$FILEPATH" | cut -f1)
SIZE_BYTES=$(stat -c%s "$FILEPATH" 2>/dev/null || stat -f%z "$FILEPATH" 2>/dev/null || echo 0)
log "✅ 備份成功: $FILENAME ($SIZE, ${DURATION}s)"
# 寫入 backup_logPostgreSQL
docker exec -e PGPASSWORD="$DB_PASS" "$DB_CONTAINER" psql -U "$DB_USER" -d "$DB_NAME" -c \
"INSERT INTO backup_log (filename, file_size_bytes, duration_seconds, status, host, storage_path, completed_at)
VALUES ('$FILENAME', $SIZE_BYTES, $DURATION, 'success', '$(hostname)', '$FILEPATH', CURRENT_TIMESTAMP);" \
> /dev/null 2>&1 || log "⚠️ backup_log 寫入失敗(不影響備份本體)"
# 清理舊備份
DELETED=$(find "$BACKUP_DIR" -name "momo_analytics_*.sql.gz" -mtime +${KEEP_DAYS} -print -delete | wc -l)
log "🗑 清理舊備份:${DELETED}"
MSG="💾 EwoooC DB 備份完成%0A✅ 狀態:成功%0A📁 ${FILENAME}%0A📦 大小:${SIZE}%0A⏱ 耗時:${DURATION}秒%0A🗑 清理:${DELETED} 個舊備份"
send_tg "$MSG"
else
END=$(date +%s)
DURATION=$((END - START))
log "❌ 備份失敗!"
docker exec -e PGPASSWORD="$DB_PASS" "$DB_CONTAINER" psql -U "$DB_USER" -d "$DB_NAME" -c \
"INSERT INTO backup_log (filename, file_size_bytes, duration_seconds, status, host, storage_path, error_message, completed_at)
VALUES ('$FILENAME', 0, $DURATION, 'failed', '$(hostname)', '$BACKUP_DIR', 'pg_dump 執行失敗', CURRENT_TIMESTAMP);" \
> /dev/null 2>&1 || true
MSG="🚨 EwoooC DB 備份失敗%0A❌ 時間:$(date '+%Y-%m-%d %H:%M')%0A⚠ 請立即檢查 momo-db 容器!"
send_tg "$MSG"
exit 1
fi
log "===== Backup 完成 ====="