DrupalDork.com was shut down on September 18, 2013. This is just a snapshot of the site as it appeared then.

Posts from July 2011

How I Manage My Generic Drupal Dev Sites

On my machine, I have two local sites I use for developing and testing modules and patches: http://d6.local and http://d7.local. After manually wiping and re-creating these sites several times in between some tests, I realized I was doing it wrong and wrote bash scripts to do it for me.

Now, I have templates for each of these sites. Whenever I want to start with a fresh dev site with some basic modules installed (like Admin Menu, Admin Role, and Devel), I run a quick bash script that drops and re-creates the database, imports a SQL file, and replaces the entire webroot directory with the contents of a tarball. I did have to manually setup the site, create my admin user, and enable the modules when I first set all this up. Now, I can easily update that template when I need to - for example, when there's a new versions of the Drupal core released.

Here's my setup:

  • The webroot for the each version of Drupal are in ~/Sites/. I've got ~/Sites/d6.local and ~/Sites/d7.local, specifically.
  • My two bash scripts, deploy and recreate, both live in ~/src/templates.
  • Alse in ~/src/templates, I have a sub-directory for each version of Drupal: d6 and d7.
  • Within those version-specific sub-dirs are db.sql and files.tar, which are used as the template.

When it's time to refresh my dev site, I go into ~/src/templates and run deploy with the version I want to refresh, like so: ./deploy 6. Running this script will execute this process:

  • Drop and re-create the database. This is to make sure that I don't wind up with stray tables left behind by modules I had been working on before.
  • Import db.sql from the subdirectory for the version specific (in this case, d6).
  • Remove the webroot directory (in this case, ~/Sites/d6.local).
  • Re-create the webroot directory by expanding files.tar, also found in the version-specific subdirectory.
  • Use Drush to clear all the caches, just to be on the safe side.

Now I've got a fresh dev site at http://d6.local. As I mentioned, I created the admin user and enabled some modules when I first setup the site, so after deploying from the template the site is ready to go.

As of tonight, I can also re-create these templates from the database and file structure. My recreate bash script is similar to deploy, but in reverse: it will take a snapshot of the site that's currenly running at http://d6.local and save that as the template for future deploys. Running ./recreate 6 will execute this process:

  • Move the existing files.tar and db.sql into a backup directory, just in case.
  • Use Drush to clear all the caches.
  • Dump the database to db.sql.
  • Tar up the webroot directory into files.tar.

When new versions of the core or contrib modules come out, I just update my local copy of the site and run the recreate script.

It's a pretty handy setup, and would be easy for other people to repurpose. If you want to use these scripts (the code is below), you may need to adjust the code to fit your own setup because of assumptions I made. These were the assumptions I worked with (using Drupal 6 as the version for these examples):

  • Webroot is ~/Sites/d6.local.
  • Templates are stored in ~/src/templates/d6.
  • Database is named dev_d6.
  • Drush alias is named @d6.local, just like the webroot subdirectory.

You'll also need to modify the code to set the MySQL username and password for each line that runs MySQL.

And now for the code:

deploy

#!/bin/bash
if [ -z "$1" ]; then
echo "Usage: ./deploy drupal_version, for example: ./deploy 6"
else
versionname=d$1
deploydir=~/src/templates/$versionname
sitesdir=$versionname.local
sitesfullpath=~/Sites/$sitesdir
dbname=dev_$versionname
if [ -d "$deploydir" ]; then
echo "Re-deploying from site template"
echo "STICK AROUND - there's a sudo call in here so we need your password"
echo "Re-create the DB, so no stray tables are left behind"
mysql -uuser -ppass -e "DROP DATABASE $dbname"
mysql -user -ppass -e "CREATE DATABASE $dbname"
echo "Import DB"
mysql -uuser -ppass $dbname < $deploydir/db.sql
echo "Untar files"
cd ~/Sites
sudo rm -rf $sitesdir
tar xfz $deploydir/files.tar
echo "Clear caches"
drush @$sitesdir cache-clear all
else
echo "The deploy directory can't be found: $deploydir"
fi
fi
view raw deploy hosted with ❤ by GitHub

recreate

#!/bin/bash
if [ -z "$1" ]; then
echo "Usage: ./recreate drupal_version, for example: ./recreate 6"
else
# Prep a bunch of vars
versionname=d$1
deploydir=~/src/templates/$versionname
sitesdir=$versionname.local
sitesfullpath=~/Sites/$sitesdir
dbname=dev_$versionname
backupdir=$deploydir/backup
timestamp=`date +%Y.%m.%d-%H.%M`
# Only continue if the given version number corresponds to a site directory
if [ -d "$sitesfullpath" ]; then
echo "Re-creating template files"
echo "Backup previous save"
# Make sure the backup directory exists
if [ ! -d "$backupdir" ]; then
mkdir $backupdir
fi
mv $deploydir/db.sql $backupdir/db-$timestamp.sql
mv $deploydir/files.tar $backupdir/files-$timestamp.tar
echo "Clear caches"
drush @$sitesdir cache-clear all
echo "Dump DB"
# Dump DB
mysqldump --opt -uuser -ppass $dbname > $deploydir/db.sql
echo "Tar files"
# Change into Sites dir and re-tar the dir
cd ~/Sites
tar -pczf $deploydir/files.tar $sitesdir/
echo "All done!"
else
echo "The sites directory can't be found: $sitesfullpath"
fi
fi
view raw recreate hosted with ❤ by GitHub

Category: