2 # Script for adding and dropping Kamailio MySQL tables
5 # 2006-04-07 removed gen_ha1 dependency - use md5sum;
6 # separated the serweb from kamailio tables;
7 # fixed the reinstall functionality (bogdan)
8 # 2006-05-16 added ability to specify MD5 from a configuration file
9 # FreeBSD does not have the md5sum function (norm)
10 # 2006-09-02 Added address table (juhe)
11 # 2006-10-27 subscriber table cleanup; some columns are created only if
12 # serweb is installed (bogdan)
13 # 2007-02-28 DB migration added (bogdan)
14 # 2007-05-21 Move SQL database definitions out of this script (henning)
15 # 2007-05-31 Move common definitions to kamdbctl.base file (henningw)
16 # 2007-06-11 Use a common control tool for database tasks, like the kamctl
18 # path to the database schemas
19 DATA_DIR="/usr/local/share/kamailio"
20 if [ -d "$DATA_DIR/mysql" ] ; then
21 DB_SCHEMA="$DATA_DIR/mysql"
26 #################################################################
28 #################################################################
30 # full privileges MySQL user
31 if [ -z "$DBROOTUSER" ]; then
35 # Uncomment this to set the database root password if you want to run this
36 # script without any user prompt. This is unsafe, but useful e.g. for
41 if [ -z "$DBPORT" ] ; then
42 CMD="mysql -h $DBHOST -u$DBROOTUSER "
43 DUMP_CMD="mysqldump -h $DBHOST -u$DBROOTUSER -c -t "
45 CMD="mysql -h $DBHOST -P $DBPORT -u$DBROOTUSER "
46 DUMP_CMD="mysqldump -h $DBHOST -P $DBPORT -u$DBROOTUSER -c -t "
49 #################################################################
56 echo -n "MySQL password for $DBROOTUSER: "
64 # execute sql command with optional db name
65 # and password parameters given
68 if [ $# -gt 1 ] ; then
70 DB="$1" # no quoting, mysql client don't like this
76 $CMD "-p$PW" $DB -e "$@"
90 kamailio_drop() # pars: <database name>
92 if [ $# -ne 1 ] ; then
93 merr "kamailio_drop function takes two params"
97 sql_query "" "DROP DATABASE $1;"
99 if [ $? -ne 0 ] ; then
100 merr "Dropping database $1 failed!"
103 minfo "Database $1 deleted"
109 if [ -n "$PW" ]; then
110 CURRCHARSET=`echo "show variables like '%character_set_server%'" | $CMD "-p$PW" | $AWK '{print $2}' | $SED -e 1d`
111 ALLCHARSETS=`echo "show character set" | $CMD "-p$PW" | $AWK '{print $1}' | $SED -e 1d | $GREP -iv -e "utf8\|ucs2"`
113 CURRCHARSET=`echo "show variables like '%character_set_server%'" | $CMD | $AWK '{print $2}' | $SED -e 1d`
114 ALLCHARSETS=`echo "show character set" | $CMD | $AWK '{print $1}' | $SED -e 1d | $GREP -iv -e "utf8\|ucs2"`
117 while [ `echo "$ALLCHARSETS" | $GREP -icw $CURRCHARSET` = "0" ]
119 mwarn "Your current default mysql characters set cannot be used to create DB. Please choice another one from the following list:"
121 mecho "Enter character set name: "
123 if [ `echo $CURRCHARSET | $GREP -cE "\w+"` = "0" ]; then
124 merr "can't continue: user break"
131 kamailio_db_create () # pars: <database name>
133 if [ $# -ne 1 ] ; then
134 merr "kamailio_db_create function takes one param"
138 if [ "$CHARSET" = "" ]; then
139 minfo "test server charset"
143 minfo "creating database $1 ..."
145 sql_query "" "CREATE DATABASE $1 CHARACTER SET $CHARSET;"
147 if [ $? -ne 0 ] ; then
148 merr "Creating database $1 failed!"
153 kamailio_db_grant () # pars: <database name>
155 if [ $# -ne 1 ] ; then
156 merr "kamailio_db_grant function takes one param"
160 minfo "granting privileges to database $1 ..."
162 # Users: kamailio is the regular user, kamailioro only for reading
163 sql_query "" "GRANT ALL PRIVILEGES ON $1.* TO '${DBRWUSER}'@'$DBHOST' IDENTIFIED BY '$DBRWPW';
164 GRANT SELECT ON $1.* TO '${DBROUSER}'@'$DBHOST' IDENTIFIED BY '$DBROPW';"
166 if [ $? -ne 0 ] ; then
167 merr "granting privileges to database $1 failed!"
171 if [ "$DBHOST" != "localhost" ] ; then
172 sql_query "" "GRANT ALL PRIVILEGES ON $1.* TO '$DBRWUSER'@'localhost' IDENTIFIED BY '$DBRWPW';
173 GRANT SELECT ON $1.* TO '$DBROUSER'@'localhost' IDENTIFIED BY '$DBROPW';"
174 if [ $? -ne 0 ] ; then
175 merr "granting localhost privileges to database $1 failed!"
180 if [ ! -z "$DBACCESSHOST" ] ; then
181 sql_query "" "GRANT ALL PRIVILEGES ON $1.* TO '$DBRWUSER'@'$DBACCESSHOST' IDENTIFIED BY '$DBRWPW';
182 GRANT SELECT ON $1.* TO '$DBROUSER'@'$DBACCESSHOST' IDENTIFIED BY '$DBROPW';"
183 if [ $? -ne 0 ] ; then
184 merr "granting access host privileges to database $1 failed!"
190 kamailio_db_revoke () # pars: <database name>
192 if [ $# -ne 1 ] ; then
193 merr "kamailio_db_revoke function takes one param"
197 minfo "revoking privileges to database $1 ..."
199 # Users: kamailio is the regular user, kamailioro only for reading
200 sql_query "" "REVOKE ALL PRIVILEGES ON $1.* FROM '${DBRWUSER}'@'$DBHOST';
201 REVOKE SELECT ON $1.* FROM '${DBROUSER}'@'$DBHOST';"
203 if [ $? -ne 0 ] ; then
204 merr "revoking privileges to database $1 failed!"
208 if [ "$DBHOST" != "localhost" ] ; then
209 sql_query "" "REVOKE ALL PRIVILEGES ON $1.* FROM '$DBRWUSER'@'localhost';
210 REVOKE SELECT ON $1.* FROM '$DBROUSER'@'localhost';"
211 if [ $? -ne 0 ] ; then
212 merr "granting localhost privileges to database $1 failed!"
217 if [ ! -z "$DBACCESSHOST" ] ; then
218 sql_query "" "REVOKE ALL PRIVILEGES ON $1.* FROM '$DBRWUSER'@'$DBACCESSHOST';
219 REVOKE SELECT ON $1.* FROM '$DBROUSER'@'$DBACCESSHOST';"
220 if [ $? -ne 0 ] ; then
221 merr "granting access host privileges to database $1 failed!"
228 kamailio_create () # pars: <database name>
230 if [ $# -ne 1 ] ; then
231 merr "kamailio_create function takes one param"
235 kamailio_db_create $1
241 get_answer $INSTALL_PRESENCE_TABLES "Install presence related tables? (y/n): "
242 if [ "$ANSWER" = "y" ]; then
246 get_answer $INSTALL_EXTRA_TABLES "Install tables for $EXTRA_MODULES? (y/n): "
247 if [ "$ANSWER" = "y" ]; then
252 get_answer $INSTALL_DBUID_TABLES "Install tables for $DBUID_MODULES? (y/n): "
253 if [ "$ANSWER" = "y" ]; then
257 } # end kamailio_create
259 standard_create () # pars: <database name>
261 if [ $# -ne 1 ] ; then
262 merr "standard_create function takes one param"
266 minfo "creating standard tables into $1 ..."
268 for TABLE in $STANDARD_MODULES; do
269 mdbg "Creating core table: $TABLE"
270 sql_query $1 < $DB_SCHEMA/$TABLE-create.sql
271 if [ $? -ne 0 ] ; then
272 merr "Creating core tables failed at $TABLE!"
277 minfo "Core Kamailio tables succesfully created."
279 if [ -e $DB_SCHEMA/extensions-create.sql ]
281 minfo "Creating custom extensions tables"
282 sql_query $1 < $DB_SCHEMA/extensions-create.sql
283 if [ $? -ne 0 ] ; then
284 merr "Creating custom extensions tables failed!"
288 } # end standard_create
291 presence_create () # pars: <database name>
293 if [ $# -ne 1 ] ; then
294 merr "presence_create function takes one param"
298 minfo "creating presence tables into $1 ..."
300 for TABLE in $PRESENCE_MODULES; do
301 mdbg "Creating presence tables for $TABLE"
302 sql_query $1 < $DB_SCHEMA/$TABLE-create.sql
303 if [ $? -ne 0 ] ; then
304 merr "Creating presence tables failed at $TABLE!"
309 minfo "Presence tables succesfully created."
310 } # end presence_create
313 extra_create () # pars: <database name>
315 if [ $# -ne 1 ] ; then
316 merr "extra_create function takes one param"
320 minfo "creating extra tables into $1 ..."
322 for TABLE in $EXTRA_MODULES; do
323 mdbg "Creating extra table: $TABLE"
324 sql_query $1 < $DB_SCHEMA/$TABLE-create.sql
325 if [ $? -ne 0 ] ; then
326 merr "Creating extra tables failed at $TABLE!"
330 minfo "Extra tables succesfully created."
333 dbuid_create () # pars: <database name>
335 if [ $# -ne 1 ] ; then
336 merr "dbuid_create function takes one param"
340 minfo "creating uid tables into $1 ..."
342 for TABLE in $DBUID_MODULES; do
343 mdbg "Creating uid table: $TABLE"
344 sql_query $1 < $DB_SCHEMA/$TABLE-create.sql
345 if [ $? -ne 0 ] ; then
346 merr "Creating uid tables failed at $TABLE!"
350 minfo "UID tables succesfully created."
354 kamailio_add_tables () # params: <database name> <tables group name>
356 if [ $# -ne 2 ] ; then
357 merr "kamailio_add_tables function takes two params"
361 minfo "creating group of tables [$2] into database [$1] ..."
363 if [ -e $DB_SCHEMA/$2-create.sql ]
365 sql_query $1 < $DB_SCHEMA/$2-create.sql
366 if [ $? -ne 0 ] ; then
367 merr "Creating group of tables [$2] failed"
371 merr "Script for creating group of tables [$2] not found"
374 } # end kamailio_add_tables
376 migrate_table () # 4 paremeters (dst_table, dst_cols, src_table, src_cols)
378 if [ $# -ne 4 ] ; then
379 merr "migrate_table function takes 4 params $@"
383 src_cols=`echo $4 | sed s/?/$3./g `
385 X=`sql_query "" "INSERT into $1 ($2) SELECT $src_cols from $3;" 2>&1`
387 if [ $? -ne 0 ] ; then
388 echo $X | $GREP "ERROR 1146" > /dev/null
389 if [ $? -eq 0 ] ; then
390 echo " -- Migrating $3 to $1.....SKIPPED (no source)"
393 echo "ERROR: failed to migrate $3 to $1!!!"
394 echo -n "Skip it and continue (y/n)? "
396 if [ "$INPUT" = "y" ] || [ "$INPUT" = "Y" ]
404 minfo " -- Migrating $3 to $1.....OK"
408 migrate_db () # 2 parameters (src_db, dst_db)
410 if [ $# -ne 2 ] ; then
411 merr "migrate_db function takes 2 params"
418 migrate_table ${dst_db}.acc \
419 "id,method,from_tag,to_tag,callid,sip_code,sip_reason,time" \
421 "?id,?method,?from_tag,?to_tag,?callid,?sip_code,?sip_reason,?time"
423 migrate_table ${dst_db}.missed_calls \
424 "id,method,from_tag,to_tag,callid,sip_code,sip_reason,time" \
425 ${src_db}.missed_calls \
426 "?id,?method,?from_tag,?to_tag,?callid,?sip_code,?sip_reason,?time"
428 migrate_table ${dst_db}.aliases \
429 "id,username,domain,contact,expires,q,callid,cseq,last_modified,\
430 flags,cflags,user_agent" \
432 "?id,?username,?domain,?contact,?expires,?q,?callid,?cseq,?last_modified,\
433 ?flags,?cflags,?user_agent"
435 migrate_table ${dst_db}.dbaliases \
436 "id,alias_username,alias_domain,username,domain" \
437 ${src_db}.dbaliases \
438 "?id,?alias_username,?alias_domain,?username,?domain"
440 migrate_table ${dst_db}.grp \
441 "id,username,domain,grp,last_modified" \
443 "?id,?username,?domain,?grp,?last_modified"
445 migrate_table ${dst_db}.re_grp \
446 "id,reg_exp,group_id" \
448 "?id,?reg_exp,?group_id"
450 migrate_table ${dst_db}.silo \
451 "id,src_addr,dst_addr,username,domain,inc_time,exp_time,snd_time,\
454 "?id,?src_addr,?dst_addr,?username,?domain,?inc_time,?exp_time,?snd_time,\
457 migrate_table ${dst_db}.domain \
458 "id,domain,last_modified" \
460 "?id,?domain,?last_modified"
462 migrate_table ${dst_db}.uri \
463 "id,username,domain,uri_user,last_modified" \
465 "?id,?username,?domain,?uri_user,?last_modified"
467 migrate_table ${dst_db}.usr_preferences \
468 "id,uuid,username,domain,attribute,type,value,last_modified" \
469 ${src_db}.usr_preferences \
470 "?id,?uuid,?username,?domain,?attribute,?type,?value,?last_modified"
472 migrate_table ${dst_db}.trusted \
473 "id,src_ip,proto,from_pattern,tag" \
475 "?id,?src_ip,?proto,?from_pattern,?tag"
477 migrate_table ${dst_db}.address \
478 "id,grp,ip_addr,mask,port" \
480 "?id,?grp,?ip_addr,?mask,?port"
482 migrate_table ${dst_db}.speed_dial \
483 "id,username,domain,sd_username,sd_domain,new_uri,\
484 fname,lname,description" \
485 ${src_db}.speed_dial \
486 "?id,?username,?domain,?sd_username,?sd_domain,?new_uri,\
487 ?fname,?lname,?description"
489 migrate_table ${dst_db}.gw \
490 "id,gw_name,grp_id,ip_addr,port,uri_scheme,transport,strip,prefix" \
492 "?id,?gw_name,?grp_id,?ip_addr,?port,?uri_scheme,?transport,?strip,?prefix"
494 migrate_table ${dst_db}.gw_grp \
499 migrate_table ${dst_db}.lcr \
500 "id,prefix,from_uri,grp_id,priority" \
502 "?id,?prefix,?from_uri,?grp_id,?priority"
504 migrate_table ${dst_db}.pdt \
505 "id,sdomain,prefix,domain" \
507 "?id,?sdomain,?prefix,?domain"
509 # migrate subscribers with no serweb support
510 migrate_table ${dst_db}.subscriber \
511 "id,username,domain,password,ha1,ha1b,rpid" \
512 ${src_db}.subscriber \
513 "?id,?username,?domain,?password,?ha1,?ha1b,?rpid"
515 if [ "$HAS_EXTRA" = "yes" ] ; then
516 migrate_table ${dst_db}.cpl \
517 "id,username,domain,cpl_xml,cpl_bin" \
519 "?id,?username,?domain,?cpl_xml,?cpl_bin"
521 migrate_table ${dst_db}.siptrace \
522 "id,date,callid,traced_user,msg,method,status,fromip,toip,\
525 "?id,?date,?callid,?traced_user,?msg,?method,?status,?fromip,?toip,\
528 migrate_table ${dst_db}.imc_rooms \
529 "id,name,domain,flag" \
530 ${src_db}.imc_rooms \
531 "?id,?name,?domain,?flag"
533 migrate_table ${dst_db}.imc_members \
534 "id,username,domain,room,flag" \
535 ${src_db}.im_members \
536 "?id,?username,?domain,?room,?flag"
544 if [ "$#" -ne 0 ] && [ "$PW" = "" ]; then
545 if [ "$PWSKIP" = "" ]; then