1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
|
#!/bin/sh
#
# $Id$
#
# Script for adding and dropping SER Postgres tables
#
#################################################################
# config vars
#################################################################
DEFAULT_DBNAME="ser"
DEFAULT_SQLUSER="postgres"
DEFAULT_SCRIPT_DIR=""
DEFAULT_PSQL="/usr/bin/psql"
DEFAULT_PG_DUMP="/usr/bin/pg_dump"
DEFAULT_CREATE_SCRIPT="pg_create.sql"
DEFAULT_DATA_SCRIPT="pg_data.sql"
DEFAULT_DROP_SCRIPT="pg_drop.sql"
#DBHOST="localhost"
usage() {
cat <<EOF
Usage: $COMMAND create [database]
$COMMAND drop [database]
$COMMAND backup [database] <file>
$COMMAND restore [database] <file>
Command 'create' creates database named '${DBNAME}' containing tables needed
for SER and SERWeb. In addition to that two users are created, one with
read/write permissions and one with read-only permissions.
Command 'drop' deletes database named '${DBNAME}' and associated users.
Command 'backup' Dumps the contents of the database in <file>. If no
database name is provided on the command line then the default '${DBNAME}'
database will be used.
Command 'restore' will load the datata previously saved with 'backup'
command in the database. If no database name is provided on the command
line then '${DBNAME}' database will be loaded.
Note: Make sure that you have no conflicting data in the database before
you execute 'restore' command.
Environment variables:
DBHOST Hostname of the Postgres server (${DBHOST})
DBNAME Default name of SER database (${DBNAME})
SQLUSER Database username with administrator privileges (${SQLUSER})
(Make sure that the specified user has sufficient permissions
to create databases, tables, and users)
PSQL Full path to mysql command (${PSQL})
Report bugs to <sr-dev@lists.kamailio.org>
EOF
} #usage
# Dump the contents of the database to stdout
db_save()
{
if [ $# -ne 2 ] ; then
echo "ERROR: Bug in $COMMAND"
exit 1
fi
$DUMP_CMD $1 > $2
}
# Load the contents of the database from a file
db_load() #pars: <database name> <filename>
{
if [ $# -ne 2 ] ; then
echo "ERROR: Bug in $COMMAND"
exit 1
fi
echo "CREATE DATABASE $1" | $CMD "template1"
$CMD $1 < $2
}
# Drop SER database
db_drop()
{
# Drop database
# Revoke user permissions
echo "Dropping database $1"
$CMD "template1" < ${SCRIPT_DIR}/${DROP_SCRIPT}
echo "DROP DATABASE $1" | $CMD "template1"
}
# Create SER database
db_create ()
{
echo "Creating database $1"
echo "CREATE DATABASE $1" | $CMD "template1"
$CMD $1 < ${SCRIPT_DIR}/${CREATE_SCRIPT}
$CMD $1 < ${SCRIPT_DIR}/${DATA_SCRIPT}
}
# Convert relative path to the script directory to absolute if necessary by
# extracting the directory of this script and prefixing the relative path with
# it.
abs_script_dir()
{
my_dir=`dirname $0`;
if [ "${SCRIPT_DIR:0:1}" != "/" ] ; then
SCRIPT_DIR="${my_dir}/${SCRIPT_DIR}"
fi
}
# Main program
COMMAND=`basename $0`
if [ ! -z "$DBHOST" ]; then
DBHOST="-h ${DBHOST}"
fi
if [ -z "$DBNAME" ]; then
DBNAME=$DEFAULT_DBNAME;
fi
if [ -z "$SQLUSER" ]; then
SQLUSER=$DEFAULT_SQLUSER;
fi
if [ -z "$PSQL" ]; then
PSQL=$DEFAULT_PSQL;
fi
if [ -z "$PG_DUMP" ]; then
PG_DUMP=$DEFAULT_PG_DUMP;
fi
if [ -z "$CREATE_SCRIPT" ]; then
CREATE_SCRIPT=$DEFAULT_CREATE_SCRIPT;
fi
if [ -z "$DATA_SCRIPT" ]; then
DATA_SCRIPT=$DEFAULT_DATA_SCRIPT;
fi
if [ -z "$DROP_SCRIPT" ]; then
DROP_SCRIPT=$DEFAULT_DROP_SCRIPT;
fi
if [ -z "$SCRIPT_DIR" ]; then
SCRIPT_DIR=$DEFAULT_SCRIPT_DIR;
fi
if [ $# -eq 0 ]; then
usage
exit 1
fi
if [ ! -x $PSQL ]; then
echo "ERROR: Could not execute Postgres tool $PSQL, please set PSQL variable"
echo " Run ($COMMAND without parameters for more information)"
exit 1
fi
CMD="$PSQL ${DBHOST} -U $SQLUSER"
DUMP_CMD="$PG_DUMP ${DBHOST} -U $SQLUSER"
abs_script_dir
case $1 in
create) # Create SER database and users
shift
if [ $# -eq 1 ]; then
db_create $1
elif [ $# -eq 0 ]; then
db_create ${DBNAME}
else
usage
exit 1
fi
exit $?
;;
drop) # Drop SER database and users
shift
if [ $# -eq 1 ]; then
db_drop $1
elif [ $# -eq 0 ]; then
db_drop ${DBNAME}
else
usage
exit 1
fi
exit $?
;;
backup) # backup SER database
shift
if [ $# -eq 1 ]; then
db_save ${DBNAME} $1
elif [ $# -eq 2 ]; then
db_save $1 $2
else
usage
exit 1
fi
exit $?
;;
restore) # restore SER database
shift
if [ $# -eq 1 ]; then
db_load ${DBNAME} $1
elif [ $# -eq 2 ]; then
db_load $1 $2
else
usage
exit 1
fi
exit $?
;;
*)
usage
exit 1;
;;
esac
|