ser_mysql.sh 12 KB

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