kamdbctl.mysql 14 KB

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