|
| 1 | +#!/bin/sh |
| 2 | +if [ $# -ne 5 ]; then |
| 3 | + echo "Usage: $0 <DB_NAME> <USER_NAME> <PASSWORD> <SOURCE_HOST_NAME> <TARGET_HOST_NAME>" |
| 4 | + exit 1 |
| 5 | +fi |
| 6 | + |
| 7 | +DB_NAME=$1 |
| 8 | +USER_NAME=$2 |
| 9 | +PASSWORD=$3 |
| 10 | +SOURCE_HOST_NAME=$4 |
| 11 | +TARGET_HOST_NAME=$5 |
| 12 | + |
| 13 | +# Capture start time |
| 14 | +START_TIME=$(date +%s) |
| 15 | + |
| 16 | +# Check if the database exists on both hosts |
| 17 | +DATABASE_1=$(mysql -u "$USER_NAME" -p"$PASSWORD" -h $SOURCE_HOST_NAME -P 3306 -e "SHOW DATABASES;" 2>/dev/null | grep "^$DB_NAME$") |
| 18 | +DATABASE_2=$(mysql -u "$USER_NAME" -p"$PASSWORD" -h $TARGET_HOST_NAME -P 3306 -e "SHOW DATABASES;" 2>/dev/null | grep "^$DB_NAME$") |
| 19 | + |
| 20 | +if [ -z "$DATABASE_1" ] || [ -z "$DATABASE_2" ]; then |
| 21 | + echo "Database not found on one or both hosts or unable to connect to the MySQL server." |
| 22 | + exit 1 |
| 23 | +fi |
| 24 | + |
| 25 | +echo "=====================================================================================================" |
| 26 | +echo "Comparing databases: $DB_NAME" |
| 27 | +echo "ORIGIN DB: $SOURCE_HOST_NAME" |
| 28 | +echo "MIGRATED DB: $TARGET_HOST_NAME" |
| 29 | +echo "=====================================================================================================" |
| 30 | +SOURCE_HOST_NAME_SHORT=$(echo $SOURCE_HOST_NAME | cut -d'.' -f1) |
| 31 | +TARGET_HOST_NAME_SHORT=$(echo $TARGET_HOST_NAME | cut -d'.' -f1) |
| 32 | + |
| 33 | +# Function to print text with word wrapping for multiple columns |
| 34 | +print_wrapped() { |
| 35 | + local text="$1" |
| 36 | + local width1="$2" |
| 37 | + local width2="$3" |
| 38 | + local width3="$4" |
| 39 | + local wrapped_text="" |
| 40 | + local col1 col2 col3 |
| 41 | + |
| 42 | + while [ ${#text} -gt $((width1 + width2 + width3 + 6)) ]; do |
| 43 | + col1="${text:0:$width1}" |
| 44 | + col2="${text:$width1:$width2}" |
| 45 | + col3="${text:$((width1 + width2)):$width3}" |
| 46 | + wrapped_text="$wrapped_text${col1}\n${col2}\n${col3}\n" |
| 47 | + text="${text:$((width1 + width2 + width3))}" |
| 48 | + done |
| 49 | + |
| 50 | + col1="${text:0:$width1}" |
| 51 | + col2="${text:$width1:$width2}" |
| 52 | + col3="${text:$((width1 + width2)):$width3}" |
| 53 | + wrapped_text="$wrapped_text${col1}\n${col2}\n${col3}" |
| 54 | + echo -e "$wrapped_text" |
| 55 | +} |
| 56 | + |
| 57 | +printf "%-30s | %-20s | %-20s | %-10s\n" "Table Name" "ORIGIN DB" "MIGRATED DB" "Status" |
| 58 | +echo "-----------------------------------------------------------------------------------------------------" |
| 59 | + |
| 60 | +# Get the list of tables in the base database on the first host |
| 61 | +TABLES=$(mysql -N -B -u "$USER_NAME" -p"$PASSWORD" -h $SOURCE_HOST_NAME -P 3306 -e "SHOW TABLES IN $DB_NAME;" 2>/dev/null) |
| 62 | + |
| 63 | +for TABLE in $TABLES; do |
| 64 | + HOST_1_ROWS=$(mysql -N -B -u "$USER_NAME" -p"$PASSWORD" -h $SOURCE_HOST_NAME -P 3306 -e "SELECT COUNT(*) FROM $DB_NAME.$TABLE;" 2>/dev/null) |
| 65 | + HOST_2_ROWS=$(mysql -N -B -u "$USER_NAME" -p"$PASSWORD" -h $TARGET_HOST_NAME -P 3306 -e "SELECT COUNT(*) FROM $DB_NAME.$TABLE;" 2>/dev/null) |
| 66 | + if [ "$HOST_1_ROWS" -eq "$HOST_2_ROWS" ]; then |
| 67 | + STATUS=$(tput setaf 2)"Consistent"$(tput sgr0) # Green color |
| 68 | + else |
| 69 | + STATUS=$(tput setaf 1)"Inconsistent"$(tput sgr0) # Red color |
| 70 | + fi |
| 71 | + TABLE_WRAPPED=$(print_wrapped "$TABLE" 30 20 20) |
| 72 | + while IFS= read -r line; do |
| 73 | + printf "%-30s | %-20s | %-20s | %-10s\n" "$line" "$HOST_1_ROWS rows" "$HOST_2_ROWS rows" "$STATUS" |
| 74 | + done <<< "$TABLE_WRAPPED" |
| 75 | +done |
| 76 | + |
| 77 | +# Capture end time |
| 78 | +END_TIME=$(date +%s) |
| 79 | + |
| 80 | +# Calculate duration |
| 81 | +DURATION=$((END_TIME - START_TIME)) |
| 82 | +DURATION_MINUTES=$((DURATION / 60)) |
| 83 | +DURATION_SECONDS=$((DURATION % 60)) |
| 84 | +echo "Running completed in $DURATION_MINUTES minutes and $DURATION_SECONDS seconds." |
0 commit comments