summaryrefslogtreecommitdiff
path: root/bin
diff options
context:
space:
mode:
authorYehonal <yehonal.azeroth@gmail.com>2017-09-20 01:18:20 +0200
committerYehonal <yehonal.azeroth@gmail.com>2017-09-20 01:18:20 +0200
commit70ab2a8771ac9da9f6eb82b975318e06689f544c (patch)
treefc1f0afb3cbd49d3b7c35838aaadc88ae25918ca /bin
parent477321be4d5a365baea82cfb92beedb40e7ce412 (diff)
Bash: Allow action non-interactive selection
You can directly run a specific action from command lines useful for cronjobs for example + splitted db_assembler in more files ready to be included from other bash scripts
Diffstat (limited to 'bin')
-rwxr-xr-xbin/db_assembler/db_assembler.sh248
-rw-r--r--bin/db_assembler/includes/functions.sh210
-rw-r--r--bin/db_assembler/includes/includes.sh11
3 files changed, 237 insertions, 232 deletions
diff --git a/bin/db_assembler/db_assembler.sh b/bin/db_assembler/db_assembler.sh
index 83ea0e96fc..8e4addaec6 100755
--- a/bin/db_assembler/db_assembler.sh
+++ b/bin/db_assembler/db_assembler.sh
@@ -1,228 +1,10 @@
#!/usr/bin/env bash
-unamestr=`uname`
-if [[ "$unamestr" == 'Darwin' ]]; then
- SRCPATH=$(greadlink -f "../../")
-else
- SRCPATH=$(readlink -f "../../")
-fi
+CURRENT_PATH="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
-source $SRCPATH"/bin/bash_shared/includes.sh"
+source "$CURRENT_PATH/includes/includes.sh"
-if [ -f "./config.sh" ]; then
- source "./config.sh" # should overwrite previous
-fi
-
-function 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 run() {
- echo "===== STARTING ASSEMBLY PROCESS ====="
-
- mkdir -p "$OUTPUT_FOLDER"
-
- for db in ${DATABASES[@]}
- do
- assemble "$db" $version".sql" $1 $2 $3
- done
-
- echo "===== DONE ====="
-}
-
-function db_backup() {
- echo "backing up $1"
-
- database=${1,,}
-
- uc=${database^^}
-
- name="DB_"$uc"_CONF"
- confs=${!name}
-
- name="DB_"$uc"_NAME"
- dbname=${!name}
-
- eval $confs;
-
- 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"
-}
-
-function db_import() {
- echo "importing $1 - $2"
-
- database=${1,,}
- type=$2
-
- uc=${database^^}
-
- name="DB_"$uc"_CONF"
- confs=${!name}
-
- name="DB_"$uc"_NAME"
- dbname=${!name}
-
- eval $confs;
-
- export MYSQL_PWD=$MYSQL_PASS
-
- "$DB_MYSQL_EXEC" -h "$MYSQL_HOST" -u "$MYSQL_USER" "$dbname" < "${OUTPUT_FOLDER}${database}_${type}.sql"
-}
-
-function import () {
- run $1 $2 $2
-
-
- with_base=$1
- with_updates=$2
- with_custom=$3
-
- #
- # BACKUP
- #
-
- if [ $BACKUP_ENABLE = true ]; then
- echo "===== STARTING BACKUP PROCESS ====="
- mkdir -p "$BACKUP_FOLDER"
-
- for db in ${DATABASES[@]}
- do
- db_backup "$db"
- done
- echo "===== DONE ====="
- fi
-
- echo "===== STARTING IMPORTING PROCESS ====="
- #
- # IMPORT
- #
- if [ $with_base = true ]; then
- for db in ${DATABASES[@]}
- do
- db_import "$db" "base"
- done
- fi
-
- if [ $with_updates = true ]; then
- for db in ${DATABASES[@]}
- do
- db_import "$db" "update"
- done
- fi
-
- if [ $with_custom = true ]; then
- for db in ${DATABASES[@]}
- do
- db_import "$db" "custom"
- done
- fi
-
- echo "===== DONE ====="
-}
+cmdopt=$1
while true
do
@@ -233,45 +15,47 @@ options=(
"Quit"
"Assemble & import ALL" "Assemble & import only bases" "Assemble & import only updates" "Assemble & import only customs"
)
-select opt in "${options[@]}"
-do
- case $opt in
+
+function _switch() {
+ case $1 in
"Assemble ALL")
run true true true
- break
;;
"Assemble only bases")
run true false false
- break
;;
"Assemble only updates")
run false true false
- break
;;
"Assemble only customs")
run false false true
- break
;;
"Assemble & import ALL")
import true true true
- break
;;
"Assemble & import only bases")
import true false false
- break
;;
"Assemble & import only updates")
import false true false
- break
;;
"Assemble & import only customs")
import false false true
- break
;;
"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
diff --git a/bin/db_assembler/includes/functions.sh b/bin/db_assembler/includes/functions.sh
new file mode 100644
index 0000000000..ea8d146fe1
--- /dev/null
+++ b/bin/db_assembler/includes/functions.sh
@@ -0,0 +1,210 @@
+function 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 run() {
+ echo "===== STARTING ASSEMBLY PROCESS ====="
+
+ mkdir -p "$OUTPUT_FOLDER"
+
+ for db in ${DATABASES[@]}
+ do
+ assemble "$db" $version".sql" $1 $2 $3
+ done
+
+ echo "===== DONE ====="
+}
+
+function db_backup() {
+ echo "backing up $1"
+
+ database=${1,,}
+
+ uc=${database^^}
+
+ name="DB_"$uc"_CONF"
+ confs=${!name}
+
+ name="DB_"$uc"_NAME"
+ dbname=${!name}
+
+ eval $confs;
+
+ 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"
+}
+
+function db_import() {
+ echo "importing $1 - $2"
+
+ database=${1,,}
+ type=$2
+
+ uc=${database^^}
+
+ name="DB_"$uc"_CONF"
+ confs=${!name}
+
+ name="DB_"$uc"_NAME"
+ dbname=${!name}
+
+ eval $confs;
+
+ export MYSQL_PWD=$MYSQL_PASS
+
+ "$DB_MYSQL_EXEC" -h "$MYSQL_HOST" -u "$MYSQL_USER" "$dbname" < "${OUTPUT_FOLDER}${database}_${type}.sql"
+}
+
+function import () {
+ run $1 $2 $2
+
+
+ with_base=$1
+ with_updates=$2
+ with_custom=$3
+
+ #
+ # BACKUP
+ #
+
+ if [ $BACKUP_ENABLE = true ]; then
+ echo "===== STARTING BACKUP PROCESS ====="
+ mkdir -p "$BACKUP_FOLDER"
+
+ for db in ${DATABASES[@]}
+ do
+ db_backup "$db"
+ done
+ echo "===== DONE ====="
+ fi
+
+ echo "===== STARTING IMPORTING PROCESS ====="
+ #
+ # IMPORT
+ #
+ if [ $with_base = true ]; then
+ for db in ${DATABASES[@]}
+ do
+ db_import "$db" "base"
+ done
+ fi
+
+ if [ $with_updates = true ]; then
+ for db in ${DATABASES[@]}
+ do
+ db_import "$db" "update"
+ done
+ fi
+
+ if [ $with_custom = true ]; then
+ for db in ${DATABASES[@]}
+ do
+ db_import "$db" "custom"
+ done
+ fi
+
+ echo "===== DONE ====="
+} \ No newline at end of file
diff --git a/bin/db_assembler/includes/includes.sh b/bin/db_assembler/includes/includes.sh
new file mode 100644
index 0000000000..f74c527577
--- /dev/null
+++ b/bin/db_assembler/includes/includes.sh
@@ -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" \ No newline at end of file