209 lines
5.8 KiB
Bash
Executable File
209 lines
5.8 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
# Script untuk mengimport database backup ke MySQL Docker container
|
|
# Usage: ./docker-import-db.sh [dev|prod] [database_file.sql]
|
|
|
|
set -e
|
|
|
|
# Colors for output
|
|
RED='\033[0;31m'
|
|
GREEN='\033[0;32m'
|
|
YELLOW='\033[1;33m'
|
|
BLUE='\033[0;34m'
|
|
NC='\033[0m' # No Color
|
|
|
|
# Default values
|
|
ENVIRONMENT="dev"
|
|
SQL_FILE="ckb.sql"
|
|
|
|
# Parse arguments
|
|
while [[ $# -gt 0 ]]; do
|
|
case $1 in
|
|
dev|development)
|
|
ENVIRONMENT="dev"
|
|
shift
|
|
;;
|
|
prod|production|staging)
|
|
ENVIRONMENT="prod"
|
|
shift
|
|
;;
|
|
*.sql)
|
|
SQL_FILE="$1"
|
|
shift
|
|
;;
|
|
*)
|
|
echo "Unknown option $1"
|
|
echo "Usage: $0 [dev|prod] [database_file.sql]"
|
|
exit 1
|
|
;;
|
|
esac
|
|
done
|
|
|
|
# Function to print colored output
|
|
print_status() {
|
|
echo -e "${BLUE}[INFO]${NC} $1"
|
|
}
|
|
|
|
print_success() {
|
|
echo -e "${GREEN}[SUCCESS]${NC} $1"
|
|
}
|
|
|
|
print_warning() {
|
|
echo -e "${YELLOW}[WARNING]${NC} $1"
|
|
}
|
|
|
|
print_error() {
|
|
echo -e "${RED}[ERROR]${NC} $1"
|
|
}
|
|
|
|
# Function to check if file exists
|
|
check_sql_file() {
|
|
if [[ ! -f "$SQL_FILE" ]]; then
|
|
print_error "SQL file '$SQL_FILE' not found!"
|
|
exit 1
|
|
fi
|
|
print_status "Found SQL file: $SQL_FILE ($(du -h "$SQL_FILE" | cut -f1))"
|
|
}
|
|
|
|
# Function to check if containers are running
|
|
check_containers() {
|
|
local compose_file=""
|
|
local db_container=""
|
|
|
|
if [[ $ENVIRONMENT == "dev" ]]; then
|
|
compose_file="docker-compose.yml"
|
|
db_container="ckb-mysql"
|
|
else
|
|
compose_file="docker-compose.prod.yml"
|
|
db_container="ckb-mysql-prod"
|
|
fi
|
|
|
|
if ! docker-compose -f "$compose_file" ps | grep -q "$db_container.*Up"; then
|
|
print_error "Database container is not running!"
|
|
print_status "Please start the containers first:"
|
|
if [[ $ENVIRONMENT == "dev" ]]; then
|
|
echo " ./docker-start.sh dev up"
|
|
else
|
|
echo " ./docker-start.sh prod up"
|
|
fi
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
# Function to get database credentials
|
|
get_db_credentials() {
|
|
if [[ $ENVIRONMENT == "dev" ]]; then
|
|
DB_HOST="ckb-mysql"
|
|
DB_NAME="ckb_db"
|
|
DB_USER="root"
|
|
DB_PASSWORD="root"
|
|
COMPOSE_FILE="docker-compose.yml"
|
|
else
|
|
DB_HOST="ckb-mysql-prod"
|
|
DB_NAME="ckb_production"
|
|
DB_USER="root"
|
|
# For production, we should read from environment or prompt
|
|
if [[ -f .env.production ]]; then
|
|
DB_PASSWORD=$(grep DB_ROOT_PASSWORD .env.production | cut -d '=' -f2)
|
|
else
|
|
read -s -p "Enter MySQL root password for production: " DB_PASSWORD
|
|
echo
|
|
fi
|
|
COMPOSE_FILE="docker-compose.prod.yml"
|
|
fi
|
|
}
|
|
|
|
# Function to backup existing database
|
|
backup_existing_db() {
|
|
local backup_file="backup_before_import_$(date +%Y%m%d_%H%M%S).sql"
|
|
|
|
print_status "Creating backup of existing database..."
|
|
|
|
if docker-compose -f "$COMPOSE_FILE" exec -T db mysqldump -u "$DB_USER" -p"$DB_PASSWORD" "$DB_NAME" > "$backup_file" 2>/dev/null; then
|
|
print_success "Existing database backed up to: $backup_file"
|
|
else
|
|
print_warning "Could not backup existing database (it might be empty)"
|
|
fi
|
|
}
|
|
|
|
# Function to import database
|
|
import_database() {
|
|
print_status "Importing database from $SQL_FILE..."
|
|
print_status "This may take a while for large files..."
|
|
|
|
# Drop and recreate database to ensure clean import
|
|
print_status "Recreating database..."
|
|
docker-compose -f "$COMPOSE_FILE" exec -T db mysql -u "$DB_USER" -p"$DB_PASSWORD" -e "DROP DATABASE IF EXISTS $DB_NAME;"
|
|
docker-compose -f "$COMPOSE_FILE" exec -T db mysql -u "$DB_USER" -p"$DB_PASSWORD" -e "CREATE DATABASE $DB_NAME CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"
|
|
|
|
# Import the SQL file
|
|
if docker-compose -f "$COMPOSE_FILE" exec -T db mysql -u "$DB_USER" -p"$DB_PASSWORD" "$DB_NAME" < "$SQL_FILE"; then
|
|
print_success "Database imported successfully!"
|
|
else
|
|
print_error "Failed to import database!"
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
# Function to run Laravel migrations (if needed)
|
|
run_migrations() {
|
|
print_status "Checking if Laravel migrations need to be run..."
|
|
|
|
if docker-compose -f "$COMPOSE_FILE" exec app php artisan migrate:status > /dev/null 2>&1; then
|
|
print_status "Running any pending migrations..."
|
|
docker-compose -f "$COMPOSE_FILE" exec app php artisan migrate --force
|
|
else
|
|
print_warning "Could not check migration status. Skipping migrations."
|
|
fi
|
|
}
|
|
|
|
# Function to clear Laravel cache
|
|
clear_cache() {
|
|
print_status "Clearing Laravel cache..."
|
|
docker-compose -f "$COMPOSE_FILE" exec app php artisan cache:clear || true
|
|
docker-compose -f "$COMPOSE_FILE" exec app php artisan config:clear || true
|
|
docker-compose -f "$COMPOSE_FILE" exec app php artisan view:clear || true
|
|
}
|
|
|
|
# Main execution
|
|
print_status "Database Import Script for CKB Laravel Application"
|
|
print_status "Environment: $ENVIRONMENT"
|
|
print_status "SQL File: $SQL_FILE"
|
|
echo ""
|
|
|
|
# Check prerequisites
|
|
check_sql_file
|
|
get_db_credentials
|
|
check_containers
|
|
|
|
# Ask for confirmation
|
|
echo ""
|
|
print_warning "This will replace the existing database in $ENVIRONMENT environment!"
|
|
read -p "Are you sure you want to continue? (y/N): " -n 1 -r
|
|
echo ""
|
|
|
|
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
|
|
print_status "Import cancelled."
|
|
exit 0
|
|
fi
|
|
|
|
# Execute import
|
|
backup_existing_db
|
|
import_database
|
|
|
|
# Post-import tasks
|
|
print_status "Running post-import tasks..."
|
|
run_migrations
|
|
clear_cache
|
|
|
|
echo ""
|
|
print_success "Database import completed successfully!"
|
|
print_status "Database: $DB_NAME"
|
|
print_status "Environment: $ENVIRONMENT"
|
|
|
|
if [[ $ENVIRONMENT == "dev" ]]; then
|
|
echo ""
|
|
print_status "You can now access your application at:"
|
|
echo " - Web App: http://localhost:8000"
|
|
echo " - phpMyAdmin: http://localhost:8080"
|
|
fi |