Skip to Content

Live, Test, and Dev for (Nearly) Everyone

In the instructions below, all instances of the text "mysite" should be replaced with some appropriate label for your own site. (The label should be relatively short, but still indicate what site it is. For example, for eggs.stanford.edu, the lable might be "eggs"; for fluffybunnies.com, it might be "fluffyb".)

Set up live, test, dev sites

For each site, try to use directory and database names that indicate both the site and whether it is live, test, or dev. For example:

  • Live (aka production)
    • Database name: mysite_livedb
    • Database user name: mysite_liveuser
    • Drupal directory name: mysite-live (or mysite, if not using a virtual host (vanity URL)
    • Domain name or virtual host: mysite.com or mysite.stanford.edu
  • Test
    • Database name: mysite_testdb
    • Database user name: mysite_testuser
    • Drupal directory name: mysite-test
    • Domain name or virtual host: test.mysite.com or mysite-test.stanford.edu
  • Dev (aka development)
    • Database name: mysite_devdb
    • Database user name: mysite_devuser
    • Drupal directory name: mysite-dev
    • Domain name or virtual host: dev.mysite.com or mysite-dev.stanford.edu

Install and/or confirm Drush

Install drush, if necessary: see drupal.org/project/drush (Stanford AFS already has Drush installed at server level). Then confirm that Drush is installed and is working for you. In the command line, type:

drush help

Create Drush aliases for live, test, and dev sites

I

Using a plain text editor, create three new files, one for each site. The file names should follow these patterns

  • Live
    • File name: mysitelive.alias.drushrc.php
  • Test
    • File name: mysitetest.alias.drushrc.php
  • Dev
    • File name: mysitedev.alias.drushrc.php

II

Put the following text at the top of each file:

<?php

III

Then, in the command line (terminal) for each site:

  1. Go to the drupal directory for the site
  2. Use drush to get the site's alias information
  3. Copy the resulting text from "$alias" to the final ");"
  4. Paste it into the relevant alias file (below "<?php")
  5. Replace all instances of "self" in the text with the first part of the filename

The *nix commands used for the first two steps above would be:

  • Live
    cd /path/to/mysite-live
    drush site-alias @self
  • Test
    cd /path/to/mysite-test
    drush site-alias @self
  • Dev
    cd /path/to/mysite-dev
    drush site-alias @self

The find and replace for the last step above would be:

  • Live
    • Find: self
    • Replace with: mysitelive
  • Test
    • Find: self
    • Replace with: mysitetest
  • Dev
    • Find: self
    • Replace with: mysitedev

IV

If necessary, create a .drush directory in your home directory (~). Note that the dot (.) in front of the directory name is important! It indicates that the directory is a hidden directory, so whatever method you use to check if the directory already exists needs to be set to show hidden files/directories.

From the command line, you can check if .drush already exists by listing the contents of your home directory:

ls -la ~

If you do need to create the .drush directory, the easiest way to do this is to use the command line:

mkdir ~/.drush

V

Upload the three alias.drushrc.php files into the ~/.drush directory.

VI

Using the command line, check to see that drush is aware of the aliases:

drush site-alias

Bonus Points: Add CSS to show you where you are

Add the following CSS to your live site (either by editing a subtheme css file or using a module such as CSS Injector) and you'll get a visual indication of when you're on your test or dev site:

/* Mark test and dev sites */

a#logo img[src*="test.mysite.com"] {
  background-color: black;
  padding: 1em;
}

a#logo img[src*="dev.mysite.com"] {
  background-color: purple;
  padding: 1em;
}

Copy live site to test site

I

Using a plain text editor, create a new file and save it with a name like mysite-copy-live-to-test.txt

II

Copy the following commands and paste them into mysite-copy-live-to-test.txt (Note that it is important not to have returns or line breaks in the middle of a command. The commands below are all separated from each other by blank lines. Note also that the quote marks are backquotes `, not normal single quotes '.)

cd `drush dd @mysitelive`

pwd

drush cache-clear all

cd ..

pwd

drush rsync @mysitelive @mysitetest --mode=rlptDkzv --delete

cd `drush dd @mysitetest`

pwd

drush sql-drop

cd ..

pwd

drush sql-sync @mysitelive @mysitetest --skip-tables-key=common

III

In the text file, replace all instances of "mysite" with the label for your site. E.g.

  • If your site is fluffybunnies.com and your site label is fluffyb:
    • Find: mysite
    • Replace with: fluffyb
  • If your site is eggs.stanford.edu and your site label is eggs:
    • Find: mysite
    • Replace with: eggs

You now have a (manual) "script" to follow when copying live to test. When you want to copy your live site to your test site, open up this file, copy the first command and paste it into the command line, then execute it by clicking the return key. When it's finished, copy the second command and past it into the command line, then execute. Keep doing this command by command through the script.

Copy live site to dev site

Make a duplicate of the mysite-copy-live-to-test.txt file and name it mysite-copy-live-to-dev.txt

In the new mysite-copy-live-to-dev.txt file, replace all instances of "test" with "dev":

  • Find: test
  • Replace with: dev

You now have a (manual) "script" to follow when cloning live to dev. As before, copy, paste, and execute each command, one at a time, in the command line.

Copy test site to dev site

Make a duplicate of the mysite-copy-live-to-dev.txt file and name it mysite-copy-test-to-dev.txt

In the new mysite-copy-test-to-dev.txt file, replace all instances of "live" with "test":

  • Find: live
  • Replace with: test

You now have a (manual) "script" to follow when cloning test to dev. As before, copy, paste, and execute each command, one at a time, in the command line.

Copy dev site to test site

Make a duplicate of the mysite-copy-live-to-test.txt file and name it mysite-copy-dev-to-test.txt

In the new mysite-copy-dev-to-test.txt file, replace all instances of "test" with "dev":

  • Find: live
  • Replace with: dev

You now have a (manual) "script" to follow when cloning dev to test. As before, copy, paste, and execute each command, one at a time, in the command line.

Never copy TO live

We've created (manual) scripts to copy live and dev to test, and to copy live and test to dev, but none to copy either test or dev to live. This is because live is where your content lives.

Example use of test site: Test before updating live site

  1. Backup live
  2. Copy live to test
  3. Update test using drush
  4. Check to see if there are any problems on test and whether the site still functions
  5. Troubleshoot, if needed, and adjust steps and repeat from beginning until successful and can be repeated without problems.
  6. Copy and adjust steps for updating code on test to updating code on live.
  7. Update live following exact same steps as for test
  8. Double check all is well

To help with repeatability and adjusting steps, I recommend creating a script, for example, mysite-update-test.txt, for steps 1-3 above. Then for step 6, I copy mysite-update-test.txt to mysite-update-live.txt; remove step 2 (copying live to test) from mysite-update-live.txt; and use search and replace within mysite-update-live.txt, to find test and replace it with live.