Browse code

- support for SCRIPT_DIRECTORY conf. option - -d cmd line option added - help text reformatted - more flexible searching of SQL script files

Jan Janak authored on 25/06/2008 23:45:51
Showing 1 changed files
... ...
@@ -2,9 +2,9 @@
2 2
 #
3 3
 # $Id$
4 4
 #
5
-# Script for adding and dropping ser MySQL tables
5
+# SER MySQL Database Administration Tool
6 6
 #
7
-# Copyright (C) 2006 iptelorg GmbH
7
+# Copyright (C) 2006-2008 iptelorg GmbH
8 8
 #
9 9
 
10 10
 #################################################################
... ...
@@ -21,6 +21,12 @@ DEFAULT_RWPASS="heslo"       # Default password of read-write user
21 21
 DEFAULT_MYSQL="mysql"
22 22
 DEFAULT_MYSQLDUMP="mysqldump"
23 23
 
24
+# The default directory which contains SQL scripts. If empty then the
25
+# directory which contains this shell script will be used. If relative then
26
+# the pathname will be made absolute with respect to the location of this
27
+# shell script.
28
+DEFAULT_SCRIPT_DIR=""
29
+
24 30
 DEFAULT_CREATE_SCRIPT="my_create.sql"
25 31
 DEFAULT_DATA_SCRIPT="my_data.sql"
26 32
 DEFAULT_DROP_SCRIPT="my_drop.sql"
