-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathload.sh
More file actions
executable file
·160 lines (125 loc) · 4.15 KB
/
load.sh
File metadata and controls
executable file
·160 lines (125 loc) · 4.15 KB
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
#!/bin/bash
HELP="
Invoked by start.sh as entry point of the shebanq container.
Or manually invoked within a running container.
Loads data.
No arguments needed.
Working directory: any.
"
if [[ $1 == "--help" || $1 == "-h" ]]; then
printf "$HELP"
exit
fi
mqlcmd=/opt/emdros/bin/mql
appdir=/app
srcdir=$appdir/src
secretdir=$appdir/secret
rundir=$appdir/run
tmpdir=$appdir/_temp
cfgdir=$rundir/cfg
mysqloptfile=$cfgdir/mysql.opt
mysqlasroot=--defaults-extra-file=$mysqloptfile
mysqlasroote="-h $mysqlhost -u $mysqlroot -p $mysqlrootpwd"
dbdir=$srcdir/databases
# set the permissions for the shebanq database user
# test which of the needed databases are already in mysql
# after this we have for each existing database a variable with name dbexists_databasename
mkdir -p $tmpdir
echo check existence of databases
for db in `echo "show databases;" | mysql $mysqlasroot`
do
if [[ $db =~ ^shebanq_ ]]; then
declare dbexists_$db=v
fi
done
echo import the missing static databases
for version in 4 4b c 2017 2021
do
db=shebanq_passage$version
dbvar=dbexists_$db
if [[ ${!dbvar} != v ]]; then
echo Importing $db
dbfile=$db.sql
dbfilez=$dbfile.gz
if [[ ! -e $tmpdir/$dbfile ]]; then
echo -e "\tunzipping $db (takes approx. 5 seconds)"
cp $dbdir/$dbfilez $tmpdir
gunzip -f $tmpdir/$dbfilez
fi
echo -e "\tloading $db (takes approx. 15 seconds)"
mysql $mysqlasroot < $tmpdir/$dbfile
echo -e "\tdone"
fi
# echo "GRANT SELECT ON $db.* TO '$mysqluser'@'$mysqluserhost'" | mysql $mysqlasroot
db=shebanq_etcbc$version
dbvar=dbexists_$db
if [[ ${!dbvar} != v ]]; then
echo Importing $db
dbfile=$db.mql
dbfilez=$dbfile.bz2
if [[ ! -e $tmpdir/$dbfile ]]; then
echo -e "\tunzipping $db (takes approx. 75 seconds)"
cp $dbdir/$dbfilez $tmpdir
bunzip2 -f $tmpdir/$dbfilez
fi
mysql $mysqlasroot -e "drop database if exists $db;"
echo -e "\tloading emdros $db (takes approx. 50 seconds)"
$mqlcmd -e UTF8 -n -b m $mysqlasroote < $tmpdir/$dbfile
echo -e "\tdone"
fi
# echo "GRANT SELECT ON $db.* TO '$mysqluser'@'$mysqluserhost';" | mysql $mysqlasroot
done
echo cleanup the dynamic databases
good=v
# Cleanup stage (only if the import of dynamic data is forced)
# The order note - web is important.
for kind in note web
do
db=shebanq_$kind
dbvar=dbexists_$db
if [[ ${!dbvar} != v || $blankuserdata == v || $newuserdata == v ]]; then
echo Checking $db
dbfile=$db.sql
dbfile_emp=${db}_empty.sql
dbfilez=$dbfile.gz
if [[ $blankuserdata != v && $newuserdata != v && -e $tmpdir/$dbfile ]]; then
echo previous db content from temp directory
else
if [[ ( $blankuserdata != v || $newuserdata == v ) && -e $secretdir/$dbfilez ]]; then
echo previous db content from secret directory
cp $secretdir/$dbfilez $tmpdir/$dbfilez
echo unzipping $db
gunzip -f $tmpdir/$dbfilez
elif [[ -e $dbdir/$dbfile_emp ]]; then
echo working with empty db
cp $dbdir/$dbfile_emp $tmpdir/$dbfile
else
echo no data
good=x
fi
fi
if [[ $good == x ]]; then
continue
fi
mysql $mysqlasroot -e "drop database if exists $db;"
mysql $mysqlasroot -e "create database $db;"
fi
done
# Import stage.
# The order web - note is important.
echo import the dynamic databases
for kind in web note
do
db=shebanq_$kind
dbvar=dbexists_$db
if [[ $blankuserdata == v || $newuserdata == v || ${!dbvar} != v ]]; then
dbfile=$db.sql
if [[ -e $tmpdir/$dbfile ]]; then
echo "use $db;\n" | cat - $tmpdir/$dbfile | mysql $mysqlasroot
echo Imported $db
else
echo "Data file $dbfile does not exist in $tmpdir"
fi
fi
echo "GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, ALTER ON $db.* TO '$mysqluser'@'$mysqluserhost';" | mysql $mysqlasroot
done