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
|
#!/bin/bash -e
usage() {
cat <<EOF
$0 [-n NAME] [-h | -d | -b] FQDN
Creates by default a Wordpress mysql configuration depending on required fully
qualified domain name(FQDN).
Options:
-n name for the mysql user and database
-h help
-d destroy and purge
-b backup
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
BUGS: See ../TODO.Debian
EOF
}
defaultsetup() {
if [ -f $CONFIG_FILE ] ; then
echo WARNING: $CONFIG_FILE exists!
exit 1
fi
# Generate a random password
MATRIX="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
LENGTH="8"
while [ "${n:=1}" -le "$LENGTH" ]
do
DB_PASSWORD="$DB_PASSWORD${MATRIX:$(($RANDOM%${#MATRIX})):1}"
let n+=1
done
# Write the config file for wordpress
cat > $CONFIG_FILE << EOF
<?php
define('DB_NAME', '$NAME');
define('DB_USER', '$NAME');
define('DB_PASSWORD', '$DB_PASSWORD');
define('DB_HOST', 'localhost');
\$table_prefix = 'wp_';
\$server = DB_HOST;
\$loginsql = DB_USER;
\$passsql = DB_PASSWORD;
\$base = DB_NAME;
?>
EOF
# Security to address #363580
chmod 640 /etc/wordpress/config*
chgrp www-data /etc/wordpress/config*
echo $CONFIG_FILE wrote
}
create() {
# Create the database and user
# Wordpress's install.php creates the tables btw
mysql --defaults-extra-file=/etc/mysql/debian.cnf <<EOF
CREATE DATABASE $NAME;
GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP,ALTER
ON $NAME.*
TO $NAME@localhost
IDENTIFIED BY '$DB_PASSWORD';
FLUSH PRIVILEGES;
EOF
ln -s /usr/share/wordpress /srv/www/$DOMAIN
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() {
echo Destroying $NAME
prompt
mysql --defaults-extra-file=/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
rm /srv/www/$DOMAIN
}
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:hbd-" opt ; do
case "$opt" in
h) usage ; exit 0 ;;
b) BACKUP=1 ;;
d) DESTROY=1 ;;
n) NAME="$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=$1
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')
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 [ $BACKUP ] ; then
backup
exit 0
fi
if [ $DESTROY ] ; then
destroy
exit 0
fi
defaultsetup
create
|