Alternc  latest
Alternc logiel libre pour l'hébergement
mysql.sh
Go to the documentation of this file.
1 #!/bin/bash
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 # * alternc_mail_user
38 # * alternc_mail_password
39 # * MYSQL_CLIENT
40 #
41 # XXX: the sed script should be generated here
42 #
43 # So this file should generally be sourced like this:
44 # . /usr/share/alternc/install/mysql.sh
45 #
46 # Those values are used to set the username/passwords...
47 
48 MYSQL_CONFIG="/etc/alternc/my.cnf"
49 MYSQL_MAIL_CONFIG="/etc/alternc/my_mail.cnf"
50 
51 # The grant all is the most important right needed in this script.
52 echo "Granting users..."
53 
54 . /etc/alternc/local.sh
55 # the purpose of this "grant" is to make sure that the generated my.cnf works
56 # this means (a) creating the user and (b) creating the database
57 grant="GRANT ALL ON *.* TO '$user'@'${MYSQL_CLIENT}' IDENTIFIED BY '$password' WITH GRANT OPTION;
58 CREATE DATABASE IF NOT EXISTS $database; "
59 grant_mail="GRANT ALL ON $database.dovecot_view TO '$alternc_mail_user'@'${MYSQL_CLIENT}' IDENTIFIED BY '$alternc_mail_password';"
60 grant_mail=$grant_mail"GRANT ALL ON $database.dovecot_quota TO '$alternc_mail_user'@'${MYSQL_CLIENT}' IDENTIFIED BY '$alternc_mail_password';"
61 grant_mail=$grant_mail"GRANT SELECT ON $database.* TO '$alternc_mail_user'@'${MYSQL_CLIENT}' IDENTIFIED BY '$alternc_mail_password';"
62 
63 
64 #if mysql_client != localhost means we are connecting to a remote server
65 #the remote sql use rshould already be configured but it is a way of confirming it.
66 if [ $MYSQL_CLIENT != "localhost" ]; then
67  mysql="/usr/bin/mysql -h$host -u$user -p$password"
68  if ! $mysql << EOF
69 $grant
70 EOF
71  then
72  echo "Fail"
73  echo "Can't grant to remote system user $user, aborting";
74  exit 1
75  fi
76 else
77  echo -n "Trying debian.cnf: "
78  mysql="/usr/bin/mysql --defaults-file=/etc/mysql/debian.cnf"
79  # If this call fail, we may be connected to a mysql-server version 5.0.
80  # In that case, change mysql parameters and retry. Use root / nopassword.
81  if ! $mysql <<EOF
82  $grant
83 EOF
84  then
85  echo "failed: debian-sys-maintainer doesn't have the right credentials"
86  echo -n "are we doing an upgrade? "
87  mysql="/usr/bin/mysql --defaults-file=$MYSQL_CONFIG"
88  if ! $mysql <<EOF
89  $grant
90 EOF
91  then
92  echo "No"
93  echo -n "Assuming clean install (empty root password)... "
94  mysql="/usr/bin/mysql -h$host -uroot "
95  if ! $mysql <<EOF
96  $grant
97 EOF
98  then
99  echo "Failed"
100  echo -n "Assuming pre 0.9.8 version... "
101  mysql="/usr/bin/mysql -h$MYSQL_HOST -u$MYSQL_USER -p$MYSQL_PASS"
102  if ! $mysql <<EOF
103  $grant
104 EOF
105  then
106  echo "No."
107  echo "Can't grant system user $user, aborting";
108  exit 1
109  fi
110  fi
111  fi
112  fi
113 fi
114 echo "ok!"
115 
116 if [ -f $MYSQL_CONFIG ]; then
117  echo "Updating mysql configuration in $MYSQL_CONFIG"
118 else
119  echo "Creating mysql configuration in $MYSQL_CONFIG"
120 fi
121 
122 if [ -f $MYSQL_MAIL_CONFIG ]; then
123  echo "Updating mysql configuration in $MYSQL_MAIL_CONFIG"
124 else
125  echo "Creating mysql configuration in $MYSQL_MAIL_CONFIG"
126 fi
127 
128 write_mysql_cnf() {
129  local filename="$1"
130  local host=$(echo "$2" | sed -e 's/["]/\\&/g')
131  local db=$(echo "$3" | sed -e 's/["]/\\&/g')
132  local user=$(echo "$4" | sed -e 's/["]/\\&/g')
133  local password=$(echo "$5" | sed -e 's/["]/\\&/g')
134  cat > "$filename" <<EOF
135 # AlternC - Web Hosting System - MySQL mail user Configuration
136 # Automatically generated by AlternC configuration, do not edit
137 # This file will be modified on package configuration
138 # (e.g. upgrade or dpkg-reconfigure alternc)
139 [mysql]
140 database="$db"
141 
142 [client]
143 host="$host"
144 user="$user"
145 password="$password"
146 EOF
147  chown root:www-data "$filename"
148  chmod 640 "$filename"
149 }
150 
151 write_mysql_cnf "$MYSQL_CONFIG" "$host" "$database" "$user" "$password"
152 write_mysql_cnf "$MYSQL_MAIL_CONFIG" "$host" "$database" "$alternc_mail_user" "$alternc_mail_password"
153 
154 # Now we should be able to use the mysql configuration
155 mysql="/usr/bin/mysql --defaults-file=$MYSQL_CONFIG"
156 mysql_mail="/usr/bin/mysql --defaults-file=$MYSQL_MAIL_CONFIG"
157 
158 echo "Checking for MySQL connectivity"
159 $mysql -e "SHOW TABLES" >/dev/null && echo "MYSQL.SH OK!" || echo "MYSQL.SH FAILED: database user setup failed"
160 # Final mysql setup: db schema
161 echo "installing AlternC schema in $database..."
162 $mysql < /usr/share/alternc/install/mysql.sql || echo "Error loading database schema, continuing..."
163 $mysql <<EOF
164 $grant_mail
165 EOF