Skip to content

Commit e0dd656

Browse files
author
quangnhnhut123
committed
Add check-db.sh
1 parent 7678fd0 commit e0dd656

File tree

3 files changed

+103
-12
lines changed

3 files changed

+103
-12
lines changed

Dockerfile

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
11
# Use the Amazon Linux-based AWS CLI image
22
FROM amazonlinux:latest
33

4-
# Install the AWS CLI
4+
# Install the AWS CLI and other necessary tools
55
RUN yum update -y && \
6-
yum install -y aws-cli nc nano vim jq
6+
yum install -y aws-cli nc nano vim jq && \
7+
yum clean all
78

8-
COPY ./check-connections.sh /usr/local/bin/check-connections.sh
9-
COPY ./check-envs.sh /usr/local/bin/check-envs.sh
10-
COPY ./ssm-compare.sh /usr/local/bin/ssm-compare.sh
11-
COPY ./ecs-utils.sh /usr/local/bin/ecs-utils.sh
12-
COPY ./lambda-utils.sh /usr/local/bin/lambda-utils.sh
9+
RUN dnf update -y
10+
RUN dnf install mariadb105 -y
1311

14-
RUN chmod +x /usr/local/bin/check-connections.sh
15-
RUN chmod +x /usr/local/bin/check-envs.sh
16-
RUN chmod +x /usr/local/bin/ssm-compare.sh
17-
RUN chmod +x /usr/local/bin/ecs-utils.sh
18-
RUN chmod +x /usr/local/bin/lambda-utils.sh
12+
# Copy scripts to the appropriate directory and make them executable
13+
COPY ./check-connections.sh /usr/local/bin/
14+
COPY ./check-envs.sh /usr/local/bin/
15+
COPY ./ssm-compare.sh /usr/local/bin/
16+
COPY ./ecs-utils.sh /usr/local/bin/
17+
COPY ./lambda-utils.sh /usr/local/bin/
18+
COPY ./check-db.sh /usr/local/bin/
19+
20+
RUN chmod +x /usr/local/bin/check-connections.sh \
21+
/usr/local/bin/check-envs.sh \
22+
/usr/local/bin/ssm-compare.sh \
23+
/usr/local/bin/ecs-utils.sh \
24+
/usr/local/bin/lambda-utils.sh \
25+
/usr/local/bin/check-db.sh

check-db.sh

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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."

check-envs.sh

100644100755
File mode changed.

0 commit comments

Comments
 (0)