railyard/apps/ritgram/scripts/backup-database.sh
caelum0x f80b04ae0f feat(ritgram): move GitGram app into railyard as Ritgram, next to Rithub
Moved later/gitgram-ai/gitgram/ to apps/ritgram/ (fresh history; full history stays in caelum0x/gitgram).
AITimeMachine stays separate in later/gitgram-ai per the Rithub master plan.
Logs, pid files and env files are ignored; GPL-3.0 mobile/proximity noted in THIRD_PARTY_NOTICES.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-11-13 04:18:46 +03:00

93 lines
2.6 KiB
Bash
Executable File

#!/bin/bash
# Automated Backup Script for Gitgram Production Database
# This script creates automated backups with rotation and compression
set -e
# Configuration
BACKUP_DIR="${BACKUP_DIR:-/var/backups/gitgram}"
RETENTION_DAYS="${RETENTION_DAYS:-30}"
S3_BUCKET="${S3_BUCKET:-}"
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
BACKUP_FILE="gitgram_backup_${TIMESTAMP}.sql.gz"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Load environment variables
if [ -f .env.production ]; then
export $(cat .env.production | grep -v '^#' | xargs)
fi
# Check if DATABASE_URL is set
if [ -z "$DATABASE_URL" ]; then
echo -e "${RED}โŒ ERROR: DATABASE_URL is not set${NC}"
exit 1
fi
# Create backup directory if it doesn't exist
mkdir -p "$BACKUP_DIR"
echo -e "${GREEN}๐Ÿ”„ Starting database backup...${NC}"
echo "Timestamp: $TIMESTAMP"
echo "Backup file: $BACKUP_FILE"
# Create backup
echo -e "${YELLOW}๐Ÿ“ฆ Creating database dump...${NC}"
pg_dump "$DATABASE_URL" | gzip > "$BACKUP_DIR/$BACKUP_FILE"
if [ $? -eq 0 ]; then
BACKUP_SIZE=$(du -h "$BACKUP_DIR/$BACKUP_FILE" | cut -f1)
echo -e "${GREEN}โœ… Backup created successfully: $BACKUP_SIZE${NC}"
else
echo -e "${RED}โŒ Backup failed${NC}"
exit 1
fi
# Upload to S3 if configured
if [ -n "$S3_BUCKET" ]; then
echo -e "${YELLOW}โ˜๏ธ Uploading to S3...${NC}"
if command -v aws &> /dev/null; then
aws s3 cp "$BACKUP_DIR/$BACKUP_FILE" "s3://$S3_BUCKET/backups/$BACKUP_FILE"
if [ $? -eq 0 ]; then
echo -e "${GREEN}โœ… Uploaded to S3 successfully${NC}"
else
echo -e "${RED}โŒ S3 upload failed${NC}"
fi
else
echo -e "${YELLOW}โš ๏ธ AWS CLI not installed, skipping S3 upload${NC}"
fi
fi
# Remove old backups
echo -e "${YELLOW}๐Ÿงน Cleaning up old backups (older than $RETENTION_DAYS days)...${NC}"
find "$BACKUP_DIR" -name "gitgram_backup_*.sql.gz" -mtime +$RETENTION_DAYS -delete
REMAINING_BACKUPS=$(ls -1 "$BACKUP_DIR"/gitgram_backup_*.sql.gz 2>/dev/null | wc -l)
echo -e "${GREEN}โœ… Cleanup complete. Remaining backups: $REMAINING_BACKUPS${NC}"
# List recent backups
echo ""
echo -e "${GREEN}๐Ÿ“‹ Recent backups:${NC}"
ls -lh "$BACKUP_DIR"/gitgram_backup_*.sql.gz 2>/dev/null | tail -5 || echo "No backups found"
# Send notification (optional)
if [ -n "$SLACK_WEBHOOK_URL" ]; then
curl -X POST "$SLACK_WEBHOOK_URL" \
-H 'Content-Type: application/json' \
-d "{\"text\":\"โœ… Gitgram database backup completed: $BACKUP_FILE ($BACKUP_SIZE)\"}" \
&> /dev/null
fi
echo ""
echo -e "${GREEN}โœ… Backup process completed successfully!${NC}"
# Exit with success
exit 0