ser_postgres.sh 4.1 KB

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