Tuesday, April 14, 2026

.::: Shell Script Sync Manual Table, Add Column, Alter Column Compare Database (teguhth,aisyah) in MariaDB, MySQL, MarinaDB :::.

 

corelation https://teguhth.blogspot.com/2026/04/create-store-procedure-sync-manual_10.html
https://teguhth.blogspot.com/2026/04/sync-manual-table-add-column-alter_10.html

1. script 

run_sp_compare_table.sh
run_sp_compare_add_column_table.sh
run_sp_compare_alter_column_table.sh

run_sp_compare_table_csv.sh
run_sp_compare_add_column_table_csv.sh
run_sp_compare_alter_column_table_csv.sh

2. run

sh run_sp_compare_table.sh teguhth aisyah
sh run_sp_compare_add_column_table.sh teguhth aisyah
sh run_sp_compare_alter_column_table.sh teguhth aisyah

sh run_sp_compare_table_csv.sh teguhth aisyah
sh run_sp_compare_add_column_table_csv.sh teguhth aisyah
sh run_sp_compare_alter_column_table_csv.sh teguhth aisyah

  
 


3. run script compare

[root@teguhth-all comp]# pwd
/data/comp
[root@teguhth-all comp]# cat run_sp_compare_table.sh
#!/bin/bash

# Parameter
DB_HOST="10.10.10.90"
DB_PORT="3306"
DB_USER="admin"
DB_PASS="admin"
DB_NAME="dbatools"

PARAM1="$1"
PARAM2="$2"

# Validasi parameter
if [ -z "$PARAM1" ] || [ -z "$PARAM2" ]; then
    echo "Usage: $0 <param1> <param2>"
    echo "Example: $0 dbsource dbdestination"
    exit 1
fi

# Eksekusi stored procedure
mariadb -h "$DB_HOST" -P "$DB_PORT" -u "$DB_USER" -p"$DB_PASS" "$DB_NAME" -e "CALL sp_compare_table('$PARAM1', '$PARAM2');"


# Cek status
if [ $? -eq 0 ]; then
    echo "Stored procedure executed successfully."
else
    echo "Error executing stored procedure."
fi
[root@teguhth-all comp]#


[root@teguhth-all comp]# cat run_sp_compare_add_column_table.sh
#!/bin/bash

# Parameter
DB_HOST="10.10.10.90"
DB_PORT="3306"
DB_USER="admin"
DB_PASS="admin"
DB_NAME="dbatools"

PARAM1="$1"
PARAM2="$2"

# Validasi parameter
if [ -z "$PARAM1" ] || [ -z "$PARAM2" ]; then
    echo "Usage: $0 <param1> <param2>"
    echo "Example: $0 dbsource dbdestination"
    exit 1
fi

# Eksekusi stored procedure
mariadb -h "$DB_HOST" -P "$DB_PORT" -u "$DB_USER" -p"$DB_PASS" "$DB_NAME" -e "CALL sp_compare_add_column_table('$PARAM1', '$PARAM2');"


# Cek status
if [ $? -eq 0 ]; then
    echo "Stored procedure executed successfully."
else
    echo "Error executing stored procedure."
fi
[root@teguhth-all comp]#


[root@teguhth-all comp]# cat run_sp_compare_alter_column_table.sh
#!/bin/bash

# Parameter
DB_HOST="10.10.10.90"
DB_PORT="3306"
DB_USER="admin"
DB_PASS="admin"
DB_NAME="dbatools"

PARAM1="$1"
PARAM2="$2"

# Validasi parameter
if [ -z "$PARAM1" ] || [ -z "$PARAM2" ]; then
    echo "Usage: $0 <param1> <param2>"
    echo "Example: $0 dbsource dbdestination"
    exit 1
fi

# Eksekusi stored procedure
mariadb -h "$DB_HOST" -P "$DB_PORT" -u "$DB_USER" -p"$DB_PASS" "$DB_NAME" -e "CALL sp_compare_alter_column_table('$PARAM1', '$PARAM2');"


# Cek status
if [ $? -eq 0 ]; then
    echo "Stored procedure executed successfully."
else
    echo "Error executing stored procedure."
fi
[root@teguhth-all comp]#



4. run script compare with csv 


[root@teguhth-all comp]# cat run_sp_compare_table_csv.sh
#!/bin/bash

# Parameter
DB_HOST="10.10.10.90"
DB_PORT="3306"
DB_USER="admin"
DB_PASS="admin"
DB_NAME="dbatools"

PARAM1="$1"
PARAM2="$2"

OUTPUT_FILE="compare_sp_compare_table_${PARAM1}_vs_${PARAM2}.csv"

# Validasi parameter
if [ -z "$PARAM1" ] || [ -z "$PARAM2" ]; then
    echo "Usage: $0 <param1> <param2>"
    echo "Example: $0 dbsource dbdestination"
    exit 1
fi

echo "Running stored procedure..."