... ...
@@ -31,7 +37,7 @@ DEFAULT_DUMP_OPTS="-c -a -e --add-locks --all"
31 31
 usage() {
32 32
 cat <<EOF
33 33
 NAME
34
-  $COMMAND - SER MySQL database administration tool
34
+  $COMMAND - SER MySQL Database Administration Tool
35 35
 
36 36
 SYNOPSIS
37 37
   $COMMAND [options] create
... ...
@@ -41,61 +47,60 @@ SYNOPSIS
41 41
   $COMMAND [options] update-data
42 42
 
43 43
 DESCRIPTION
44
-  This tool is a simple shell wrapper over mysql client utility that can
45
-  be used to create, drop, or backup SER database stored on a MySQL server.
46
-  See section COMMANDS for brief overview of supported actions.
44
+  This tool is a simple shell wrapper over mysql client utility that can be
45
+  used to create, drop, or backup SER database stored on a MySQL server.  See
46
+  section COMMANDS for brief overview of supported actions.
47 47
 
48
-  The SQL definition of tables within SER database is stored in a separate
49
-  file which can be usualy found in /usr/lib/ser/my_create.sql (depending
50
-  on installation). You can use that file to create SER database manually
51
-  if you cannot or do not want to use this shell wrapper.
48
+  The SQL definition of tables and initial data within SER database is stored
49
+  in a separate files which can be usualy found under /usr/local/share/ser
50
+  (depending on installation). You can use those files to create SER database
51
+  manually if you cannot or do not want to use this shell wrapper.
52 52
 
53 53
   This tool requires mysql client utility to create or drop SER database.
54
-  Furthemore backup and restore commands require mysqldump. Both tools
55
-  can be found in mysql-client package.
54
+  Additionally backup and restore commands require mysqldump. Both tools can
55
+  be found in mysql-client package.
56 56
 
57 57
 COMMANDS
58 58
   create
59
-    Create a new SER database from scratch. The database must not exist.
60
-    This command creates the database, the default name of the database
61
-    is '${DEFAULT_DBNAME}' (the default name can be changed using a command line
62
-    parameter, see below). Furthemore the script will load table definition
63
-    from the external SQL file and create users with access to the newly
64
-    created database. You can use command line options to change the
65
-    default database name, usernames and passwords. Note that you need to
66
-    change SER and SERWeb configuration if you change database name or
67
-    usernames because SER and SERWeb are pre-configured to use the default
68
-    names.
59
+    Create a new SER database from scratch. The database must not exist.  This
60
+    command creates the database, the default name of the database is
61
+    '${DEFAULT_DBNAME}' (the default name can be changed using a command line
62
+    parameter, see below). Furthemore the script will load table definitions
63
+    from an external SQL file and create users with access to the newly
64
+    created database. You can use command line options to change the default
65
+    database name, usernames and passwords. Note that you need to change SER
66
+    and SERWeb configuration if you change the database name or usernames
67
+    because SER and SERWeb are pre-configured to use the default names.
69 68
 
70 69
   drop
71
-    This command can be used to delete SER database and corresponding
72
-    database users. WARNING: This command will delete all data in the
73
-    database and this action cannot be undone afterwards. Make sure that
74
-    you have backups if you want to keep the data from the database.
75
-    The command also deletes the database users by default. You can change
76
-    that behavior using -k command line options, see below.
70
+    This command can be used to delete SER database and corresponding database
71
+    users. WARNING: This command will delete all data in the database and this
72
+    action cannot be undone! Make sure that you have backups if you want to
73
+    keep the data from the database.  The command also deletes the database
74
+    users by default. You can change that behavior using -k command line
75
+    options, see below.
77 76
 
78 77
   backup <filename>
79 78
     Backup the contents of SER database. If you specify a filename then the
80 79
     contents of the database will be saved in that file, otherwise the tool
81
-    will dumps the contents on the standard output. By default the backup
82
-    SQL data contains CREATE TABLE statements that will drop and recreate
83
-    database tables being loaded. This ensures that the tables are empty
84
-    and have correct structure. You can change this behavior using -t command 
85
-    line option.
80
+    will dumps the contents to the standard output. By default the backup SQL
81
+    data contains CREATE TABLE statements that will drop and recreate database
82
+    tables being loaded. This ensures that the tables are empty and have
83
+    correct structure. You can change this behavior using -t command line
84
+    option.
86 85
 
87 86
   restore <filename>
88
-    Load the contents of SER database from a file (if you specify one) or
89
-    from the standard input. Make sure that the database exists before you
90
-    load the data. Make sure that the database is empty if you have backups 
91
-    without create table statements (i.e. created with -t command line option) 
92
-    and that the tables are empty.
87
+    Load the contents of SER database from a file (if you specify one) or from
88
+    the standard input. Make sure that the database exists before you load the
89
+    data. Make sure that the database is empty if you have backups without
90
+    create table statements (i.e. created with -t command line option) and
91
+    that the tables are empty.
93 92
 
94 93
   update-data
95
-    Update initial data in database. This command delete vendor-controled
96
-    rows from databaze and replace them with new ones.
97
-    
98
-    
94
+    Update initial data in the database. This command deletes vendor-controled
95
+    rows from the database and replaces them with new data.
96
+
97
+
99 98
 OPTIONS
100 99
   -h, --help
101 100
       Display this help text.
... ...
@@ -138,15 +143,19 @@ OPTIONS
138 138
       administrator is needed and will not ask for it.
139 139
       (No default value)
140 140
 
141
+  -d DIRECTORY, --script-dir=DIRECTORY
142
+  	  Directory containing the SQL scripts with database schema and initial
143
+  	  data definition.
144
+  	  (Default value is '$DEFAULT_SCRIPT_DIR')
145
+
141 146
   -k, --keep-users
142
-      Do not delete database users when removing the database. This
143
-      is useful if you have multiple databases and use the same users
144
-      to access them.
147
+      Do not delete database users when removing the database. This is useful
148
+      if you have multiple databases and use the same users to access them.
145 149
 
146 150
   -v, --verbose
147
-      Enable verbose mode. This option can be given multiple times
148
-      to produce more and more output.
149
-        
151
+      Enable verbose mode. This option can be given multiple times to produce
152
+      more and more output.
153
+
150 154
 ENVIRONMENT VARIABLES
151 155
   MYSQL     Path to mysql command (Currently ${MYSQL})
152 156
   MYSQLDUMP Path to mysqldump command (Currently ${MYSQLDUMP})
... ...
@@ -155,15 +164,15 @@ AUTHOR
155 155
   Written by Jan Janak <jan@iptel.org>
156 156
 
157 157
 COPYRIGHT
158
-  Copyright (C) 2006 iptelorg GmbH
159
-  This is free software. You may redistribute copies of it under the
160
-  termp of the GNU General Public License. There is NO WARRANTY, to the
161
-  extent permitted by law.
158
+  Copyright (C) 2006-2008 iptelorg GmbH
159
+  This is free software. You may redistribute copies of it under the termp of
160
+  the GNU General Public License. There is NO WARRANTY, to the extent
161
+  permitted by law.
162 162
 
163 163
 FILES
164
-  $CREATE_SCRIPT
165
-  $DATA_SCRIPT
166
-  $DROP_SCRIPT
164
+  ${SCRIPT_DIR}/${CREATE_SCRIPT}
165
+  ${SCRIPT_DIR}/${DATA_SCRIPT}
166
+  ${SCRIPT_DIR}/${DROP_SCRIPT}
167 167
     
168 168
 REPORTING BUGS
169 169
   Report bugs to <ser-bugs@iptel.org>             
... ...
@@ -171,9 +180,7 @@ EOF
171 171
 } #usage
