123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493 |
- #!/bin/sh
- #
- # control tool for maintaining Kamailio databases
- #
- #===================================================================
- ### version for this script
- VERSION='5.2.0'
- PATH=$PATH:/usr/local/sbin/
- # for testing only, please don't enable this in production environments
- # as this introduce security risks
- TEST="false"
- ### include resource files, if any
- # check for rc file at same location with kamdbctl
- which greadlink > /dev/null 2>&1
- ret=$?
- if [ $ret -eq 0 ] ; then
- KAMCTLFULLPATH=$(greadlink -f "$0")
- else
- which readlink > /dev/null 2>&1
- ret=$?
- if [ $ret -eq 0 ] ; then
- KAMCTLFULLPATH=$(readlink -f "$0")
- fi
- fi
- if [ -n "$KAMCTLFULLPATH" ] ; then
- KAMCTLDIRPATH=$(dirname "$KAMCTLFULLPATH")
- if [ -f $KAMCTLDIRPATH/kamctlrc ]; then
- . $KAMCTLDIRPATH/kamctlrc
- fi
- fi
- # check for rc file at standard locations
- if [ -f /etc/kamailio/kamctlrc ]; then
- . /etc/kamailio/kamctlrc
- fi
- if [ -f /usr/local/etc/kamailio/kamctlrc ]; then
- . /usr/local/etc/kamailio/kamctlrc
- fi
- if [ -f ~/.kamctlrc ]; then
- . ~/.kamctlrc
- fi
- if [ $TEST = "true" ]; then
- if [ -f ./kamctlrc ]; then
- . ./kamctlrc
- fi
- fi
- if [ -z "$MYDIR" ] ; then
- MYDIR=`dirname $0`
- fi
- if [ -z "$MYLIBDIR" ] ; then
- MYLIBDIR="/usr/local/lib/kamailio/kamctl"
- if [ ! -d "$MYLIBDIR" ]; then
- MYLIBDIR=$MYDIR
- fi
- fi
- ##### ------------------------------------------------ #####
- ### load base functions
- #
- if [ -f "$MYLIBDIR/kamdbctl.base" ]; then
- . "$MYLIBDIR/kamdbctl.base"
- else
- echo -e "Cannot load core functions '$MYLIBDIR/kamdbctl.base' - exiting ...\n"
- exit -1
- fi
- #
- ##### ------------------------------------------------ #####
- ### DBENGINE
- #
- unset USED_DBENGINE
- if [ -z "$DBENGINE" ] ; then
- merr "database engine not specified, please setup one in the config script"
- exit 1
- fi
- case $DBENGINE in
- MYSQL|mysql|MySQL)
- if [ -f "$MYLIBDIR/kamdbctl.mysql" ]; then
- . "$MYLIBDIR/kamdbctl.mysql"
- USED_DBENGINE="mysql"
- else
- merr "could not load the script in $MYLIBDIR/kamdbctl.mysql for database engine $DBENGINE"
- fi
- ;;
- PGSQL|pgsql|postgres|postgresql|POSTGRESQL)
- if [ -f "$MYLIBDIR/kamdbctl.pgsql" ]; then
- . "$MYLIBDIR/kamdbctl.pgsql"
- USED_DBENGINE="postgres"
- else
- merr "could not load the script in $MYLIBDIR/kamdbctl.pgsql for database engine $DBENGINE"
- fi
- ;;
- ORACLE|oracle|Oracle)
- if [ -f "$MYLIBDIR/kamdbctl.oracle" ]; then
- . "$MYLIBDIR/kamdbctl.oracle"
- USED_DBENGINE="oracle"
- else
- merr "could not load the script in $MYLIBDIR/kamdbctl.oracle for database engine $DBENGINE"
- fi
- ;;
- DBTEXT|dbtext|textdb)
- if [ -f "$MYLIBDIR/kamdbctl.dbtext" ]; then
- . "$MYLIBDIR/kamdbctl.dbtext"
- USED_DBENGINE="dbtext"
- DBNAME=$DB_PATH
- else
- merr "could not load the script in $MYLIBDIR/kamdbctl.dbtext for database engine $DBENGINE"
- fi
- ;;
- DB_BERKELEY|db_berkeley|BERKELEY|berkeley)
- if [ -f "$MYLIBDIR/kamdbctl.db_berkeley" ]; then
- . "$MYLIBDIR/kamdbctl.db_berkeley"
- USED_DBENGINE="berkeley"
- DBNAME=$DB_PATH
- else
- merr "could not load the script in $MYLIBDIR/kamdbctl.db_berkeley for database engine $DBENGINE"
- fi
- ;;
- SQLITE|sqlite)
- if [ -f "$MYLIBDIR/kamdbctl.sqlite" ]; then
- . "$MYLIBDIR/kamdbctl.sqlite"
- USED_DBENGINE="sqlite"
- DBNAME=$DB_PATH
- else
- merr "could not load the script in $MYLIBDIR/kamdbctl.sqlite for database engine $DBENGINE"
- fi
- ;;
- esac
- if [ -z "$USED_DBENGINE" ] ; then
- merr "database engine not loaded - tried '$DBENGINE'"
- exit 1
- else
- mdbg "database engine '$USED_DBENGINE' loaded"
- fi
- # dump all rows
- kamailio_dump() # pars: <database name>
- {
- if [ $# -ne 2 ] ; then
- merr "kamailio_dump function takes two param"
- exit 1
- fi
- if [ "$USED_DBENGINE" == "oracle" ]; then
- oracle_dump $1 $2
- elif [ "$PW" = "" ] ; then
- $DUMP_CMD $1 > $2
- else
- $DUMP_CMD "-p$PW" $1 > $2
- fi
- if [ "$?" -ne 0 ]; then
- merr "db dump failed"
- exit 1
- fi
- minfo "db dump successful"
- }
- kamailio_restore() #pars: <database name> <filename>
- {
- if [ $# -ne 2 ] ; then
- merr "kamailio_restore function takes two params"
- exit 1
- fi
- if [ "$USED_DBENGINE" == "oracle" ]; then
- oracle_restore $1 $2
- else
- sql_query $1 < $2
- fi
- if [ "$?" -ne 0 ]; then
- merr "db restore failed"
- exit 1
- fi
- minfo "db restore successful"
- }
- kamailio_pframework_create() #pars: none
- {
- if [ -e $DEFAULT_CFG_DIR/pi_framework_sample ] ; then
- get_answer ask "Sample already exists. Overwrite? (y/n): "
- if [ "$ANSWER" != "y" ]; then
- exit 1
- fi
- fi
- touch $DEFAULT_CFG_DIR/pi_framework_sample
- if [ $? -ne 0 ] ; then
- merr "Unable to create $DEFAULT_CFG_DIR/pi_framework_sample"
- exit 1
- fi
- if [ -d "$DATA_DIR/xhttp_pi" ] ; then
- PI_MODULES="$STANDARD_MODULES"
- else
- merr "Please install first the xhttp_pi module"
- exit 1
- fi
- get_answer $INSTALL_EXTRA_TABLES "Add provisionning framework for extra tables? (y/n): "
- if [ "$ANSWER" = "y" ]; then
- PI_MODULES="$PI_MODULES $EXTRA_MODULES"
- fi
- get_answer $INSTALL_PRESENCE_TABLES "Add provisionning framework for presence tables? (y/n): "
- if [ "$ANSWER" = "y" ]; then
- PI_PRESENCE_MODULES="TRUE"
- fi
- cat $DATA_DIR/xhttp_pi/pi_framework-00 > $DEFAULT_CFG_DIR/pi_framework_sample
- for TABLE in $PI_MODULES; do
- if [ -e $DATA_DIR/xhttp_pi/$TABLE-table ]; then
- cat $DATA_DIR/xhttp_pi/$TABLE-table >> $DEFAULT_CFG_DIR/pi_framework_sample
- else
- merr "Unable to configure: $TABLE - missing table descriptor"
- fi
- done
- if [ "$PI_PRESENCE_MODULES" = "TRUE" ]; then
- if [ -e $DATA_DIR/xhttp_pi/presence-table ]; then
- cat $DATA_DIR/xhttp_pi/presence-table >> $DEFAULT_CFG_DIR/pi_framework_sample
- else
- merr "Unable to configure: presence - missing table descriptor"
- fi
- if [ -e $DATA_DIR/xhttp_pi/rls-table ]; then
- cat $DATA_DIR/xhttp_pi/rls-table >> $DEFAULT_CFG_DIR/pi_framework_sample
- else
- merr "Unable to configure: rls - missing table descriptor"
- fi
- fi
- cat $DATA_DIR/xhttp_pi/pi_framework-01 >> $DEFAULT_CFG_DIR/pi_framework_sample
- for TABLE in $PI_MODULES; do
- if [ -e $DATA_DIR/xhttp_pi/$TABLE-mod ]; then
- cat $DATA_DIR/xhttp_pi/$TABLE-mod >> $DEFAULT_CFG_DIR/pi_framework_sample
- else
- merr "Unable to configure: $TABLE - missing mod descriptor"
- fi
- done
- if [ "$PI_PRESENCE_MODULES" = "TRUE" ]; then
- if [ -e $DATA_DIR/xhttp_pi/presence-mod ]; then
- cat $DATA_DIR/xhttp_pi/presence-mod >> $DEFAULT_CFG_DIR/pi_framework_sample
- else
- merr "Unable to configure: presence - missing mod descriptor"
- fi
- if [ -e $DATA_DIR/xhttp_pi/rls-mod ]; then
- cat $DATA_DIR/xhttp_pi/rls-mod >> $DEFAULT_CFG_DIR/pi_framework_sample
- else
- merr "Unable to configure: rls - missing mod descriptor"
- fi
- fi
- cat $DATA_DIR/xhttp_pi/pi_framework-02 >> $DEFAULT_CFG_DIR/pi_framework_sample
- minfo "Sample provisionning framework saved as: $DEFAULT_CFG_DIR/pi_framework_sample"
- }
- kamailio_pframework() #pars: <action>
- {
- if [ $# -ne 1 ] ; then
- merr "kamailio_pframework function takes one parameter"
- exit 1
- fi
- case $1 in
- create)
- shift
- kamailio_pframework_create "$@"
- exit $?
- ;;
- *)
- merr "Unexpected pframework action: $1"
- usage
- exit 1
- ;;
- esac
- }
- case $1 in
- copy)
- # copy database to some other name
- if [ "$USED_DBENGINE" == "berkeley" -o "$USED_DBENGINE" == "dbtext" ] ; then
- merr "$USED_DBENGINE don't support this operation"
- exit 1
- fi
- shift
- if [ $# -ne 1 ]; then
- usage
- exit 1
- fi
- if [ "$USED_DBENGINE" = "sqlite" ]; then
- cp $DB_PATH $1
- exit $?
- fi
- tmp_file=`mktemp /tmp/kamdbctl.XXXXXXXXXX` || exit 1
- kamailio_dump $DBNAME $tmp_file
- ret=$?
- if [ "$ret" -ne 0 ]; then
- rm $tmp_file
- exit $ret
- fi
- NO_USER_INIT="yes"
- kamailio_create $1
- ret=$?
- if [ "$ret" -ne 0 ]; then
- rm $tmp_file
- exit $ret
- fi
- kamailio_restore $1 $tmp_file
- ret=$?
- rm -f $tmp_file
- exit $ret
- ;;
- backup)
- if [ "$USED_DBENGINE" == "berkeley" -o "$USED_DBENGINE" == "dbtext" ] ; then
- merr "$USED_DBENGINE don't support this operation"
- exit 1
- fi
- # backup current database
- shift
- if [ $# -ne 1 ]; then
- usage
- exit 1
- fi
- kamailio_dump $DBNAME $1
- exit $?
- ;;
- restore)
- if [ "$USED_DBENGINE" == "berkeley" -o "$USED_DBENGINE" == "dbtext" ] ; then
- merr "$USED_DBENGINE don't support this operation"
- exit 1
- fi
- # restore database from a backup
- shift
- if [ $# -ne 1 ]; then
- usage
- exit 1
- fi
- kamailio_restore $DBNAME $1
- exit $?
- ;;
- create)
- # create new database structures
- shift
- if [ $# -eq 1 ] ; then
- DBNAME="$1"
- fi
- kamailio_create $DBNAME
- exit $?
- ;;
- presence)
- presence_create $DBNAME
- exit $?
- ;;
- extra)
- extra_create $DBNAME
- exit $?
- ;;
- dbuid)
- dbuid_create $DBNAME
- exit $?
- ;;
- drop)
- # delete kamailio database
- # create new database structures
- # confirm dropping of database
- echo -e "This will drop your current database.\nIt is recommended to first backup your database.\n"
- get_answer ask "Continue with drop? (y/n): "
- if [ "$ANSWER" != "y" ]; then
- exit 1
- fi
- shift
- if [ $# -eq 1 ] ; then
- DBNAME="$1"
- fi
- kamailio_drop $DBNAME
- exit $?
- ;;
- reinit)
- # delete database and create a new one
- # create new database structures
- # confirm dropping of database
- echo -e "This will drop your current database and create a new one.\nIt is recommended to first backup your database.\n"
- get_answer ask "Continue with reinit? (y/n): "
- if [ "$ANSWER" != "y" ]; then
- exit 1
- fi
- shift
- if [ $# -eq 1 ] ; then
- DBNAME="$1"
- fi
- kamailio_drop $DBNAME
- ret=$?
- if [ "$ret" -ne 0 ]; then
- exit $ret
- fi
- kamailio_create $DBNAME
- exit $?
- ;;
- dbonly)
- # create only an empty database
- if [ "$USED_DBENGINE" != "mysql" ] ; then
- merr "$USED_DBENGINE db engine doesn't support this operation"
- exit 1
- fi
- shift
- if [ $# -eq 1 ] ; then
- DBNAME="$1"
- fi
- kamailio_db_create $DBNAME
- exit $?
- ;;
- grant)
- # grant privileges to database
- if [ "$USED_DBENGINE" != "mysql" ] ; then
- merr "$USED_DBENGINE db engine doesn't support this operation"
- exit 1
- fi
- shift
- if [ $# -eq 1 ] ; then
- DBNAME="$1"
- fi
- kamailio_db_grant $DBNAME
- exit $?
- ;;
- revoke)
- # revoke privileges to database
- if [ "$USED_DBENGINE" != "mysql" ] ; then
- merr "$USED_DBENGINE db engine doesn't support this operation"
- exit 1
- fi
- shift
- if [ $# -eq 1 ] ; then
- DBNAME="$1"
- fi
- kamailio_db_revoke $DBNAME
- exit $?
- ;;
- add-tables)
- if [ "$USED_DBENGINE" != "mysql" ] ; then
- merr "$USED_DBENGINE don't support add-tables operation"
- exit 1
- fi
- if [ $# -ne 2 ] ; then
- merr "add-tables requires 1 parameter: group id of tables"
- exit 1
- fi
- if [ -z "$DBNAME" ] ; then
- merr "DBNAME is not set"
- exit 1
- fi
- kamailio_add_tables $DBNAME $2
- exit $?
- ;;
- bdb|db_berkeley)
- shift
- kamailio_berkeley "$@"
- exit $?
- ;;
- pframework)
- shift
- kamailio_pframework "$@"
- exit $?
- ;;
- version)
- echo "$0 $VERSION"
- ;;
- *)
- usage
- exit 1;
- ;;
- esac
|