drupal-setup.sh 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #!@l_bash@
  2. ##
  3. ## drupal-setup.sh -- Drupal RDBMS Setup Utility
  4. ##
  5. # command line argument sanity check
  6. prg="$0"
  7. if [ $# -eq 0 ]; then
  8. echo "$prg:ERROR: invalid command line" 1>&2
  9. echo "$prg:USAGE: $prg install [<database-directory>]" 1>&2
  10. echo "$prg:USAGE: $prg uninstall" 1>&2
  11. echo "$prg:USAGE: $prg backup [<dump-file>]" 1>&2
  12. echo "$prg:USAGE: $prg restore [<dump-file>|<dump-number>]" 1>&2
  13. exit 1
  14. fi
  15. # database configuration
  16. db_dir="@l_prefix@/var/drupal/db"
  17. db_dump="@l_prefix@/var/drupal/dump"
  18. db_type="@l_dbtype@"
  19. db_name="drupal"
  20. db_user="drupal"
  21. db_pass="drupal"
  22. # determine RDBMS-specific details
  23. if [ ".$db_type" = .mysql ]; then
  24. db_sname="mysql"
  25. db_suser=`grep "^user" @l_prefix@/etc/mysql/my.pwd |\
  26. sed -e 's;^user[^=]*= *;;' -e 's; *$;;'`
  27. db_spass=`grep "^password" @l_prefix@/etc/mysql/my.pwd |\
  28. sed -e 's;^password[^=]*= *;;' -e 's; *$;;'`
  29. elif [ ".$db_type" = .pgsql ]; then
  30. db_sname=`grep "^superuser_database" @l_prefix@/var/postgresql/db/pg_superuser.conf |\
  31. sed -e 's;^ *superuser_database="\(.*\)".*;\1;'`
  32. db_suser=`grep "^superuser_username" @l_prefix@/var/postgresql/db/pg_superuser.conf |\
  33. sed -e 's;^ *superuser_username="\(.*\)".*;\1;'`
  34. db_spass=`grep "^superuser_password" @l_prefix@/var/postgresql/db/pg_superuser.conf |\
  35. sed -e 's;^ *superuser_password="\(.*\)".*;\1;'`
  36. fi
  37. # dispatch operation
  38. cmd="${1:-"install"}"
  39. shift
  40. case "$cmd" in
  41. install )
  42. ##
  43. ## create the database
  44. ##
  45. if [ $# -gt 0 ]; then
  46. db_dir="$1"
  47. shift
  48. fi
  49. if [ ".$db_type" = .mysql ]; then
  50. # FIXME: MySQL 5.0 still doesn't allow easy relocation of tablespaces
  51. @l_prefix@/bin/mysqladmin --user="$db_suser" --password="$db_spass" create "$db_name"
  52. ( echo "GRANT ALL PRIVILEGES ON $db_name.* TO $db_user@localhost IDENTIFIED BY '$db_pass';"
  53. echo "FLUSH PRIVILEGES;"
  54. ) | @l_prefix@/bin/mysql --user="$db_suser" --password="$db_spass" "$db_sname"
  55. elif [ ".$db_type" = .pgsql ]; then
  56. ( echo "CREATE ROLE $db_user LOGIN ENCRYPTED PASSWORD '$db_pass' NOCREATEDB NOCREATEUSER;"
  57. echo "CREATE TABLESPACE $db_name OWNER $db_user LOCATION '$db_dir';"
  58. echo "CREATE DATABASE $db_name OWNER $db_user TABLESPACE $db_name ENCODING 'UTF8';"
  59. ) | PGPASSWORD="$db_spass" @l_prefix@/bin/psql -q -U "$db_suser" -d "$db_sname" -f-
  60. fi
  61. # activate configuration directory
  62. # (is automatically deactivated by installer afterwards)
  63. if [ ".$DRUPAL_SETUP_RESTORE" = . ]; then
  64. chmod 777 @l_prefix@/share/drupal/sites/default
  65. fi
  66. ;;
  67. uninstall )
  68. ##
  69. ## remove the database
  70. ##
  71. if [ ".$db_type" = .mysql ]; then
  72. ( echo "DROP DATABASE $db_name;"
  73. ) | @l_prefix@/bin/mysql --user="$db_suser" --password="$db_spass" "$db_sname"
  74. elif [ ".$db_type" = .pgsql ]; then
  75. ( echo "DROP DATABASE $db_name;"
  76. echo "DROP TABLESPACE $db_name;"
  77. echo "DROP ROLE $db_user;"
  78. ) | PGPASSWORD="$db_spass" @l_prefix@/bin/psql -q -U "$db_suser" -d "$db_sname" -f-
  79. fi
  80. # remove the generated configuration
  81. if [ ".$DRUPAL_SETUP_RESTORE" = . ]; then
  82. rm -f @l_prefix@/share/drupal/sites/default/settings.php
  83. fi
  84. ;;
  85. backup )
  86. ##
  87. ## backup the database
  88. ##
  89. # determine dumpfile
  90. if [ $# -gt 0 ]; then
  91. # manually managed dumpfile
  92. dumpfile="$1"
  93. else
  94. # automatically managed (rotated) dumpfile
  95. rm -f $db_dump/drupal-dump-9.sql.gz >/dev/null 2>&1 || true
  96. i=8
  97. while [ $i -ge 0 ]; do
  98. if [ -f $db_dump/drupal-dump-$i.sql.bz2 ]; then
  99. mv $db_dump/drupal-dump-$i.sql.bz2 $db_dump/drupal-dump-`expr $i + 1`.sql.bz2
  100. fi
  101. i=`expr $i - 1`
  102. done
  103. dumpfile="$db_dump/drupal-dump-0.sql.bz2"
  104. fi
  105. # dump database content to dumpfile (compressed plain SQL format)
  106. umask 007
  107. if [ ".$db_type" = .mysql ]; then
  108. @l_prefix@/bin/mysqldump --user="$db_suser" --password="$db_spass" "$db_name" | \
  109. @l_prefix@/lib/openpkg/bzip2 -9 >$dumpfile
  110. elif [ ".$db_type" = .pgsql ]; then
  111. PGPASSWORD="$db_spass" @l_prefix@/bin/pg_dump -U "$db_suser" -S "$db_suser" "$db_name" | \
  112. @l_prefix@/lib/openpkg/bzip2 -9 >$dumpfile
  113. fi
  114. chown @l_rusr@:@l_rgrp@ $dumpfile >/dev/null 2>&1 || true
  115. chmod 640 $dumpfile >/dev/null 2>&1 || true
  116. ;;
  117. restore )
  118. ##
  119. ## restore the database
  120. ##
  121. # determine dumpfile
  122. if [ $# -gt 0 ]; then
  123. dumpfile="$1"
  124. else
  125. dumpfile="0"
  126. fi
  127. case "$dumpfile" in
  128. [0-9] ) dumpfile="$db_dump/drupal-dump-$dumpfile.sql.bz2" ;;
  129. esac
  130. if [ ! -f $dumpfile ]; then
  131. echo "drupal-setup:restore:ERROR: no such dump file: $dumpfile" 1>&2
  132. exit 1
  133. fi
  134. # optionally stop Drupal
  135. eval `@l_prefix@/bin/openpkg rc drupal status 2>/dev/null || true`
  136. if [ ".$drupal_active" = .yes ]; then
  137. @l_prefix@/bin/openpkg rc drupal stop >/dev/null 2>&1 || true
  138. fi
  139. # drop old and initialize new database
  140. DRUPAL_SETUP_RESTORE=1
  141. export DRUPAL_SETUP_RESTORE
  142. $prg uninstall || exit $?
  143. $prg install || exit $?
  144. # restore database content from dumpfile (compressed plain SQL format)
  145. if [ ".$db_type" = .mysql ]; then
  146. @l_prefix@/lib/openpkg/bzip2 -d -c $dumpfile | \
  147. @l_prefix@/bin/mysql --user="$db_suser" --password="$db_spass" "$db_name" >/dev/null
  148. elif [ ".$db_type" = .pgsql ]; then
  149. @l_prefix@/lib/openpkg/bzip2 -d -c $dumpfile | \
  150. PGPASSWORD="$db_spass" @l_prefix@/bin/psql -q -U "$db_suser" -d "$db_name" -f- >/dev/null
  151. fi
  152. # optionally restart Drupal
  153. if [ ".$drupal_active" = .yes ]; then
  154. @l_prefix@/bin/openpkg rc drupal start >/dev/null 2>&1 || true
  155. fi
  156. ;;
  157. esac