# ?? Eksekusi + gabungkan jadi 1 baris per statement
mariadb -h "$DB_HOST" -P "$DB_PORT" -u "$DB_USER" -p"$DB_PASS" "$DB_NAME" \
--batch --raw --skip-column-names \
-e "CALL sp_compare_table('$PARAM1', '$PARAM2');" \
| sed 's/\r//g' \
| awk '
BEGIN { RS=";"; ORS="" }
{
    gsub(/\n/, " ");      # hapus newline ? jadi 1 baris
    gsub(/\t/, " ");      # hapus tab
    gsub(/  +/, " ");     # rapikan spasi
    sub(/^ +/, "", $0);   # ?? hapus spasi di depan
    sub(/ +$/, "", $0);   # ?? hapus spasi di belakang
    if (length($0) > 5) { # skip kosong
        print "\"" $0 ";\"\n"
    }
}
' > "$OUTPUT_FILE"

# Cek status
if [ $? -eq 0 ]; then
    echo "Stored procedure executed successfully."
    echo "Output saved to: $OUTPUT_FILE"
else
    echo "Error executing stored procedure."
fi
[root@teguhth-all comp]#



[root@teguhth-all comp]# cat run_sp_compare_add_column_table_csv.sh
#!/bin/bash

# Parameter
DB_HOST="10.10.10.90"
DB_PORT="3306"
DB_USER="admin"
DB_PASS="admin"
DB_NAME="dbatools"

PARAM1="$1"
PARAM2="$2"

OUTPUT_FILE="compare_sp_compare_add_column_table_${PARAM1}_vs_${PARAM2}.csv"

# Validasi parameter
if [ -z "$PARAM1" ] || [ -z "$PARAM2" ]; then
    echo "Usage: $0 <param1> <param2>"
    echo "Example: $0 dbsource dbdestination"
    exit 1
fi

echo "Running stored procedure..."

# ?? Eksekusi + gabungkan jadi 1 baris per statement
mariadb -h "$DB_HOST" -P "$DB_PORT" -u "$DB_USER" -p"$DB_PASS" "$DB_NAME" \
--batch --raw --skip-column-names \
-e "CALL sp_compare_add_column_table('$PARAM1', '$PARAM2');" \
| sed 's/\r//g' \
| awk '
BEGIN { RS=";"; ORS="" }
{
    gsub(/\n/, " ");      # hapus newline ? jadi 1 baris
    gsub(/\t/, " ");      # hapus tab
    gsub(/  +/, " ");     # rapikan spasi
    sub(/^ +/, "", $0);   # ?? hapus spasi di depan
    sub(/ +$/, "", $0);   # ?? hapus spasi di belakang
    if (length($0) > 5) { # skip kosong
        print "\"" $0 ";\"\n"
    }
}
' > "$OUTPUT_FILE"

# Cek status
if [ $? -eq 0 ]; then
    echo "Stored procedure executed successfully."
    echo "Output saved to: $OUTPUT_FILE"
else
    echo "Error executing stored procedure."
fi
[root@teguhth-all comp]#

[root@teguhth-all comp]# cat run_sp_compare_alter_column_table_csv.sh
#!/bin/bash

# Parameter
DB_HOST="10.10.10.90"
DB_PORT="3306"
DB_USER="admin"
DB_PASS="admin"
DB_NAME="dbatools"

PARAM1="$1"
PARAM2="$2"

OUTPUT_FILE="compare_sp_compare_alter_column_table_${PARAM1}_vs_${PARAM2}.csv"

# Validasi parameter
if [ -z "$PARAM1" ] || [ -z "$PARAM2" ]; then
    echo "Usage: $0 <param1> <param2>"
    echo "Example: $0 dbsource dbdestination"
    exit 1
fi

echo "Running stored procedure..."

# ?? Eksekusi + gabungkan jadi 1 baris per statement
mariadb -h "$DB_HOST" -P "$DB_PORT" -u "$DB_USER" -p"$DB_PASS" "$DB_NAME" \
--batch --raw --silent --skip-column-names \
-e "CALL sp_compare_alter_column_table('$PARAM1', '$PARAM2');" \
| sed 's/\r//g' \
| awk '
BEGIN { RS=";"; ORS="" }
{
    gsub(/\n/, " ");      # hapus newline ? jadi 1 baris
    gsub(/\t/, " ");      # hapus tab
    gsub(/  +/, " ");     # rapikan spasi
    gsub(/DEFAULT '\''NULL'\''/, "DEFAULT NULL");  # ?? FIX UTAMA
    sub(/^ +/, "", $0);   # ?? hapus spasi di depan
    sub(/ +$/, "", $0);   # ?? hapus spasi di belakang
    if (length($0) > 5) { # skip kosong
        print "\"" $0 ";\"\n"
    }
}
' > "$OUTPUT_FILE"

# Cek status
if [ $? -eq 0 ]; then
    echo "Stored procedure executed successfully."
    echo "Output saved to: $OUTPUT_FILE"
else
    echo "Error executing stored procedure."
fi
[root@teguhth-all comp]#

No comments:

Post a Comment

Popular Posts