kamdbctl 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. #!/bin/sh
  2. #
  3. # control tool for maintaining Kamailio databases
  4. #
  5. #===================================================================
  6. ### version for this script
  7. VERSION='6.1.0'
  8. PATH=$PATH:/usr/local/sbin/
  9. # for testing only, please don't enable this in production environments
  10. # as this introduce security risks
  11. TEST="false"
  12. ### include resource files, if any
  13. # check for rc file at same location with kamdbctl
  14. which greadlink > /dev/null 2>&1
  15. ret=$?
  16. if [ $ret -eq 0 ] ; then
  17. KAMCTLFULLPATH=$(greadlink -f "$0")
  18. else
  19. which readlink > /dev/null 2>&1
  20. ret=$?
  21. if [ $ret -eq 0 ] ; then
  22. KAMCTLFULLPATH=$(readlink -f "$0")
  23. fi
  24. fi
  25. if [ -n "$KAMCTLFULLPATH" ] ; then
  26. KAMCTLDIRPATH=$(dirname "$KAMCTLFULLPATH")
  27. if [ -f $KAMCTLDIRPATH/kamctlrc ]; then
  28. . $KAMCTLDIRPATH/kamctlrc
  29. fi
  30. fi
  31. # check for rc file at standard locations
  32. if [ -f /etc/kamailio/kamctlrc -a -r /etc/kamailio/kamctlrc ]; then
  33. . /etc/kamailio/kamctlrc
  34. fi
  35. if [ -f /usr/local/etc/kamailio/kamctlrc -a -r /usr/local/etc/kamailio/kamctlrc ]; then
  36. . /usr/local/etc/kamailio/kamctlrc
  37. fi
  38. if [ -f ~/.kamctlrc -a -r ~/.kamctlrc ]; then
  39. . ~/.kamctlrc
  40. fi
  41. if [ $TEST = "true" ]; then
  42. if [ -f ./kamctlrc ]; then
  43. . ./kamctlrc
  44. fi
  45. fi
  46. if [ -z "$MYDIR" ] ; then
  47. MYDIR=`dirname $0`
  48. fi
  49. if [ -z "$MYLIBDIR" ] ; then
  50. MYLIBDIR="/usr/local/lib/kamailio/kamctl"
  51. if [ ! -d "$MYLIBDIR" ]; then
  52. MYLIBDIR=$MYDIR
  53. fi
  54. fi
  55. ##### ------------------------------------------------ #####
  56. ### load base functions
  57. #
  58. if [ -f "$MYLIBDIR/kamdbctl.base" ]; then
  59. . "$MYLIBDIR/kamdbctl.base"
  60. else
  61. printf "Cannot load core functions '%s' - exiting ...\n\n" "$MYLIBDIR/kamdbctl.base"
  62. exit -1
  63. fi
  64. #
  65. ##### ------------------------------------------------ #####
  66. ### DBENGINE
  67. #
  68. unset USED_DBENGINE
  69. if [ -z "$DBENGINE" ] ; then
  70. merr "database engine not specified, please set one up in the config script"
  71. exit 1
  72. fi
  73. case $DBENGINE in
  74. MYSQL|mysql|MySQL)
  75. if [ -f "$MYLIBDIR/kamdbctl.mysql" ]; then
  76. . "$MYLIBDIR/kamdbctl.mysql"
  77. USED_DBENGINE="mysql"
  78. else
  79. merr "could not load the script in $MYLIBDIR/kamdbctl.mysql for database engine $DBENGINE"
  80. fi
  81. ;;
  82. PGSQL|pgsql|postgres|postgresql|POSTGRESQL)
  83. if [ -f "$MYLIBDIR/kamdbctl.pgsql" ]; then
  84. . "$MYLIBDIR/kamdbctl.pgsql"
  85. USED_DBENGINE="postgres"
  86. else
  87. merr "could not load the script in $MYLIBDIR/kamdbctl.pgsql for database engine $DBENGINE"
  88. fi
  89. ;;
  90. ORACLE|oracle|Oracle)
  91. if [ -f "$MYLIBDIR/kamdbctl.oracle" ]; then
  92. . "$MYLIBDIR/kamdbctl.oracle"
  93. USED_DBENGINE="oracle"
  94. else
  95. merr "could not load the script in $MYLIBDIR/kamdbctl.oracle for database engine $DBENGINE"
  96. fi
  97. ;;
  98. DBTEXT|dbtext|textdb)
  99. if [ -f "$MYLIBDIR/kamdbctl.dbtext" ]; then
  100. . "$MYLIBDIR/kamdbctl.dbtext"
  101. USED_DBENGINE="dbtext"
  102. DBNAME=$DB_PATH
  103. else
  104. merr "could not load the script in $MYLIBDIR/kamdbctl.dbtext for database engine $DBENGINE"
  105. fi
  106. ;;
  107. SQLITE|sqlite)
  108. if [ -f "$MYLIBDIR/kamdbctl.sqlite" ]; then
  109. . "$MYLIBDIR/kamdbctl.sqlite"
  110. USED_DBENGINE="sqlite"
  111. DBNAME=$DB_PATH
  112. else
  113. merr "could not load the script in $MYLIBDIR/kamdbctl.sqlite for database engine $DBENGINE"
  114. fi
  115. ;;
  116. esac
  117. if [ -z "$USED_DBENGINE" ] ; then
  118. merr "database engine not loaded - tried '$DBENGINE'"
  119. exit 1
  120. else
  121. mdbg "database engine '$USED_DBENGINE' loaded"
  122. fi
  123. # dump all rows
  124. kamailio_dump() # pars: <database name>
  125. {
  126. if [ $# -ne 2 ] ; then
  127. merr "kamailio_dump function takes two param"
  128. exit 1
  129. fi
  130. if [ "$USED_DBENGINE" = "oracle" ]; then
  131. oracle_dump $1 $2
  132. elif [ "$PW" = "" ] ; then
  133. $DUMP_CMD $1 > $2
  134. else
  135. $DUMP_CMD "-p$PW" $1 > $2
  136. fi
  137. if [ "$?" -ne 0 ]; then
  138. merr "db dump failed"
  139. exit 1
  140. fi
  141. minfo "db dump successful"
  142. }
  143. kamailio_restore() #pars: <database name> <filename>
  144. {
  145. if [ $# -ne 2 ] ; then
  146. merr "kamailio_restore function takes two params"
  147. exit 1
  148. fi
  149. if [ "$USED_DBENGINE" = "oracle" ]; then
  150. oracle_restore $1 $2
  151. else
  152. sql_query $1 < $2
  153. fi
  154. if [ "$?" -ne 0 ]; then
  155. merr "db restore failed"
  156. exit 1
  157. fi
  158. minfo "db restore successful"
  159. }
  160. kamailio_pframework_create() #pars: none
  161. {
  162. if [ -e $DEFAULT_CFG_DIR/pi_framework_sample ] ; then
  163. get_answer ask "Sample already exists. Overwrite? (y/n): "
  164. if [ "$ANSWER" != "y" ]; then
  165. exit 1
  166. fi
  167. fi
  168. touch $DEFAULT_CFG_DIR/pi_framework_sample
  169. if [ $? -ne 0 ] ; then
  170. merr "Unable to create $DEFAULT_CFG_DIR/pi_framework_sample"
  171. exit 1
  172. fi
  173. if [ -d "$DATA_DIR/xhttp_pi" ] ; then
  174. PI_MODULES="$STANDARD_MODULES"
  175. else
  176. merr "Please install first the xhttp_pi module"
  177. exit 1
  178. fi
  179. get_answer $INSTALL_EXTRA_TABLES "Add provisioning framework for extra tables? (y/n): "
  180. if [ "$ANSWER" = "y" ]; then
  181. PI_MODULES="$PI_MODULES $EXTRA_MODULES"
  182. fi
  183. get_answer $INSTALL_PRESENCE_TABLES "Add provisioning framework for presence tables? (y/n): "
  184. if [ "$ANSWER" = "y" ]; then
  185. PI_PRESENCE_MODULES="TRUE"
  186. fi
  187. cat $DATA_DIR/xhttp_pi/pi_framework-00 > $DEFAULT_CFG_DIR/pi_framework_sample
  188. for TABLE in $PI_MODULES; do
  189. if [ -e $DATA_DIR/xhttp_pi/$TABLE-table ]; then
  190. cat $DATA_DIR/xhttp_pi/$TABLE-table >> $DEFAULT_CFG_DIR/pi_framework_sample
  191. else
  192. merr "Unable to configure: $TABLE - missing table descriptor"
  193. fi
  194. done
  195. if [ "$PI_PRESENCE_MODULES" = "TRUE" ]; then
  196. if [ -e $DATA_DIR/xhttp_pi/presence-table ]; then
  197. cat $DATA_DIR/xhttp_pi/presence-table >> $DEFAULT_CFG_DIR/pi_framework_sample
  198. else
  199. merr "Unable to configure: presence - missing table descriptor"
  200. fi
  201. if [ -e $DATA_DIR/xhttp_pi/rls-table ]; then
  202. cat $DATA_DIR/xhttp_pi/rls-table >> $DEFAULT_CFG_DIR/pi_framework_sample
  203. else
  204. merr "Unable to configure: rls - missing table descriptor"
  205. fi
  206. fi
  207. cat $DATA_DIR/xhttp_pi/pi_framework-01 >> $DEFAULT_CFG_DIR/pi_framework_sample
  208. for TABLE in $PI_MODULES; do
  209. if [ -e $DATA_DIR/xhttp_pi/$TABLE-mod ]; then
  210. cat $DATA_DIR/xhttp_pi/$TABLE-mod >> $DEFAULT_CFG_DIR/pi_framework_sample
  211. else
  212. merr "Unable to configure: $TABLE - missing mod descriptor"
  213. fi
  214. done
  215. if [ "$PI_PRESENCE_MODULES" = "TRUE" ]; then
  216. if [ -e $DATA_DIR/xhttp_pi/presence-mod ]; then
  217. cat $DATA_DIR/xhttp_pi/presence-mod >> $DEFAULT_CFG_DIR/pi_framework_sample
  218. else
  219. merr "Unable to configure: presence - missing mod descriptor"
  220. fi
  221. if [ -e $DATA_DIR/xhttp_pi/rls-mod ]; then
  222. cat $DATA_DIR/xhttp_pi/rls-mod >> $DEFAULT_CFG_DIR/pi_framework_sample
  223. else
  224. merr "Unable to configure: rls - missing mod descriptor"
  225. fi
  226. fi
  227. cat $DATA_DIR/xhttp_pi/pi_framework-02 >> $DEFAULT_CFG_DIR/pi_framework_sample
  228. minfo "Sample provisioning framework saved as: $DEFAULT_CFG_DIR/pi_framework_sample"
  229. }
  230. kamailio_pframework() #pars: <action>
  231. {
  232. if [ $# -ne 1 ] ; then
  233. merr "kamailio_pframework function takes one parameter"
  234. exit 1
  235. fi
  236. case $1 in
  237. create)
  238. shift
  239. kamailio_pframework_create "$@"
  240. exit $?
  241. ;;
  242. *)
  243. merr "Unexpected pframework action: $1"
  244. usage
  245. exit 1
  246. ;;
  247. esac
  248. }
  249. case $1 in
  250. copy)
  251. # copy database to some other name
  252. if [ "$USED_DBENGINE" = "dbtext" ] ; then
  253. merr "$USED_DBENGINE don't support this operation"
  254. exit 1
  255. fi
  256. shift
  257. if [ $# -ne 1 ]; then
  258. usage
  259. exit 1
  260. fi
  261. if [ "$USED_DBENGINE" = "sqlite" ]; then
  262. cp $DB_PATH $1
  263. exit $?
  264. fi
  265. tmp_file=`mktemp /tmp/kamdbctl.XXXXXXXXXX` || exit 1
  266. kamailio_dump $DBNAME $tmp_file
  267. ret=$?
  268. if [ "$ret" -ne 0 ]; then
  269. rm $tmp_file
  270. exit $ret
  271. fi
  272. NO_USER_INIT="yes"
  273. kamailio_create $1
  274. ret=$?
  275. if [ "$ret" -ne 0 ]; then
  276. rm $tmp_file
  277. exit $ret
  278. fi
  279. kamailio_restore $1 $tmp_file
  280. ret=$?
  281. rm -f $tmp_file
  282. exit $ret
  283. ;;
  284. backup)
  285. if [ "$USED_DBENGINE" = "dbtext" ] ; then
  286. merr "$USED_DBENGINE don't support this operation"
  287. exit 1
  288. fi
  289. # backup current database
  290. shift
  291. if [ $# -ne 1 ]; then
  292. usage
  293. exit 1
  294. fi
  295. kamailio_dump $DBNAME $1
  296. exit $?
  297. ;;
  298. restore)
  299. if [ "$USED_DBENGINE" = "dbtext" ] ; then
  300. merr "$USED_DBENGINE don't support this operation"
  301. exit 1
  302. fi
  303. # restore database from a backup
  304. shift
  305. if [ $# -ne 1 ]; then
  306. usage
  307. exit 1
  308. fi
  309. kamailio_restore $DBNAME $1
  310. exit $?
  311. ;;
  312. create)
  313. # create new database structures
  314. shift
  315. if [ $# -eq 1 ] ; then
  316. DBNAME="$1"
  317. fi
  318. kamailio_create $DBNAME
  319. exit $?
  320. ;;
  321. presence)
  322. presence_create $DBNAME
  323. exit $?
  324. ;;
  325. extra)
  326. extra_create $DBNAME
  327. exit $?
  328. ;;
  329. dbuid)
  330. dbuid_create $DBNAME
  331. exit $?
  332. ;;
  333. drop)
  334. # delete kamailio database
  335. # create new database structures
  336. # confirm dropping of database
  337. printf "This will drop your current database.\nIt is recommended to first backup your database.\n\n"
  338. get_answer ask "Continue with drop? (y/n): "
  339. if [ "$ANSWER" != "y" ]; then
  340. exit 1
  341. fi
  342. shift
  343. if [ $# -eq 1 ] ; then
  344. DBNAME="$1"
  345. fi
  346. kamailio_drop $DBNAME
  347. exit $?
  348. ;;
  349. reinit)
  350. # delete database and create a new one
  351. # create new database structures
  352. # confirm dropping of database
  353. printf "This will drop your current database and create a new one.\nIt is recommended to first backup your database.\n\n"
  354. get_answer ask "Continue with reinit? (y/n): "
  355. if [ "$ANSWER" != "y" ]; then
  356. exit 1
  357. fi
  358. shift
  359. if [ $# -eq 1 ] ; then
  360. DBNAME="$1"
  361. fi
  362. kamailio_drop $DBNAME
  363. ret=$?
  364. if [ "$ret" -ne 0 ]; then
  365. exit $ret
  366. fi
  367. kamailio_create $DBNAME
  368. exit $?
  369. ;;
  370. dbonly)
  371. # create only an empty database
  372. if [ "$USED_DBENGINE" != "mysql" ] ; then
  373. merr "$USED_DBENGINE db engine doesn't support this operation"
  374. exit 1
  375. fi
  376. shift
  377. if [ $# -eq 1 ] ; then
  378. DBNAME="$1"
  379. fi
  380. kamailio_db_create $DBNAME
  381. exit $?
  382. ;;
  383. grant)
  384. # grant privileges to database
  385. if [ "$USED_DBENGINE" != "mysql" ] ; then
  386. merr "$USED_DBENGINE db engine doesn't support this operation"
  387. exit 1
  388. fi
  389. shift
  390. if [ $# -eq 1 ] ; then
  391. DBNAME="$1"
  392. fi
  393. kamailio_db_grant $DBNAME
  394. exit $?
  395. ;;
  396. revoke)
  397. # revoke privileges to database
  398. if [ "$USED_DBENGINE" != "mysql" ] ; then
  399. merr "$USED_DBENGINE db engine doesn't support this operation"
  400. exit 1
  401. fi
  402. shift
  403. if [ $# -eq 1 ] ; then
  404. DBNAME="$1"
  405. fi
  406. kamailio_db_revoke $DBNAME
  407. exit $?
  408. ;;
  409. add-tables)
  410. if [ "$USED_DBENGINE" != "mysql" ] ; then
  411. merr "$USED_DBENGINE don't support add-tables operation"
  412. exit 1
  413. fi
  414. if [ $# -ne 2 ] ; then
  415. merr "add-tables requires 1 parameter: group id of tables"
  416. exit 1
  417. fi
  418. if [ -z "$DBNAME" ] ; then
  419. merr "DBNAME is not set"
  420. exit 1
  421. fi
  422. kamailio_add_tables $DBNAME $2
  423. exit $?
  424. ;;
  425. pframework)
  426. shift
  427. kamailio_pframework "$@"
  428. exit $?
  429. ;;
  430. version)
  431. echo "$0 $VERSION"
  432. ;;
  433. *)
  434. usage
  435. exit 1;
  436. ;;
  437. esac