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