Spaces:
Running
Running
File size: 1,545 Bytes
cd61bf7 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | #!/bin/bash
# PostgreSQL backup script for MicroFunnel
# Run this script daily via cron: 0 2 * * * /path/to/backup_postgres.sh
# Configuration
BACKUP_DIR="/backups/postgres"
TIMESTAMP=$(date +"%Y%m%d_%H%M%S")
BACKUP_FILE="microfunnel_backup_${TIMESTAMP}.sql.gz"
DAYS_TO_KEEP=30
# Database credentials (read from env or .env file)
source /path/to/your/.env
DB_HOST="${POSTGRES_HOST:-localhost}"
DB_PORT="${POSTGRES_PORT:-5432}"
DB_NAME="${POSTGRES_DB:-microfunnel}"
DB_USER="${POSTGRES_USER:-postgres}"
# S3 Configuration (optional - for cloud backups)
S3_BUCKET="${S3_BACKUP_BUCKET}"
S3_PATH="postgres-backups/"
# Create backup directory if it doesn't exist
mkdir -p $BACKUP_DIR
# Perform backup
echo "Starting backup at $(date)"
pg_dump -h $DB_HOST -p $DB_PORT -U $DB_USER -d $DB_NAME | gzip > "${BACKUP_DIR}/${BACKUP_FILE}"
if [ $? -eq 0 ]; then
echo "Backup completed successfully: ${BACKUP_FILE}"
# Upload to S3 if configured
if [ ! -z "$S3_BUCKET" ]; then
echo "Uploading to S3..."
aws s3 cp "${BACKUP_DIR}/${BACKUP_FILE}" "s3://${S3_BUCKET}/${S3_PATH}${BACKUP_FILE}"
if [ $? -eq 0 ]; then
echo "S3 upload successful"
else
echo "ERROR: S3 upload failed"
fi
fi
# Clean up old backups (keep last 30 days)
echo "Cleaning up old backups..."
find $BACKUP_DIR -name "microfunnel_backup_*.sql.gz" -mtime +$DAYS_TO_KEEP -delete
echo "Backup process completed at $(date)"
else
echo "ERROR: Backup failed"
exit 1
fi
|