- Add Go modules with required dependencies (Gin, UUID, JWT, etc.) - Implement main web server with landing page endpoint - Add comprehensive API endpoints for health and status - Include proper error handling and request validation - Set up CORS middleware and security headers
147 lines
3.9 KiB
Bash
Executable File
147 lines
3.9 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
# YourDreamNameHere Backup Script
|
|
# This script creates automated backups of the PostgreSQL database
|
|
|
|
set -euo pipefail
|
|
|
|
# Configuration
|
|
DB_HOST="${DB_HOST:-ydn-db}"
|
|
DB_PORT="${DB_PORT:-5432}"
|
|
DB_USER="${DB_USER:-ydn_user}"
|
|
DB_NAME="${DB_NAME:-ydn_db}"
|
|
DOLIBARR_DB="dolibarr_db"
|
|
BACKUP_DIR="/backups"
|
|
TIMESTAMP=$(date +"%Y%m%d_%H%M%S")
|
|
RETENTION_DAYS=30
|
|
|
|
# Create backup directory if it doesn't exist
|
|
mkdir -p "$BACKUP_DIR"
|
|
|
|
echo "Starting database backup at $(date)"
|
|
|
|
# Function to create backup
|
|
create_backup() {
|
|
local database=$1
|
|
local filename="${database}_backup_${TIMESTAMP}.sql"
|
|
local filepath="$BACKUP_DIR/$filename"
|
|
|
|
echo "Creating backup for database: $database"
|
|
|
|
# Create compressed backup
|
|
pg_dump -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" \
|
|
--no-password --verbose --clean --if-exists \
|
|
--format=custom --compress=9 \
|
|
--file="$filepath" "$database"
|
|
|
|
if [ $? -eq 0 ]; then
|
|
echo "Backup created successfully: $filepath"
|
|
|
|
# Create checksum for integrity verification
|
|
sha256sum "$filepath" > "${filepath}.sha256"
|
|
|
|
# Compress further with gzip if needed
|
|
# gzip "$filepath"
|
|
|
|
echo "Backup size: $(du -h "$filepath" | cut -f1)"
|
|
else
|
|
echo "Failed to create backup for database: $database"
|
|
return 1
|
|
fi
|
|
}
|
|
|
|
# Function to clean old backups
|
|
cleanup_old_backups() {
|
|
echo "Cleaning up backups older than $RETENTION_DAYS days"
|
|
|
|
# Remove old SQL backups
|
|
find "$BACKUP_DIR" -name "*_backup_*.sql" -type f -mtime +$RETENTION_DAYS -delete
|
|
find "$BACKUP_DIR" -name "*_backup_*.sql.sha256" -type f -mtime +$RETENTION_DAYS -delete
|
|
|
|
echo "Cleanup completed"
|
|
}
|
|
|
|
# Function to verify backup integrity
|
|
verify_backup() {
|
|
local filepath=$1
|
|
local checksum_file="${filepath}.sha256"
|
|
|
|
if [ -f "$checksum_file" ]; then
|
|
if sha256sum -c "$checksum_file" >/dev/null 2>&1; then
|
|
echo "Backup integrity verified: $filepath"
|
|
return 0
|
|
else
|
|
echo "Backup integrity check failed: $filepath"
|
|
return 1
|
|
fi
|
|
else
|
|
echo "Checksum file not found for: $filepath"
|
|
return 1
|
|
fi
|
|
}
|
|
|
|
# Main execution
|
|
echo "========================================"
|
|
echo "Database Backup Started: $(date)"
|
|
echo "========================================"
|
|
|
|
# Set password for PostgreSQL if environment variable is set
|
|
if [ -n "${DB_PASSWORD:-}" ]; then
|
|
export PGPASSWORD="$DB_PASSWORD"
|
|
fi
|
|
|
|
# Create backups
|
|
BACKUP_SUCCESS=true
|
|
|
|
echo "Backing up main application database..."
|
|
if create_backup "$DB_NAME"; then
|
|
# Verify main backup
|
|
main_backup="$BACKUP_DIR/${DB_NAME}_backup_${TIMESTAMP}.sql"
|
|
if ! verify_backup "$main_backup"; then
|
|
BACKUP_SUCCESS=false
|
|
fi
|
|
else
|
|
BACKUP_SUCCESS=false
|
|
fi
|
|
|
|
echo "Backing up Dolibarr database..."
|
|
if create_backup "$DOLIBARR_DB"; then
|
|
# Verify Dolibarr backup
|
|
dolibarr_backup="$BACKUP_DIR/${DOLIBARR_DB}_backup_${TIMESTAMP}.sql"
|
|
if ! verify_backup "$dolibarr_backup"; then
|
|
BACKUP_SUCCESS=false
|
|
fi
|
|
else
|
|
BACKUP_SUCCESS=false
|
|
fi
|
|
|
|
# Clean old backups
|
|
cleanup_old_backups
|
|
|
|
# Summary
|
|
echo "========================================"
|
|
echo "Database Backup Completed: $(date)"
|
|
|
|
if [ "$BACKUP_SUCCESS" = true ]; then
|
|
echo "✅ All backups completed successfully"
|
|
|
|
# List current backups
|
|
echo "Current backups:"
|
|
ls -lh "$BACKUP_DIR"/*_backup_*.sql 2>/dev/null || echo "No backup files found"
|
|
else
|
|
echo "❌ Some backups failed"
|
|
exit 1
|
|
fi
|
|
|
|
echo "========================================"
|
|
|
|
# Send notification if webhook URL is configured
|
|
if [ -n "${BACKUP_WEBHOOK_URL:-}" ]; then
|
|
status=$([ "$BACKUP_SUCCESS" = true ] && echo "success" || echo "failed")
|
|
curl -X POST "$BACKUP_WEBHOOK_URL" \
|
|
-H "Content-Type: application/json" \
|
|
-d "{\"text\":\"Database backup $status at $(date)\",\"status\":\"$status\"}" \
|
|
2>/dev/null || true
|
|
fi
|
|
|
|
exit 0 |