utils/kamctl/kamdbctl
30857dfe
 #!/bin/sh
96a2629b
 #
860c33c5
 # control tool for maintaining Kamailio databases
96a2629b
 #
 #===================================================================
 
bc4792f3
 ### version for this script
223380dc
 VERSION='5.4.0'
bc4792f3
 
96a2629b
 PATH=$PATH:/usr/local/sbin/
9476def1
 
 # for testing only, please don't enable this in production environments
 # as this introduce security risks
5f72f337
 TEST="false"
96a2629b
 
 ### include resource files, if any
ba1967df
 
 # check for rc file at same location with kamdbctl
2bd19e23
 which greadlink > /dev/null 2>&1
bc4792f3
 ret=$?
 if [ $ret -eq 0 ] ; then
ba1967df
 	KAMCTLFULLPATH=$(greadlink -f "$0")
 else
2bd19e23
 	which readlink > /dev/null 2>&1
ba1967df
 	ret=$?
 	if [ $ret -eq 0 ] ; then
 		KAMCTLFULLPATH=$(readlink -f "$0")
 	fi
 fi
 if [ -n "$KAMCTLFULLPATH" ] ; then
bc4792f3
 	KAMCTLDIRPATH=$(dirname "$KAMCTLFULLPATH")
 	if [ -f $KAMCTLDIRPATH/kamctlrc ]; then
 		. $KAMCTLDIRPATH/kamctlrc
 	fi
 fi
ba1967df
 
 # check for rc file at standard locations
860c33c5
 if [ -f /etc/kamailio/kamctlrc ]; then
 	. /etc/kamailio/kamctlrc
5f72f337
 fi
860c33c5
 if [ -f /usr/local/etc/kamailio/kamctlrc ]; then
 	. /usr/local/etc/kamailio/kamctlrc
96a2629b
 fi
de1a4247
 if [ -f ~/.kamctlrc ]; then
 	. ~/.kamctlrc
96a2629b
 fi
9476def1
 
5f72f337
 if [ $TEST = "true" ]; then
de1a4247
 	if [ -f ./kamctlrc ]; then
 		. ./kamctlrc
5f72f337
 	fi
df43e7d9
 fi
96a2629b
 
 
 if [ -z "$MYDIR" ] ; then
 	MYDIR=`dirname $0`
 fi
 
 if [ -z "$MYLIBDIR" ] ; then
860c33c5
 	MYLIBDIR="/usr/local/lib/kamailio/kamctl"
96a2629b
 	if [ ! -d "$MYLIBDIR" ]; then
 		MYLIBDIR=$MYDIR
 	fi
 fi
 
 
 ##### ------------------------------------------------ #####
 ### load base functions
 #
de1a4247
 if [ -f "$MYLIBDIR/kamdbctl.base" ]; then
 	. "$MYLIBDIR/kamdbctl.base"
96a2629b
 else
de1a4247
 	echo -e "Cannot load core functions '$MYLIBDIR/kamdbctl.base' - exiting ...\n"
96a2629b
 	exit -1
 fi
 
 #
 ##### ------------------------------------------------ #####
 ### DBENGINE
 #
5849564c
 unset USED_DBENGINE
96a2629b
 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)
de1a4247
 		if [ -f "$MYLIBDIR/kamdbctl.mysql" ]; then
 			. "$MYLIBDIR/kamdbctl.mysql"
5849564c
 			USED_DBENGINE="mysql"
f4aebf72
 		else
de1a4247
 			merr "could not load the script in $MYLIBDIR/kamdbctl.mysql for database engine $DBENGINE"
96a2629b
 		fi
 		;;
 	PGSQL|pgsql|postgres|postgresql|POSTGRESQL)
de1a4247
 		if [ -f "$MYLIBDIR/kamdbctl.pgsql" ]; then
 			. "$MYLIBDIR/kamdbctl.pgsql"
5849564c
 			USED_DBENGINE="postgres"
f4aebf72
 		else
