#!/bin/bash

# Database configuration
DB_USER="root"
DB_PASS="Piglet1969!!"
DB_NAME="ukdataservices"
DB_HOST="localhost"

# Backup filename with timestamp
BACKUP_FILE="db_backup_$(date +%y%m%d_%H%M%S).sql"

echo "Starting database backup..."

# Create database backup
mysqldump -u"$DB_USER" -p"$DB_PASS" -h"$DB_HOST" "$DB_NAME" > "$BACKUP_FILE"

if [ $? -eq 0 ]; then
    echo "Database backup created: $BACKUP_FILE"
    
    # Add all changes to git
    echo "Adding changes to git..."
    git add -A
    
    # Commit with timestamp
    COMMIT_MSG="Backup database and code changes - $(date '+%Y-%m-%d %H:%M:%S')"
    git commit -m "$COMMIT_MSG"
    
    if [ $? -eq 0 ]; then
        echo "Changes committed successfully!"
        echo "Commit message: $COMMIT_MSG"
    else
        echo "No changes to commit or commit failed"
    fi
else
    echo "Database backup failed!"
    exit 1
fi

echo "Script completed."
