|
@@ -8,25 +8,23 @@
|
|
|
#################################################################
|
|
|
# config vars
|
|
|
#################################################################
|
|
|
-DEFAULT_DBHOST="localhost"
|
|
|
DEFAULT_DBNAME="ser"
|
|
|
-DEFAULT_SQLUSER="root"
|
|
|
+DEFAULT_SQLUSER="postgres"
|
|
|
|
|
|
-DEFAULT_MYSQL="/usr/bin/mysql"
|
|
|
-DEFAULT_MYSQLDUMP="/usr/bin/mysqldump"
|
|
|
+DEFAULT_PSQL="/usr/bin/psql"
|
|
|
+DEFAULT_PG_DUMP="/usr/bin/pg_dump"
|
|
|
|
|
|
-DEFAULT_CREATE_SCRIPT="my_create.sql"
|
|
|
-DEFAULT_DROP_SCRIPT="my_drop.sql"
|
|
|
+DEFAULT_CREATE_SCRIPT="pg_create.sql"
|
|
|
+DEFAULT_DROP_SCRIPT="pg_drop.sql"
|
|
|
|
|
|
-CMD="$MYSQL -f -h$DBHOST -u$SQLUSER"
|
|
|
+#DBHOST="localhost"
|
|
|
|
|
|
usage() {
|
|
|
cat <<EOF
|
|
|
-Usage: $COMMAND create
|
|
|
- $COMMAND drop
|
|
|
- $COMMAND backup [database] <file>
|
|
|
+Usage: $COMMAND create [database]
|
|
|
+ $COMMAND drop [database]
|
|
|
+ $COMMAND backup [database] <file>
|
|
|
$COMMAND restore [database] <file>
|
|
|
- $COMMAND copy <source_db> <dest_db>
|
|
|
|
|
|
Command 'create' creates database named '${DBNAME}' containing tables
|
|
|
needed for SER and SERWeb. In addition to that two users are created,
|
|
@@ -44,43 +42,19 @@ Usage: $COMMAND create
|
|
|
Note: Make sure that you have no conflicting data in the database before
|
|
|
you execute 'restore' command.
|
|
|
|
|
|
- Command 'copy' will copy the contents of <source_db> to database <dest_db>.
|
|
|
- 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})
|
|
|
+ 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)
|
|
|
- MYSQL Full path to mysql command (${MYSQL})
|
|
|
- MYSQLDUMP Full path to mysqldump command (${MYSQLDUMP})
|
|
|
+ PSQL Full path to mysql command (${PSQL})
|
|
|
|
|
|
Report bugs to <[email protected]>
|
|
|
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()
|
|
|
{
|
|
@@ -88,7 +62,7 @@ db_save()
|
|
|
echo "ERROR: Bug in $COMMAND"
|
|
|
exit 1
|
|
|
fi
|
|
|
- $DUMP_CMD -t $PW $1 > $2
|
|
|
+ $DUMP_CMD $1 > $2
|
|
|
}
|
|
|
|
|
|
|
|
@@ -99,54 +73,30 @@ db_load() #pars: <database name> <filename>
|
|
|
echo "ERROR: Bug in $COMMAND"
|
|
|
exit 1
|
|
|
fi
|
|
|
- sql_query $1 < $2
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-# copy a database to database_bak
|
|
|
-db_copy() # par: <source_database> <destination_database>
|
|
|
-{
|
|
|
- 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 <<EOF
|
|
|
- CREATE DATABASE $2;
|
|
|
-EOF
|
|
|
-
|
|
|
- db_load $2 $BU
|
|
|
- if [ "$?" -ne 0 ]; then
|
|
|
- echo "ERROR: Failed to create the destination database (database exists or insuffucient permissions)"
|
|
|
- rm -f $BU
|
|
|
- exit 1
|
|
|
- fi
|
|
|
+ echo "CREATE DATABASE $1" | $CMD "template1"
|
|
|
+ $CMD $1 < $2
|
|
|
}
|
|
|
|
|
|
|
|
|
# Drop SER database
|
|
|
-ser_drop()
|
|
|
+db_drop()
|
|
|
{
|
|
|
# Drop dabase
|
|
|
# Revoke user permissions
|
|
|
|
|
|
- echo "Dropping SER database"
|
|
|
- sql_query < $DROP_SCRIPT
|
|
|
-} #ser_drop
|
|
|
+ echo "Dropping database $1"
|
|
|
+ $CMD "template1" < ${DROP_SCRIPT}
|
|
|
+ echo "DROP DATABASE $1" | $CMD "template1"
|
|
|
+}
|
|
|
|
|
|
|
|
|
# Create SER database
|
|
|
-ser_create ()
|
|
|
+db_create ()
|
|
|
{
|
|
|
- echo "Creating SER database"
|
|
|
- sql_query < $CREATE_SCRIPT
|
|
|
-} # ser_create
|
|
|
+ echo "Creating database $1"
|
|
|
+ echo "CREATE DATABASE $1" | $CMD "template1"
|
|
|
+ $CMD $1 < $CREATE_SCRIPT
|
|
|
+}
|
|
|
|
|
|
|
|
|
|
|
@@ -154,8 +104,8 @@ ser_create ()
|
|
|
|
|
|
COMMAND=`basename $0`
|
|
|
|
|
|
-if [ -z "$DBHOST" ]; then
|
|
|
- DBHOST=$DEFAULT_DBHOST;
|
|
|
+if [ ! -z "$DBHOST" ]; then
|
|
|
+ DBHOST="-h ${DBHOST}"
|
|
|
fi
|
|
|
|
|
|
if [ -z "$DBNAME" ]; then
|
|
@@ -166,13 +116,13 @@ if [ -z "$SQLUSER" ]; then
|
|
|
SQLUSER=$DEFAULT_SQLUSER;
|
|
|
fi
|
|
|
|
|
|
-if [ -z "$MYSQL" ]; then
|
|
|
- MYSQL=$DEFAULT_MYSQL;
|
|
|
+if [ -z "$PSQL" ]; then
|
|
|
+ PSQL=$DEFAULT_PSQL;
|
|
|
fi
|
|
|
|
|
|
-if [ -z "$MYSQLDUMP" ]; then
|
|
|
- MYSQLDUMP=$DEFAULT_MYSQLDUMP;
|
|
|
-fi
|
|
|
+if [ -z "$PG_DUMP" ]; then
|
|
|
+ PG_DUMP=$DEFAULT_PG_DUMP;
|
|
|
+fi
|
|
|
|
|
|
if [ -z "$CREATE_SCRIPT" ]; then
|
|
|
CREATE_SCRIPT=$DEFAULT_CREATE_SCRIPT;
|
|
@@ -187,45 +137,46 @@ if [ $# -eq 0 ]; then
|
|
|
exit 1
|
|
|
fi
|
|
|
|
|
|
-if [ ! -x $MYSQL ]; then
|
|
|
- echo "ERROR: Could not execute MySQL tool $MYSQL, please set MYSQL variable"
|
|
|
+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
|
|
|
|
|
|
-if [ ! -x $MYSQLDUMP ]; then
|
|
|
- echo "ERROR: Could not execute MySQL tool $MYSQLDUMP, please set MYSQLDUMP variable"
|
|
|
- echo " (Run $COMMAND without parameters for more information"
|
|
|
- exit 1
|
|
|
-fi
|
|
|
-
|
|
|
-CMD="$MYSQL -h$DBHOST -u$SQLUSER"
|
|
|
-DUMP_CMD="${MYSQLDUMP} -h$DBHOST -u$SQLUSER -c -a -e --add-locks --all"
|
|
|
-
|
|
|
-export PW
|
|
|
-prompt_pw
|
|
|
-
|
|
|
-if [ -z "$PW" ]; then
|
|
|
- unset PW
|
|
|
-else
|
|
|
- PW="-p$PW"
|
|
|
-fi
|
|
|
+CMD="$PSQL ${DBHOST} -U $SQLUSER"
|
|
|
+DUMP_CMD="$PG_DUMP ${DBHOST} -U $SQLUSER"
|
|
|
|
|
|
case $1 in
|
|
|
create) # Create SER database and users
|
|
|
- ser_create
|
|
|
+ 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
|
|
|
- ser_drop
|
|
|
+ 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
|
|
|
+ db_save ${DBNAME} $1
|
|
|
elif [ $# -eq 2 ]; then
|
|
|
db_save $1 $2
|
|
|
else
|
|
@@ -238,7 +189,7 @@ case $1 in
|
|
|
restore) # restore SER database
|
|
|
shift
|
|
|
if [ $# -eq 1 ]; then
|
|
|
- db_load $DBNAME $1
|
|
|
+ db_load ${DBNAME} $1
|
|
|
elif [ $# -eq 2 ]; then
|
|
|
db_load $1 $2
|
|
|
else
|
|
@@ -247,17 +198,7 @@ case $1 in
|
|
|
fi
|
|
|
exit $?
|
|
|
;;
|
|
|
-
|
|
|
- copy)
|
|
|
- shift
|
|
|
- if [ $# -ne 2 ]; then
|
|
|
- usage
|
|
|
- exit 1
|
|
|
- fi
|
|
|
- db_copy $1 $2
|
|
|
- exit $?
|
|
|
- ;;
|
|
|
-
|
|
|
+
|
|
|
*)
|
|
|
usage
|
|
|
exit 1;
|