root/alternc/trunk/install/mysql.sh
| Revision 2169, 4.7 kB (checked in by anarcat, 9 months ago) | |
|---|---|
| |
| Line | |
|---|---|
| 1 | #!/bin/sh |
| 2 | # |
| 3 | # $Id: mysql.sh,v 1.11 2006/01/11 22:51:28 anarcat Exp $ |
| 4 | # ---------------------------------------------------------------------- |
| 5 | # AlternC - Web Hosting System |
| 6 | # Copyright (C) 2002 by the AlternC Development Team. |
| 7 | # http://alternc.org/ |
| 8 | # ---------------------------------------------------------------------- |
| 9 | # Based on: |
| 10 | # Valentin Lacambre's web hosting softwares: http://altern.org/ |
| 11 | # ---------------------------------------------------------------------- |
| 12 | # LICENSE |
| 13 | # |
| 14 | # This program is free software; you can redistribute it and/or |
| 15 | # modify it under the terms of the GNU General Public License (GPL) |
| 16 | # as published by the Free Software Foundation; either version 2 |
| 17 | # of the License, or (at your option) any later version. |
| 18 | # |
| 19 | # This program is distributed in the hope that it will be useful, |
| 20 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | # GNU General Public License for more details. |
| 23 | # |
| 24 | # To read the license please visit http://www.gnu.org/copyleft/gpl.html |
| 25 | # ---------------------------------------------------------------------- |
| 26 | # Original Author of file: Benjamin Sonntag |
| 27 | # Purpose of file: Install a fresh new mysql database system |
| 28 | # USAGE : "mysql.sh loginroot passroot systemdb" |
| 29 | # ---------------------------------------------------------------------- |
| 30 | # |
| 31 | |
| 32 | # This script expects the following environment to exist: |
| 33 | # * host |
| 34 | # * user |
| 35 | # * password |
| 36 | # * database |
| 37 | # * MYSQL_CLIENT |
| 38 | # |
| 39 | # XXX: the sed script should be generated here |
| 40 | # |
| 41 | # So this file should generally be sourced like this: |
| 42 | # . /usr/share/alternc/install/mysql.sh |
| 43 | # |
| 44 | # Those values are used to set the username/passwords... |
| 45 | |
| 46 | # The grant all is the most important right needed in this script. |
| 47 | echo "Granting users..." |
| 48 | # cat <<EOF |
| 49 | # host: $host |
| 50 | # user: $user |
| 51 | # password: $password |
| 52 | # database: $database |
| 53 | # EOF |
| 54 | |
| 55 | MYSQL_CONFIG="/etc/alternc/my.cnf" |
| 56 | |
| 57 | . /etc/alternc/local.sh |
| 58 | # the purpose of this "grant" is to make sure that the generated my.cnf works |
| 59 | # this means (a) creating the user and (b) creating the database |
| 60 | grant="GRANT ALL ON *.* TO '$user'@'${MYSQL_CLIENT}' IDENTIFIED BY '$password' WITH GRANT OPTION; |
| 61 | CREATE DATABASE IF NOT EXISTS $database;" |
| 62 | |
| 63 | echo -n "Trying debian.cnf: " |
| 64 | mysql="/usr/bin/mysql --defaults-file=/etc/mysql/debian.cnf" |
| 65 | # If this call fail, we may be connected to a mysql-server version 5.0. |
| 66 | # In that case, change mysql parameters and retry. Use root / nopassword. |
| 67 | if ! $mysql <<EOF |
| 68 | $grant |
| 69 | EOF |
| 70 | then |
| 71 | echo "failed: debian-sys-maintainer doesn't have the right credentials" |
| 72 | echo -n "are we doing an upgrade? " |
| 73 | mysql="/usr/bin/mysql --defaults-file=$MYSQL_CONFIG" |
| 74 | if ! $mysql <<EOF |
| 75 | $grant |
| 76 | EOF |
| 77 | then |
| 78 | echo "No" |
| 79 | echo -n "Assuming clean install (empty root password)... " |
| 80 | mysql="/usr/bin/mysql -h$host -uroot " |
| 81 | if ! $mysql <<EOF |
| 82 | $grant |
| 83 | EOF |
| 84 | then |
| 85 | echo "Failed" |
| 86 | echo -n "Assuming pre 0.9.8 version... " |
| 87 | mysql="/usr/bin/mysql -h$MYSQL_HOST -u$MYSQL_USER -p$MYSQL_PASS" |
| 88 | if ! $mysql <<EOF |
| 89 | $grant |
| 90 | EOF |
| 91 | then |
| 92 | echo "No." |
| 93 | echo "Can't grant system user $user, aborting"; |
| 94 | exit 1 |
| 95 | fi |
| 96 | fi |
| 97 | fi |
| 98 | fi |
| 99 | echo "ok!" |
| 100 | |
| 101 | if [ -f $MYSQL_CONFIG ]; then |
| 102 | echo "Updating mysql configuration in $MYSQL_CONFIG" |
| 103 | else |
| 104 | echo "Creating mysql configuration in $MYSQL_CONFIG" |
| 105 | cat > $MYSQL_CONFIG <<EOF |
| 106 | # AlternC - Web Hosting System - MySQL Configuration |
| 107 | # Automatically generated by AlternC configuration, do not edit |
| 108 | # This file will be modified on package configuration |
| 109 | # (e.g. upgrade or dpkg-reconfigure alternc) |
| 110 | [mysql] |
| 111 | database= |
| 112 | |
| 113 | [client] |
| 114 | EOF |
| 115 | chown root:www-data $MYSQL_CONFIG |
| 116 | chmod 640 $MYSQL_CONFIG |
| 117 | fi |
| 118 | |
| 119 | # create a sed script to create/update the file |
| 120 | set_value() { |
| 121 | var=$1 |
| 122 | RET=$2 |
| 123 | grep -Eq "^ *$var=" $MYSQL_CONFIG || echo "$var=" >> $MYSQL_CONFIG |
| 124 | SED_SCRIPT="$SED_SCRIPT;s\\^ *$var=.*\\$var=\"$RET\"\\" |
| 125 | } |
| 126 | |
| 127 | SED_SCRIPT="" |
| 128 | # hostname was empty in older (pre-0.9.6?) versions |
| 129 | if [ -z "$host" ]; then |
| 130 | host="localhost" |
| 131 | fi |
| 132 | set_value host $host |
| 133 | set_value database $database |
| 134 | set_value user $user |
| 135 | set_value password $password |
| 136 | |
| 137 | # take extra precautions here with the mysql password: |
| 138 | # put the sed script in a temporary file |
| 139 | SED_SCRIPT_NAME=`mktemp` |
| 140 | cat > $SED_SCRIPT_NAME <<EOF |
| 141 | $SED_SCRIPT |
| 142 | EOF |
| 143 | sed -f "$SED_SCRIPT_NAME" < $MYSQL_CONFIG > $MYSQL_CONFIG.$$ |
| 144 | mv -f $MYSQL_CONFIG.$$ $MYSQL_CONFIG |
| 145 | rm -f $SED_SCRIPT_NAME |
| 146 | |
| 147 | # Now we should be able to use the mysql configuration |
| 148 | mysql="/usr/bin/mysql --defaults-file=$MYSQL_CONFIG" |
| 149 | |
| 150 | echo "Checking for MySQL connectivity" |
| 151 | $mysql -e "SHOW TABLES" >/dev/null && echo "MYSQL.SH OK!" || echo "MYSQL.SH FAILED!" |
| 152 | |
| 153 | # Final mysql setup: db schema |
| 154 | echo "installing AlternC schema in $database..." |
| 155 | $mysql < /usr/share/alternc/install/mysql.sql || echo cannot load database schema |
| 156 |
Note: See TracBrowser for help on using the browser.