de1a4247
 			merr "could not load the script in $MYLIBDIR/kamdbctl.pgsql for database engine $DBENGINE"
96a2629b
 		fi
 		;;
38fac614
 	ORACLE|oracle|Oracle)
de1a4247
 		if [ -f "$MYLIBDIR/kamdbctl.oracle" ]; then
 			. "$MYLIBDIR/kamdbctl.oracle"
38fac614
 			USED_DBENGINE="oracle"
 		else
de1a4247
 			merr "could not load the script in $MYLIBDIR/kamdbctl.oracle for database engine $DBENGINE"
38fac614
 		fi
 		;;
96a2629b
 	DBTEXT|dbtext|textdb)
de1a4247
 		if [ -f "$MYLIBDIR/kamdbctl.dbtext" ]; then
 			. "$MYLIBDIR/kamdbctl.dbtext"
5849564c
 			USED_DBENGINE="dbtext"
 			DBNAME=$DB_PATH
f4aebf72
 		else
de1a4247
 			merr "could not load the script in $MYLIBDIR/kamdbctl.dbtext for database engine $DBENGINE"
96a2629b
 		fi
 		;;
5849564c
 	DB_BERKELEY|db_berkeley|BERKELEY|berkeley)
de1a4247
 		if [ -f "$MYLIBDIR/kamdbctl.db_berkeley" ]; then
 			. "$MYLIBDIR/kamdbctl.db_berkeley"
5849564c
 			USED_DBENGINE="berkeley"
 			DBNAME=$DB_PATH
df68a2c2
 		else
de1a4247
 			merr "could not load the script in $MYLIBDIR/kamdbctl.db_berkeley for database engine $DBENGINE"
df68a2c2
 		fi
 		;;
9d36d853
 	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
 		;;
96a2629b
 esac
 
5849564c
 if [ -z "$USED_DBENGINE" ] ; then
f4aebf72
 	merr "database engine not loaded - tried '$DBENGINE'"
96a2629b
 	exit 1
5849564c
 else
 	mdbg "database engine '$USED_DBENGINE' loaded"
96a2629b
 fi
 
 
 # dump all rows
e102ae72
 kamailio_dump()  # pars: <database name>
