ser_postgres.sh 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #!/bin/sh
  2. #
  3. # $Id$
  4. #
  5. # Script for adding and dropping SER Postgres tables
  6. #
  7. #################################################################
  8. # config vars
  9. #################################################################
  10. DEFAULT_DBNAME="ser"
  11. DEFAULT_SQLUSER="postgres"
  12. DEFAULT_PSQL="/usr/bin/psql"
  13. DEFAULT_PG_DUMP="/usr/bin/pg_dump"
  14. DEFAULT_CREATE_SCRIPT="pg_create.sql"
  15. DEFAULT_DATA_SCRIPT="pg_data.sql"
  16. DEFAULT_DROP_SCRIPT="pg_drop.sql"
  17. #DBHOST="localhost"
  18. usage() {
  19. cat <<EOF
  20. Usage: $COMMAND create [database]
  21. $COMMAND drop [database]
  22. $COMMAND backup [database] <file>
  23. $COMMAND restore [database] <file>
  24. Command 'create' creates database named '${DBNAME}' containing tables
  25. needed for SER and SERWeb. In addition to that two users are created,
  26. one with read/write permissions and one with read-only permissions.
  27. Commmand 'drop' deletes database named '${DBNAME}' and associated users.
  28. Command 'backup' Dumps the contents of the database in <file>. If no
  29. database name is provided on the command line then the default '${DBNAME}'
  30. database will be used.
  31. Command 'restore' will load the datata previously saved with 'backup'
  32. command in the database. If no database name is provided on the command
  33. line then '${DBNAME}' database will be loaded.
  34. Note: Make sure that you have no conflicting data in the database before
  35. you execute 'restore' command.
  36. Environment variables:
  37. DBHOST Hostname of the Postgres server (${DBHOST})
  38. DBNAME Default name of SER database (${DBNAME})
  39. SQLUSER Database username with administrator privileges (${SQLUSER})
  40. (Make sure that the specified user has sufficient permissions
  41. to create databases, tables, and users)
  42. PSQL Full path to mysql command (${PSQL})
  43. Report bugs to <[email protected]>
  44. EOF
  45. } #usage
  46. # Dump the contents of the database to stdout
  47. db_save()
  48. {
  49. if [ $# -ne 2 ] ; then
  50. echo "ERROR: Bug in $COMMAND"
  51. exit 1
  52. fi
  53. $DUMP_CMD $1 > $2
  54. }
  55. # Load the contents of the database from a file
  56. db_load() #pars: <database name> <filename>
  57. {
  58. if [ $# -ne 2 ] ; then
  59. echo "ERROR: Bug in $COMMAND"
  60. exit 1
  61. fi
  62. echo "CREATE DATABASE $1" | $CMD "template1"
  63. $CMD $1 < $2
  64. }
  65. # Drop SER database
  66. db_drop()
  67. {
  68. # Drop dabase
  69. # Revoke user permissions
  70. echo "Dropping database $1"
  71. $CMD "template1" < ${DROP_SCRIPT}
  72. echo "DROP DATABASE $1" | $CMD "template1"
  73. }
  74. # Create SER database
  75. db_create ()
  76. {
  77. echo "Creating database $1"
  78. echo "CREATE DATABASE $1" | $CMD "template1"
  79. $CMD $1 < $CREATE_SCRIPT
  80. $CMD $1 < $DATA_SCRIPT
  81. }
  82. # Main program
  83. COMMAND=`basename $0`
  84. if [ ! -z "$DBHOST" ]; then
  85. DBHOST="-h ${DBHOST}"
  86. fi
  87. if [ -z "$DBNAME" ]; then
  88. DBNAME=$DEFAULT_DBNAME;
  89. fi
  90. if [ -z "$SQLUSER" ]; then
  91. SQLUSER=$DEFAULT_SQLUSER;
  92. fi
  93. if [ -z "$PSQL" ]; then
  94. PSQL=$DEFAULT_PSQL;
  95. fi
  96. if [ -z "$PG_DUMP" ]; then
  97. PG_DUMP=$DEFAULT_PG_DUMP;
  98. fi
  99. if [ -z "$CREATE_SCRIPT" ]; then
  100. CREATE_SCRIPT=$DEFAULT_CREATE_SCRIPT;
  101. fi
  102. if [ -z "$DATA_SCRIPT" ]; then
  103. DATA_SCRIPT=$DEFAULT_DATA_SCRIPT;
  104. fi
  105. if [ -z "$DROP_SCRIPT" ]; then
  106. DROP_SCRIPT=$DEFAULT_DROP_SCRIPT;
  107. fi
  108. if [ $# -eq 0 ]; then
  109. usage
  110. exit 1
  111. fi
  112. if [ ! -x $PSQL ]; then
  113. echo "ERROR: Could not execute Postgres tool $PSQL, please set PSQL variable"
  114. echo " Run ($COMMAND without parameters for more information)"
  115. exit 1
  116. fi
  117. CMD="$PSQL ${DBHOST} -U $SQLUSER"
  118. DUMP_CMD="$PG_DUMP ${DBHOST} -U $SQLUSER"
  119. case $1 in
  120. create) # Create SER database and users
  121. shift
  122. if [ $# -eq 1 ]; then
  123. db_create $1
  124. elif [ $# -eq 0 ]; then
  125. db_create ${DBNAME}
  126. else
  127. usage
  128. exit 1
  129. fi
  130. exit $?
  131. ;;
  132. drop) # Drop SER database and users
  133. shift
  134. if [ $# -eq 1 ]; then
  135. db_drop $1
  136. elif [ $# -eq 0 ]; then
  137. db_drop ${DBNAME}
  138. else
  139. usage
  140. exit 1
  141. fi
  142. exit $?
  143. ;;
  144. backup) # backup SER database
  145. shift
  146. if [ $# -eq 1 ]; then
  147. db_save ${DBNAME} $1
  148. elif [ $# -eq 2 ]; then
  149. db_save $1 $2
  150. else
  151. usage
  152. exit 1
  153. fi
  154. exit $?
  155. ;;
  156. restore) # restore SER database
  157. shift
  158. if [ $# -eq 1 ]; then
  159. db_load ${DBNAME} $1
  160. elif [ $# -eq 2 ]; then
  161. db_load $1 $2
  162. else
  163. usage
  164. exit 1
  165. fi
  166. exit $?
  167. ;;
  168. *)
  169. usage
  170. exit 1;
  171. ;;
  172. esac