drupal-setup.sh 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. if [ ".$db_type" = .mysql ]; then
  107. @l_prefix@/bin/mysqldump --user="$db_suser" --password="$db_spass" "$db_name" | \
  108. @l_prefix@/lib/openpkg/bzip2 -9 >$dumpfile
  109. elif [ ".$db_type" = .pgsql ]; then
  110. PGPASSWORD="$db_spass" @l_prefix@/bin/pg_dump -U "$db_suser" -S "$db_suser" "$db_name" | \
  111. @l_prefix@/lib/openpkg/bzip2 -9 >$dumpfile
  112. fi
  113. ;;
  114. restore )
  115. ##
  116. ## restore the database
  117. ##
  118. # determine dumpfile
  119. if [ $# -gt 0 ]; then
  120. dumpfile="$1"
  121. else
  122. dumpfile="0"
  123. fi
  124. case "$dumpfile" in
  125. [0-9] ) dumpfile="$db_dump/drupal-dump-$dumpfile.sql.bz2" ;;
  126. esac
  127. if [ ! -f $dumpfile ]; then
  128. echo "drupal-setup:restore:ERROR: no such dump file: $dumpfile" 1>&2
  129. exit 1
  130. fi
  131. # optionally stop Drupal
  132. eval `@l_prefix@/bin/openpkg rc drupal status >/dev/null 2>&1 || true`
  133. if [ ".$drupal_active" = .yes ]; then
  134. @l_prefix@/bin/openpkg rc drupal stop >/dev/null 2>&1 || true
  135. fi
  136. # drop old and initialize new database
  137. DRUPAL_SETUP_RESTORE=1
  138. export DRUPAL_SETUP_RESTORE
  139. $prg uninstall || exit $?
  140. $prg install || exit $?
  141. # restore database content from dumpfile (compressed plain SQL format)
  142. if [ ".$db_type" = .mysql ]; then
  143. @l_prefix@/lib/openpkg/bzip2 -d -c $dumpfile | \
  144. @l_prefix@/bin/mysql --user="$db_suser" --password="$db_spass" "$db_name" >/dev/null
  145. elif [ ".$db_type" = .pgsql ]; then
  146. @l_prefix@/lib/openpkg/bzip2 -d -c $dumpfile | \
  147. PGPASSWORD="$db_spass" @l_prefix@/bin/psql -q -U "$db_suser" -d "$db_name" -f- >/dev/null
  148. fi
  149. # optionally restart Drupal
  150. if [ ".$drupal_active" = .yes ]; then
  151. @l_prefix@/bin/openpkg rc drupal start >/dev/null 2>&1 || true
  152. fi
  153. ;;
  154. esac