2
0

ser_postgres.sh 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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_SCRIPT_DIR=""
  13. DEFAULT_PSQL="/usr/bin/psql"
  14. DEFAULT_PG_DUMP="/usr/bin/pg_dump"
  15. DEFAULT_CREATE_SCRIPT="pg_create.sql"
  16. DEFAULT_DATA_SCRIPT="pg_data.sql"
  17. DEFAULT_DROP_SCRIPT="pg_drop.sql"
  18. #DBHOST="localhost"
  19. usage() {
  20. cat <<EOF
  21. Usage: $COMMAND create [database]
  22. $COMMAND drop [database]
  23. $COMMAND backup [database] <file>
  24. $COMMAND restore [database] <file>
  25. Command 'create' creates database named '${DBNAME}' containing tables needed
  26. for SER and SERWeb. In addition to that two users are created, one with
  27. read/write permissions and one with read-only permissions.
  28. Command '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. Environment variables:
  38. DBHOST Hostname of the Postgres server (${DBHOST})
  39. DBNAME Default name of SER database (${DBNAME})
  40. SQLUSER Database username with administrator privileges (${SQLUSER})
  41. (Make sure that the specified user has sufficient permissions
  42. to create databases, tables, and users)
  43. PSQL Full path to mysql command (${PSQL})
  44. Report bugs to <[email protected]>
  45. EOF
  46. } #usage
  47. # Dump the contents of the database to stdout
  48. db_save()
  49. {
  50. if [ $# -ne 2 ] ; then
  51. echo "ERROR: Bug in $COMMAND"
  52. exit 1
  53. fi
  54. $DUMP_CMD $1 > $2
  55. }
  56. # Load the contents of the database from a file
  57. db_load() #pars: <database name> <filename>
  58. {
  59. if [ $# -ne 2 ] ; then
  60. echo "ERROR: Bug in $COMMAND"
  61. exit 1
  62. fi
  63. echo "CREATE DATABASE $1" | $CMD "template1"
  64. $CMD $1 < $2
  65. }
  66. # Drop SER database
  67. db_drop()
  68. {
  69. # Drop database
  70. # Revoke user permissions
  71. echo "Dropping database $1"
  72. $CMD "template1" < ${SCRIPT_DIR}/${DROP_SCRIPT}
  73. echo "DROP DATABASE $1" | $CMD "template1"
  74. }
  75. # Create SER database
  76. db_create ()
  77. {
  78. echo "Creating database $1"
  79. echo "CREATE DATABASE $1" | $CMD "template1"
  80. $CMD $1 < ${SCRIPT_DIR}/${CREATE_SCRIPT}
  81. $CMD $1 < ${SCRIPT_DIR}/${DATA_SCRIPT}
  82. }
  83. # Convert relative path to the script directory to absolute if necessary by
  84. # extracting the directory of this script and prefixing the relative path with
  85. # it.
  86. abs_script_dir()
  87. {
  88. my_dir=`dirname $0`;
  89. if [ "${SCRIPT_DIR:0:1}" != "/" ] ; then
  90. SCRIPT_DIR="${my_dir}/${SCRIPT_DIR}"
  91. fi
  92. }
  93. # Main program
  94. COMMAND=`basename $0`
  95. if [ ! -z "$DBHOST" ]; then
  96. DBHOST="-h ${DBHOST}"
  97. fi
  98. if [ -z "$DBNAME" ]; then
  99. DBNAME=$DEFAULT_DBNAME;
  100. fi
  101. if [ -z "$SQLUSER" ]; then
  102. SQLUSER=$DEFAULT_SQLUSER;
  103. fi
  104. if [ -z "$PSQL" ]; then
  105. PSQL=$DEFAULT_PSQL;
  106. fi
  107. if [ -z "$PG_DUMP" ]; then
  108. PG_DUMP=$DEFAULT_PG_DUMP;
  109. fi
  110. if [ -z "$CREATE_SCRIPT" ]; then
  111. CREATE_SCRIPT=$DEFAULT_CREATE_SCRIPT;
  112. fi
  113. if [ -z "$DATA_SCRIPT" ]; then
  114. DATA_SCRIPT=$DEFAULT_DATA_SCRIPT;
  115. fi
  116. if [ -z "$DROP_SCRIPT" ]; then
  117. DROP_SCRIPT=$DEFAULT_DROP_SCRIPT;
  118. fi
  119. if [ -z "$SCRIPT_DIR" ]; then
  120. SCRIPT_DIR=$DEFAULT_SCRIPT_DIR;
  121. fi
  122. if [ $# -eq 0 ]; then
  123. usage
  124. exit 1
  125. fi
  126. if [ ! -x $PSQL ]; then
  127. echo "ERROR: Could not execute Postgres tool $PSQL, please set PSQL variable"
  128. echo " Run ($COMMAND without parameters for more information)"
  129. exit 1
  130. fi
  131. CMD="$PSQL ${DBHOST} -U $SQLUSER"
  132. DUMP_CMD="$PG_DUMP ${DBHOST} -U $SQLUSER"
  133. abs_script_dir
  134. case $1 in
  135. create) # Create SER database and users
  136. shift
  137. if [ $# -eq 1 ]; then
  138. db_create $1
  139. elif [ $# -eq 0 ]; then
  140. db_create ${DBNAME}
  141. else
  142. usage
  143. exit 1
  144. fi
  145. exit $?
  146. ;;
  147. drop) # Drop SER database and users
  148. shift
  149. if [ $# -eq 1 ]; then
  150. db_drop $1
  151. elif [ $# -eq 0 ]; then
  152. db_drop ${DBNAME}
  153. else
  154. usage
  155. exit 1
  156. fi
  157. exit $?
  158. ;;
  159. backup) # backup SER database
  160. shift
  161. if [ $# -eq 1 ]; then
  162. db_save ${DBNAME} $1
  163. elif [ $# -eq 2 ]; then
  164. db_save $1 $2
  165. else
  166. usage
  167. exit 1
  168. fi
  169. exit $?
  170. ;;
  171. restore) # restore SER database
  172. shift
  173. if [ $# -eq 1 ]; then
  174. db_load ${DBNAME} $1
  175. elif [ $# -eq 2 ]; then
  176. db_load $1 $2
  177. else
  178. usage
  179. exit 1
  180. fi
  181. exit $?
  182. ;;
  183. *)
  184. usage
  185. exit 1;
  186. ;;
  187. esac