ElixirVault/development/database/bin/update_sub_db_by_file.sh

117 lines
3.6 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env bash
## -------------------------------------------------------------------
## RUNNING THE SCRIPT FILE FOR DOCKER CONTAINER - TEST / DEVELOP
## docker exec -it loreta_mysql bash -c "cd /opt && ./bin/load_all_dbs.sh -folder=data/bulk_db_load_test"
## -------------------------------------------------------------------
# Configure mysql for further usage
credentialsFile="/opt/bin/mysql-credentials.cnf"
#credentialsFile="/opt/dbmigration/mysql-credentials.cnf"
load_file=""
progress_msg=""
default_db_name=""
# Set passed arguments
for i in "$@"; do
case $i in
-file=*|--load_file=*) load_file="${i#*=}" ;;
*) echo "Unknown parameter passed: $i"; exit 1 ;;
esac
done
# Show progress bar
function ProgressBar {
# Process data
let _progress=(${1}*100/${2}*100)/100
let _done=(${_progress}*4)/10
let _left=40-$_done
# Build progressbar string lengths
_fill=$(printf "%${_done}s")
_empty=$(printf "%${_left}s")
# Then, we come to the “magic” part: “\033[0K\r“. Now, let’s understand what it means step by step:
#
# \033 – It’s the escape sequence. In other words, it’s ESC.
# \033[ – Then this becomes “ESC [“, which is the control sequence introducer (CSI).
# \033[0k – So it’s “CSI 0 K“. Further, “CSI 0 K” erases the text from the cursor to the end of the line.
# \r – This is the carriage return. It brings the cursor to the beginning of the line.
# Therefore, the command echo -ne “Message\033[0K\r” performs the following steps:
#
# print “Message“
# erase everything after “Message“. In other words, it removes the text from the previous output. Now, the cursor is at the end of the line.
# finally, “\r” moves the cursor back to the beginning
# In this way, each echo command overwrites the previous message on the same line.
printf "\rProgress : ${_fill// /█}${_empty// / } | ${_progress} \t$progress_msg\033[0K\r"
if [ $_progress -eq 100 ]; then
echo -e
fi
}
echo "NOTHING TO DO"
exit 1
# Show progress bar
function LoadDatabaseTable {
_file=(${1})
_db_title=(${2})
if [[ -e $_file ]]; then
# Remove the existing schema if it exists and recreate it
mysql --defaults-extra-file=$credentialsFile -e "DROP SCHEMA IF EXISTS \`$_db_title\`"
mysql --defaults-extra-file=$credentialsFile -e "CREATE SCHEMA \`$_db_title\` COLLATE utf8mb4_general_ci"
# Load dump file
mysql --defaults-extra-file=$credentialsFile -D "$_db_title" < $_file;
fi
}
# Check if dir exists
if [ -d "$load_dir" ]; then
# Setup progressbar
loaded_files=0
total_files=$(find "$PWD/$load_dir" -depth -maxdepth 1 -type f -name "*.sql" | wc -l)
# Loop over each db import file
for file in $PWD/$load_dir/*.sql; do
if [[ -e $file ]]; then
base_file=${file##*/}
current_db_name="${base_file%.*}"
# Check if base db exists & if not load elixir_vault_lv db
if [ $loaded_files -eq 0 ]; then
progress_msg="LOADING BASE STRUCTURE 'elixir_vault_lv' DB STRUCTURE"
ProgressBar ${loaded_files} ${total_files}
# Needs to load base structure for linked view not to throw errors
LoadDatabaseTable "$(dirname "${file}")/elixir_vault_lv.sql" "elixir_vault_lv"
fi
# Continue with the rest of db dump load
progress_msg="LOADING '$current_db_name' DB STRUCTURE"
ProgressBar ${loaded_files} ${total_files}
LoadDatabaseTable ${file} ${current_db_name}
let loaded_files++
ProgressBar ${loaded_files} ${total_files}
fi
done
echo -e "DONE;"
exit 0
fi
echo "NOTHING TO DO"
exit 1