172 172
 
173 173
 
174
-#
175 174
 # Read password from user
176
-#
177 175
 prompt_pw()
178 176
 {
179 177
     export PW
... ...
@@ -196,9 +203,20 @@ prompt_pw()
196 196
     fi
197 197
 }
198 198
 
199
-#
199
+
200
+# Convert relative path to the script directory to absolute if necessary by
201
+# extracting the directory of this script and prefixing the relative path with
202
+# it.
203
+abs_script_dir()
204
+{
205
+	my_dir=`dirname $0`;
206
+	if [ "${SCRIPT_DIR:0:1}" != "/" ] ; then
207
+		SCRIPT_DIR="${my_dir}/${SCRIPT_DIR}"
208
+	fi
209
+}
210
+
211
+
200 212
 # Execute an SQL command
201
-#
202 213
 sql_query()
203 214
 {
204 215
 	if [ $# -gt 1 ] ; then
... ...
@@ -222,6 +240,7 @@ sql_query()
222 222
 	fi
223 223
 }
224 224
 
225
+
225 226
 # Drop SER database
226 227
 drop_db()
227 228
 {
... ...
@@ -271,39 +290,44 @@ create_db ()
271 271
     sql_query "" "FLUSH PRIVILEGES"
272 272
 
273 273
     # Load table definitions
274
-    sql_query $DBNAME < $CREATE_SCRIPT
274
+    sql_query $DBNAME < ${SCRIPT_DIR}/${CREATE_SCRIPT}
275 275
 
276 276
     # Load initial data
277
-    sql_query $DBNAME < $DATA_SCRIPT
277
+    sql_query $DBNAME < ${SCRIPT_DIR}/${DATA_SCRIPT}
278 278
 } # create_db
279 279
 
280 280
 
281 281
 # Update initial data
282 282
 update_db_data ()
283 283
 {
284
-    sql_query $DBNAME < $DATA_SCRIPT
284
+    sql_query $DBNAME < ${SCRIPT_DIR}/${DATA_SCRIPT}
285 285
 } # update_db_data
286 286
 
287 287
 
288 288
 # Main program
289 289
 COMMAND=`basename $0`
290 290
 
291
-if [ -z "$DBNAME" ] ; then DBNAME=$DEFAULT_DBNAME; fi;
292
-if [ -z "$ROUSER" ] ; then ROUSER=$DEFAULT_ROUSER; fi;
293
-if [ -z "$RWUSER" ] ; then RWUSER=$DEFAULT_RWUSER; fi;
294
-if [ -z "$ROPASS" ] ; then ROPASS=$DEFAULT_ROPASS; fi;
295
-if [ -z "$RWPASS" ] ; then RWPASS=$DEFAULT_RWPASS; fi;
296
-if [ -z "$DBHOST" ] ; then DBHOST=$DEFAULT_DBHOST; fi;
297
-if [ -z "$SQLUSER" ] ; then SQLUSER=$DEFAULT_SQLUSER; fi;
298
-if [ -z "$MYSQL" ] ; then MYSQL=$DEFAULT_MYSQL; fi
299
-if [ -z "$MYSQLDUMP" ] ; then MYSQLDUMP=$DEFAULT_MYSQLDUMP; fi
300
-if [ -z "$DUMP_OPTS" ] ; then DUMP_OPTS=$DEFAULT_DUMP_OPTS; fi 
301
-if [ -z "$CREATE_SCRIPT" ] ; then CREATE_SCRIPT=`dirname $0`"/"$DEFAULT_CREATE_SCRIPT; fi
302
-if [ -z "$DATA_SCRIPT" ] ; then DATA_SCRIPT=`dirname $0`"/"$DEFAULT_DATA_SCRIPT; fi
303
-if [ -z "$DROP_SCRIPT" ] ; then DROP_SCRIPT=`dirname $0`"/"$DEFAULT_DROP_SCRIPT; fi
304
-
305
-TEMP=`getopt -o hn:r:w:p:P:ts:u:vkq:: --long help,name:,ro-username:,rw-username:,\
306
-ro-password:,rw-password:,tables,server:,username:,verbose,keep-users,sql-password:: -n $COMMAND -- "$@"`
291
+if [ -z "$DBNAME" ] ; then DBNAME="$DEFAULT_DBNAME"; fi;
292
+if [ -z "$ROUSER" ] ; then ROUSER="$DEFAULT_ROUSER"; fi;
293
+if [ -z "$RWUSER" ] ; then RWUSER="$DEFAULT_RWUSER"; fi;
294
+if [ -z "$ROPASS" ] ; then ROPASS="$DEFAULT_ROPASS"; fi;
295
+if [ -z "$RWPASS" ] ; then RWPASS="$DEFAULT_RWPASS"; fi;
296
+if [ -z "$DBHOST" ] ; then DBHOST="$DEFAULT_DBHOST"; fi;
297
+if [ -z "$SQLUSER" ] ; then SQLUSER="$DEFAULT_SQLUSER"; fi;
298
+if [ -z "$MYSQL" ] ; then MYSQL="$DEFAULT_MYSQL"; fi
299
+if [ -z "$MYSQLDUMP" ] ; then MYSQLDUMP="$DEFAULT_MYSQLDUMP"; fi
300
+if [ -z "$DUMP_OPTS" ] ; then DUMP_OPTS="$DEFAULT_DUMP_OPTS"; fi 
301
+if [ -z "SCRIPT_DIR" ] ; then SCRIPT_DIR="$DEFAULT_SCRIPT_DIR"; fi
302
+if [ -z "$CREATE_SCRIPT" ] ; then CREATE_SCRIPT="$DEFAULT_CREATE_SCRIPT"; fi
303
+if [ -z "$DATA_SCRIPT" ] ; then DATA_SCRIPT="$DEFAULT_DATA_SCRIPT"; fi
304
+if [ -z "$DROP_SCRIPT" ] ; then DROP_SCRIPT="$DEFAULT_DROP_SCRIPT"; fi
305
+
306
+# Make the path to the script directory absolute
307
+abs_script_dir
308
+
309
+TEMP=`getopt -o hn:r:w:p:P:ts:u:vkq::d: --long help,name:,ro-username:,rw-username:,\
310
+ro-password:,rw-password:,tables,server:,username:,verbose,keep-users],\
311
+sql-password::,script-dir: -n $COMMAND -- "$@"`
307 312
 if [ $? != 0 ] ; then exit 1; fi
308 313
 eval set -- "$TEMP"
309 314
 
... ...
@@ -318,9 +342,15 @@ while true ; do
318 318
 	-t|--tables)       DUMP_OPTS="$DUMP_OPTS -t "; shift ;;
319 319
 	-s|--server)       DBHOST=$2; shift 2 ;;
320 320
 	-u|--username)     SQLUSER=$2; shift 2 ;;
321
-        -v|--verbose)      MYSQL_OPTS="$MYSQL_OPTS -v "; shift ;;
321
+    -v|--verbose)      MYSQL_OPTS="$MYSQL_OPTS -v "; shift ;;
322 322
 	-k|--keep-users)   KEEP_USERS=1; shift ;;
323
-        -q|--sql-password)
323
+    -d|--script-dir)
324
+        SCRIPT_DIR=$2;
325
+  	    # The script directory changed, make it absolute again
326
+  	    abs_script_dir
327
+  	    shift 2
328
+  	    ;;
329
+     -q|--sql-password)
324 330
 	    case "$2" in
325 331
 		"") DONT_ASK=1; shift 2 ;;
326 332
 		*)  PW=$2; shift 2 ;;