Directory Structure [step 1]: moving files

working on #672

NOTE: This commit can't be compiled!!
This commit is contained in:
Yehonal
2017-10-12 20:00:52 +02:00
parent 4df28fd29c
commit f06f32849f
2508 changed files with 0 additions and 1325 deletions

3
apps/db_assembler/.gitignore vendored Normal file
View File

@@ -0,0 +1,3 @@
/output/
/backup/
config.sh

View File

@@ -0,0 +1,16 @@
## How to use:
First of all, if you need some custom configuration you have to copy and rename
/conf/config.sh.dist in /conf/config.sh and configure it
db_assembler.sh script contains an interactive menu to assemble and import sql files.
Just run it to display the options.
Note: You can even use actions directly by command lines specifying the option.
Ex:
./db_assembler.sh 1
It will start the assemble process of all sql files.

View File

@@ -0,0 +1,61 @@
#!/usr/bin/env bash
CURRENT_PATH="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source "$CURRENT_PATH/includes/includes.sh"
cmdopt=$1
while true
do
echo "===== DB ASSEMBLER MENU ====="
PS3='Please enter your choice: '
options=(
"Assemble ALL" "Assemble only bases" "Assemble only updates" "Assemble only customs"
"Quit"
"Assemble & import ALL" "Assemble & import only bases" "Assemble & import only updates" "Assemble & import only customs"
)
function _switch() {
case $1 in
"Assemble ALL")
dbasm_run true true true
;;
"Assemble only bases")
dbasm_run true false false
;;
"Assemble only updates")
dbasm_run false true false
;;
"Assemble only customs")
dbasm_run false false true
;;
"Assemble & import ALL")
dbasm_import true true true
;;
"Assemble & import only bases")
dbasm_import true false false
;;
"Assemble & import only updates")
dbasm_import false true false
;;
"Assemble & import only customs")
dbasm_import false false true
;;
"Quit")
echo "Goodbye!"
exit
;;
*) echo invalid option;;
esac
}
# run option directly if specified in argument
[ ! -z $1 ] && _switch "${options[$cmdopt-1]}" && exit 0
select opt in "${options[@]}"
do
_switch "$opt"
break
done
done

View File

