kamdbctl 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. #!/bin/sh
  2. #
  3. # control tool for maintaining Kamailio databases
  4. #
  5. #===================================================================
  6. ### version for this script
  7. VERSION='5.2.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 ]; then
  33. . /etc/kamailio/kamctlrc
  34. fi
  35. if [ -f /usr/local/etc/kamailio/kamctlrc ]; then
  36. . /usr/local/etc/kamailio/kamctlrc
  37. fi
  38. if [ -f ~/.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. echo -e "Cannot load core functions '$MYLIBDIR/kamdbctl.base' - exiting ...\n"
  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 setup one 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. DB_BERKELEY|db_berkeley|BERKELEY|berkeley)
  108. if [ -f "$MYLIBDIR/kamdbctl.db_berkeley" ]; then
  109. . "$MYLIBDIR/kamdbctl.db_berkeley"
  110. USED_DBENGINE="berkeley"
  111. DBNAME=$DB_PATH
  112. else
  113. merr "could not load the script in $MYLIBDIR/kamdbctl.db_berkeley for database engine $DBENGINE"
  114. fi
  115. ;;
  116. SQLITE|sqlite)
  117. if [ -f "$MYLIBDIR/kamdbctl.sqlite" ]; then
  118. . "$MYLIBDIR/kamdbctl.sqlite"
  119. USED_DBENGINE="sqlite"
  120. DBNAME=$DB_PATH
  121. else
  122. merr "could not load the script in $MYLIBDIR/kamdbctl.sqlite for database engine $DBENGINE"
  123. fi
  124. ;;
  125. esac
  126. if [ -z "$USED_DBENGINE" ] ; then
  127. merr "database engine not loaded - tried '$DBENGINE'"
  128. exit 1
  129. else
  130. mdbg "database engine '$USED_DBENGINE' loaded"
  131. fi
  132. # dump all rows
  133. kamailio_dump() # pars: <database name>
  134. {
  135. if [ $# -ne 2 ] ; then
  136. merr "kamailio_dump function takes two param"
  137. exit 1
  138. fi
  139. if [ "$USED_DBENGINE" == "oracle" ]; then
  140. oracle_dump $1 $2
  141. elif [ "$PW" = "" ] ; then
  142. $DUMP_CMD $1 > $2
  143. else
  144. $DUMP_CMD "-p$PW" $1 > $2
  145. fi
  146. if [ "$?" -ne 0 ]; then
  147. merr "db dump failed"
  148. exit 1
  149. fi
  150. minfo "db dump successful"
  151. }
  152. kamailio_restore() #pars: <database name> <filename>
  153. {
  154. if [ $# -ne 2 ] ; then
  155. merr "kamailio_restore function takes two params"
  156. exit 1
  157. fi
  158. if [ "$USED_DBENGINE" == "oracle" ]; then
  159. oracle_restore $1 $2
  160. else
  161. sql_query $1 < $2
  162. fi
  163. if [ "$?" -ne 0 ]; then
  164. merr "db restore failed"
  165. exit 1
  166. fi
  167. minfo "db restore successful"
  168. }
  169. kamailio_pframework_create() #pars: none
  170. {
  171. if [ -e $DEFAULT_CFG_DIR/pi_framework_sample ] ; then
  172. get_answer ask "Sample already exists. Overwrite? (y/n): "
  173. if [ "$ANSWER" != "y" ]; then
  174. exit 1
  175. fi
  176. fi
  177. touch $DEFAULT_CFG_DIR/pi_framework_sample
  178. if [ $? -ne 0 ] ; then
  179. merr "Unable to create $DEFAULT_CFG_DIR/pi_framework_sample"
  180. exit 1
  181. fi
  182. if [ -d "$DATA_DIR/xhttp_pi" ] ; then
  183. PI_MODULES="$STANDARD_MODULES"
  184. else
  185. merr "Please install first the xhttp_pi module"
  186. exit 1
  187. fi
  188. get_answer $INSTALL_EXTRA_TABLES "Add provisionning framework for extra tables? (y/n): "
  189. if [ "$ANSWER" = "y" ]; then
  190. PI_MODULES="$PI_MODULES $EXTRA_MODULES"
  191. fi
  192. get_answer $INSTALL_PRESENCE_TABLES "Add provisionning framework for presence tables? (y/n): "
  193. if [ "$ANSWER" = "y" ]; then
  194. PI_PRESENCE_MODULES="TRUE"
  195. fi
  196. cat $DATA_DIR/xhttp_pi/pi_framework-00 > $DEFAULT_CFG_DIR/pi_framework_sample
  197. for TABLE in $PI_MODULES; do
  198. if [ -e $DATA_DIR/xhttp_pi/$TABLE-table ]; then
  199. cat $DATA_DIR/xhttp_pi/$TABLE-table >> $DEFAULT_CFG_DIR/pi_framework_sample
  200. else
  201. merr "Unable to configure: $TABLE - missing table descriptor"
  202. fi
  203. done
  204. if [ "$PI_PRESENCE_MODULES" = "TRUE" ]; then
  205. if [ -e $DATA_DIR/xhttp_pi/presence-table ]; then
  206. cat $DATA_DIR/xhttp_pi/presence-table >> $DEFAULT_CFG_DIR/pi_framework_sample
  207. else
  208. merr "Unable to configure: presence - missing table descriptor"
  209. fi
  210. if [ -e $DATA_DIR/xhttp_pi/rls-table ]; then
  211. cat $DATA_DIR/xhttp_pi/rls-table >> $DEFAULT_CFG_DIR/pi_framework_sample
  212. else
  213. merr "Unable to configure: rls - missing table descriptor"
  214. fi
  215. fi
  216. cat $DATA_DIR/xhttp_pi/pi_framework-01 >> $DEFAULT_CFG_DIR/pi_framework_sample
  217. for TABLE in $PI_MODULES; do
  218. if [ -e $DATA_DIR/xhttp_pi/$TABLE-mod ]; then
  219. cat $DATA_DIR/xhttp_pi/$TABLE-mod >> $DEFAULT_CFG_DIR/pi_framework_sample
  220. else
  221. merr "Unable to configure: $TABLE - missing mod descriptor"
  222. fi
  223. done
  224. if [ "$PI_PRESENCE_MODULES" = "TRUE" ]; then
  225. if [ -e $DATA_DIR/xhttp_pi/presence-mod ]; then
  226. cat $DATA_DIR/xhttp_pi/presence-mod >> $DEFAULT_CFG_DIR/pi_framework_sample
  227. else
  228. merr "Unable to configure: presence - missing mod descriptor"
  229. fi
  230. if [ -e $DATA_DIR/xhttp_pi/rls-mod ]; then
  231. cat $DATA_DIR/xhttp_pi/rls-mod >> $DEFAULT_CFG_DIR/pi_framework_sample
  232. else
  233. merr "Unable to configure: rls - missing mod descriptor"
  234. fi
  235. fi
  236. cat $DATA_DIR/xhttp_pi/pi_framework-02 >> $DEFAULT_CFG_DIR/pi_framework_sample
  237. minfo "Sample provisionning framework saved as: $DEFAULT_CFG_DIR/pi_framework_sample"
  238. }
  239. kamailio_pframework() #pars: <action>
  240. {
  241. if [ $# -ne 1 ] ; then
  242. merr "kamailio_pframework function takes one parameter"
  243. exit 1
  244. fi
  245. case $1 in
  246. create)
  247. shift
  248. kamailio_pframework_create "$@"
  249. exit $?
  250. ;;
  251. *)
  252. merr "Unexpected pframework action: $1"
  253. usage
  254. exit 1
  255. ;;
  256. esac
  257. }
  258. case $1 in
  259. copy)
  260. # copy database to some other name
  261. if [ "$USED_DBENGINE" == "berkeley" -o "$USED_DBENGINE" == "dbtext" ] ; then
  262. merr "$USED_DBENGINE don't support this operation"
  263. exit 1
  264. fi
  265. shift
  266. if [ $# -ne 1 ]; then
  267. usage
  268. exit 1
  269. fi
  270. if [ "$USED_DBENGINE" = "sqlite" ]; then
  271. cp $DB_PATH $1
  272. exit $?
  273. fi
  274. tmp_file=`mktemp /tmp/kamdbctl.XXXXXXXXXX` || exit 1
  275. kamailio_dump $DBNAME $tmp_file
  276. ret=$?
  277. if [ "$ret" -ne 0 ]; then
  278. rm $tmp_file
  279. exit $ret
  280. fi
  281. NO_USER_INIT="yes"
  282. kamailio_create $1
  283. ret=$?
  284. if [ "$ret" -ne 0 ]; then
  285. rm $tmp_file
  286. exit $ret
  287. fi
  288. kamailio_restore $1 $tmp_file
  289. ret=$?
  290. rm -f $tmp_file
  291. exit $ret
  292. ;;
  293. backup)
  294. if [ "$USED_DBENGINE" == "berkeley" -o "$USED_DBENGINE" == "dbtext" ] ; then
  295. merr "$USED_DBENGINE don't support this operation"
  296. exit 1
  297. fi
  298. # backup current database
  299. shift
  300. if [ $# -ne 1 ]; then
  301. usage
  302. exit 1
  303. fi
  304. kamailio_dump $DBNAME $1
  305. exit $?
  306. ;;
  307. restore)
  308. if [ "$USED_DBENGINE" == "berkeley" -o "$USED_DBENGINE" == "dbtext" ] ; then
  309. merr "$USED_DBENGINE don't support this operation"
  310. exit 1
  311. fi
  312. # restore database from a backup
  313. shift
  314. if [ $# -ne 1 ]; then
  315. usage
  316. exit 1
  317. fi
  318. kamailio_restore $DBNAME $1
  319. exit $?
  320. ;;
  321. create)
  322. # create new database structures
  323. shift
  324. if [ $# -eq 1 ] ; then
  325. DBNAME="$1"
  326. fi
  327. kamailio_create $DBNAME
  328. exit $?
  329. ;;
  330. presence)
  331. presence_create $DBNAME
  332. exit $?
  333. ;;
  334. extra)
  335. extra_create $DBNAME
  336. exit $?
  337. ;;
  338. dbuid)
  339. dbuid_create $DBNAME
  340. exit $?
  341. ;;
  342. drop)
  343. # delete kamailio database
  344. # create new database structures
  345. # confirm dropping of database
  346. echo -e "This will drop your current database.\nIt is recommended to first backup your database.\n"
  347. get_answer ask "Continue with drop? (y/n): "
  348. if [ "$ANSWER" != "y" ]; then
  349. exit 1
  350. fi
  351. shift
  352. if [ $# -eq 1 ] ; then
  353. DBNAME="$1"
  354. fi
  355. kamailio_drop $DBNAME
  356. exit $?
  357. ;;
  358. reinit)
  359. # delete database and create a new one
  360. # create new database structures
  361. # confirm dropping of database
  362. echo -e "This will drop your current database and create a new one.\nIt is recommended to first backup your database.\n"
  363. get_answer ask "Continue with reinit? (y/n): "
  364. if [ "$ANSWER" != "y" ]; then
  365. exit 1
  366. fi
  367. shift
  368. if [ $# -eq 1 ] ; then
  369. DBNAME="$1"
  370. fi
  371. kamailio_drop $DBNAME
  372. ret=$?
  373. if [ "$ret" -ne 0 ]; then
  374. exit $ret
  375. fi
  376. kamailio_create $DBNAME
  377. exit $?
  378. ;;
  379. dbonly)
  380. # create only an empty database
  381. if [ "$USED_DBENGINE" != "mysql" ] ; then
  382. merr "$USED_DBENGINE db engine doesn't support this operation"
  383. exit 1
  384. fi
  385. shift
  386. if [ $# -eq 1 ] ; then
  387. DBNAME="$1"
  388. fi
  389. kamailio_db_create $DBNAME
  390. exit $?
  391. ;;
  392. grant)
  393. # grant privileges to database
  394. if [ "$USED_DBENGINE" != "mysql" ] ; then
  395. merr "$USED_DBENGINE db engine doesn't support this operation"
  396. exit 1
  397. fi
  398. shift
  399. if [ $# -eq 1 ] ; then
  400. DBNAME="$1"
  401. fi
  402. kamailio_db_grant $DBNAME
  403. exit $?
  404. ;;
  405. revoke)
  406. # revoke privileges to database
  407. if [ "$USED_DBENGINE" != "mysql" ] ; then
  408. merr "$USED_DBENGINE db engine doesn't support this operation"
  409. exit 1
  410. fi
  411. shift
  412. if [ $# -eq 1 ] ; then
  413. DBNAME="$1"
  414. fi
  415. kamailio_db_revoke $DBNAME
  416. exit $?
  417. ;;
  418. add-tables)
  419. if [ "$USED_DBENGINE" != "mysql" ] ; then
  420. merr "$USED_DBENGINE don't support add-tables operation"
  421. exit 1
  422. fi
  423. if [ $# -ne 2 ] ; then
  424. merr "add-tables requires 1 parameter: group id of tables"
  425. exit 1
  426. fi
  427. if [ -z "$DBNAME" ] ; then
  428. merr "DBNAME is not set"
  429. exit 1
  430. fi
  431. kamailio_add_tables $DBNAME $2
  432. exit $?
  433. ;;
  434. bdb|db_berkeley)
  435. shift
  436. kamailio_berkeley "$@"
  437. exit $?
  438. ;;
  439. pframework)
  440. shift
  441. kamailio_pframework "$@"
  442. exit $?
  443. ;;
  444. version)
  445. echo "$0 $VERSION"
  446. ;;
  447. *)
  448. usage
  449. exit 1;
  450. ;;
  451. esac