summaryrefslogtreecommitdiffstats
path: root/debian/setup-mysql
blob: b2110dedfedd6e8687c3279c9591efbdd3a86fb2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/bin/bash -e

usage() {
cat <<EOF
$0 [-h | -d | -b] [-n NAME | -e DB Name] [-u MySQL user] [-t MySQL host] FQDN

Creates by default a Wordpress mysql configuration depending on required fully
qualified domain name(FQDN).

Options:
    -n name for the wordpress site; see also -e below
    -h help
    -d destroy and purge
    -b backup
    -u mysql username, will require password
    -t mysql server hostname, if unset localhost will be used
    -e existing empty mysql database name; will replace -n

Example: You want your blog to be served from http://blog.example.com
         for user 'wordpress'.

Then run:
sudo bash setup-mysql -n wordpress blog.example.com

EOF
}

checkforexistingsetup() {
if [ -f $CONFIG_FILE ] ; then
    echo WARNING: $CONFIG_FILE exists!
    exit 1
fi

# Generate a random password without Perl
MATRIX="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
if [ ! $MYSQLUSER ]; then
    LENGTH="8"
    while [ "${n:=1}" -le "$LENGTH" ]
    do
        DB_PASSWORD="$DB_PASSWORD${MATRIX:$(($RANDOM%${#MATRIX})):1}"
        let n+=1
    done
    DB_USER=$NAME
else
    DB_USER=$MYSQLUSER
    echo "Enter MySQL password for user $DB_USER."
    read -p "> " DB_PASSWORD
fi

LENGTH="50"
while [ "${n:=1}" -le "$LENGTH" ]
do
    SECRET_KEY="$SECRET_KEY${MATRIX:$(($RANDOM%${#MATRIX})):1}"
    let n+=1
done

if [ ! $DB_DB ]; then
    DB_NAME=$NAME
else
    DB_NAME=$DB_DB
fi

# Write the config file for wordpress
(umask 0027;
cat > $CONFIG_FILE << EOF
<?php
# Created by $0 $@
define('DB_NAME', '$DB_NAME');
define('DB_USER', '$DB_USER');
define('DB_PASSWORD', '$DB_PASSWORD');
define('DB_HOST', '$DB_HOST');
define('SECRET_KEY', '$SECRET_KEY');
define('WP_CONTENT_DIR', '$CONTENT');
define('FS_METHOD', 'direct');
?>
EOF
)
chgrp www-data "${CONFIG_FILE}"
echo $CONFIG_FILE written
}

create() {
# Create the database and user
# Wordpress's install.php creates the tables btw
if [ ! $DB_DB ]; then
    MYSQLCOMMAND="mysql -u $DB_USER -p -h $DB_HOST"
    $MYSQLCOMMAND <<EOF
CREATE DATABASE $NAME;
GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP,ALTER
ON $DB_NAME.*
TO $DB_USER@$DB_HOST
IDENTIFIED BY '$DB_PASSWORD';
FLUSH PRIVILEGES;
EOF
fi

echo Goto http://$DOMAIN to setup Wordpress
}

backup() {
echo Enter root password for mysql:
BACKUPFILE=/tmp/blog-$NAME.bak.sql.bz2
mysqldump --add-drop-table -u root -p $NAME | bzip2 -c > $BACKUPFILE && echo Wrote $BACKUPFILE
}

destroy() {
# The destroy method need root to be able to drop databases and such.
# People using complex setups with remote servers probably know how to
# cleanup after the user any way.
if [ ! -e /etc/mysql/debian.cnf ]; then
    echo "Default mysql settings not available. Manual clean up is required."
    exit 1
fi
echo Destroying $NAME
prompt
mysql /etc/mysql/debian.cnf <<EOF
CONNECT mysql;
delete from user where user='$NAME';
DELETE FROM db WHERE User = '$NAME';
DELETE FROM tables_priv WHERE User = '$NAME';
DELETE FROM columns_priv WHERE User = '$NAME';
FLUSH PRIVILEGES ;
DROP DATABASE IF EXISTS $NAME;
EOF
rm $CONFIG_FILE
}

prompt() {
while true; do
  echo -n "Are you sure? (y/n) "
  read yn
  case $yn in
    y* | Y* ) break ;;
    [nN]* )   exit 0 ; break ;;
    * ) echo "unknown response.  Asking again" ;;
  esac
done
}


while getopts "n:hbdu:t:e:-" opt ; do
    case "$opt" in
    h) usage ; exit 0 ;;
    b) BACKUP=1 ;;
    d) DESTROY=1 ;;
    n) NAME="$OPTARG" ;;
    u) MYSQLUSER="$OPTARG" ;;
    t) DB_HOST="$OPTARG" ;;
    e) DB_DB="$OPTARG" ;;
    -) break ;;
    *) usage 1>&2 ; exit 1 ;;
    esac
done
shift $(($OPTIND - 1))

if [ "$(id -u)" != "0" ]
then
  echo "You must be root to use this script."
  exit 1
fi

DOMAIN=$(echo $1 | tr "[:upper:]" "[:lower:]")
CONTENT="/var/lib/wordpress/wp-content"

if [ $DOMAIN ] ; then
ping -c 1 `echo $DOMAIN | sed 's/:[0-9]\+//'` || exit 1
else
    usage
    exit 0
fi

CONFIG_FILE=/etc/wordpress/config-$DOMAIN.php

if [ ! $NAME ] ; then
NAME=$(echo $DOMAIN | sed 's,\.,,g;s,-,,g')
echo Constructed database name and user: $NAME from $DOMAIN
fi

if [ ${#NAME} -gt 16 ]; then
echo "$NAME is longer than MySQL's limit of 16 characters. Please specify a shorter one."
exit 1
fi

if [ ! $DB_HOST ]; then
    DB_HOST="localhost"
fi

if [ $BACKUP ] ; then
backup
exit 0
fi

if [ $DESTROY ] ; then
destroy
exit 0
fi

checkforexistingsetup
create