summaryrefslogtreecommitdiff
path: root/bin/db_assembler/includes/functions.sh
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/db_assembler/includes/functions.sh
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/db_assembler/includes/functions.sh')
-rw-r--r--bin/db_assembler/includes/functions.sh210
1 files changed, 210 insertions, 0 deletions
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