#!/bin/sh # # $Id$ # # Script for adding and dropping SER Postgres tables # ################################################################# # config vars ################################################################# DEFAULT_DBHOST="localhost" DEFAULT_DBNAME="ser" DEFAULT_SQLUSER="root" DEFAULT_MYSQL="/usr/bin/mysql" DEFAULT_MYSQLDUMP="/usr/bin/mysqldump" DEFAULT_CREATE_SCRIPT="my_create.sql" DEFAULT_DROP_SCRIPT="my_drop.sql" CMD="$MYSQL -f -h$DBHOST -u$SQLUSER" usage() { cat < $COMMAND restore [database] $COMMAND copy 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. Commmand 'drop' deletes database named '${DBNAME}' and associated users. Command 'backup' Dumps the contents of the database in . 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. Command 'copy' will copy the contents of to database . The destination database must not exist -- it will be created. Note: The default users (ser, serro) will not have sufficient permissions to access the new database. Environment variables: DBHOST Hostname of the MySQL 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) MYSQL Full path to mysql command (${MYSQL}) MYSQLDUMP Full path to mysqldump command (${MYSQLDUMP}) Report bugs to EOF } #usage # read password prompt_pw() { savetty=`stty -g` printf "Enter password for MySQL user ${SQLUSER} (hit enter for no password): " stty -echo read PW stty $savetty echo } # execute sql command sql_query() { $CMD $PW "$@" } # Dump the contents of the database to stdout db_save() { if [ $# -ne 2 ] ; then echo "ERROR: Bug in $COMMAND" exit 1 fi $DUMP_CMD -t $PW $1 > $2 } # Load the contents of the database from a file db_load() #pars: { if [ $# -ne 2 ] ; then echo "ERROR: Bug in $COMMAND" exit 1 fi sql_query $1 < $2 } # copy a database to database_bak db_copy() # par: { if [ $# -ne 2 ] ; then echo "ERROR: Bug in $COMMAND" exit 1 fi BU=/tmp/mysql_bup.$$ $DUMP_CMD $PW $1 > $BU if [ "$?" -ne 0 ] ; then echo "ERROR: Failed to copy the source database" exit 1 fi sql_query <