kamdbctl.mysql 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. # $Id$
  2. #
  3. # Script for adding and dropping Kamailio MySQL tables
  4. #
  5. # History:
  6. # 2006-04-07 removed gen_ha1 dependency - use md5sum;
  7. # separated the serweb from kamailio tables;
  8. # fixed the reinstall functionality (bogdan)
  9. # 2006-05-16 added ability to specify MD5 from a configuration file
  10. # FreeBSD does not have the md5sum function (norm)
  11. # 2006-09-02 Added address table (juhe)
  12. # 2006-10-27 subscriber table cleanup; some columns are created only if
  13. # serweb is installed (bogdan)
  14. # 2007-02-28 DB migration added (bogdan)
  15. # 2007-05-21 Move SQL database definitions out of this script (henning)
  16. # 2007-05-31 Move common definitions to kamdbctl.base file (henningw)
  17. # 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"
  22. else
  23. DB_SCHEMA="./mysql"
  24. fi
  25. #################################################################
  26. # config vars
  27. #################################################################
  28. # full privileges MySQL user
  29. if [ -z "$DBROOTUSER" ]; then
  30. DBROOTUSER="root"
  31. fi
  32. # Uncomment this to set the database root password if you want to run this
  33. # script without any user prompt. This is unsafe, but useful e.g. for
  34. # automatic testing.
  35. #PW=""
  36. CMD="mysql -h $DBHOST -u$DBROOTUSER "
  37. DUMP_CMD="mysqldump -h $DBHOST -u$DBROOTUSER -c -t "
  38. #################################################################
  39. # read password
  40. prompt_pw()
  41. {
  42. savetty=`stty -g`
  43. echo -n "MySQL password for $DBROOTUSER: "
  44. stty -echo
  45. read PW
  46. stty $savetty
  47. echo
  48. export PW
  49. }
  50. # execute sql command with optional db name
  51. # and password parameters given
  52. sql_query()
  53. {
  54. if [ $# -gt 1 ] ; then
  55. if [ -n "$1" ]; then
  56. DB="$1" # no quoting, mysql client don't like this
  57. else
  58. DB=""
  59. fi
  60. shift
  61. if [ -n "$PW" ]; then
  62. $CMD "-p$PW" $DB -e "$@"
  63. else
  64. $CMD $DB -e "$@"
  65. fi
  66. else
  67. if [ -n "$PW" ]; then
  68. $CMD "-p$PW" "$@"
  69. else
  70. $CMD "$@"
  71. fi
  72. fi
  73. }
  74. kamailio_drop() # pars: <database name>
  75. {
  76. if [ $# -ne 1 ] ; then
  77. merr "kamailio_drop function takes two params"
  78. exit 1
  79. fi
  80. sql_query "" "DROP DATABASE $1;"
  81. if [ $? -ne 0 ] ; then
  82. merr "Dropping database $1 failed!"
  83. exit 1
  84. fi
  85. minfo "Database $1 deleted"
  86. }
  87. db_charset_test()
  88. {
  89. if [ -n "$PW" ]; then
  90. CURRCHARSET=`echo "show variables like '%character_set_server%'" | $CMD "-p$PW" | $AWK '{print $2}' | $SED -e 1d`
  91. ALLCHARSETS=`echo "show character set" | $CMD "-p$PW" | $AWK '{print $1}' | $SED -e 1d | $GREP -iv -e "utf8\|ucs2"`
  92. else
  93. CURRCHARSET=`echo "show variables like '%character_set_server%'" | $CMD | $AWK '{print $2}' | $SED -e 1d`
  94. ALLCHARSETS=`echo "show character set" | $CMD | $AWK '{print $1}' | $SED -e 1d | $GREP -iv -e "utf8\|ucs2"`
  95. fi
  96. while [ `echo "$ALLCHARSETS" | $GREP -icw $CURRCHARSET` = "0" ]
  97. do
  98. mwarn "Your current default mysql characters set cannot be used to create DB. Please choice another one from the following list:"
  99. mecho "$ALLCHARSETS"
  100. mecho "Enter character set name: "
  101. read CURRCHARSET
  102. if [ `echo $CURRCHARSET | $GREP -cE "\w+"` = "0" ]; then
  103. merr "can't continue: user break"
  104. exit 1
  105. fi
  106. done
  107. CHARSET=$CURRCHARSET
  108. }
  109. kamailio_db_create () # pars: <database name>
  110. {
  111. if [ $# -ne 1 ] ; then
  112. merr "kamailio_db_create function takes one param"
  113. exit 1
  114. fi
  115. if [ "$CHARSET" = "" ]; then
  116. minfo "test server charset"
  117. db_charset_test
  118. fi
  119. minfo "creating database $1 ..."
  120. sql_query "" "CREATE DATABASE $1 CHARACTER SET $CHARSET;"
  121. if [ $? -ne 0 ] ; then
  122. merr "Creating database $1 failed!"
  123. exit 1
  124. fi
  125. }
  126. kamailio_db_grant () # pars: <database name>
  127. {
  128. if [ $# -ne 1 ] ; then
  129. merr "kamailio_db_grant function takes one param"
  130. exit 1
  131. fi
  132. minfo "granting privileges to database $1 ..."
  133. # Users: kamailio is the regular user, kamailioro only for reading
  134. sql_query "" "GRANT ALL PRIVILEGES ON $1.* TO '${DBRWUSER}'@'$DBHOST' IDENTIFIED BY '$DBRWPW';
  135. GRANT SELECT ON $1.* TO '${DBROUSER}'@'$DBHOST' IDENTIFIED BY '$DBROPW';"
  136. if [ $? -ne 0 ] ; then
  137. merr "granting privileges to database $1 failed!"
  138. exit 1
  139. fi
  140. if [ "$DBHOST" != "localhost" ] ; then
  141. sql_query "" "GRANT ALL PRIVILEGES ON $1.* TO '$DBRWUSER'@'localhost' IDENTIFIED BY '$DBRWPW';
  142. GRANT SELECT ON $1.* TO '$DBROUSER'@'localhost' IDENTIFIED BY '$DBROPW';"
  143. if [ $? -ne 0 ] ; then
  144. merr "granting localhost privileges to database $1 failed!"
  145. exit 1
  146. fi
  147. fi
  148. if [ ! -z "$DBACCESSHOST" ] ; then
  149. sql_query "" "GRANT ALL PRIVILEGES ON $1.* TO '$DBRWUSER'@'$DBACCESSHOST' IDENTIFIED BY '$DBRWPW';
  150. GRANT SELECT ON $1.* TO '$DBROUSER'@'$DBACCESSHOST' IDENTIFIED BY '$DBROPW';"
  151. if [ $? -ne 0 ] ; then
  152. merr "granting access host privileges to database $1 failed!"
  153. exit 1
  154. fi
  155. fi
  156. }
  157. kamailio_db_revoke () # pars: <database name>
  158. {
  159. if [ $# -ne 1 ] ; then
  160. merr "kamailio_db_revoke function takes one param"
  161. exit 1
  162. fi
  163. minfo "revoking privileges to database $1 ..."
  164. # Users: kamailio is the regular user, kamailioro only for reading
  165. sql_query "" "REVOKE ALL PRIVILEGES ON $1.* FROM '${DBRWUSER}'@'$DBHOST';
  166. REVOKE SELECT ON $1.* FROM '${DBROUSER}'@'$DBHOST';"
  167. if [ $? -ne 0 ] ; then
  168. merr "revoking privileges to database $1 failed!"
  169. exit 1
  170. fi
  171. if [ "$DBHOST" != "localhost" ] ; then
  172. sql_query "" "REVOKE ALL PRIVILEGES ON $1.* FROM '$DBRWUSER'@'localhost';
  173. REVOKE SELECT ON $1.* FROM '$DBROUSER'@'localhost';"
  174. if [ $? -ne 0 ] ; then
  175. merr "granting localhost privileges to database $1 failed!"
  176. exit 1
  177. fi
  178. fi
  179. if [ ! -z "$DBACCESSHOST" ] ; then
  180. sql_query "" "REVOKE ALL PRIVILEGES ON $1.* FROM '$DBRWUSER'@'$DBACCESSHOST';
  181. REVOKE SELECT ON $1.* FROM '$DBROUSER'@'$DBACCESSHOST';"
  182. if [ $? -ne 0 ] ; then
  183. merr "granting access host privileges to database $1 failed!"
  184. exit 1
  185. fi
  186. fi
  187. }
  188. kamailio_create () # pars: <database name>
  189. {
  190. if [ $# -ne 1 ] ; then
  191. merr "kamailio_create function takes one param"
  192. exit 1
  193. fi
  194. kamailio_db_create $1
  195. kamailio_db_grant $1
  196. standard_create $1
  197. get_answer $INSTALL_PRESENCE_TABLES "Install presence related tables? (y/n): "
  198. if [ "$ANSWER" = "y" ]; then
  199. presence_create $1
  200. fi
  201. get_answer $INSTALL_EXTRA_TABLES "Install tables for $EXTRA_MODULES? (y/n): "
  202. if [ "$ANSWER" = "y" ]; then
  203. HAS_EXTRA="yes"
  204. extra_create $1
  205. fi
  206. get_answer $INSTALL_DBUID_TABLES "Install tables for $DBUID_MODULES? (y/n): "
  207. if [ "$ANSWER" = "y" ]; then
  208. HAS_EXTRA="yes"
  209. dbuid_create $1
  210. fi
  211. } # end kamailio_create
  212. standard_create () # pars: <database name>
  213. {
  214. if [ $# -ne 1 ] ; then
  215. merr "standard_create function takes one param"
  216. exit 1
  217. fi
  218. minfo "creating standard tables into $1 ..."
  219. for TABLE in $STANDARD_MODULES; do
  220. mdbg "Creating core table: $TABLE"
  221. sql_query $1 < $DB_SCHEMA/$TABLE-create.sql
  222. if [ $? -ne 0 ] ; then
  223. merr "Creating core tables failed at $TABLE!"
  224. exit 1
  225. fi
  226. done
  227. minfo "Core Kamailio tables succesfully created."
  228. if [ -e $DB_SCHEMA/extensions-create.sql ]
  229. then
  230. minfo "Creating custom extensions tables"
  231. sql_query $1 < $DB_SCHEMA/extensions-create.sql
  232. if [ $? -ne 0 ] ; then
  233. merr "Creating custom extensions tables failed!"
  234. exit 1
  235. fi
  236. fi
  237. } # end standard_create
  238. presence_create () # pars: <database name>
  239. {
  240. if [ $# -ne 1 ] ; then
  241. merr "presence_create function takes one param"
  242. exit 1
  243. fi
  244. minfo "creating presence tables into $1 ..."
  245. for TABLE in $PRESENCE_MODULES; do
  246. mdbg "Creating presence tables for $TABLE"
  247. sql_query $1 < $DB_SCHEMA/$TABLE-create.sql
  248. if [ $? -ne 0 ] ; then
  249. merr "Creating presence tables failed at $TABLE!"
  250. exit 1
  251. fi
  252. done
  253. minfo "Presence tables succesfully created."
  254. } # end presence_create
  255. extra_create () # pars: <database name>
  256. {
  257. if [ $# -ne 1 ] ; then
  258. merr "extra_create function takes one param"
  259. exit 1
  260. fi
  261. minfo "creating extra tables into $1 ..."
  262. for TABLE in $EXTRA_MODULES; do
  263. mdbg "Creating extra table: $TABLE"
  264. sql_query $1 < $DB_SCHEMA/$TABLE-create.sql
  265. if [ $? -ne 0 ] ; then
  266. merr "Creating extra tables failed at $TABLE!"
  267. exit 1
  268. fi
  269. done
  270. minfo "Extra tables succesfully created."
  271. } # end extra_create
  272. dbuid_create () # pars: <database name>
  273. {
  274. if [ $# -ne 1 ] ; then
  275. merr "dbuid_create function takes one param"
  276. exit 1
  277. fi
  278. minfo "creating uid tables into $1 ..."
  279. for TABLE in $DBUID_MODULES; do
  280. mdbg "Creating uid table: $TABLE"
  281. sql_query $1 < $DB_SCHEMA/$TABLE-create.sql
  282. if [ $? -ne 0 ] ; then
  283. merr "Creating uid tables failed at $TABLE!"
  284. exit 1
  285. fi
  286. done
  287. minfo "UID tables succesfully created."
  288. } # end uid_create
  289. kamailio_add_tables () # params: <database name> <tables group name>
  290. {
  291. if [ $# -ne 2 ] ; then
  292. merr "kamailio_add_tables function takes two params"
  293. exit 1
  294. fi
  295. minfo "creating group of tables [$2] into database [$1] ..."
  296. if [ -e $DB_SCHEMA/$2-create.sql ]
  297. then
  298. sql_query $1 < $DB_SCHEMA/$2-create.sql
  299. if [ $? -ne 0 ] ; then
  300. merr "Creating group of tables [$2] failed"
  301. exit 1
  302. fi
  303. else
  304. merr "Script for creating group of tables [$2] not found"
  305. exit 1
  306. fi
  307. } # end kamailio_add_tables
  308. migrate_table () # 4 paremeters (dst_table, dst_cols, src_table, src_cols)
  309. {
  310. if [ $# -ne 4 ] ; then
  311. merr "migrate_table function takes 4 params $@"
  312. exit 1
  313. fi
  314. src_cols=`echo $4 | sed s/?/$3./g `
  315. X=`sql_query "" "INSERT into $1 ($2) SELECT $src_cols from $3;" 2>&1`
  316. if [ $? -ne 0 ] ; then
  317. echo $X | $GREP "ERROR 1146" > /dev/null
  318. if [ $? -eq 0 ] ; then
  319. echo " -- Migrating $3 to $1.....SKIPPED (no source)"
  320. return 0
  321. fi
  322. echo "ERROR: failed to migrate $3 to $1!!!"
  323. echo -n "Skip it and continue (y/n)? "
  324. read INPUT
  325. if [ "$INPUT" = "y" ] || [ "$INPUT" = "Y" ]
  326. then
  327. return 0
  328. fi
  329. exit 1;
  330. fi
  331. minfo " -- Migrating $3 to $1.....OK"
  332. }
  333. migrate_db () # 2 parameters (src_db, dst_db)
  334. {
  335. if [ $# -ne 2 ] ; then
  336. merr "migrate_db function takes 2 params"
  337. exit 1
  338. fi
  339. dst_db=$2
  340. src_db=$1
  341. migrate_table ${dst_db}.acc \
  342. "id,method,from_tag,to_tag,callid,sip_code,sip_reason,time" \
  343. ${src_db}.acc \
  344. "?id,?method,?from_tag,?to_tag,?callid,?sip_code,?sip_reason,?time"
  345. migrate_table ${dst_db}.missed_calls \
  346. "id,method,from_tag,to_tag,callid,sip_code,sip_reason,time" \
  347. ${src_db}.missed_calls \
  348. "?id,?method,?from_tag,?to_tag,?callid,?sip_code,?sip_reason,?time"
  349. migrate_table ${dst_db}.aliases \
  350. "id,username,domain,contact,expires,q,callid,cseq,last_modified,\
  351. flags,cflags,user_agent" \
  352. ${src_db}.aliases \
  353. "?id,?username,?domain,?contact,?expires,?q,?callid,?cseq,?last_modified,\
  354. ?flags,?cflags,?user_agent"
  355. migrate_table ${dst_db}.dbaliases \
  356. "id,alias_username,alias_domain,username,domain" \
  357. ${src_db}.dbaliases \
  358. "?id,?alias_username,?alias_domain,?username,?domain"
  359. migrate_table ${dst_db}.grp \
  360. "id,username,domain,grp,last_modified" \
  361. ${src_db}.grp \
  362. "?id,?username,?domain,?grp,?last_modified"
  363. migrate_table ${dst_db}.re_grp \
  364. "id,reg_exp,group_id" \
  365. ${src_db}.re_grp \
  366. "?id,?reg_exp,?group_id"
  367. migrate_table ${dst_db}.silo \
  368. "id,src_addr,dst_addr,username,domain,inc_time,exp_time,snd_time,\
  369. ctype,body" \
  370. ${src_db}.silo \
  371. "?id,?src_addr,?dst_addr,?username,?domain,?inc_time,?exp_time,?snd_time,\
  372. ?ctype,?body"
  373. migrate_table ${dst_db}.domain \
  374. "id,domain,last_modified" \
  375. ${src_db}.domain \
  376. "?id,?domain,?last_modified"
  377. migrate_table ${dst_db}.uri \
  378. "id,username,domain,uri_user,last_modified" \
  379. ${src_db}.uri \
  380. "?id,?username,?domain,?uri_user,?last_modified"
  381. migrate_table ${dst_db}.usr_preferences \
  382. "id,uuid,username,domain,attribute,type,value,last_modified" \
  383. ${src_db}.usr_preferences \
  384. "?id,?uuid,?username,?domain,?attribute,?type,?value,?last_modified"
  385. migrate_table ${dst_db}.trusted \
  386. "id,src_ip,proto,from_pattern,tag" \
  387. ${src_db}.trusted \
  388. "?id,?src_ip,?proto,?from_pattern,?tag"
  389. migrate_table ${dst_db}.address \
  390. "id,grp,ip_addr,mask,port" \
  391. ${src_db}.address \
  392. "?id,?grp,?ip_addr,?mask,?port"
  393. migrate_table ${dst_db}.speed_dial \
  394. "id,username,domain,sd_username,sd_domain,new_uri,\
  395. fname,lname,description" \
  396. ${src_db}.speed_dial \
  397. "?id,?username,?domain,?sd_username,?sd_domain,?new_uri,\
  398. ?fname,?lname,?description"
  399. migrate_table ${dst_db}.gw \
  400. "id,gw_name,grp_id,ip_addr,port,uri_scheme,transport,strip,prefix" \
  401. ${src_db}.gw \
  402. "?id,?gw_name,?grp_id,?ip_addr,?port,?uri_scheme,?transport,?strip,?prefix"
  403. migrate_table ${dst_db}.gw_grp \
  404. "grp_id,grp_name" \
  405. ${src_db}.gw_grp \
  406. "?grp_id,?grp_name"
  407. migrate_table ${dst_db}.lcr \
  408. "id,prefix,from_uri,grp_id,priority" \
  409. ${src_db}.lcr \
  410. "?id,?prefix,?from_uri,?grp_id,?priority"
  411. migrate_table ${dst_db}.pdt \
  412. "id,sdomain,prefix,domain" \
  413. ${src_db}.pdt \
  414. "?id,?sdomain,?prefix,?domain"
  415. # migrate subscribers with no serweb support
  416. migrate_table ${dst_db}.subscriber \
  417. "id,username,domain,password,ha1,ha1b,rpid" \
  418. ${src_db}.subscriber \
  419. "?id,?username,?domain,?password,?ha1,?ha1b,?rpid"
  420. if [ "$HAS_EXTRA" = "yes" ] ; then
  421. migrate_table ${dst_db}.cpl \
  422. "id,username,domain,cpl_xml,cpl_bin" \
  423. ${src_db}.cpl \
  424. "?id,?username,?domain,?cpl_xml,?cpl_bin"
  425. migrate_table ${dst_db}.siptrace \
  426. "id,date,callid,traced_user,msg,method,status,fromip,toip,\
  427. fromtag,direction" \
  428. ${src_db}.siptrace \
  429. "?id,?date,?callid,?traced_user,?msg,?method,?status,?fromip,?toip,\
  430. ?fromtag,?direction"
  431. migrate_table ${dst_db}.imc_rooms \
  432. "id,name,domain,flag" \
  433. ${src_db}.imc_rooms \
  434. "?id,?name,?domain,?flag"
  435. migrate_table ${dst_db}.imc_members \
  436. "id,username,domain,room,flag" \
  437. ${src_db}.im_members \
  438. "?id,?username,?domain,?room,?flag"
  439. fi
  440. } #end migrate_db()
  441. export PW
  442. if [ "$#" -ne 0 ] && [ "$PW" = "" ]; then
  443. if [ "$PWSKIP" = "" ]; then
  444. prompt_pw
  445. fi
  446. fi