drupal-setup.sh 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. # create the database
  43. if [ $# -gt 0 ]; then
  44. db_dir="$1"
  45. shift
  46. fi
  47. if [ ".$db_type" = .mysql ]; then
  48. # FIXME: MySQL 5.0 still doesn't allow easy relocation of tablespaces
  49. @l_prefix@/bin/mysqladmin --user="$db_suser" --password="$db_spass" create "$db_name"
  50. ( echo "GRANT ALL PRIVILEGES ON $db_name.* TO $db_user@localhost IDENTIFIED BY '$db_pass';"
  51. echo "FLUSH PRIVILEGES;"
  52. ) | @l_prefix@/bin/mysql --user="$db_suser" --password="$db_spass" "$db_sname"
  53. elif [ ".$db_type" = .pgsql ]; then
  54. ( echo "CREATE ROLE $db_user LOGIN ENCRYPTED PASSWORD '$db_pass' NOCREATEDB NOCREATEUSER;"
  55. echo "CREATE TABLESPACE $db_name OWNER $db_user LOCATION '$db_dir';"
  56. echo "CREATE DATABASE $db_name OWNER $db_user TABLESPACE $db_name ENCODING 'UTF8';"
  57. ) | PGPASSWORD="$db_spass" @l_prefix@/bin/psql -q -U "$db_suser" -d "$db_sname" -f-
  58. fi
  59. # activate configuration directory
  60. # (is automatically deactivated by installer afterwards)
  61. chmod 777 @l_prefix@/share/drupal/sites/default
  62. ;;
  63. uninstall )
  64. # remove the database
  65. if [ ".$db_type" = .mysql ]; then
  66. ( echo "DROP DATABASE $db_name;"
  67. ) | @l_prefix@/bin/mysql --user="$db_suser" --password="$db_spass" "$db_sname"
  68. elif [ ".$db_type" = .pgsql ]; then
  69. ( echo "DROP DATABASE $db_name;"
  70. echo "DROP TABLESPACE $db_name;"
  71. echo "DROP ROLE $db_user;"
  72. ) | PGPASSWORD="$db_spass" @l_prefix@/bin/psql -q -U "$db_suser" -d "$db_sname" -f-
  73. fi
  74. # remove the generated configuration
  75. rm -f @l_prefix@/share/drupal/sites/default/settings.php
  76. ;;
  77. backup )
  78. # backup the database
  79. if [ $# -gt 0 ]; then
  80. dumpfile="$1"
  81. else
  82. rm -f $db_dump/drupal-dump-9.sql.gz >/dev/null 2>&1 || true
  83. i=8
  84. while [ $i -ge 0 ]; do
  85. if [ -f $db_dump/drupal-dump-$i.sql.bz2 ]; then
  86. mv $db_dump/drupal-dump-$i.sql.bz2 $db_dump/drupal-dump-`expr $i + 1`.sql.bz2
  87. fi
  88. i=`expr $i - 1`
  89. done
  90. dumpfile="$db_dump/drupal-dump-0.sql.bz2"
  91. fi
  92. if [ ".$db_type" = .mysql ]; then
  93. @l_prefix@/bin/mysqldump --user="$db_suser" --password="$db_spass" "$db_name" | \
  94. @l_prefix@/lib/openpkg/bzip2 -9 >$dumpfile
  95. elif [ ".$db_type" = .pgsql ]; then
  96. PGPASSWORD="$db_spass" @l_prefix@/bin/pq_dump -U "$db_suser" "$db_sname" -f- | \
  97. @l_prefix@/lib/openpkg/bzip2 -9 >$dumpfile
  98. fi
  99. ;;
  100. restore )
  101. # restore the database
  102. if [ $# -gt 0 ]; then
  103. dumpfile="$1"
  104. else
  105. dumpfile="0"
  106. fi
  107. case "$dumpfile" in
  108. [0-9] ) dumpfile="$db_dump/drupal-dump-$dumpfile.sql.bz2" ;;
  109. esac
  110. if [ ! -f $dumpfile ]; then
  111. echo "drupal-setup:restore:ERROR: no such dump file: $dumpfile" 1>&2
  112. exit 1
  113. fi
  114. $prg uninstall || exit $?
  115. $prg install || exit $?
  116. if [ ".$db_type" = .mysql ]; then
  117. @l_prefix@/lib/openpkg/bzip2 -d -c $dumpfile | \
  118. @l_prefix@/bin/mysql --user="$db_suser" --password="$db_spass" "$db_sname"
  119. elif [ ".$db_type" = .pgsql ]; then
  120. @l_prefix@/lib/openpkg/bzip2 -d -c $dumpfile | \
  121. PGPASSWORD="$db_spass" @l_prefix@/bin/psql -q -U "$db_suser" -d "$db_sname" -f-
  122. fi
  123. ;;
  124. esac