ser_mysql.sh 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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 [ ! $KEEP_USERS ] ; then
  181. sql_query "" "DROP USER '${RWUSER}'@'%'"
  182. sql_query "" "DROP USER '${RWUSER}'@'localhost'"
  183. sql_query "" "DROP USER '${ROUSER}'@'%'"
  184. sql_query "" "DROP USER '${ROUSER}'@'localhost'"
  185. fi
  186. sql_query "" "FLUSH PRIVILEGES"
  187. } # drop_db
  188. # Create SER database
  189. create_db ()
  190. {
  191. # Create the database
  192. sql_query "" "CREATE DATABASE IF NOT EXISTS ${DBNAME}"
  193. # Add read/write access to RWUSER
  194. sql_query "" "GRANT ALL ON ${DBNAME}.* TO '${RWUSER}'@'%' IDENTIFIED BY '${RWPASS}'"
  195. sql_query "" "GRANT ALL ON ${DBNAME}.* TO '${RWUSER}'@'localhost' IDENTIFIED BY '${RWPASS}'"
  196. # Add read-only access to ROUSER
  197. sql_query "" "GRANT ALL ON ${DBNAME}.* TO '${ROUSER}'@'%' IDENTIFIED BY '${ROPASS}'"
  198. sql_query "" "GRANT ALL ON ${DBNAME}.* TO '${ROUSER}'@'localhost' IDENTIFIED BY '${ROPASS}'"
  199. # Activate changes
  200. sql_query "" "FLUSH PRIVILEGES"
  201. # Load table definitions
  202. sql_query $DBNAME < $CREATE_SCRIPT
  203. } # create_db
  204. # Main program
  205. COMMAND=`basename $0`
  206. if [ -z "$DBNAME" ] ; then DBNAME=$DEFAULT_DBNAME; fi;
  207. if [ -z "$ROUSER" ] ; then ROUSER=$DEFAULT_ROUSER; fi;
  208. if [ -z "$RWUSER" ] ; then RWUSER=$DEFAULT_RWUSER; fi;
  209. if [ -z "$ROPASS" ] ; then ROPASS=$DEFAULT_ROPASS; fi;
  210. if [ -z "$RWPASS" ] ; then RWPASS=$DEFAULT_RWPASS; fi;
  211. if [ -z "$DBHOST" ] ; then DBHOST=$DEFAULT_DBHOST; fi;
  212. if [ -z "$SQLUSER" ] ; then SQLUSER=$DEFAULT_SQLUSER; fi;
  213. if [ -z "$MYSQL" ] ; then MYSQL=$DEFAULT_MYSQL; fi
  214. if [ -z "$MYSQLDUMP" ] ; then MYSQLDUMP=$DEFAULT_MYSQLDUMP; fi
  215. if [ -z "$DUMP_OPTS" ] ; then DUMP_OPTS=$DEFAULT_DUMP_OPTS; fi
  216. if [ -z "$CREATE_SCRIPT" ] ; then CREATE_SCRIPT=`dirname $0`"/"$DEFAULT_CREATE_SCRIPT; fi
  217. if [ -z "$DROP_SCRIPT" ] ; then DROP_SCRIPT=`dirname $0`"/"$DEFAULT_DROP_SCRIPT; fi
  218. TEMP=`getopt -o hn:r:w:p:P:ts:u:vkq:: --long help,name:,ro-username:,rw-username:,\
  219. ro-password:,rw-password:,tables,server:,username:,verbose,keep-users,sql-password:: -n $COMMAND -- "$@"`
  220. if [ $? != 0 ] ; then exit 1; fi
  221. eval set -- "$TEMP"
  222. while true ; do
  223. case "$1" in
  224. -h|--help) usage; exit 0 ;;
  225. -n|--name) DBNAME=$2; shift 2 ;;
  226. -r|--ro-username) ROUSER=$2; shift 2 ;;
  227. -w|--rw-username) RWUSER=$2; shift 2 ;;
  228. -p|--ro-password) ROPASS=$2; shift 2 ;;
  229. -P|--rw-password) RWPASS=$2; shift 2 ;;
  230. -t|--tables) DUMP_OPTS="$DUMP_OPTS -t "; shift ;;
  231. -s|--server) DBHOST=$2; shift 2 ;;
  232. -u|--username) SQLUSER=$2; shift 2 ;;
  233. -v|--verbose) MYSQL_OPTS="$MYSQL_OPTS -v "; shift ;;
  234. -k|--keep-users) KEEP_USERS=1; shift ;;
  235. -q|--sql-password)
  236. case "$2" in
  237. "") DONT_ASK=1; shift 2 ;;
  238. *) PW=$2; shift 2 ;;
  239. esac
  240. ;;
  241. --) shift; break ;;
  242. *) echo "Internal error"; exit 1 ;;
  243. esac
  244. done
  245. if [ $# -eq 0 ]; then
  246. usage
  247. exit 1
  248. fi
  249. # Make sure we can execute mysql command
  250. TEMP=`which $MYSQL`
  251. if [ $? != 0 ] ; then
  252. echo "Could not find mysql client utility"
  253. echo "Set MYSQL environment variable properly (see -h for more info)"
  254. exit 1
  255. fi
  256. # Make sure we can execute mysqldump command
  257. TEMP=`which $MYSQLDUMP`
  258. if [ $? != 0 ] ; then
  259. echo "Could not find mysqldump utility"
  260. echo "Set MYSQLDUMP environment variable properly (see -h for more info)"
  261. exit 1
  262. fi
  263. CMD="$MYSQL -h$DBHOST -u$SQLUSER"
  264. DUMP_CMD="${MYSQLDUMP} -h$DBHOST -u$SQLUSER $DUMP_OPTS"
  265. case $1 in
  266. create) # Create SER database and users
  267. prompt_pw
  268. create_db
  269. exit $?
  270. ;;
  271. drop) # Drop SER database and users
  272. prompt_pw
  273. drop_db
  274. exit $?
  275. ;;
  276. backup) # backup SER database
  277. shift
  278. if [ $# -eq 1 ] ; then
  279. prompt_pw
  280. $DUMP_CMD $PW $MYSQL_OPTS ${DBNAME} > $1
  281. elif [ $# -eq 0 ] ; then
  282. prompt_pw
  283. $DUMP_CMD $PW $MYSQL_OPTS ${DBNAME}
  284. else
  285. usage
  286. exit 1
  287. fi
  288. exit $?
  289. ;;
  290. restore) # restore SER database
  291. shift
  292. if [ $# -eq 1 ]; then
  293. prompt_pw
  294. sql_query $DBNAME < $1
  295. elif [ $# -eq 0 ] ; then
  296. prompt_pw
  297. cat | sql_query $DBNAME
  298. else
  299. usage
  300. exit 1
  301. fi
  302. exit $?
  303. ;;
  304. *)
  305. echo "Unknown command '$1'"
  306. usage
  307. exit 1;
  308. ;;
  309. esac