Drupal Site Reset BASH Script

I'm experimenting with Drupal 8 and some its new features like migration and configuration management. A reset script is a convenient timesaver and many people have shared their techniques for doing so. Having benefited from other's generosity I wanted to return the favour by sharing my current work-in-progress.

This script:

  • Leaves the codebase as-is
  • Deletes and reinstalls the database
  • Deletes the 'files' directory
  • Deletes the specified configuration management directory
  • Enables additional modules, as required
  • Deletes settings.php and allows Drupal install to recreate
  • Updates the new settings.php $config_directories['sync'] entry
  • Adds a settings-migrate.php include to settings.php

I call the script via Drush with an entry in drushrc.php:

$options['shell-aliases']['428-reset'] = '!sh /Users/dale/.drush/sha-scripts/g428-reset.sh';

The script is evolving as I learn more about Drupal 8 and refine my workflow. Comments and suggestions are welcome.

drupal-reset.sh:

#!/bin/bash

# Reinstall a Drupal instance to reset it back to a know state.
# A file base and Drush alias must already be configured.

DRUSH8='/Users/dale/bin/drush8/vendor/bin/drush'
DRUPALDIR='/Users/dale/Sites/group428'
CONFIGDIR='sites/default/group42config/sync'
DRUSHID='@g428'
SITE_NAME='Group 428'
ACCOUNT='admin'
PASS='staring-password'
EMAIL='no-reply@group42.ca'
DB_URL='mysql://group428:group428@localhost/group428'


# Nuke the database
$DRUSH8 $DRUSHID sql-drop --yes

# Nuke the filebase
echo "Resetting files"
chmod -R u+w $DRUPALDIR/*
rm $DRUPALDIR/sites/default/settings.php
rm -r $DRUPALDIR/sites/default/files
rm -r $DRUPALDIR/$CONFIGDIR

# Fresh Drupal install
cd $DRUPALDIR
$DRUSH8 site-install standard --db-url=$DB_URL --site-name=$SITE_NAME --account-name=$ACCOUNT --account-pass=$PASS --account-mail=$EMAIL --yes

# Base configuration
$DRUSH8 $DRUSHID en admin_toolbar,admin_toolbar_tools --yes

# Allow upcoming changes to settings.php
chmod u+w $DRUPALDIR/sites/default
chmod u+w $DRUPALDIR/sites/default/settings.php

# Configuration Management
sed -i '' "/config\_directories\['sync'\]/d" $DRUPALDIR/sites/default/settings.php
echo "\$config_directories['sync'] = '$CONFIGDIR';" >> $DRUPALDIR/sites/default/settings.php

# Migrate
echo "\ninclude 'settings-migrate.php';" >> $DRUPALDIR/sites/default/settings.php
$DRUSH8 $DRUSHID en migrate,migrate_drupal,migrate_plus,migrate_tools,migrate_upgrade --yes

# Login
$DRUSH8 $DRUSHID uli
AttachmentSize
File Drupal Reset BASH Script1.43 KB

Comments

Hi Dale,

with a few changes this could be a nice addition to my drupal-init-tools, check it out.

It could be a reinstall sub-command reusing the values in the bootstrap.conf file.

If you find drupal-init-tools useful please get in touch via email.

Ciao,
Antonio