96a2629b
 {
 	if [ $# -ne 2 ] ; then
e102ae72
 		merr "kamailio_dump function takes two param"
96a2629b
 		exit 1
 	fi
e8b8251f
 	if [ "$USED_DBENGINE" = "oracle" ]; then
38fac614
 		oracle_dump $1 $2
 	elif [ "$PW" = "" ] ; then
96a2629b
 		$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"
 }
 
 
e102ae72
 kamailio_restore() #pars: <database name> <filename>
96a2629b
 {
 	if [ $# -ne 2 ] ; then
e102ae72
 		merr "kamailio_restore function takes two params"
96a2629b
 		exit 1
 	fi
e8b8251f
 	if [ "$USED_DBENGINE" = "oracle" ]; then
38fac614
 		oracle_restore $1 $2
 	else
 		sql_query $1 < $2
 	fi
96a2629b
 	if [ "$?" -ne 0 ]; then
 			merr "db restore failed"
 			exit 1
 	fi
 	minfo "db restore successful"
 }
 
 
ef2b3a6e
 kamailio_pframework_create() #pars: none
 {
c579bce3
 	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
ef2b3a6e
 	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
c579bce3
 		PI_PRESENCE_MODULES="TRUE"
ef2b3a6e
 	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
c579bce3
 	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
ef2b3a6e
 	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
c579bce3
 	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
ef2b3a6e
 	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
 }
 
 
 
96a2629b
 case $1 in
 	copy)
 		# copy database to some other name
e8b8251f
 		if [ "$USED_DBENGINE" = "berkeley" -o "$USED_DBENGINE" = "dbtext" ] ; then
5849564c
 			merr "$USED_DBENGINE don't support this operation"
df43e7d9
 			exit 1
 		fi
96a2629b
 		shift
 		if [ $# -ne 1 ]; then
 			usage
 			exit 1
 		fi
9d36d853
 		if [ "$USED_DBENGINE" = "sqlite" ]; then
 			cp $DB_PATH $1
 			exit $?
 		fi
de1a4247
 		tmp_file=`mktemp /tmp/kamdbctl.XXXXXXXXXX` || exit 1
e102ae72
 		kamailio_dump $DBNAME $tmp_file
96a2629b
 		ret=$?
 		if [ "$ret" -ne 0 ]; then
 			rm $tmp_file
 			exit $ret
 		fi
 		NO_USER_INIT="yes"
e102ae72
 		kamailio_create $1
96a2629b
 		ret=$?
 		if [ "$ret" -ne 0 ]; then
 			rm $tmp_file
 			exit $ret
 		fi
e102ae72
 		kamailio_restore $1 $tmp_file
96a2629b
 		ret=$?
 		rm -f $tmp_file
 		exit $ret
 		;;
 	backup)
e8b8251f
 		if [ "$USED_DBENGINE" = "berkeley" -o "$USED_DBENGINE" = "dbtext" ] ; then
5849564c
 			merr "$USED_DBENGINE don't support this operation"
df43e7d9
 			exit 1
 		fi
96a2629b
 		# backup current database
 		shift
 		if [ $# -ne 1 ]; then
 			usage
 			exit 1
 		fi
e102ae72
 		kamailio_dump $DBNAME $1
96a2629b
 		exit $?
 		;;
 	restore)
e8b8251f
 		if [ "$USED_DBENGINE" = "berkeley" -o "$USED_DBENGINE" = "dbtext" ] ; then
5849564c
 			merr "$USED_DBENGINE don't support this operation"
df43e7d9
 			exit 1
 		fi
96a2629b
 		# restore database from a backup
 		shift
 		if [ $# -ne 1 ]; then
 			usage
 			exit 1
 		fi
e102ae72
 		kamailio_restore $DBNAME $1
96a2629b
 		exit $?
 		;;
 	create)
 		# create new database structures
 		shift
 		if [ $# -eq 1 ] ; then
 			DBNAME="$1"
bdaae70d
 		fi
 
e102ae72
 		kamailio_create $DBNAME
96a2629b
 		exit $?
 		;;
 	presence)
5849564c
 		presence_create $DBNAME
96a2629b
 		exit $?
 		;;
 	extra)
5849564c
 		extra_create $DBNAME
96a2629b
 		exit $?
 		;;
1fd2b4ec
 	dbuid)
 		dbuid_create $DBNAME
 		exit $?
 		;;
96a2629b
 	drop)
e102ae72
 		# delete kamailio database
df43e7d9
 		# create new database structures
61eb1ac7
 
 		# 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
 
df43e7d9
 		shift
 		if [ $# -eq 1 ] ; then
 			DBNAME="$1"
bdaae70d
 		fi
 
e102ae72
 		kamailio_drop $DBNAME
96a2629b
 		exit $?
 		;;
 	reinit)
 		# delete database and create a new one
df43e7d9
 		# create new database structures
39756fff
 
 		# 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
 
df43e7d9
 		shift
 		if [ $# -eq 1 ] ; then
 			DBNAME="$1"
 		fi
e102ae72
 		kamailio_drop $DBNAME
96a2629b
 		ret=$?
 		if [ "$ret" -ne 0 ]; then
 			exit $ret
 		fi
e102ae72
 		kamailio_create $DBNAME
96a2629b
 		exit $?
 		;;
1fd2b4ec
 	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 $?
 		;;
727aa357
 	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 $?
 		;;
78045945
 	bdb|db_berkeley)
 		shift
e102ae72
 		kamailio_berkeley "$@"
78045945
 		exit $?
 		;;
ef2b3a6e
 	pframework)
 		shift
 		kamailio_pframework "$@"
 		exit $?
 		;;
5f72f337
 	version)
 		echo  "$0 $VERSION"
 		;;
96a2629b
 	*)
 		usage
 		exit 1;
 		;;
 esac