#!/bin/sh # # $Id$ # # Script for adding and dropping ser MySql tables # # TO-DO: update_structures command for migriting to new # table definitons # USAGE: call the command without any parameters for info # # 2003-01-21 changed SILO table definition, by dcm # # History: # 2003-03-12 added replication mark and state columns to location (nils) # 2003-03-05: Changed user to username, user is reserved word (janakj) # 2003-01-26 statistics table introduced (jiri) # 2003-01-25: Optimized keys of some core tables (janakj) # 2003-01-25: USER_ID changed to user everywhere (janakj) # 2003-01-24: Changed realm column of subscriber and pending # tables to domain (janakj) # 2003-04-14 reinstall introduced (jiri) # 2004-07-05 new definition of table silo (dcm) ################################################################# # config vars ################################################################# DBNAME=ser DBHOST=localhost USERNAME=ser DEFAULT_PW=heslo ROUSER=serro RO_PW=47serro11 SQL_USER="root" CMD="mysql -h $DBHOST -u$SQL_USER " DUMP_CMD="mysqldump -h $DBHOST -u$SQL_USER -c -t " BACKUP_CMD="mysqldump -h $DBHOST -u$SQL_USER -c " TABLE_TYPE="TYPE=MyISAM" # user name column USERCOL="username" GENHA1='gen_ha1' FOREVER="2020-05-28 21:32:15" DEFAULT_ALIASES_EXPIRES=$FOREVER DEFAULT_Q="1.0" DEFAULT_CALLID="Default-Call-ID" DEFAULT_CSEQ="42" DEFAULT_LOCATION_EXPIRES=$FOREVER ################################################################# usage() { COMMAND=`basename $0` cat < (restores tables from a file) $COMMAND copy (creates a new db from an existing one) $COMMAND reinstall (updates to a new SER database) if you want to manipulate database as other MySql user than root, want to change database name from default value "$DBNAME", or want to use other values for users and password, edit the "config vars" section of the command $COMMAND EOF } #usage # read password prompt_pw() { savetty=`stty -g` printf "MySql password for $SQL_USER: " stty -echo read PW stty $savetty echo } # execute sql command sql_query() { $CMD "-p$PW" "$@" } # dump all rows ser_dump() # pars: { if [ $# -ne 1 ] ; then echo "ser_dump function takes one param" exit 1 fi $DUMP_CMD "-p$PW" $1 } # copy a database to database_bak ser_backup() # par: { if [ $# -ne 1 ] ; then echo "ser_backup function takes one param" exit 1 fi BU=/tmp/mysql_bup.$$ $BACKUP_CMD "-p$PW" $1 > $BU if [ "$?" -ne 0 ] ; then echo "ser backup dump failed" exit 1 fi sql_query < { if [ $# -ne 2 ] ; then echo "ser_restore function takes two params" exit 1 fi sql_query $1 < $2 } ser_drop() # pars: { if [ $# -ne 1 ] ; then echo "ser_drop function takes two params" exit 1 fi sql_query << EOF drop database $1; EOF } #ser_drop # read realm prompt_realm() { printf "Domain (realm) for the default user 'admin': " read SIP_DOMAIN echo } # calculate credentials for admin credentials() { HA1=`$GENHA1 admin $SIP_DOMAIN heslo` if [ $? -ne 0 ] ; then echo "HA1 calculation failed" exit 1 fi HA1B=`$GENHA1 "admin@$SIP_DOMAIN" $SIP_DOMAIN heslo` if [ $? -ne 0 ] ; then echo "HA1B calculation failed" exit 1 fi #PHPLIB_ID of users should be difficulty to guess for security reasons NOW=`date`; PHPLIB_ID=`$GENHA1 "$RANDOM" "$NOW" $SIP_DOMAIN` if [ $? -ne 0 ] ; then echo "PHPLIB_ID calculation failed" exit 1 fi } ser_create () # pars: [] { if [ $# -eq 1 ] ; then if [ -z "$SIP_DOMAIN" ] ; then prompt_realm fi credentials # by default we create initial user INITIAL_USER="INSERT INTO subscriber ($USERCOL, password, first_name, last_name, phone, email_address, datetime_created, datetime_modified, confirmation, flag, sendnotification, greeting, ha1, domain, ha1b, phplib_id ) VALUES ( 'admin', 'heslo', 'Initial', 'Admin', '123', 'root@localhost', '2002-09-04 19:37:45', '0000-00-00 00:00:00', '57DaSIPuCm52UNe54LF545750cfdL48OMZfroM53', 'o', '', '', '$HA1', '$SIP_DOMAIN', '$HA1B', '$PHPLIB_ID' ); INSERT INTO admin_privileges ($USERCOL, domain, priv_name, priv_value) VALUES ('admin', '$SIP_DOMAIN', 'is_admin', '1'); INSERT INTO admin_privileges ($USERCOL, domain, priv_name, priv_value) VALUES ('admin', '$SIP_DOMAIN', 'change_privileges', '1');" elif [ $# -eq 2 ] ; then # if 3rd param set, don't create any initial user INITIAL_USER="" else echo "ser_create function takes one or two params" exit 1 fi echo "creating database $1 ..." sql_query < $tmp_file if [ "$?" -ne 0 ] ; then echo "reinstall: dumping original db failed" exit 1 fi sed "s/[uU][sS][eE][rR]_[iI][dD]/user/g" $tmp_file | sed "s/[uU][sS][eE][rR]\($\|[^a-zA-Z]\)/$USERCOL\1/g" | sed "s/[rR][eE][aA][lL][mM]/domain/g"> ${tmp_file}.2 #3 drop original database echo "dropping table ($DBNAME)" ser_drop $DBNAME if [ "$?" -ne 0 ] ; then echo "reinstall: dropping table failed" rm $tmp_file* exit 1 fi #4 change names in table definition and restore echo "creating new structures" ser_create $DBNAME no_init_user if [ "$?" -ne 0 ] ; then echo "reinstall: creating new table failed" rm $tmp_file* exit 1 fi #5 restoring table content echo "restoring table content" # Recreate perms column here so that subsequent # restore succeeds sql_query $DBNAME << EOF ALTER TABLE subscriber ADD perms VARCHAR(32) AFTER ha1b; ALTER TABLE pending ADD perms VARCHAR(32) AFTER ha1b; EOF ser_restore $DBNAME ${tmp_file}.2 if [ "$?" -ne 0 ] ; then echo "reinstall: restoring table failed" rm $tmp_file* exit 1 fi sql_query $DBNAME << EOF # Move perms from subscriber to admin_privileges INSERT INTO admin_privileges ($USERCOL, domain, priv_name, priv_value) SELECT $USERCOL, domain, 'is_admin', '1' FROM subscriber WHERE perms='admin'; # Drop perms column here ALTER TABLE subscriber DROP perms; ALTER TABLE pending DROP perms; EOF #XX # rm $tmp_file* exit 0 ;; copy) # copy database to some other name shift if [ $# -ne 1 ]; then usage exit 1 fi tmp_file=/tmp/ser_mysql.$$ ser_dump $DBNAME > $tmp_file ret=$? if [ "$ret" -ne 0 ]; then rm $tmp_file exit $ret fi ser_create $1 no_init_user ret=$? if [ "$ret" -ne 0 ]; then rm $tmp_file exit $ret fi ser_restore $1 $tmp_file ret=$? rm $tmp_file exit $ret ;; backup) # backup current database ser_dump $DBNAME exit $? ;; restore) # restore database from a backup shift if [ $# -ne 1 ]; then usage exit 1 fi ser_restore $DBNAME $1 exit $? ;; create) # create new database structures shift if [ $# -eq 1 ] ; then DBNAME="$1" fi ser_create $DBNAME exit $? ;; drop) # delete ser database ser_drop $DBNAME exit $? ;; reinit) # delete database and create a new one ser_drop $DBNAME ret=$? if [ "$ret" -ne 0 ]; then exit $ret fi ser_create $DBNAME exit $? ;; *) usage exit 1; ;; esac