kamdbctl.mysql 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. # Set DBROOTPW in kamctlrc or via next line to set the database
  32. # root password if you want to run this script without any user prompt.
  33. # This is unsafe, but useful e.g. for automatic testing.
  34. #DBROOTPW=""
  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 DBROOTPW
  50. stty $savetty
  51. echo
  52. export DBROOTPW
  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 "$DBROOTPW" ]; then
  66. $CMD "-p$DBROOTPW" $DB -e "$@"
  67. else
  68. $CMD $DB -e "$@"
  69. fi
  70. else
  71. if [ -n "$DBROOTPW" ]; then
  72. $CMD "-p$DBROOTPW" "$@"
  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 "$DBROOTPW" ]; then
  94. CURRCHARSET=`echo "show variables like '%character_set_server%'" | $CMD "-p$DBROOTPW" | $AWK '{print $2}' | $SED -e 1d`
  95. ALLCHARSETS=`echo "show character set" | $CMD "-p$DBROOTPW" | $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. export DBROOTPW
  313. if [ "$#" -ne 0 ] && [ "$DBROOTPW" = "" ]; then
  314. if [ "$DBROOTPWSKIP" = "" ]; then
  315. prompt_pw
  316. fi
  317. fi