2
0

sip-router_mysql.sh 13 KB

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