forked from cmangos/wotlk-db
-
Notifications
You must be signed in to change notification settings - Fork 0
/
InstallFullDB.sh
executable file
·2985 lines (2670 loc) · 80.4 KB
/
InstallFullDB.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
####################################################################################################
#
# Helper script to install and manage CMaNGOS WoTLK-DB
#
####################################################################################################
# need to be changed on each official DB/CORE release
FULLDB_FILE_ZIP="WoTLKDB_1_8_14080.sql.gz"
FULLDB_FILE=${FULLDB_FILE_ZIP%.gz}
NEXT_MILESTONES="0.19 0.20"
# specific to this core
EXPANSION="WoTLK" #warning only 'Classic' or 'TBC' or 'WoTLK' else acid filename will be wrong
DATABASE_UPDATE_FILE_PREFIX=""
EXPANSION_LC="$(tr [A-Z] [a-z] <<< "$EXPANSION")"
# internal use
SOURCE_CONTENT_RELEASE_VERSION=""
DB_CONTENT_RELEASE_VERSION=""
SOURCE_LAST_CONTENT_VERSION_UPDATE=""
DB_LAST_CONTENT_VERSION_UPDATE=""
SCRIPT_FILE="InstallFullDB.sh"
CONFIG_FILE="InstallFullDB.config"
SOURCE_LOGSDB_VER="0"
SOURCE_REALMDB_VER="0"
SOURCE_CHARACTERDB_VER="0"
SOURCE_WORLDDB_VER="0"
DB_RELEASE_TITLE=""
DB_RELEASE_NEXT_MILESTONE=""
DB_WORLDDB_VERSION=""
DB_REALMDB_VERSION=""
DB_CHARDB_VERSION=""
DB_LOGSDB_VERSION=""
ROOTUSERNAME=""
ROOTPASSWORD=""
STATUS_ROOT_SUCCESS=false
STATUS_USER_SUCCESS=false
STATUS_WORLD_DB_FOUND=false
STATUS_CHAR_DB_FOUND=false
STATUS_REALM_DB_FOUND=false
STATUS_LOGS_DB_FOUND=false
STATUS_CONFIG_JUST_CREATED=false
STATUS_BASH_4_SUPPORT=false
OLDIFS="$IFS"
# testing only
ADDITIONAL_PATH=""
# default value
MYSQL_HOST_DEFAULT="localhost"
MYSQL_PORT_DEFAULT="3306"
MYSQL_USERNAME_DEFAULT="mangos"
MYSQL_PASSWORD_DEFAULT="mangos"
MYSQL_USERIP_DEFAULT="localhost"
MYSQL_COLSTAT_DEFAULT="" # important to avoid issue with mysqldump using other db than OracleMySQL 8>
WORLD_DB_NAME_DEFAULT="${EXPANSION_LC}mangos"
REALM_DB_NAME_DEFAULT="${EXPANSION_LC}realmd"
CHAR_DB_NAME_DEFAULT="${EXPANSION_LC}characters"
LOGS_DB_NAME_DEFAULT="${EXPANSION_LC}logs"
MYSQL_PATH_DEFAULT=""
CORE_PATH_DEFAULT=""
MYSQL_DUMP_PATH_DEFAULT=""
LOCALES_DEFAULT="YES"
DEV_UPDATES_DEFAULT="NO"
AHBOT_DEFAULT="NO"
FORCE_WAIT_DEFAULT="YES"
# variables assigned and read from $CONFIG_FILE
MYSQL_HOST="${MYSQL_HOST_DEFAULT}"
MYSQL_PORT="${MYSQL_PORT_DEFAULT}"
MYSQL_USERNAME="${MYSQL_USERNAME_DEFAULT}"
MYSQL_PASSWORD="${MYSQL_PASSWORD_DEFAULT}"
MYSQL_USERIP="${MYSQL_USERIP_DEFAULT}"
MYSQL_COLSTAT="${MYSQL_COLSTAT_DEFAULT}"
WORLD_DB_NAME="${WORLD_DB_NAME_DEFAULT}"
REALM_DB_NAME="${REALM_DB_NAME_DEFAULT}"
CHAR_DB_NAME="${CHAR_DB_NAME_DEFAULT}"
LOGS_DB_NAME="${LOGS_DB_NAME_DEFAULT}"
MYSQL_PATH="${MYSQL_PATH_DEFAULT}"
CORE_PATH="${CORE_PATH_DEFAULT}"
MYSQL_DUMP_PATH="${MYSQL_DUMP_PATH_DEFAULT}"
LOCALES="${LOCALES_DEFAULT}"
DEV_UPDATES="${DEV_UPDATES_DEFAULT}"
AHBOT="${AHBOT_DEFAULT}"
FORCE_WAIT="${FORCE_WAIT_DEFAULT}"
#possible search folder for core path
DEFAULT_CORE_FOLDER="$EXPANSION_LC"
# download backup address
LAST_BACKUP_ADR="https://github.com/cmangos/${EXPANSION_LC}-db/releases/download/latest/${EXPANSION_LC}-all-backups.tar.gz"
# extract db tittle, db content version, and last content update revision from content db
function initialize()
{
if [ ! -f "${ADDITIONAL_PATH}Full_DB/$FULLDB_FILE_ZIP" ]; then
echo "Unable to locate full db file ${ADDITIONAL_PATH}Full_DB/$FULLDB_FILE_ZIP"
false
return
fi
local foundGZPath=$(type -P "gzip") # 2> /dev/null)
if [[ "$foundGZPath" = "" ]]; then
echo "Unable to find gzip binaries on your system!"
echo "GZIP 1.6 or greater should be installed"
echo "Aborting..."
false
return
fi
echo -n " - Unziping $FULLDB_FILE_ZIP ... "
gzip -kdf "${ADDITIONAL_PATH}Full_DB/$FULLDB_FILE_ZIP"
if [[ $? != 0 ]]; then
echo ">>> ERROR: while trying to unzip ${ADDITIONAL_PATH}Full_DB/$FULLDB_FILE_ZIP"
false
return
else
echo "SUCCESS"
fi
echo -n " - Extracting db information ... "
local grepResult=()
IFS=$'\n'
while read -r line; do
grepResult+=("$line")
done < <(grep -h -A15 "CREATE TABLE .db_version" "${ADDITIONAL_PATH}Full_DB/${FULLDB_FILE}")
local coreVerRegex="^[ ]+.required_([0-9A-Za-z_]+)"
# todo this may fail, we need to uniformise all db title with some title schema
local contentDBVerRegex="\('${EXPANSION}[A-Za-z -]+([0-9\.]+)[ \"']+([0-9A-Za-z _'-]+)[\"'\. ]+[fF]or"
local coreDBVer=""
local contentDBVer=""
local contentDBTittle=""
#echo "${grepResult[*]}"
for line in ${grepResult[@]};do
local currLine=$(echo "$line" | tr -d '\\')
if [[ $currLine =~ $coreVerRegex ]]; then
coreDBVer=${BASH_REMATCH[1]}
fi
if [[ $currLine =~ $contentDBVerRegex ]]; then
contentDBVer=${BASH_REMATCH[1]}
contentDBTittle=${BASH_REMATCH[2]}
fi
done
IFS="$OLDIFS"
if [[ $coreDBVer = "" ]]; then
echo "FAILED"
echo ">>> Error: Unable to extract last core revision from ${FULLDB_FILE}"
false
return
fi
if [[ $contentDBVer = "" ]]; then
echo "FAILED"
echo ">>> Error: Unable to extract content DB release version from ${FULLDB_FILE}"
false
return
fi
if [[ $contentDBTittle = "" ]]; then
echo "FAILED"
echo ">>> Error: Unable to extract content DB title from ${FULLDB_FILE}"
false
return
fi
if [[ $contentDBTittle =~ [\"\']$ ]]; then
contentDBTittle="${contentDBTittle%?}"
fi
#echo "core db ver: $coreDBVer"
#echo "db ver: $contentDBVer"
#echo "db title: $contentDBTittle"
DB_RELEASE_TITLE="$contentDBTittle"
DB_WORLDDB_VERSION="$coreDBVer"
SOURCE_CONTENT_RELEASE_VERSION="$contentDBVer"
local versRegex="0*([0-9]+)"
IFS=$'\n'
while IFS= read -r file;do
local cVers=""
if [ -f "$file" ]; then
cVers=$(basename "$file" ".sql")
if [[ $cVers =~ $versRegex ]]; then
if [[ ${BASH_REMATCH[1]} -lt 9999 ]]; then
SOURCE_LAST_CONTENT_VERSION_UPDATE="$cVers"
break
fi
fi
fi
done < <(printf '%s\n' Updates/*.sql | sort -Vr)
IFS="$OLDIFS"
#echo "last upd: $SOURCE_LAST_CONTENT_VERSION_UPDATE"
echo "SUCCESS"
true
}
## All SQLs used in this script
function set_sql_queries
{
# create databases
SQL_CREATE_WORLD_DB="CREATE DATABASE \`$WORLD_DB_NAME\` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;"
SQL_CREATE_CHAR_DB="CREATE DATABASE \`$CHAR_DB_NAME\` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;"
SQL_CREATE_REALM_DB="CREATE DATABASE \`$REALM_DB_NAME\` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;"
SQL_CREATE_LOGS_DB="CREATE DATABASE \`$LOGS_DB_NAME\` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;"
# create database user and grant privileges
SQL_CREATE_DATABASE_USER="CREATE USER IF NOT EXISTS '$MYSQL_USERNAME'@'$MYSQL_USERIP' IDENTIFIED BY '$MYSQL_PASSWORD';"
SQL_GRANT_TO_WORLD_DATABASE="GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, ALTER, LOCK TABLES, CREATE TEMPORARY TABLES, EXECUTE, ALTER ROUTINE, CREATE ROUTINE ON \`$WORLD_DB_NAME\`.* TO '$MYSQL_USERNAME'@'$MYSQL_USERIP';"
SQL_GRANT_TO_CHAR_DATABASE=("GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, ALTER, LOCK TABLES, CREATE TEMPORARY TABLES ON \`$CHAR_DB_NAME\`.* TO '$MYSQL_USERNAME'@'$MYSQL_USERIP';")
SQL_GRANT_TO_REALM_DATABASE=("GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, ALTER, LOCK TABLES, CREATE TEMPORARY TABLES ON \`$REALM_DB_NAME\`.* TO '$MYSQL_USERNAME'@'$MYSQL_USERIP';")
SQL_GRANT_TO_LOGS_DATABASE=("GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, ALTER, LOCK TABLES, CREATE TEMPORARY TABLES ON \`$LOGS_DB_NAME\`.* TO '$MYSQL_USERNAME'@'$MYSQL_USERIP';")
# delete user
SQL_DROP_DATABASE_USER="DROP USER IF EXISTS '$MYSQL_USERNAME'@'$MYSQL_USERIP';"
# deletes databases
SQL_DROP_WORLD_DB="DROP DATABASE IF EXISTS \`$WORLD_DB_NAME\`;"
SQL_DROP_CHAR_DB="DROP DATABASE IF EXISTS \`$CHAR_DB_NAME\`;"
SQL_DROP_REALM_DB="DROP DATABASE IF EXISTS \`$REALM_DB_NAME\`;"
SQL_DROP_LOGS_DB="DROP DATABASE IF EXISTS \`$LOGS_DB_NAME\`;"
# query realm list
SQL_QUERY_REALM_LIST="SELECT * FROM realmlist;"
SQL_UPDATE_REALM_LIST="UPDATE realmlist SET " # field=value WHERE id=choosenid
SQL_INSERT_REALM_LIST="INSERT INTO realmlist (\`id\`,\`name\`,\`address\`,\`port\`) VALUES "
SQL_DELETE_REALM_ID="DELETE FROM realmlist WHERE id="
# base files from core
SQL_FILE_BASE_WORLD="${CORE_PATH}/sql/base/mangos.sql"
SQL_FILE_BASE_CHAR="${CORE_PATH}/sql/base/characters.sql"
SQL_FILE_BASE_REALM="${CORE_PATH}/sql/base/realmd.sql"
SQL_FILE_BASE_LOGS="${CORE_PATH}/sql/base/logs.sql"
}
# save current setting to $CONFIG_FILE
function save_settings()
{
local allsettings=()
allsettings=("####################################################################################################")
allsettings+=("# This is the config file for the '$SCRIPT_FILE' script")
allsettings+=("#")
allsettings+=("# You need to customize")
allsettings+=("# MYSQL_HOST: Host on which the database resides")
allsettings+=("# MYSQL_PORT: Port on which the database is running")
allsettings+=("# MYSQL_USERNAME: Your a valid mysql username")
allsettings+=("# MYSQL_PASSWORD: Your corresponding mysql password")
allsettings+=("# MYSQL_PATH: Your mysql command (usually mysql)")
allsettings+=("# WORLD_DB_NAME: Your content database")
allsettings+=("# CORE_PATH: Your path to core's directory")
allsettings+=("#")
allsettings+=("####################################################################################################")
allsettings+=("")
allsettings+=("## Define the host on which the mangos database resides (typically localhost)")
allsettings+=("MYSQL_HOST=\"$MYSQL_HOST\"")
allsettings+=("")
allsettings+=("## Define the port on which the mangos database is running (typically 3306)")
allsettings+=("MYSQL_PORT=\"$MYSQL_PORT\"")
allsettings+=("")
allsettings+=("## Define your username")
allsettings+=("MYSQL_USERNAME=\"$MYSQL_USERNAME\"")
allsettings+=("")
allsettings+=("## Define your password (It is suggested to restrict read access to this file!)")
allsettings+=("MYSQL_PASSWORD=\"$MYSQL_PASSWORD\"")
allsettings+=("")
allsettings+=("## Define default mysql address binding(you can set \"%\" to be able to connect from any computer)")
allsettings+=("MYSQL_USERIP=\"$MYSQL_USERIP\"")
allsettings+=("")
allsettings+=("## Define the databases name (let them empty for default name '"$EXPANSION_LC"dbtype')")
allsettings+=("WORLD_DB_NAME=\"$WORLD_DB_NAME\"")
allsettings+=("REALM_DB_NAME=\"$REALM_DB_NAME\"")
allsettings+=("CHAR_DB_NAME=\"$CHAR_DB_NAME\"")
allsettings+=("LOGS_DB_NAME=\"$LOGS_DB_NAME\"")
allsettings+=("")
allsettings+=("## Define your mysql program if this differs")
allsettings+=("MYSQL_PATH=\"$MYSQL_PATH\"")
allsettings+=("")
allsettings+=("## Define the path to your mysql dump binary folder")
allsettings+=("MYSQL_DUMP_PATH=\"$MYSQL_DUMP_PATH\"")
allsettings+=("")
allsettings+=("## Define the path to your core's folder")
allsettings+=("CORE_PATH=\"$CORE_PATH\"")
allsettings+=("")
allsettings+=("## Define if the 'locales' directory for processing localization/multi-language SQL files needs to be used")
allsettings+=("## Set the variable to \"YES\" to use the locales directory")
allsettings+=("LOCALES=\"$LOCALES\"")
allsettings+=("")
allsettings+=("## Define if you want to wait a bit before applying the full database")
allsettings+=("FORCE_WAIT=\"$FORCE_WAIT\"")
allsettings+=("")
allsettings+=("## Define if the 'dev' directory for processing development SQL files needs to be used")
allsettings+=("## Set the variable to \"YES\" to use the dev directory")
allsettings+=("DEV_UPDATES=\"$DEV_UPDATES\"")
allsettings+=("")
allsettings+=("## Define if AHBot SQL updates need to be applied (by default, assumes the core is built without AHBot)")
allsettings+=("## Set the variable to \"YES\" to import AHBot sql.")
allsettings+=("AHBOT=\"$AHBOT\"")
allsettings+=("")
allsettings+=("# Enjoy using the tool")
# save to file
for j in "${allsettings[@]}"; do
echo $j
done > $CONFIG_FILE
}
# Give chance to break the script
function force_wait()
{
if [ "$FORCE_WAIT" != "NO" ]; then
echo "WARNING: all you previous data will be discarded!"
echo "Press CTRL+C to exit"
# show counter
for i in {9..0}; do
echo -ne "$i"
echo -ne "\033[0K\r"
sleep 1
done
echo
fi
}
# some basic check to see if argument $1 is a cmangos core folder
# return false and error message in ERRORS varible or true
function check_core_folders()
{
ERRORS=""
if [[ "$1" = "" ]]; then
ERRORS="CORE_PATH is not set. Please try (8) to autodetect it or manually edit $CONFIG_FILE"
false
return
fi
if [[ ! -e "${1}/sql/updates/mangos" ]]; then
ERRORS="Unable to locate core update directory in '${1}/sql/updates/mangos'"
false
return
fi
if [[ ! -e "${1}/sql/base/dbc/original_data" ]]; then
ERRORS="Unable to locate core update directory in '${1}/sql/base/dbc/original_data'"
false
return
fi
if [[ ! -e "${1}/sql/base/dbc/cmangos_fixes" ]]; then
ERRORS="Unable to locate core update directory in '${1}/sql/base/dbc/cmangos_fixes'"
false
return
fi
if [[ ! -e "${1}/sql/scriptdev2" ]]; then
ERRORS="Unable to locate core update directory in '${1}/sql/scriptdev2'"
false
return
fi
true
}
# try to found CORE_PATH in current folder and parents folders.
# will not search in root folder to avoid too much time for find
# also limit search to 5 subfolder to not go too deep
# if it fail CORE_PATH will remain empty
function try_set_core_path()
{
currentFolder=$(pwd)
if [[ "$CORE_PATH" != "" ]]; then
true
return
fi
IFS=$'\n'
#avoid searchinig in entire root
while [[ "$currentFolder" != "/" ]]; do
for foundfolder in $(find "$currentFolder" -mindepth 1 -maxdepth 5 -iname "*$DEFAULT_CORE_FOLDER*" -type d 2> /dev/null)
do
if [[ "$foundfolder" != "" ]]; then
if check_core_folders "$foundfolder"; then
CORE_PATH="${foundfolder}"
break
fi
fi
done
currentFolder=$(dirname "$currentFolder")
done
IFS="$OLDIFS"
if [[ "$CORE_PATH" != "" ]]; then
true
return
fi
false
}
# try to set MYSQL_PATH
function try_set_mysql_path()
{
if [[ $MYSQL_PATH != "" ]]
then
true
return
fi
local foundMysqlPath=$(type -P "mysql" 2> /dev/null)
if [ foundMysqlPath != "" ]
then
MYSQL_PATH="$foundMysqlPath"
else
local defaultMysqlFolders="program files"
IFS=$'\n'
#avoid searchinig in entire root
for foundfolder in $(find "/c" -mindepth 1 -maxdepth 1 -iname "$defaultMysqlFolders*" -type d 2> /dev/null)
do
if [[ "$foundfolder" != "" ]]; then
for foundFile in $(find "$foundfolder/MySQL" -mindepth 1 -maxdepth 3 -iname "mysql.exe" -type f 2> /dev/null)
do
if [[ "$foundFile" != "" ]]; then
MYSQL_PATH="${foundFile}"
break
fi
done
fi
done
IFS="$OLDIFS"
fi
if [[ $MYSQL_PATH != "" ]]; then
local mysqldump=$(dirname "${MYSQL_PATH}")
MYSQL_DUMP_PATH="${mysqldump}/mysqldump"
true
return
fi
false
}
# print string and add underline to it.
# (arg1 is string to print and arg2 is the char to use to for underline)
print_underline()
{
echo $1
echo "${1//?/${2:--}}"
}
function print_header()
{
clear
print_underline "Welcome to the CMaNGOS $EXPANSION databases manager" "="
echo
}
# try connect to db
function try_connect_to_db()
{
export MYSQL_PWD="$MYSQL_PASSWORD"
ERRORS=$("$MYSQL_PATH" -h$MYSQL_HOST -P$MYSQL_PORT -u$MYSQL_USERNAME --connect-timeout=2 -s -e';' 2>&1)
if [[ $? != 0 ]]
then
STATUS_USER_SUCCESS=false
false
return
fi
true
STATUS_USER_SUCCESS=true
}
# Set root access right
# return true if success or already set
# arg 1 type bool disable any output if false
# arg 2 type bool force to set new root access if already set
function set_try_root_connect_to_db
{
local showstatus=${1:true}
local forceset=${2:false}
if [ "$STATUS_ROOT_SUCCESS" = true ]; then
true
return
fi
while true; do
if [ "$showstatus" = true ]; then
clear
print_underline "Root access required to continue" "="
echo
read -e -p "Enter MySQL root user...........: " ROOTUSERNAME
read -e -s -p "Enter MySQL root password.......: " ROOTPASSWORD
echo "**********"
echo -n "Checking mysql database accessibility with root access..."
fi
export MYSQL_PWD="$ROOTPASSWORD"
ERRORS=$("$MYSQL_PATH" -u"$ROOTUSERNAME" -h"$MYSQL_HOST" -P"$MYSQL_PORT" --connect-timeout=3 -s -N -e";" 2>&1)
if [[ $? != 0 ]]
then
if [ "$showstatus" = true ]; then echo "FAILED!"; fi
echo ">>> $ERRORS"
STATUS_ROOT_SUCCESS=false
read -n 1 -e -p "Do you want to retry? (y/N).....: " CHOICE
if [ "${CHOICE}" = "y" ] || [ "${CHOICE}" = "Y"]; then
continue
fi
false
return
else
if [ "$showstatus" = true ]; then echo "SUCCESS"; fi
break;
fi
done
export MYSQL_PWD="$MYSQL_PASSWORD"
STATUS_ROOT_SUCCESS=true
true
}
# Check mysql binary
function check_mysql_binary()
{
ERRORS=""
local foundMysqlPath=""
if [[ "$MYSQL_PATH" != "" ]]; then
foundMysqlPath=$(type -P "$MYSQL_PATH" 2> /dev/null)
else
ERRORS="MYSQL_PATH is not set! Please try (7) to autodetect it or manually edit $CONFIG_FILE"
false
return
fi
if [[ "$foundMysqlPath" = "" ]]; then
ERRORS="Unable to find mysql binaries using '$MYSQL_PATH'"
false
return
fi
true
}
# Check core folder
function check_core_path_setting()
{
ERRORS=""
# check_core_folders will fill ERRORS if any
if ! check_core_folders "${CORE_PATH}"; then
false
return
fi
true
}
# Check pre requisite
function check_minimum_requierements()
{
local current_errors=()
if ! check_core_path_setting; then
current_errors+=("$ERRORS")
fi
if ! check_mysql_binary; then
current_errors+=("$ERRORS")
else
if ! try_connect_to_db; then
local normal_connection_error="$ERRORS"
if [[ "$STATUS_ROOT_SUCCESS" = false ]] ; then
current_errors+=("$normal_connection_error")
fi
fi
fi
ERRORS=("${current_errors[@]}")
if [[ ${#ERRORS[@]} > 0 ]]; then
false
return
fi
true
}
# execute sql command and print error if any
# execute_sql_command "database" "sql" "message"(if empty no message is shown) return true if success
function execute_sql_command()
{
if [ ! -z "$3" ]; then echo -n "$3 ... "; fi
export MYSQL_PWD="$MYSQL_PASSWORD"
MYSQL_ERROR=$("$MYSQL_PATH" -u"$MYSQL_USERNAME" -h"$MYSQL_HOST" -P"$MYSQL_PORT" -s -N -D "$1" -e"$2" 2>&1)
if [[ $? != 0 ]]; then
if [ ! -z "$3" ]; then
echo "FAILED!"
echo ">>> $MYSQL_ERROR";
fi
false
return
else
if [ ! -z "$3" ]; then echo "SUCCESS"; fi
fi
true
}
# execute file that contain sql and print error if any message is provided
# execute_sql_file "database" "filename" "message"(if empty no message is shown) return true if success else MYSQL_ERROR contain the error
function execute_sql_file()
{
local showstatus=true
if [[ "$3" = "" ]]; then showstatus=false; fi
if [[ "$showstatus" = true ]]; then echo -n "$3 ... "; fi
export MYSQL_PWD="$MYSQL_PASSWORD"
ERRORS=$("$MYSQL_PATH" -u"$MYSQL_USERNAME" -h"$MYSQL_HOST" -P"$MYSQL_PORT" -s -N -D "$1" < "$2" 2>&1)
if [[ $? != 0 ]]; then
if [[ "$showstatus" = true ]]; then
echo "FAILED!"
echo ">>> $ERRORS";
fi
false
return
else
if [[ "$showstatus" = true ]]; then echo "SUCCESS"; fi
fi
true
}
function show_db_name()
{
echo -e "World database name.....: $WORLD_DB_NAME"
echo -e "Character database name.: $CHAR_DB_NAME"
echo -e "Realm database name.....: $REALM_DB_NAME"
echo -e "Logs database name......: $LOGS_DB_NAME"
}
function show_mysql_settings()
{
echo -e "Database host...........: $MYSQL_HOST"
echo -e "Database port...........: $MYSQL_PORT"
echo -e "MySQL user..............: $MYSQL_USERNAME (password is defined in $CONFIG_FILE)"
echo -e "MySQL user IP access....: $MYSQL_USERIP"
echo -e "MySQL binary path.......: $MYSQL_PATH"
echo -e "MySQL dump binary path..: $MYSQL_DUMP_PATH"
echo -e "Core path...............: $CORE_PATH"
show_db_name
echo -e "LOCALES.................: $LOCALES"
echo -e "DEV_UPDATES.............: $DEV_UPDATES"
echo -e "AHBOT...................: $AHBOT"
}
# arg1 = arg2 (if arg2 is empty then arg1 = arg3, if arg3 is empty do nothing)
function assign_new_value()
{
local v="$1"
if [[ "$2" != "" ]]; then
eval "$v='${2}'"
else
if [[ "$3" != "" ]]; then
eval "$v='${3}'"
fi
fi
}
function change_db_name()
{
local nameSettings=( "$WORLD_DB_NAME" "$CHAR_DB_NAME" "$REALM_DB_NAME" "$LOGS_DB_NAME" )
if [[ $BASH_VERSION > 4 ]]; then
read -e -p "Enter world database name.......: " -i $WORLD_DB_NAME WORLD_DB_NAME
read -e -p "Enter characters database name..: " -i $CHAR_DB_NAME CHAR_DB_NAME
read -e -p "Enter realm database name.......: " -i $REALM_DB_NAME REALM_DB_NAME
read -e -p "Enter log database name.........: " -i $LOGS_DB_NAME LOGS_DB_NAME
else
read -e -p "Enter world database name......current($WORLD_DB_NAME).: " wname
read -e -p "Enter characters database name.current($CHAR_DB_NAME).: " cname
read -e -p "Enter realm database name......current($REALM_DB_NAME).: " rname
read -e -p "Enter log database name........current($LOGS_DB_NAME).: " lname
assign_new_value 'WORLD_DB_NAME' "${wname}"
assign_new_value 'CHAR_DB_NAME' "${cname}"
assign_new_value 'REALM_DB_NAME' "${rname}"
assign_new_value 'LOGS_DB_NAME' "${lname}"
fi
}
function change_mysql_settings()
{
print_header
if [[ $BASH_VERSION > 4 ]]; then
read -e -p "Enter MySQL host................: " -i $MYSQL_HOST MYSQL_HOST
read -e -p "Enter MySQL port................: " -i $MYSQL_PORT MYSQL_PORT
read -e -p "Enter MySQL user................: " -i $MYSQL_USERNAME MYSQL_USERNAME
read -e -s -p "Enter MySQL password............: " MYSQL_PASSWORD
echo "***********"
read -e -p "MySQL user IP access............: " -i $MYSQL_USERIP MYSQL_USERIP
read -e -p "Enter MySQL binary path.........: " -i "$MYSQL_PATH" MYSQL_PATH
read -e -p "Enter MySQL dump binary path....: " -i "$MYSQL_DUMP_PATH" MYSQL_DUMP_PATH
read -e -p "Enter core path.................: " -i "$CORE_PATH" CORE_PATH
change_db_name
echo -e "Choose YES or NO for following options"
read -e -p "LOCALE(default:YES).............: " -i "$LOCALES" LOCALES
read -e -p "DEV_UPDATES(default:NO).........: " -i "$DEV_UPDATES" DEV_UPDATES
read -e -p "AHBOT(default:NO)...............: " -i "$AHBOT" AHBOT
else
read -e -p "Enter MySQL host...............current($MYSQL_HOST).: " mhost
read -e -p "Enter MySQL port...............current($MYSQL_PORT).: " mport
read -e -p "Enter MySQL user...............current($MYSQL_USERNAME).: " muser
read -e -s -p "Enter MySQL password............: " mpass
echo "***********"
read -e -p "MySQL user IP access...........current($MYSQL_USERIP).: " musip
read -e -p "Enter MySQL binary path........current($MYSQL_PATH).: " mpath
read -e -p "Enter MySQL dump binary path...current($MYSQL_DUMP_PATH).: " mdpath
read -e -p "Enter core path................current($CORE_PATH).: " cpath
change_db_name
echo -e "Choose YES or NO for following options"
read -e -p "LOCALE(default:YES)............current($LOCALES).: " loc
read -e -p "DEV_UPDATES(default:NO)........current($DEV_UPDATES).: " dev
read -e -p "AHBOT(default:NO)..............current($AHBOT).: " ahb
assign_new_value 'MYSQL_HOST' "${mhost}"
assign_new_value 'MYSQL_PORT' "${mport}"
assign_new_value 'MYSQL_USERNAME' "${muser}"
assign_new_value 'MYSQL_PASSWORD' "${mpass}"
assign_new_value 'MYSQL_USERIP' "${musip}"
assign_new_value 'MYSQL_PATH' "${mpath}"
assign_new_value 'MYSQL_DUMP_PATH' "${mdpath}"
assign_new_value 'CORE_PATH' "${cpath}"
assign_new_value 'LOCALES' "${loc}"
assign_new_value 'DEV_UPDATES' "${dev}"
assign_new_value 'AHBOT' "${ahb}"
fi
# some basic checks
if [[ "$MYSQL_HOST" = "" ]]; then MYSQL_HOST="${MYSQL_HOST_DEFAULT}"; fi
if [[ "$MYSQL_PORT" = "" ]]; then MYSQL_PORT="${MYSQL_PORT_DEFAULT}"; fi
if [[ "$MYSQL_USERNAME" = "" ]]; then MYSQL_USERNAME="${MYSQL_USERNAME_DEFAULT}"; fi
if [[ "$MYSQL_PASSWORD" = "" ]]; then MYSQL_PASSWORD="${MYSQL_PASSWORD_DEFAULT}"; fi
if [[ "$MYSQL_USERIP" = "" ]]; then MYSQL_USERIP="${MYSQL_USERIP_DEFAULT}"; fi
if [[ "$WORLD_DB_NAME" = "" ]]; then WORLD_DB_NAME="${WORLD_DB_NAME_DEFAULT}"; fi
if [[ "$CHAR_DB_NAME" = "" ]]; then CHAR_DB_NAME="${CHAR_DB_NAME_DEFAULT}"; fi
if [[ "$REALM_DB_NAME" = "" ]]; then REALM_DB_NAME="${REALM_DB_NAME_DEFAULT}"; fi
if [[ "$LOGS_DB_NAME" = "" ]]; then LOGS_DB_NAME="${LOGS_DB_NAME_DEFAULT}"; fi
LOCALES="$(tr [a-z] [A-Z] <<< "$LOCALES")"
DEV_UPDATES="$(tr [a-z] [A-Z] <<< "$DEV_UPDATES")"
AHBOT="$(tr [a-z] [A-Z] <<< "$AHBOT")"
save_settings
}
function wait_key()
{
read -n1 -r -p "Press space to continue..." key
}
function print_mysql_connection_status()
{
echo -ne "Trying to connect to DATABASE: "
if ! try_connect_to_db ; then
echo "Error: unable to connect to db"
echo "$ERRORS"
else
echo "CONNECTED"
MYSQL_CONNECTED=true
fi
}
# Get current db version
# get_current_db_version "database name" "table name"
# result will be in CURRENT_DB_VERSION and in CURRENT_LAST_CONTENT_DB_VERSION
function get_current_db_version()
{
CURRENT_DB_VERSION=""
CURRENT_LAST_CONTENT_DB_VERSION=""
sql="SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA='$1' AND TABLE_NAME='$2';"
#echo "$sql"
IFS=$'\n'
while read -a row
do
#echo "${row[0]}"
case "${row[0]}" in
*required_*)
CURRENT_DB_VERSION=$(echo "${row[0]//[$'\n\r']}") # remove eventual carriage return
CURRENT_DB_VERSION=$(echo -n "${CURRENT_DB_VERSION//required_}") # remove "required_"
;;
content_*)
CURRENT_LAST_CONTENT_DB_VERSION=$(echo "${row[0]//[$'\n\r']}") # remove eventual carriage return
CURRENT_LAST_CONTENT_DB_VERSION=$(echo -n "${CURRENT_LAST_CONTENT_DB_VERSION//content_}") # remove "content_"
;;
esac
done < <("$MYSQL_PATH" -u"$MYSQL_USERNAME" -h"$MYSQL_HOST" -P"$MYSQL_PORT" -s -N -e"$sql")
IFS="$OLDIFS"
}
# check if we can access DBs and retrieve their version
# check_dbs_accessibility bool (if parameter is true the result is displayed)
function check_dbs_accessibility()
{
local showstatus=${1:false}
local current_error
local UNAVAILABLE_DB=()
local sql=""
ERRORS=()
DB_WORLDDB_VERSION="0"
DB_REALMDB_VERSION="0"
DB_CHARDB_VERSION="0"
DB_LOGSDB_VERSION="0"
STATUS_WORLD_DB_FOUND=false
STATUS_CHAR_DB_FOUND=false
STATUS_REALM_DB_FOUND=false
STATUS_LOGS_DB_FOUND=false
DB_CONTENT_RELEASE_VERSION=""
if ! try_connect_to_db ; then
false
return
fi
if [[ "$showstatus" = true ]]; then echo -ne "Checking '$WORLD_DB_NAME' db access, please wait..."; fi
ERRORS+=($("$MYSQL_PATH" -h$MYSQL_HOST -P$MYSQL_PORT -u$MYSQL_USERNAME -D$WORLD_DB_NAME --connect-timeout=2 -s -e";" 2>&1))
if [[ $? != 0 ]]; then
DB_WORLDDB_VERSION="0"
UNAVAILABLE_DB+=("$WORLD_DB_NAME")
else
if [[ "$showstatus" = true ]]; then echo -ne "SUCCESS"; fi
get_current_db_version "$WORLD_DB_NAME" "db_version"
DB_WORLDDB_VERSION="$CURRENT_DB_VERSION"
DB_LAST_CONTENT_VERSION_UPDATE="$CURRENT_LAST_CONTENT_DB_VERSION"
STATUS_WORLD_DB_FOUND=true
#select version from db_version;
sql="SELECT version FROM db_version;"
#echo "$sql"
local result=$("$MYSQL_PATH" -h$MYSQL_HOST -P$MYSQL_PORT -u$MYSQL_USERNAME -D$WORLD_DB_NAME --connect-timeout=2 -sNe"$sql" 2>&1)
if [[ $? != 0 ]]; then
DB_CONTENT_RELEASE_VERSION=""
else
# todo this may fail, we need to uniformise all db title with some title schema
local contentDBVerRegex="[A-Za-z -]+([0-9\.]+)"
if [[ "$result" =~ $contentDBVerRegex ]]; then
DB_CONTENT_RELEASE_VERSION=${BASH_REMATCH[1]}
fi
fi
fi
if [[ "$showstatus" = true ]]; then echo -ne "\033[0K\r"; echo -ne "Checking '$REALM_DB_NAME' db access, please wait... "; fi
ERRORS+=($("$MYSQL_PATH" -h$MYSQL_HOST -P$MYSQL_PORT -u$MYSQL_USERNAME -D$REALM_DB_NAME --connect-timeout=2 -s -e";" 2>&1))
if [[ $? != 0 ]]; then
DB_REALMDB_VERSION="0"
UNAVAILABLE_DB+=("$REALM_DB_NAME")
else
if [[ "$showstatus" = true ]]; then echo -ne "SUCCESS"; fi
get_current_db_version "$REALM_DB_NAME" "realmd_db_version"
DB_REALMDB_VERSION="$CURRENT_DB_VERSION"
STATUS_REALM_DB_FOUND=true
fi
if [[ "$showstatus" = true ]]; then echo -ne "\033[0K\r"; echo -ne "Checking '$CHAR_DB_NAME' db access, please wait... "; fi
ERRORS+=($("$MYSQL_PATH" -h$MYSQL_HOST -P$MYSQL_PORT -u$MYSQL_USERNAME -D$CHAR_DB_NAME --connect-timeout=2 -s -e";" 2>&1))
if [[ $? != 0 ]]
then
DB_CHARDB_VERSION="0"
UNAVAILABLE_DB+=("$CHAR_DB_NAME")
else
if [[ "$showstatus" = true ]]; then echo -ne "SUCCESS"; fi
get_current_db_version "$CHAR_DB_NAME" "character_db_version"
DB_CHARDB_VERSION="$CURRENT_DB_VERSION"
STATUS_CHAR_DB_FOUND=true
fi
if [[ "$showstatus" = true ]]; then echo -ne "\033[0K\r"; echo -ne "Checking '$LOGS_DB_NAME' db access, please wait... "; fi
ERRORS+=($("$MYSQL_PATH" -h$MYSQL_HOST -P$MYSQL_PORT -u$MYSQL_USERNAME -D$LOGS_DB_NAME --connect-timeout=2 -s -e";" 2>&1))
if [[ $? != 0 ]]
then
DB_LOGSDB_VERSION="0"
UNAVAILABLE_DB+=("$LOGS_DB_NAME")
else
if [[ "$showstatus" = true ]]; then echo -ne "SUCCESS"; fi
get_current_db_version "$LOGS_DB_NAME" "logs_db_version"
DB_LOGSDB_VERSION="$CURRENT_DB_VERSION"
STATUS_LOGS_DB_FOUND=true
fi
if [[ "$showstatus" = true ]]; then
if [[ "${#UNAVAILABLE_DB[@]}" > 0 ]]; then
echo -ne "\033[0K\r"
echo -ne " "
echo -ne "\033[0K\r"
echo -ne ">>> Error: Found some inaccessible DB> "
for mdb in "${UNAVAILABLE_DB[@]}";do
echo -ne "'$mdb' "
done
echo
echo ">>> Either you want to create new DB or there is spelling problem."
else
echo -ne "\033[0K\r"
echo -ne " "
echo -ne "\033[0K\r"
fi
fi
local mysqldumpInfo=$("$MYSQL_DUMP_PATH" --version)
local mysqlOraReg="MySQL Community Server"
MYSQL_COLSTAT=""
if [[ $mysqldumpInfo =~ $mysqlOraReg ]]; then
local mysqlVerReg="Ver 8.[0-9]+\.[0-9]+"
if [[ $mysqldumpInfo =~ $mysqlVerReg ]]; then
MYSQL_COLSTAT=" --column-statistics=0"
fi
fi
if [[ "${#UNAVAILABLE_DB[@]}" > 0 ]]; then
false
else
true
fi
}
function show_installation_status()
{
local db_need_update=()
local db_need_create=()
local pullNeeded=false
local allGood=true
if [[ "$STATUS_USER_SUCCESS" = false ]]; then
echo "Warning: MySQL is not able to connect with the current user!"
echo " You can either create it (option 5) or change user in settings (option 1)"
fi
if [[ "$STATUS_USER_SUCCESS" = false ]] && [[ "$STATUS_ROOT_SUCCESS" = false ]]; then
echo "Warning: Unable to access your db!"
return
fi
if [[ "$STATUS_WORLD_DB_FOUND" = true ]]; then
if [[ "$SOURCE_WORLDDB_VER" > "$DB_WORLDDB_VERSION" ]]; then
db_need_update+=("$WORLD_DB_NAME")
else
if [[ "$SOURCE_WORLDDB_VER" < "$DB_WORLDDB_VERSION" ]]; then
pullNeeded=true
fi
fi
else
db_need_create+=("$WORLD_DB_NAME")
fi
if [[ "$STATUS_REALM_DB_FOUND" = true ]]; then
if [[ "$SOURCE_REALMDB_VER" > "$DB_REALMDB_VERSION" ]]; then
db_need_update+=("$REALM_DB_NAME")
else
if [[ "$SOURCE_REALMDB_VER" < "$DB_REALMDB_VERSION" ]]; then
pullNeeded=true
fi
fi
else
db_need_create+=("$REALM_DB_NAME")
fi
if [[ "$STATUS_REALM_DB_FOUND" = true ]]; then
if [[ "$SOURCE_CHARACTERDB_VER" > "$DB_CHARDB_VERSION" ]]; then
db_need_update+=("$CHAR_DB_NAME")
else
if [[ "$SOURCE_CHARACTERDB_VER" < "$DB_CHARDB_VERSION" ]]; then
pullNeeded=true
fi
fi
else
db_need_create+=("$CHAR_DB_NAME")
fi
if [[ "$STATUS_REALM_DB_FOUND" = true ]]; then
if [[ "$SOURCE_LOGSDB_VER" > "$DB_LOGSDB_VERSION" ]]; then
db_need_update+=("$LOGS_DB_NAME")
else
if [[ "$SOURCE_LOGSDB_VER" < "$DB_LOGSDB_VERSION" ]]; then
pullNeeded=true
fi
fi
else
db_need_create+=("$LOGS_DB_NAME")
fi
if [ "$pullNeeded" = true ]; then
echo "Warning: your local core has to be updated, use git pull in your core folder"
allGood=false
fi
if [[ "${#db_need_update}" > 0 ]]; then
echo -ne "Warning: These DBs need core update: "
for nu in "${db_need_update[@]}"; do
echo -ne "'$nu' "
done
echo " You can use option 3 to apply latest core update"
echo
allGood=false
fi
if [[ "${#db_need_create}" > 0 ]]; then
echo -ne "Warning: Create or made accessible these DBs: "
for nc in "${db_need_create[@]}"; do
echo -ne "'$nc' "
done
echo " You can create either create them (option 5) or adjust your settings (option 1)"
echo
allGood=false
fi