forked from Ntipa/docker-kamailio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkamdbctl.mysql
543 lines (446 loc) · 13.4 KB
/
kamdbctl.mysql
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
# $Id$
#
# Script for adding and dropping Kamailio MySQL tables
#
# History:
# 2006-04-07 removed gen_ha1 dependency - use md5sum;
# separated the serweb from kamailio tables;
# fixed the reinstall functionality (bogdan)
# 2006-05-16 added ability to specify MD5 from a configuration file
# FreeBSD does not have the md5sum function (norm)
# 2006-09-02 Added address table (juhe)
# 2006-10-27 subscriber table cleanup; some columns are created only if
# serweb is installed (bogdan)
# 2007-02-28 DB migration added (bogdan)
# 2007-05-21 Move SQL database definitions out of this script (henning)
# 2007-05-31 Move common definitions to kamdbctl.base file (henningw)
# 2007-06-11 Use a common control tool for database tasks, like the kamctl
# path to the database schemas
DATA_DIR="/usr/share/kamailio/"
if [ -d "$DATA_DIR/mysql" ] ; then
DB_SCHEMA="$DATA_DIR/mysql"
else
DB_SCHEMA="./mysql"
fi
#################################################################
# config vars
#################################################################
# full privileges MySQL user
if [ -z "$DBROOTUSER" ]; then
DBROOTUSER="admin"
fi
# Uncomment this to set the database root password if you want to run this
# script without any user prompt. This is unsafe, but useful e.g. for
# automatic testing.
PW="12345678"
CMD="mysql -h $DBHOST -u$DBROOTUSER "
DUMP_CMD="mysqldump -h $DBHOST -u$DBROOTUSER -c -t "
#################################################################
# read password
prompt_pw()
{
savetty=`stty -g`
echo -n "MySQL password for $DBROOTUSER: "
stty -echo
read PW
stty $savetty
echo
export PW
}
# execute sql command with optional db name
# and password parameters given
sql_query()
{
if [ $# -gt 1 ] ; then
if [ -n "$1" ]; then
DB="$1" # no quoting, mysql client don't like this
else
DB=""
fi
shift
if [ -n "$PW" ]; then
$CMD "-p$PW" $DB -e "$@"
else
$CMD $DB -e "$@"
fi
else
if [ -n "$PW" ]; then
$CMD "-p$PW" "$@"
else
$CMD "$@"
fi
fi
}
kamailio_drop() # pars: <database name>
{
if [ $# -ne 1 ] ; then
merr "kamailio_drop function takes two params"
exit 1
fi
sql_query "" "DROP DATABASE $1;"
if [ $? -ne 0 ] ; then
merr "Dropping database $1 failed!"
exit 1
fi
minfo "Database $1 deleted"
}
db_charset_test()
{
if [ -n "$PW" ]; then
CURRCHARSET=`echo "show variables like '%character_set_server%'" | $CMD "-p$PW" | $AWK '{print $2}' | $SED -e 1d`
ALLCHARSETS=`echo "show character set" | $CMD "-p$PW" | $AWK '{print $1}' | $SED -e 1d | $GREP -iv -e "utf8\|ucs2"`
else
CURRCHARSET=`echo "show variables like '%character_set_server%'" | $CMD | $AWK '{print $2}' | $SED -e 1d`
ALLCHARSETS=`echo "show character set" | $CMD | $AWK '{print $1}' | $SED -e 1d | $GREP -iv -e "utf8\|ucs2"`
fi
while [ `echo "$ALLCHARSETS" | $GREP -icw $CURRCHARSET` = "0" ]
do
mwarn "Your current default mysql characters set cannot be used to create DB. Please choice another one from the following list:"
mecho "$ALLCHARSETS"
mecho "Enter character set name: "
read CURRCHARSET
if [ `echo $CURRCHARSET | $GREP -cE "\w+"` = "0" ]; then
merr "can't continue: user break"
exit 1
fi
done
CHARSET=$CURRCHARSET
}
kamailio_db_create () # pars: <database name>
{
if [ $# -ne 1 ] ; then
merr "kamailio_db_create function takes one param"
exit 1
fi
if [ "$CHARSET" = "" ]; then
minfo "test server charset"
db_charset_test
fi
minfo "creating database $1 ..."
sql_query "" "CREATE DATABASE $1 CHARACTER SET $CHARSET;"
if [ $? -ne 0 ] ; then
merr "Creating database $1 failed!"
exit 1
fi
}
kamailio_db_grant () # pars: <database name>
{
if [ $# -ne 1 ] ; then
merr "kamailio_db_grant function takes one param"
exit 1
fi
minfo "granting privileges to database $1 ..."
# Users: kamailio is the regular user, kamailioro only for reading
sql_query "" "GRANT ALL PRIVILEGES ON $1.* TO '${DBRWUSER}'@'$DBHOST' IDENTIFIED BY '$DBRWPW';
GRANT SELECT ON $1.* TO '${DBROUSER}'@'$DBHOST' IDENTIFIED BY '$DBROPW';"
if [ $? -ne 0 ] ; then
merr "granting privileges to database $1 failed!"
exit 1
fi
if [ "$DBHOST" != "localhost" ] ; then
sql_query "" "GRANT ALL PRIVILEGES ON $1.* TO '$DBRWUSER'@'%' IDENTIFIED BY '$DBRWPW';
GRANT SELECT ON $1.* TO '$DBROUSER'@'%' IDENTIFIED BY '$DBROPW';"
if [ $? -ne 0 ] ; then
merr "granting localhost privileges to database $1 failed!"
exit 1
fi
fi
if [ ! -z "$DBACCESSHOST" ] ; then
sql_query "" "GRANT ALL PRIVILEGES ON $1.* TO '$DBRWUSER'@'$DBACCESSHOST' IDENTIFIED BY '$DBRWPW';
GRANT SELECT ON $1.* TO '$DBROUSER'@'$DBACCESSHOST' IDENTIFIED BY '$DBROPW';"
if [ $? -ne 0 ] ; then
merr "granting access host privileges to database $1 failed!"
exit 1
fi
fi
}
kamailio_db_revoke () # pars: <database name>
{
if [ $# -ne 1 ] ; then
merr "kamailio_db_revoke function takes one param"
exit 1
fi
minfo "revoking privileges to database $1 ..."
# Users: kamailio is the regular user, kamailioro only for reading
sql_query "" "REVOKE ALL PRIVILEGES ON $1.* FROM '${DBRWUSER}'@'$DBHOST';
REVOKE SELECT ON $1.* FROM '${DBROUSER}'@'$DBHOST';"
if [ $? -ne 0 ] ; then
merr "revoking privileges to database $1 failed!"
exit 1
fi
if [ "$DBHOST" != "localhost" ] ; then
sql_query "" "REVOKE ALL PRIVILEGES ON $1.* FROM '$DBRWUSER'@'localhost';
REVOKE SELECT ON $1.* FROM '$DBROUSER'@'localhost';"
if [ $? -ne 0 ] ; then
merr "granting localhost privileges to database $1 failed!"
exit 1
fi
fi
if [ ! -z "$DBACCESSHOST" ] ; then
sql_query "" "REVOKE ALL PRIVILEGES ON $1.* FROM '$DBRWUSER'@'$DBACCESSHOST';
REVOKE SELECT ON $1.* FROM '$DBROUSER'@'$DBACCESSHOST';"
if [ $? -ne 0 ] ; then
merr "granting access host privileges to database $1 failed!"
exit 1
fi
fi
}
kamailio_create () # pars: <database name>
{
if [ $# -ne 1 ] ; then
merr "kamailio_create function takes one param"
exit 1
fi
kamailio_db_create $1
kamailio_db_grant $1
standard_create $1
get_answer $INSTALL_PRESENCE_TABLES "Install presence related tables? (y/n): "
if [ "$ANSWER" = "y" ]; then
presence_create $1
fi
get_answer $INSTALL_EXTRA_TABLES "Install tables for $EXTRA_MODULES? (y/n): "
if [ "$ANSWER" = "y" ]; then
HAS_EXTRA="yes"
extra_create $1
fi
get_answer $INSTALL_DBUID_TABLES "Install tables for $DBUID_MODULES? (y/n): "
if [ "$ANSWER" = "y" ]; then
HAS_EXTRA="yes"
dbuid_create $1
fi
} # end kamailio_create
standard_create () # pars: <database name>
{
if [ $# -ne 1 ] ; then
merr "standard_create function takes one param"
exit 1
fi
minfo "creating standard tables into $1 ..."
for TABLE in $STANDARD_MODULES; do
mdbg "Creating core table: $TABLE"
sql_query $1 < $DB_SCHEMA/$TABLE-create.sql
if [ $? -ne 0 ] ; then
merr "Creating core tables failed at $TABLE!"
exit 1
fi
done
minfo "Core Kamailio tables succesfully created."
if [ -e $DB_SCHEMA/extensions-create.sql ]
then
minfo "Creating custom extensions tables"
sql_query $1 < $DB_SCHEMA/extensions-create.sql
if [ $? -ne 0 ] ; then
merr "Creating custom extensions tables failed!"
exit 1
fi
fi
} # end standard_create
presence_create () # pars: <database name>
{
if [ $# -ne 1 ] ; then
merr "presence_create function takes one param"
exit 1
fi
minfo "creating presence tables into $1 ..."
for TABLE in $PRESENCE_MODULES; do
mdbg "Creating presence tables for $TABLE"
sql_query $1 < $DB_SCHEMA/$TABLE-create.sql
if [ $? -ne 0 ] ; then
merr "Creating presence tables failed at $TABLE!"
exit 1
fi
done
minfo "Presence tables succesfully created."
} # end presence_create
extra_create () # pars: <database name>
{
if [ $# -ne 1 ] ; then
merr "extra_create function takes one param"
exit 1
fi
minfo "creating extra tables into $1 ..."
for TABLE in $EXTRA_MODULES; do
mdbg "Creating extra table: $TABLE"
sql_query $1 < $DB_SCHEMA/$TABLE-create.sql
if [ $? -ne 0 ] ; then
merr "Creating extra tables failed at $TABLE!"
exit 1
fi
done
minfo "Extra tables succesfully created."
} # end extra_create
dbuid_create () # pars: <database name>
{
if [ $# -ne 1 ] ; then
merr "dbuid_create function takes one param"
exit 1
fi
minfo "creating uid tables into $1 ..."
for TABLE in $DBUID_MODULES; do
mdbg "Creating uid table: $TABLE"
sql_query $1 < $DB_SCHEMA/$TABLE-create.sql
if [ $? -ne 0 ] ; then
merr "Creating uid tables failed at $TABLE!"
exit 1
fi
done
minfo "UID tables succesfully created."
} # end uid_create
kamailio_add_tables () # params: <database name> <tables group name>
{
if [ $# -ne 2 ] ; then
merr "kamailio_add_tables function takes two params"
exit 1
fi
minfo "creating group of tables [$2] into database [$1] ..."
if [ -e $DB_SCHEMA/$2-create.sql ]
then
sql_query $1 < $DB_SCHEMA/$2-create.sql
if [ $? -ne 0 ] ; then
merr "Creating group of tables [$2] failed"
exit 1
fi
else
merr "Script for creating group of tables [$2] not found"
exit 1
fi
} # end kamailio_add_tables
migrate_table () # 4 paremeters (dst_table, dst_cols, src_table, src_cols)
{
if [ $# -ne 4 ] ; then
merr "migrate_table function takes 4 params $@"
exit 1
fi
src_cols=`echo $4 | sed s/?/$3./g `
X=`sql_query "" "INSERT into $1 ($2) SELECT $src_cols from $3;" 2>&1`
if [ $? -ne 0 ] ; then
echo $X | $GREP "ERROR 1146" > /dev/null
if [ $? -eq 0 ] ; then
echo " -- Migrating $3 to $1.....SKIPPED (no source)"
return 0
fi
echo "ERROR: failed to migrate $3 to $1!!!"
echo -n "Skip it and continue (y/n)? "
read INPUT
if [ "$INPUT" = "y" ] || [ "$INPUT" = "Y" ]
then
return 0
fi
exit 1;
fi
minfo " -- Migrating $3 to $1.....OK"
}
migrate_db () # 2 parameters (src_db, dst_db)
{
if [ $# -ne 2 ] ; then
merr "migrate_db function takes 2 params"
exit 1
fi
dst_db=$2
src_db=$1
migrate_table ${dst_db}.acc \
"id,method,from_tag,to_tag,callid,sip_code,sip_reason,time" \
${src_db}.acc \
"?id,?method,?from_tag,?to_tag,?callid,?sip_code,?sip_reason,?time"
migrate_table ${dst_db}.missed_calls \
"id,method,from_tag,to_tag,callid,sip_code,sip_reason,time" \
${src_db}.missed_calls \
"?id,?method,?from_tag,?to_tag,?callid,?sip_code,?sip_reason,?time"
migrate_table ${dst_db}.aliases \
"id,username,domain,contact,expires,q,callid,cseq,last_modified,\
flags,cflags,user_agent" \
${src_db}.aliases \
"?id,?username,?domain,?contact,?expires,?q,?callid,?cseq,?last_modified,\
?flags,?cflags,?user_agent"
migrate_table ${dst_db}.dbaliases \
"id,alias_username,alias_domain,username,domain" \
${src_db}.dbaliases \
"?id,?alias_username,?alias_domain,?username,?domain"
migrate_table ${dst_db}.grp \
"id,username,domain,grp,last_modified" \
${src_db}.grp \
"?id,?username,?domain,?grp,?last_modified"
migrate_table ${dst_db}.re_grp \
"id,reg_exp,group_id" \
${src_db}.re_grp \
"?id,?reg_exp,?group_id"
migrate_table ${dst_db}.silo \
"id,src_addr,dst_addr,username,domain,inc_time,exp_time,snd_time,\
ctype,body" \
${src_db}.silo \
"?id,?src_addr,?dst_addr,?username,?domain,?inc_time,?exp_time,?snd_time,\
?ctype,?body"
migrate_table ${dst_db}.domain \
"id,domain,last_modified" \
${src_db}.domain \
"?id,?domain,?last_modified"
migrate_table ${dst_db}.uri \
"id,username,domain,uri_user,last_modified" \
${src_db}.uri \
"?id,?username,?domain,?uri_user,?last_modified"
migrate_table ${dst_db}.usr_preferences \
"id,uuid,username,domain,attribute,type,value,last_modified" \
${src_db}.usr_preferences \
"?id,?uuid,?username,?domain,?attribute,?type,?value,?last_modified"
migrate_table ${dst_db}.trusted \
"id,src_ip,proto,from_pattern,tag" \
${src_db}.trusted \
"?id,?src_ip,?proto,?from_pattern,?tag"
migrate_table ${dst_db}.address \
"id,grp,ip_addr,mask,port" \
${src_db}.address \
"?id,?grp,?ip_addr,?mask,?port"
migrate_table ${dst_db}.speed_dial \
"id,username,domain,sd_username,sd_domain,new_uri,\
fname,lname,description" \
${src_db}.speed_dial \
"?id,?username,?domain,?sd_username,?sd_domain,?new_uri,\
?fname,?lname,?description"
migrate_table ${dst_db}.gw \
"id,gw_name,grp_id,ip_addr,port,uri_scheme,transport,strip,prefix" \
${src_db}.gw \
"?id,?gw_name,?grp_id,?ip_addr,?port,?uri_scheme,?transport,?strip,?prefix"
migrate_table ${dst_db}.gw_grp \
"grp_id,grp_name" \
${src_db}.gw_grp \
"?grp_id,?grp_name"
migrate_table ${dst_db}.lcr \
"id,prefix,from_uri,grp_id,priority" \
${src_db}.lcr \
"?id,?prefix,?from_uri,?grp_id,?priority"
migrate_table ${dst_db}.pdt \
"id,sdomain,prefix,domain" \
${src_db}.pdt \
"?id,?sdomain,?prefix,?domain"
# migrate subscribers with no serweb support
migrate_table ${dst_db}.subscriber \
"id,username,domain,password,ha1,ha1b,rpid" \
${src_db}.subscriber \
"?id,?username,?domain,?password,?ha1,?ha1b,?rpid"
if [ "$HAS_EXTRA" = "yes" ] ; then
migrate_table ${dst_db}.cpl \
"id,username,domain,cpl_xml,cpl_bin" \
${src_db}.cpl \
"?id,?username,?domain,?cpl_xml,?cpl_bin"
migrate_table ${dst_db}.siptrace \
"id,date,callid,traced_user,msg,method,status,fromip,toip,\
fromtag,direction" \
${src_db}.siptrace \
"?id,?date,?callid,?traced_user,?msg,?method,?status,?fromip,?toip,\
?fromtag,?direction"
migrate_table ${dst_db}.imc_rooms \
"id,name,domain,flag" \
${src_db}.imc_rooms \
"?id,?name,?domain,?flag"
migrate_table ${dst_db}.imc_members \
"id,username,domain,room,flag" \
${src_db}.im_members \
"?id,?username,?domain,?room,?flag"
fi
} #end migrate_db()
export PW
if [ "$#" -ne 0 ] && [ "$PW" = "" ]; then
if [ "$PWSKIP" = "" ]; then
prompt_pw
fi
fi