#!/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 "$@"