Script for automating Drupal installation

I do a lot of testing with Drupal, so I need a quick and easy way to create a new Drupal site, create the associated database, and get started. There are probably better solutions out there, but this is what I use. It's a bash script that I developed with the help of killes sometime last year. I just updated it for Drupal 4.7.

Now when I want to create a new Drupal site I just type

newdrupal47 foo bar

where foo is the name I'm giving the new site and bar is the name of the MySQL user (the MySQL user is optional; it defaults to root). It gets the Drupal 4.7 branch from CVS, configures the settings file, creates the database, optionally runs some local SQL, and opens Safari. Here's the script (it lost the indentation, oh well):

#!/bin/bash

if [ $# = "0" ]; then
  echo "newdrupal47: usage: newdrupal47 sitename db_user"
  exit 1;
fi

# You may want to set HOST to be your box's domain name
HOST='localhost'
DB_USER='root'
# If second argument is nonzero we were given a db_user;
# use it instead of defaulting to root
if [ -n "$2" ]; then
  DB_USER=$2
fi

# This is the location of an SQL file to run after the Drupal
# database has been given to MySQL. I use it to insert one
# line into the user roles table and one line into the user
# table, thus establishing the admin user.
LOCAL_SQL=~/newdrupal47.sql

# This is the location of your htdocs directory.
DIR="/Library/WebServer/Documents"
echo "Changing directory to $DIR"
cd $DIR

echo "Retrieving Drupal 4.7 branch..."
# Pull down drupal 4.7
cvs -z3 -d:pserver:anonymous:anonymous@cvs.drupal.org:/cvs/drupal checkout -r DRUPAL-4-7 drupal

echo "Configuring..."
# Rename the drupal site from "drupal" to whatever the first parameter was,
# e.g. newdrupal47 drupaltest results in a directory named drupaltest
mv $DIR/drupal $DIR/$1

# Make a copy of the default settings folder and name it localhost
# You may want to substitute your machine's DNS name
cp -R $DIR/$1/sites/default $DIR/$1/sites/$HOST

MYSQL_LOC=`which mysql`
if [ -x $MYSQL_LOC ]; then
  echo "Enter the database password for user '$DB_USER'"
  echo -n "Password: "
  read -s PASS

  # set local database connection
  # -i means edit file in-place
  # we search only lines 85-90 in the settings file
  sed -i '' 85,90s#username:password@localhost/databasename#$DB_USER:$PASS@localhost/$1# $DIR/$1/sites/$HOST/settings.php

  # set the base url
  sed -i  '' 108,110s#www.example.com#$HOST/$1# $DIR/$1/sites/$HOST/settings.php

  echo "Creating database..."
  mysqladmin -u$DB_USER -p$PASS create $1
  $MYSQL_LOC -u$DB_USER -p$PASS $1 < $DIR/$1/database/database.4.1.mysql
  echo "Checking for local configuration..."
  if [ -r $LOCAL_SQL ]; then
    echo "Found local configuration; executing SQL"
    $MYSQL_LOC -u$DB_USER -p$PASS $1 < $LOCAL_SQL
  fi
fi

echo "Done"
#opens Safari to the new site on OS X
open http://$HOST/$1

Comments

John, your script is great. Have you had to change it for current versions of Drupal? We're currently automating the install, and your script has been helpful.

Thanks!

Keep in mind this was written in 2006 B.D. (before Drush).