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
This commit is contained in:
Yehonal
2017-09-20 01:18:20 +02:00
parent 477321be4d
commit 70ab2a8771
3 changed files with 237 additions and 232 deletions

View File

@@ -1,228 +1,10 @@
#!/usr/bin/env bash #!/usr/bin/env bash
unamestr=`uname` CURRENT_PATH="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
if [[ "$unamestr" == 'Darwin' ]]; then
SRCPATH=$(greadlink -f "../../")
else
SRCPATH=$(readlink -f "../../")
fi
source $SRCPATH"/bin/bash_shared/includes.sh" source "$CURRENT_PATH/includes/includes.sh"
if [ -f "./config.sh" ]; then cmdopt=$1
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 ====="
}
while true while true
do do
@@ -233,45 +15,47 @@ options=(
"Quit" "Quit"
"Assemble & import ALL" "Assemble & import only bases" "Assemble & import only updates" "Assemble & import only customs" "Assemble & import ALL" "Assemble & import only bases" "Assemble & import only updates" "Assemble & import only customs"
) )
select opt in "${options[@]}"
do function _switch() {
case $opt in case $1 in
"Assemble ALL") "Assemble ALL")
run true true true run true true true
break
;; ;;
"Assemble only bases") "Assemble only bases")
run true false false run true false false
break
;; ;;
"Assemble only updates") "Assemble only updates")
run false true false run false true false
break
;; ;;
"Assemble only customs") "Assemble only customs")
run false false true run false false true
break
;; ;;
"Assemble & import ALL") "Assemble & import ALL")
import true true true import true true true
break
;; ;;
"Assemble & import only bases") "Assemble & import only bases")
import true false false import true false false
break
;; ;;
"Assemble & import only updates") "Assemble & import only updates")
import false true false import false true false
break
;; ;;
"Assemble & import only customs") "Assemble & import only customs")
import false false true import false false true
break
;; ;;
"Quit") "Quit")
echo "Goodbye!"
exit exit
;; ;;
*) echo invalid option;; *) echo invalid option;;
esac 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
done done

View File

@@ -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 ====="
}

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"