Monday, 1 June 2009

Drupal sandbox creator shell script

Hi Readers,

I'm a really bad blogger. First, I never write. Secondly, I never write about new stuffs. I have my reason and to be honest, I've seen too many very good article so I had to realise, it's not going to be a tech blog. I'm really curious about how my coming MacBook affects to my reading habit. I've got a lot of hope in it. And another big news for me. I'll be a proud cat holder? Call me crazy but she will be my first child. (Besides that if her teacher says she is the smartest in the class - I won't believe it:) Anyway. To restart this stuff I intend to share a small Drupal site generator script with you.

What is the problem?
I'm a Drupal developer. I need to test modules before using it on production sites. I need to try out a lot of things. And the best for doing that is a Drupal sandbox. But it takes 1 or 2 minutes to create one. (At least for me.) ((It should be a Drupal install contest - yeah:))

Solution?
Create a sandbox once and pre-populate it when needs a new one.

Prepaire:
  1. Download the latest Drupal install: http://drupal.org/
  2. Install it in a directory called: drupal_sandbox
  3. Install all the modules you want by default (contrib ones as well)
  4. Use the database name of: drupal_sandbox in the settings.php
  5. Tar the drupal_sandbox directory: tar -cvzf drupal_sandbox.tar.gz drupal_sandbox
  6. Create an sql dump: mysqldump -u YOURUSER --password=YOURPASS drupal_sandbox > drupal_sandbox.sql


The script:

#!/usr/bin/env bash

# Directory where the tar.gz and sql file lies
package_dir=~/download
# Base file name for the tar.gz and sql files
package_name=drupal_sandbox
# Your web root
www_dir=~/www
# Your MySQL user
mysql_pwd=YOURMYSQLUSER
mysql_user=YOURMYSQLPASSWORD

cd $www_dir
tar -xvzf $package_dir/$package_name.tar.gz
mv $package_name $1
cd $1/sites/default
mv settings.php settings.php.bckup
sed "s/$package_name/$1/g" settings.php.bckup > settings.php

mysql -u $mysql_user --password=$mysql_pwd -e "create database $1"
mysql -u $mysql_user --password=$mysql_pwd $1 < $package_dir/$package_name.sql


Usage:
Call the script with a parameter (the name of the new drupal site (directory & sql))
sh ./drupal_creator.sh drupal_site_for_testing


Thats it. I hope it works for you. On my environment its fine.

Good luck,
Peter

2 comments:

  1. Nice one, been looking for something like this for ages.

    ReplyDelete
  2. I'm glad. But check this out:
    http://cvs.drupal.org/viewvc.py/drupal/drupal/CHANGELOG.txt?revision=1.321&view=markup
    It is written for Drupal 7:
    "- Installer: * Refactored the installer into an API that allows Drupal to be installed
    via a command line script."

    ReplyDelete

Note: only a member of this blog may post a comment.