2
0

ser_mysql.sh 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. #!/bin/sh
  2. #
  3. # $Id$
  4. #
  5. # Script for adding and dropping ser MySQL tables
  6. #
  7. # Copyright (C) 2006 iptelorg GmbH
  8. #
  9. #################################################################
  10. # configuration variables
  11. #################################################################
  12. DEFAULT_DBHOST="localhost" # Default hostname of the database server
  13. DEFAULT_SQLUSER="root" # Database username with admin privileges
  14. DEFAULT_DBNAME="ser" # Default name of SER database
  15. DEFAULT_ROUSER="serro" # Default read-only username to SER database
  16. DEFAULT_ROPASS="47serro11" # Default password of read-only user
  17. DEFAULT_RWUSER="ser" # Default username of read-write user
  18. DEFAULT_RWPASS="heslo" # Default password of read-write user
  19. DEFAULT_MYSQL="mysql"
  20. DEFAULT_MYSQLDUMP="mysqldump"
  21. DEFAULT_CREATE_SCRIPT="my_create.sql"
  22. DEFAULT_DROP_SCRIPT="my_drop.sql"
  23. CMD="$MYSQL -f -h$DBHOST -u$SQLUSER"
  24. DEFAULT_DUMP_OPTS="-c -a -e --add-locks --all"
  25. usage() {
  26. cat <<EOF
  27. NAME
  28. $COMMAND - SER MySQL database administration tool
  29. SYNOPSIS
  30. $COMMAND [options] create
  31. $COMMAND [options] drop
  32. $COMMAND [options] backup [filename]
  33. $COMMAND [options] restore [filename]
  34. DESCRIPTION
  35. This tool is a simple shell wrapper over mysql client utility that can
  36. be used to create, drop, or backup SER database stored on a MySQL server.
  37. See section COMMANDS for brief overview of supported actions.
  38. The SQL definition of tables within SER database is stored in a separate
  39. file which can be usualy found in /usr/lib/ser/my_create.sql (depending
  40. on installation). You can use that file to create SER database manually
  41. if you cannot or do not want to use this shell wrapper.
  42. This tool requires mysql client utility to create or drop SER database.
  43. Furthemore backup and restore commands require mysqldump. Both tools
  44. can be found in mysql-client package.
  45. COMMANDS
  46. create
  47. Create a new SER database from scratch. The database must not exist.
  48. This command creates the database, the default name of the database
  49. is '${DEFAULT_DBNAME}' (the default name can be changed using a command line
  50. parameter, see below). Furthemore the script will load table definition
  51. from the external SQL file and create users with access to the newly
  52. created database. You can use command line options to change the
  53. default database name, usernames and passwords. Note that you need to
  54. change SER and SERWeb configuration if you change database name or
  55. usernames because SER and SERWeb are pre-configured to use the default
  56. names.
  57. drop
  58. This command can be used to delete SER database and corresponding
  59. database users. WARNING: This command will delete all data in the
  60. database and this action cannot be undone afterwards. Make sure that
  61. you have backups if you want to keep the data from the database.
  62. The command also deletes the database users by default. You can change
  63. that behavior using -k command line options, see below.
  64. backup <filename>
  65. Backup the contents of SER database. If you specify a filename then the
  66. contents of the database will be saved in that file, otherwise the tool
  67. will dumps the contents on the standard output. By default the backup
  68. SQL data contains CREATE TABLE statements that will drop and recreate
  69. database tables being loaded. This ensures that the tables are empty
  70. and have correct structure. You can change this behavior using -t command
  71. line option.
  72. restore <filename>
  73. Load the contents of SER database from a file (if you specify one) or
  74. from the standard input. Make sure that the database exists before you
  75. load the data. Make sure that the database is empty if you have backups
  76. without create table statements (i.e. created with -t command line option)
  77. and that the tables are empty.
  78. OPTIONS
  79. -h, --help
  80. Display this help text.
  81. -n NAME, --name=NAME
  82. Database name of SER database.
  83. (Default value is '$DEFAULT_DBNAME')
  84. -r USERNAME, --ro-username=USERNAME
  85. Username of user with read-only permissions to SER database.
  86. (Default value is '$DEFAULT_ROUSER')
  87. -w USERNAME, --rw-username=USERNAME
  88. Username of user with read-write permissions to SER database.
  89. (Default value is '$DEFAULT_RWUSER')
  90. -p PASSWORD, --ro-password=PASSWORD
  91. Password of user with read-only permissions to SER database.
  92. (Default value is '$DEFAULT_ROPASS')
  93. -P PASSWORD, --rw-password=PASSWORD
  94. Password of user with read-write permissions to SER database.
  95. (Default value is '$DEFAULT_RWPASS')
  96. -t, --tables
  97. Do not write CREATE TABLE statements that recreate tables when
  98. restoring data from backup.
  99. -s HOST, --server=HOST
  100. Hostname or IP address of database server.
  101. (Default value is '$DEFAULT_DBHOST')
  102. -u USERNAME, --username=USERNAME
  103. Username of database administrator.
  104. (Default value is '$DEFAULT_SQLUSER')
  105. -q[PASSWORD], --sql-password[=PASSWORD]
  106. Database administrator password. If you specify this option without
  107. value then the script will assume that no password for database
  108. administrator is needed and will not ask for it.
  109. (No default value)
  110. -k, --keep-users
  111. Do not delete database users when removing the database. This
  112. is useful if you have multiple databases and use the same users
  113. to access them.
  114. -v, --verbose
  115. Enable verbose mode. This option can be given multiple times
  116. to produce more and more output.
  117. ENVIRONMENT VARIABLES
  118. MYSQL Path to mysql command (Currently ${MYSQL})
  119. MYSQLDUMP Path to mysqldump command (Currently ${MYSQLDUMP})
  120. AUTHOR
  121. Written by Jan Janak <[email protected]>
  122. COPYRIGHT
  123. Copyright (C) 2006 iptelorg GmbH
  124. This is free software. You may redistribute copies of it under the
  125. termp of the GNU General Public License. There is NO WARRANTY, to the
  126. extent permitted by law.
  127. FILES
  128. $CREATE_SCRIPT
  129. $DROP_SCRIPT
  130. REPORTING BUGS
  131. Report bugs to <[email protected]>
  132. EOF
  133. } #usage
  134. #
  135. # Read password from user
  136. #
  137. prompt_pw()
  138. {
  139. export PW
  140. if [ ! -z $DONT_ASK ] ; then
  141. unset PW
  142. return 0
  143. elif [ -z "$PW" ] ; then
  144. savetty=`stty -g`
  145. printf "Enter password for MySQL user ${SQLUSER} (Hit enter for no password): "
  146. stty -echo
  147. read PW
  148. stty $savetty
  149. echo
  150. fi
  151. if [ -z "$PW" ]; then
  152. unset PW
  153. else
  154. PW="-p$PW"
  155. fi
  156. }
  157. #
  158. # Execute an SQL command
  159. #
  160. sql_query()
  161. {
  162. if [ $# -gt 1 ] ; then
  163. DB=$1
  164. shift
  165. $CMD $PW $MYSQL_OPTS "$DB" -e "$@"
  166. else
  167. $CMD $PW $MYSQL_OPTS "$@"
  168. fi
  169. }
  170. # Drop SER database
  171. drop_db()
  172. {
  173. # Drop the database if it exists
  174. sql_query "" "DROP DATABASE IF EXISTS ${DBNAME}"
  175. # Revoke permissions to both RW and RO users
  176. sql_query "" "REVOKE ALL PRIVILEGES ON ${DBNAME}.* FROM '${RWUSER}'@'%'"
  177. sql_query "" "REVOKE ALL PRIVILEGES ON ${DBNAME}.* FROM '${RWUSER}'@'localhost'"
  178. sql_query "" "REVOKE ALL PRIVILEGES ON ${DBNAME}.* FROM '${ROUSER}'@'%'"
  179. sql_query "" "REVOKE ALL PRIVILEGES ON ${DBNAME}.* FROM '${ROUSER}'@'localhost'"
  180. if [ ! -z "$KEEP_USERS" ] ; then
  181. # Works only with MySQL 4.1.1 and higher
  182. #sql_query "" "DROP USER '${RWUSER}'@'%'"
  183. #sql_query "" "DROP USER '${RWUSER}'@'localhost'"
  184. #sql_query "" "DROP USER '${ROUSER}'@'%'"
  185. #sql_query "" "DROP USER '${ROUSER}'@'localhost'"
  186. # Works with older MySQL versions
  187. sql_query "" "DELETE FROM mysql.user WHERE User='${RWUSER}' and Host='%'"
  188. sql_query "" "DELETE FROM mysql.user WHERE User='${RWUSER}' and Host='localhost'"
  189. sql_query "" "DELETE FROM mysql.user WHERE User='${ROUSER}' and Host='%'"
  190. sql_query "" "DELETE FROM mysql.user WHERE User='${ROUSER}' and Host='localhost'"
  191. fi
  192. sql_query "" "FLUSH PRIVILEGES"
  193. } # drop_db
  194. # Create SER database
  195. create_db ()
  196. {
  197. # Create the database
  198. sql_query "" "CREATE DATABASE IF NOT EXISTS ${DBNAME}"
  199. # Add read/write access to RWUSER
  200. sql_query "" "GRANT ALL ON ${DBNAME}.* TO '${RWUSER}'@'%' IDENTIFIED BY '${RWPASS}'"
  201. sql_query "" "GRANT ALL ON ${DBNAME}.* TO '${RWUSER}'@'localhost' IDENTIFIED BY '${RWPASS}'"
  202. # Add read-only access to ROUSER
  203. sql_query "" "GRANT ALL ON ${DBNAME}.* TO '${ROUSER}'@'%' IDENTIFIED BY '${ROPASS}'"
  204. sql_query "" "GRANT ALL ON ${DBNAME}.* TO '${ROUSER}'@'localhost' IDENTIFIED BY '${ROPASS}'"
  205. # Activate changes
  206. sql_query "" "FLUSH PRIVILEGES"
  207. # Load table definitions
  208. sql_query $DBNAME < $CREATE_SCRIPT
  209. } # create_db
  210. # Main program
  211. COMMAND=`basename $0`
  212. if [ -z "$DBNAME" ] ; then DBNAME=$DEFAULT_DBNAME; fi;
  213. if [ -z "$ROUSER" ] ; then ROUSER=$DEFAULT_ROUSER; fi;
  214. if [ -z "$RWUSER" ] ; then RWUSER=$DEFAULT_RWUSER; fi;
  215. if [ -z "$ROPASS" ] ; then ROPASS=$DEFAULT_ROPASS; fi;
  216. if [ -z "$RWPASS" ] ; then RWPASS=$DEFAULT_RWPASS; fi;
  217. if [ -z "$DBHOST" ] ; then DBHOST=$DEFAULT_DBHOST; fi;
  218. if [ -z "$SQLUSER" ] ; then SQLUSER=$DEFAULT_SQLUSER; fi;
  219. if [ -z "$MYSQL" ] ; then MYSQL=$DEFAULT_MYSQL; fi
  220. if [ -z "$MYSQLDUMP" ] ; then MYSQLDUMP=$DEFAULT_MYSQLDUMP; fi
  221. if [ -z "$DUMP_OPTS" ] ; then DUMP_OPTS=$DEFAULT_DUMP_OPTS; fi
  222. if [ -z "$CREATE_SCRIPT" ] ; then CREATE_SCRIPT=`dirname $0`"/"$DEFAULT_CREATE_SCRIPT; fi
  223. if [ -z "$DROP_SCRIPT" ] ; then DROP_SCRIPT=`dirname $0`"/"$DEFAULT_DROP_SCRIPT; fi
  224. TEMP=`getopt -o hn:r:w:p:P:ts:u:vkq:: --long help,name:,ro-username:,rw-username:,\
  225. ro-password:,rw-password:,tables,server:,username:,verbose,keep-users,sql-password:: -n $COMMAND -- "$@"`
  226. if [ $? != 0 ] ; then exit 1; fi
  227. eval set -- "$TEMP"
  228. while true ; do
  229. case "$1" in
  230. -h|--help) usage; exit 0 ;;
  231. -n|--name) DBNAME=$2; shift 2 ;;
  232. -r|--ro-username) ROUSER=$2; shift 2 ;;
  233. -w|--rw-username) RWUSER=$2; shift 2 ;;
  234. -p|--ro-password) ROPASS=$2; shift 2 ;;
  235. -P|--rw-password) RWPASS=$2; shift 2 ;;
  236. -t|--tables) DUMP_OPTS="$DUMP_OPTS -t "; shift ;;
  237. -s|--server) DBHOST=$2; shift 2 ;;
  238. -u|--username) SQLUSER=$2; shift 2 ;;
  239. -v|--verbose) MYSQL_OPTS="$MYSQL_OPTS -v "; shift ;;
  240. -k|--keep-users) KEEP_USERS=1; shift ;;
  241. -q|--sql-password)
  242. case "$2" in
  243. "") DONT_ASK=1; shift 2 ;;
  244. *) PW=$2; shift 2 ;;
  245. esac
  246. ;;
  247. --) shift; break ;;
  248. *) echo "Internal error"; exit 1 ;;
  249. esac
  250. done
  251. if [ $# -eq 0 ]; then
  252. usage
  253. exit 1
  254. fi
  255. # Make sure we can execute mysql command
  256. TEMP=`which $MYSQL`
  257. if [ $? != 0 ] ; then
  258. echo "Could not find mysql client utility"
  259. echo "Set MYSQL environment variable properly (see -h for more info)"
  260. exit 1
  261. fi
  262. # Make sure we can execute mysqldump command
  263. TEMP=`which $MYSQLDUMP`
  264. if [ $? != 0 ] ; then
  265. echo "Could not find mysqldump utility"
  266. echo "Set MYSQLDUMP environment variable properly (see -h for more info)"
  267. exit 1
  268. fi
  269. CMD="$MYSQL -h$DBHOST -u$SQLUSER"
  270. DUMP_CMD="${MYSQLDUMP} -h$DBHOST -u$SQLUSER $DUMP_OPTS"
  271. case $1 in
  272. create) # Create SER database and users
  273. prompt_pw
  274. create_db
  275. exit $?
  276. ;;
  277. drop) # Drop SER database and users
  278. prompt_pw
  279. drop_db
  280. exit $?
  281. ;;
  282. backup) # backup SER database
  283. shift
  284. if [ $# -eq 1 ] ; then
  285. prompt_pw
  286. $DUMP_CMD $PW $MYSQL_OPTS ${DBNAME} > $1
  287. elif [ $# -eq 0 ] ; then
  288. prompt_pw
  289. $DUMP_CMD $PW $MYSQL_OPTS ${DBNAME}
  290. else
  291. usage
  292. exit 1
  293. fi
  294. exit $?
  295. ;;
  296. restore) # restore SER database
  297. shift
  298. if [ $# -eq 1 ]; then
  299. prompt_pw
  300. sql_query $DBNAME < $1
  301. elif [ $# -eq 0 ] ; then
  302. prompt_pw
  303. cat | sql_query $DBNAME
  304. else
  305. usage
  306. exit 1
  307. fi
  308. exit $?
  309. ;;
  310. *)
  311. echo "Unknown command '$1'"
  312. usage
  313. exit 1;
  314. ;;
  315. esac