limesurvey-setup.sh 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #!@l_bash@
  2. ##
  3. ## limesurvey-setup.sh -- LimeSurvey 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/limesurvey/db"
  17. db_type="@l_dbtype@"
  18. db_name="limesurvey"
  19. db_user="limesurvey"
  20. db_pass="limesurvey"
  21. # determine RDBMS-specific details
  22. if [ ".$db_type" = .mysql ]; then
  23. db_sname="mysql"
  24. db_suser=`grep "^user" @l_prefix@/etc/mysql/my.pwd |\
  25. sed -e 's;^user[^=]*= *;;' -e 's; *$;;'`
  26. db_spass=`grep "^password" @l_prefix@/etc/mysql/my.pwd |\
  27. sed -e 's;^password[^=]*= *;;' -e 's; *$;;'`
  28. elif [ ".$db_type" = .pgsql ]; then
  29. db_sname=`grep "^superuser_database" @l_prefix@/var/postgresql/db/pg_superuser.conf |\
  30. sed -e 's;^ *superuser_database="\(.*\)".*;\1;'`
  31. db_suser=`grep "^superuser_username" @l_prefix@/var/postgresql/db/pg_superuser.conf |\
  32. sed -e 's;^ *superuser_username="\(.*\)".*;\1;'`
  33. db_spass=`grep "^superuser_password" @l_prefix@/var/postgresql/db/pg_superuser.conf |\
  34. sed -e 's;^ *superuser_password="\(.*\)".*;\1;'`
  35. fi
  36. # dispatch operation
  37. cmd="${1:-"install"}"
  38. shift
  39. case "$cmd" in
  40. install )
  41. ##
  42. ## create the database
  43. ##
  44. if [ $# -gt 0 ]; then
  45. db_dir="$1"
  46. shift
  47. fi
  48. if [ ".$db_type" = .mysql ]; then
  49. # FIXME: MySQL 5.0 still doesn't allow easy relocation of tablespaces
  50. @l_prefix@/bin/mysqladmin --user="$db_suser" --password="$db_spass" create "$db_name"
  51. ( echo "GRANT ALL PRIVILEGES ON $db_name.* TO $db_user@localhost IDENTIFIED BY '$db_pass';"
  52. echo "FLUSH PRIVILEGES;"
  53. ) | @l_prefix@/bin/mysql --user="$db_suser" --password="$db_spass" "$db_sname"
  54. elif [ ".$db_type" = .pgsql ]; then
  55. ( echo "CREATE ROLE $db_user LOGIN ENCRYPTED PASSWORD '$db_pass' NOCREATEDB NOCREATEUSER;"
  56. echo "CREATE TABLESPACE $db_name OWNER $db_user LOCATION '$db_dir';"
  57. echo "CREATE DATABASE $db_name OWNER $db_user TABLESPACE $db_name ENCODING 'UTF8';"
  58. ) | PGPASSWORD="$db_spass" @l_prefix@/bin/psql -q -U "$db_suser" -d "$db_sname" -f-
  59. fi
  60. ;;
  61. uninstall )
  62. ##
  63. ## remove the database
  64. ##
  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. ;;
  75. esac