#!/bin/bash # Creates a new database for Drupal usage # Generates the dbname/drupal username & password and # creates database with correct permissions. # # Notes: # MySQL db name max length is 64 characters. # MySQL username max length is 16 characters. # Defaults pRootUser="root" pRootPass="" pRootPassPrompt="Non Blank Value Causes Prompt For Password Entry" # include library file . "`dirname ${0}`/drupalLibrary.sh" printUsage() { echo -e " " echo -e "Usage:" echo -e "$0 [ -getdbname | -getusername | -getpassword ] [ -noconfirm | -nocreate ] [ -rootuser ] [ -rootpass ] [ -root | -site ] [ -dbhost ] [ -dbname ] [ -username ] [ -password ]" echo -e "where:" echo -e " " echo -e "{ -getdbname | -getusername | -getpassword } returns only one of:" echo -e "\t-getdbname -- the generated username" echo -e "\t-getusername -- the generated username" echo -e "\t-getpassword -- the generated password" echo -e "\tOnly one of these may be specified. Implies -nocreate" echo -e " " echo -e "-noconfirm supresses comfirmation request on database creation." echo -e "-nocreate supresses the actual creation of the database. Implies -noconfirm" echo -e " " echo -e " is the new site domain (e.g. foo.org)" echo -e "\tUsed to generate a root name by stripping out dots and suffix (.org, .com, etc.)" echo -e " specifies the root directly." echo -e "\tThe root is used to generate the db name and the drupal db user/password." echo -e "\tMust specify one of site name or root (unless all of -dbname, -username & -password are specified)." echo -e " " echo -e "-dbhost" echo -e "\tSpecifies the hostname of the mysql server upon which the db will be created. Defaults to 'localhost'." echo -e " " echo -e "-dbname, -username, -password specify overrides for the generated" echo -e "\tdatabase name, db username, and db password respectively." echo -e " " echo -e "-rootuser, -rootpass specify the MySQL username/password to use when creating the database." echo -e "\tThe supplied username must have permission to create databases and grant permissions." echo -e "\tdefaults are 'root'/" echo -e " " } ######### # Param handling if [ $# -eq 0 ]; then printUsage exit 0 fi echo " " # Defaults pDbHost="localhost" # Handle flags while (true); do case "${1}" in -getdbname | -getusername | -getpassword ) if [ -n "${pGetItem}" ]; then echo -e "** Can only specify one of { -getdbname | -getusername | -getpassword }. Aborting.\n" printUsage exit 1 fi pGetItem="$1" shift ;; # Process params with prompt -rootuser | -rootpass | -root | -site | -dbhost | -dbname | -username | -password ) if [ $# -lt 2 ]; then echo -e "** Must specify a parameter with '${1}'. Aborting.\n" printUsage exit 1 fi case "${1}" in -rootuser ) pRootUser="${2}" ;; -rootpass ) pRootPass="${2}" pRootPassPrompt="" ;; -root ) pRoot="${2}" ;; -site ) pSite="${2}" ;; -dbhost ) pDbHost="${2}" ;; -dbname ) pDbName="${2}" ;; -username ) pUsername="${2}" ;; -password ) pPassword="${2}" ;; esac shift; shift ;; -noconfirm ) pNoconfirm="-noconfirm" shift ;; -nocreate ) pNocreate="-nocreate" shift ;; * ) break ;; esac done # Check parameters if [ $# -ne 0 ]; then echo -e "Invalid parameters. Aborting.\n" printUsage exit 1 fi # Can only specify one of 'root' and 'site' # (don't bother checking if all the generated params are supplied...) if [ -z "${pDbName}" ] || [ -z "${pUsername}" ] || [ -z "${pPassword}" ]; then if [ -z "${pRoot}" ] && [ -z "${pSite}" ]; then echo -e "** Must specify exactly one of -root or -site. Aborting.\n" printUsage exit 1 fi if [ -n "${pRoot}" ] && [ -n "${pSite}" ]; then echo -e "Must specify exactly one of -root or -site. Aborting.\n" printUsage exit 1 fi fi # Determine root; then db name, user, pass if [ -z "${pRoot}" ]; then pRoot="`getDomainRoot "${pSite}"`" fi if [ -z "${pDbName}" ]; then pDbName="`getGeneratedDBName "${pRoot}"`" fi if [ -z "${pUsername}" ]; then pUsername="`getGeneratedDBUser "${pRoot}"`" fi if [ -z "${pPassword}" ]; then pPassword="`getGeneratedDBPass "${pRoot}"`" fi # Output one component only? if [ -n "${pGetItem}" ]; then case "${pGetItem}" in -getdbname ) echo "${pDbName}" ;; -getusername ) echo "${pUsername}" ;; -getpassword ) echo "${pPassword}" ;; esac # All done! exit 0 fi # Echo dbname/username/password here echo " " echo -e "Creating database '${pDbName}'" echo -e "\tDrupal user username/password: ${pUsername}/${pPassword}" echo " " if [ -n "${pNocreate}" ]; then echo "No Create specified. Not creating db." echo " " exit 0 fi # Determine MySQL root user password if [ -n "${pRootPassPrompt}" ]; then echo -n "Enter the MySQL password for user '${pRootUser}': " read pRootPass echo " " fi # Last chance to opt out... if [ -z "${pNoconfirm}" ]; then echo "About To Create db..." echo "Hit to continue or ^C to abort..." read echo " " fi # create db and grant permissions rootAuthString="`getMySQLAuthString "${pDbHost}" "${pRootUser}" "${pRootPass}"`" ${MYSQLADMIN} ${rootAuthString} create ${pDbName} || { echo -e "** Unable to create database.\n" exit 1 } echo "GRANT ALL PRIVILEGES ON ${pDbName}.* TO '${pUsername}'@'localhost' IDENTIFIED BY '${pPassword}'; FLUSH PRIVILEGES;" | ${MYSQL} ${rootAuthString} || { echo -e "** Database created, but unable to set permissions.\n" exit 1 } echo "Success! Database '${pDbName}' created and permissions granted." echo -e "\tYou can access your new db with the command:" echo -e "\t${MYSQL} `getMySQLConnectionString "${pDbHost}" "${pDbName}" "${pUsername}" "${pPassword}"`" echo " " # Done echo -e "\n** Done."