ser_postgres.sh 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. #!/bin/sh
  2. #
  3. # $Id$
  4. #
  5. # Script for adding and dropping SER Postgres tables
  6. #
  7. #################################################################
  8. # config vars
  9. #################################################################
  10. DEFAULT_DBHOST="localhost"
  11. DEFAULT_DBNAME="ser"
  12. DEFAULT_SQLUSER="root"
  13. DEFAULT_MYSQL="/usr/bin/mysql"
  14. DEFAULT_MYSQLDUMP="/usr/bin/mysqldump"
  15. DEFAULT_CREATE_SCRIPT="my_create.sql"
  16. DEFAULT_DROP_SCRIPT="my_drop.sql"
  17. CMD="$MYSQL -f -h$DBHOST -u$SQLUSER"
  18. usage() {
  19. cat <<EOF
  20. Usage: $COMMAND create
  21. $COMMAND drop
  22. $COMMAND backup [database] <file>
  23. $COMMAND restore [database] <file>
  24. $COMMAND copy <source_db> <dest_db>
  25. Command 'create' creates database named '${DBNAME}' containing tables
  26. needed for SER and SERWeb. In addition to that two users are created,
  27. one with read/write permissions and one with read-only permissions.
  28. Commmand 'drop' deletes database named '${DBNAME}' and associated users.
  29. Command 'backup' Dumps the contents of the database in <file>. If no
  30. database name is provided on the command line then the default '${DBNAME}'
  31. database will be used.
  32. Command 'restore' will load the datata previously saved with 'backup'
  33. command in the database. If no database name is provided on the command
  34. line then '${DBNAME}' database will be loaded.
  35. Note: Make sure that you have no conflicting data in the database before
  36. you execute 'restore' command.
  37. Command 'copy' will copy the contents of <source_db> to database <dest_db>.
  38. The destination database must not exist -- it will be created.
  39. Note: The default users (ser, serro) will not have sufficient permissions
  40. to access the new database.
  41. Environment variables:
  42. DBHOST Hostname of the MySQL server (${DBHOST})
  43. DBNAME Default name of SER database (${DBNAME})
  44. SQLUSER Database username with administrator privileges (${SQLUSER})
  45. (Make sure that the specified user has sufficient permissions
  46. to create databases, tables, and users)
  47. MYSQL Full path to mysql command (${MYSQL})
  48. MYSQLDUMP Full path to mysqldump command (${MYSQLDUMP})
  49. Report bugs to <[email protected]>
  50. EOF
  51. } #usage
  52. # read password
  53. prompt_pw()
  54. {
  55. savetty=`stty -g`
  56. printf "Enter password for MySQL user ${SQLUSER} (hit enter for no password): "
  57. stty -echo
  58. read PW
  59. stty $savetty
  60. echo
  61. }
  62. # execute sql command
  63. sql_query()
  64. {
  65. $CMD $PW "$@"
  66. }
  67. # Dump the contents of the database to stdout
  68. db_save()
  69. {
  70. if [ $# -ne 2 ] ; then
  71. echo "ERROR: Bug in $COMMAND"
  72. exit 1
  73. fi
  74. $DUMP_CMD -t $PW $1 > $2
  75. }
  76. # Load the contents of the database from a file
  77. db_load() #pars: <database name> <filename>
  78. {
  79. if [ $# -ne 2 ] ; then
  80. echo "ERROR: Bug in $COMMAND"
  81. exit 1
  82. fi
  83. sql_query $1 < $2
  84. }
  85. # copy a database to database_bak
  86. db_copy() # par: <source_database> <destination_database>
  87. {
  88. if [ $# -ne 2 ] ; then
  89. echo "ERROR: Bug in $COMMAND"
  90. exit 1
  91. fi
  92. BU=/tmp/mysql_bup.$$
  93. $DUMP_CMD $PW $1 > $BU
  94. if [ "$?" -ne 0 ] ; then
  95. echo "ERROR: Failed to copy the source database"
  96. exit 1
  97. fi
  98. sql_query <<EOF
  99. CREATE DATABASE $2;
  100. EOF
  101. db_load $2 $BU
  102. if [ "$?" -ne 0 ]; then
  103. echo "ERROR: Failed to create the destination database (database exists or insuffucient permissions)"
  104. rm -f $BU
  105. exit 1
  106. fi
  107. }
  108. # Drop SER database
  109. ser_drop()
  110. {
  111. # Drop dabase
  112. # Revoke user permissions
  113. echo "Dropping SER database"
  114. sql_query < $DROP_SCRIPT
  115. } #ser_drop
  116. # Create SER database
  117. ser_create ()
  118. {
  119. echo "Creating SER database"
  120. sql_query < $CREATE_SCRIPT
  121. } # ser_create
  122. # Main program
  123. COMMAND=`basename $0`
  124. if [ -z "$DBHOST" ]; then
  125. DBHOST=$DEFAULT_DBHOST;
  126. fi
  127. if [ -z "$DBNAME" ]; then
  128. DBNAME=$DEFAULT_DBNAME;
  129. fi
  130. if [ -z "$SQLUSER" ]; then
  131. SQLUSER=$DEFAULT_SQLUSER;
  132. fi
  133. if [ -z "$MYSQL" ]; then
  134. MYSQL=$DEFAULT_MYSQL;
  135. fi
  136. if [ -z "$MYSQLDUMP" ]; then
  137. MYSQLDUMP=$DEFAULT_MYSQLDUMP;
  138. fi
  139. if [ -z "$CREATE_SCRIPT" ]; then
  140. CREATE_SCRIPT=$DEFAULT_CREATE_SCRIPT;
  141. fi
  142. if [ -z "$DROP_SCRIPT" ]; then
  143. DROP_SCRIPT=$DEFAULT_DROP_SCRIPT;
  144. fi
  145. if [ $# -eq 0 ]; then
  146. usage
  147. exit 1
  148. fi
  149. if [ ! -x $MYSQL ]; then
  150. echo "ERROR: Could not execute MySQL tool $MYSQL, please set MYSQL variable"
  151. echo " Run ($COMMAND without parameters for more information)"
  152. exit 1
  153. fi
  154. if [ ! -x $MYSQLDUMP ]; then
  155. echo "ERROR: Could not execute MySQL tool $MYSQLDUMP, please set MYSQLDUMP variable"
  156. echo " (Run $COMMAND without parameters for more information"
  157. exit 1
  158. fi
  159. CMD="$MYSQL -h$DBHOST -u$SQLUSER"
  160. DUMP_CMD="${MYSQLDUMP} -h$DBHOST -u$SQLUSER -c -a -e --add-locks --all"
  161. export PW
  162. prompt_pw
  163. if [ -z "$PW" ]; then
  164. unset PW
  165. else
  166. PW="-p$PW"
  167. fi
  168. case $1 in
  169. create) # Create SER database and users
  170. ser_create
  171. exit $?
  172. ;;
  173. drop) # Drop SER database and users
  174. ser_drop
  175. exit $?
  176. ;;
  177. backup) # backup SER database
  178. shift
  179. if [ $# -eq 1 ]; then
  180. db_save $DBNAME $1
  181. elif [ $# -eq 2 ]; then
  182. db_save $1 $2
  183. else
  184. usage
  185. exit 1
  186. fi
  187. exit $?
  188. ;;
  189. restore) # restore SER database
  190. shift
  191. if [ $# -eq 1 ]; then
  192. db_load $DBNAME $1
  193. elif [ $# -eq 2 ]; then
  194. db_load $1 $2
  195. else
  196. usage
  197. exit 1
  198. fi
  199. exit $?
  200. ;;
  201. copy)
  202. shift
  203. if [ $# -ne 2 ]; then
  204. usage
  205. exit 1
  206. fi
  207. db_copy $1 $2
  208. exit $?
  209. ;;
  210. *)
  211. usage
  212. exit 1;
  213. ;;
  214. esac