Files
trading_bot_v4/ha-setup/sync-db-daily.sh
mindesbunister 880aae9a77 feat: Add High Availability setup roadmap and scripts
Created comprehensive HA roadmap with 6 phases:
- Phase 1: Warm standby (CURRENT - manual failover)
- Phase 2: Database replication
- Phase 3: Health monitoring
- Phase 4: Reverse proxy + floating IP
- Phase 5: Automated failover
- Phase 6: Geographic redundancy

Includes:
- Decision gates based on capital and stability
- Cost-benefit analysis
- Scripts for healthcheck, failover, DB sync
- Recommendation to defer full HA until capital > $5k

Secondary server ready at 72.62.39.24 for emergency manual failover.

Related: User concern about system uptime, but full HA complexity
not justified at current scale (~$600 capital). Revisit in Q1 2026.
2025-11-19 20:52:12 +01:00

77 lines
2.2 KiB
Bash

#!/bin/bash
#
# Daily Database Sync from Primary to Secondary
# Run on PRIMARY server via cron
#
set -eu
PRIMARY_HOST="localhost"
SECONDARY_HOST="root@72.62.39.24"
PROJECT_DIR="/home/icke/traderv4"
BACKUP_FILE="/tmp/trading_bot_backup_$(date +%Y%m%d_%H%M%S).sql"
LOG_FILE="/var/log/trading-bot-db-sync.log"
log() {
echo "[$(date +'%Y-%m-%d %H:%M:%S')] $*" | tee -a "$LOG_FILE"
}
# Telegram notification
telegram_notify() {
local message="$1"
if [ -f "${PROJECT_DIR}/.env" ]; then
source "${PROJECT_DIR}/.env"
curl -s -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage" \
-d chat_id="${TELEGRAM_CHAT_ID}" \
-d text="📊 DB Sync: ${message}" \
-d parse_mode="HTML" > /dev/null
fi
}
main() {
log "🔄 Starting daily database backup..."
# Create backup
if docker exec trading-bot-postgres pg_dump -U postgres trading_bot_v4 > "$BACKUP_FILE" 2>>"$LOG_FILE"; then
local size=$(du -h "$BACKUP_FILE" | cut -f1)
log "✅ Backup created: $BACKUP_FILE ($size)"
else
log "❌ Backup failed!"
telegram_notify "⚠️ Database backup failed on primary"
exit 1
fi
# Transfer to secondary
log "📤 Transferring to secondary..."
if rsync -avz --compress "$BACKUP_FILE" "${SECONDARY_HOST}:/tmp/" >> "$LOG_FILE" 2>&1; then
log "✅ Transfer complete"
else
log "❌ Transfer failed!"
telegram_notify "⚠️ Database transfer to secondary failed"
exit 1
fi
# Restore on secondary
log "📥 Restoring on secondary..."
if ssh "${SECONDARY_HOST}" "docker exec -i trading-bot-postgres psql -U postgres trading_bot_v4 < /tmp/$(basename $BACKUP_FILE)" >> "$LOG_FILE" 2>&1; then
log "✅ Restore complete on secondary"
else
log "❌ Restore failed on secondary!"
telegram_notify "⚠️ Database restore failed on secondary"
exit 1
fi
# Cleanup old backups (keep last 7 days)
find /tmp -name "trading_bot_backup_*.sql" -mtime +7 -delete
ssh "${SECONDARY_HOST}" "find /tmp -name 'trading_bot_backup_*.sql' -mtime +7 -delete"
log "🎉 Daily sync completed successfully"
# Only notify on first sync of the day or if there were issues
if [ "$(date +%H)" -eq 2 ]; then
telegram_notify "✅ Daily database sync completed"
fi
}
main "$@"