@@ -0,0 +1,348 @@
# globals
PROMPT_USER=""
PROMPT_PASS=""
# use in a subshell
function dbasm_resetExitCode() {
exit 0
}
function dbasm_mysqlExec() {
confs=$1
command=$2
options=$3
eval $confs
if [[ ! -z "${PROMPT_USER// }" ]]; then
MYSQL_USER=$PROMPT_USER
MYSQL_PASS=$PROMPT_PASS
fi
export MYSQL_PWD=$MYSQL_PASS
retval=$("$DB_MYSQL_EXEC" -h "$MYSQL_HOST" -u "$MYSQL_USER" $options -e "$command")
if [[ $? -ne 0 ]]; then
err=$("$DB_MYSQL_EXEC" -h "$MYSQL_HOST" -u "$MYSQL_USER" $options -e "$command" 2>&1 )
if [[ "$err" == *"Access denied"* ]]; then
read -p "Insert mysql user:" PROMPT_USER
read -p "Insert mysql pass:" -s PROMPT_PASS
export MYSQL_PWD=$PROMPT_PASS
# create configured account if not exists
"$DB_MYSQL_EXEC" -h "$MYSQL_HOST" -u "$PROMPT_USER" $options -e "CREATE USER '${MYSQL_USER}'@'${MYSQL_HOST}' IDENTIFIED BY '${MYSQL_PASS}' WITH MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0;"
"$DB_MYSQL_EXEC" -h "$MYSQL_HOST" -u "$PROMPT_USER" $options -e "GRANT USAGE ON * . * TO '${MYSQL_USER}'@'${MYSQL_HOST}' WITH GRANT OPTION;"
retval=$("$DB_MYSQL_EXEC" -h "$MYSQL_HOST" -u "$PROMPT_USER" $options -e "$command")
else
exit
fi
fi
}
function dbasm_isNotEmpty() {
dbname=$1
conf=$2
dbasm_mysqlExec "$conf" "SELECT COUNT(DISTINCT table_name) FROM information_schema.columns WHERE table_schema = '${dbname}'" "--skip-column-names"
if (( $retval > 0 )); then
true
else
false
fi
}
function dbasm_dbExists() {
dbname=$1
conf=$2
dbasm_mysqlExec "$conf" "SHOW DATABASES LIKE '${dbname}'" "--skip-column-names"
if [ "$retval" == "${dbname}" ]; then
true
else
false
fi
}
function dbasm_createDB() {
database=${1,,}
uc=${database^^}
name="DB_"$uc"_CONF"
confs=${!name}
name="DB_"$uc"_NAME"
dbname=${!name}
eval $confs
CONF_USER=$MYSQL_USER
CONF_PASS=$MYSQL_PASS
if dbasm_dbExists $dbname "$confs"; then
echo "$dbname database exists"
else
echo "Creating DB ${dbname} ..."
dbasm_mysqlExec "$confs" "CREATE DATABASE \`${dbname}\`" ""
dbasm_mysqlExec "$confs" "GRANT ALL PRIVILEGES ON \`${dbname}\` . * TO '${CONF_USER}'@'${MYSQL_HOST}' WITH GRANT OPTION;"
fi
}
function dbasm_assemble() {
# to lowercase
database=${1,,}
start_sql=$2
with_base=$3
with_updates=$4
with_custom=$5
uc=${database^^}
name="DB_"$uc"_PATHS"
v="$name[@]"
base=("${!v}")
name="DB_"$uc"_UPDATE_PATHS"
v="$name[@]"
updates=("${!v}")
name='DB_'$uc'_CUSTOM_PATHS'
v="$name[@]"
custom=("${!v}")
suffix_base="_base"
suffix_upd="_update"
suffix_custom="_custom"
curTime=`date +%Y_%m_%d_%H_%M_%S`
if [ $with_base = true ]; then
echo "" > $OUTPUT_FOLDER$database$suffix_base".sql"
if [ ! ${#base[@]} -eq 0 ]; then
echo "Generating $OUTPUT_FOLDER$database$suffix_base ..."
for d in "${base[@]}"
do
echo "Searching on $d ..."
if [ ! -z $d ]; then
for entry in "$d"/*.sql "$d"/**/*.sql
do
if [[ -e $entry ]]; then
cat "$entry" >> $OUTPUT_FOLDER$database$suffix_base".sql"
fi
done
fi
done
fi
fi
if [ $with_updates = true ]; then
updFile=$OUTPUT_FOLDER$database$suffix_upd".sql"
echo "" > $updFile
if [ ! ${#updates[@]} -eq 0 ]; then
echo "Generating $OUTPUT_FOLDER$database$suffix_upd ..."
for d in "${updates[@]}"
do
echo "Searching on $d ..."
if [ ! -z $d ]; then
for entry in "$d"/*.sql "$d"/**/*.sql
do
if [[ ! -e $entry ]]; then
continue
fi
echo "-- $file" >> $updFile
cat "$entry" >> $updFile
done
fi
done
fi
fi
if [ $with_custom = true ]; then
custFile=$OUTPUT_FOLDER$database$suffix_custom".sql"
echo "" > $custFile
if [ ! ${#custom[@]} -eq 0 ]; then
echo "Generating $OUTPUT_FOLDER$database$suffix_custom ..."
for d in "${custom[@]}"
do
echo "Searching on $d ..."
if [ ! -z $d ]; then
for entry in "$d"/*.sql "$d"/**/*.sql
do
if [[ ! -e $entry ]]; then
continue
fi
echo "-- $file" >> $custFile
cat "$entry" >> $custFile
done
fi
done
fi
fi
}
function dbasm_run() {
echo "===== STARTING ASSEMBLY PROCESS ====="
mkdir -p "$OUTPUT_FOLDER"
for db in ${DATABASES[@]}
do
dbasm_assemble "$db" $version".sql" $1 $2 $3
done
echo "===== DONE ====="
}
function dbasm_db_backup() {
echo "backing up $1"
database=${1,,}
uc=${database^^}
name="DB_"$uc"_CONF"
confs=${!name}
name="DB_"$uc"_NAME"
dbname=${!name}
eval $confs;
if [[ ! -z "${PROMPT_USER// }" ]]; then
MYSQL_USER=$PROMPT_USER
MYSQL_PASS=$PROMPT_PASS
fi
export MYSQL_PWD=$MYSQL_PASS
now=`date +%s`
"$DB_MYSQL_DUMP_EXEC" --opt --user="$MYSQL_USER" --host="$MYSQL_HOST" "$dbname" > "${BACKUP_FOLDER}${database}_backup_${now}.sql" && echo "done"
if [[ $? -ne 0 ]]; then
err=$("$DB_MYSQL_DUMP_EXEC" --opt --user="$MYSQL_USER" --host="$MYSQL_HOST" "$dbname" 2>&1 )
if [[ "$err" == *"Access denied"* ]]; then
read -p "Insert mysql user:" PROMPT_USER
read -p "Insert mysql pass:" -s PROMPT_PASS
export MYSQL_PWD=$PROMPT_PASS
"$DB_MYSQL_DUMP_EXEC" --opt --user="$PROMPT_USER" --host="$MYSQL_HOST" "$dbname" > "${BACKUP_FOLDER}${database}_backup_${now}.sql" && echo "done"
else
exit
fi
fi
}
function dbasm_db_import() {
database=${1,,}
type=$2
uc=${database^^}
name="DB_"$uc"_CONF"
confs=${!name}
name="DB_"$uc"_NAME"
dbname=${!name}
if [[ $type = "base" && $DB_SKIP_BASE_IMPORT_IF_EXISTS = true ]]; then
if dbasm_isNotEmpty $dbname "$confs"; then
echo "$dbname is not empty, base importing skipped"
return
fi
fi
echo "importing $1 - $2 ..."
eval $confs;
if [[ ! -z "${PROMPT_USER// }" ]]; then
MYSQL_USER=$PROMPT_USER
MYSQL_PASS=$PROMPT_PASS
fi
export MYSQL_PWD=$MYSQL_PASS
"$DB_MYSQL_EXEC" -h "$MYSQL_HOST" -u "$MYSQL_USER" "$dbname" < "${OUTPUT_FOLDER}${database}_${type}.sql"
if [[ $? -ne 0 ]]; then
err=$("$DB_MYSQL_EXEC" -h "$MYSQL_HOST" -u "$MYSQL_USER" "$dbname" 2>&1 )
if [[ "$err" == *"Access denied"* ]]; then
read -p "Insert mysql user:" PROMPT_USER
read -p "Insert mysql pass:" -s PROMPT_PASS
export MYSQL_PWD=$PROMPT_PASS
"$DB_MYSQL_EXEC" -h "$MYSQL_HOST" -u "$PROMPT_USER" "$dbname" < "${OUTPUT_FOLDER}${database}_${type}.sql"
else
exit
fi
fi
}
function dbasm_import() {
dbasm_run $1 $2 $3
with_base=$1
with_updates=$2
with_custom=$3
echo "===== CHECKING DBs ====="
for db in ${DATABASES[@]}
do
dbasm_createDB "$db"
done
echo "===== DONE ====="
#
# BACKUP
#
if [ $BACKUP_ENABLE = true ]; then
echo "===== STARTING BACKUP PROCESS ====="
mkdir -p "$BACKUP_FOLDER"
for db in ${DATABASES[@]}
do
dbasm_db_backup "$db"
done
echo "===== DONE ====="
fi
echo "===== STARTING IMPORTING PROCESS ====="
#
# IMPORT
#
if [ $with_base = true ]; then
for db in ${DATABASES[@]}
do
dbasm_db_import "$db" "base"
done
fi
if [ $with_updates = true ]; then
for db in ${DATABASES[@]}
do
dbasm_db_import "$db" "update"
done
fi
if [ $with_custom = true ]; then
for db in ${DATABASES[@]}
do
dbasm_db_import "$db" "custom"
done
fi
echo "===== DONE ====="
}

View File

@@ -0,0 +1,11 @@
CURRENT_PATH="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source "$CURRENT_PATH/../../bash_shared/includes.sh"
AC_PATH_DBASSEMBLER="$AC_PATH_BIN/db_assembler"
if [ -f "$AC_PATH_DBASSEMBLER/config.sh" ]; then
source "$AC_PATH_DBASSEMBLER/config.sh" # should overwrite previous
fi
source "$AC_PATH_DBASSEMBLER/includes/functions.sh"