Testing against an exact copy of a live site is essential to successful Drupal deployments. This, unfortunately, can be both time consuming and mind-numbingly tedious. If, for example, something goes wrong with an update script that changes the database schema you need to fix the issue, rebuild the database, and test again. A potentially laborious task.
The process involves some variation of the following:
After going through the process of rebuilding sites manually hundreds of times, I finally wised up and wrote a script to do everything in one swoop. I can run it, get a coffee, and come back to a completely rebuilt staging environment with no further action required.
An approximation of this script can be used for any Drupal build with some minor tweaks. Here's how to set it up:
Before running the script you'll need to install Drush (Drupal + Shell) on your server. Drush allows you to trigger many common Drupal actions from the command line like enabling/disabling modules, clearing the cache, running cron, etc. It's a powerful time saver and used extensively in the script.
For this script the following is assumed:
OS: Linux/Unix based server
Production Site
Database Name: prod_db
Path: /var/www/html/prod
Staging Site
Database Name: staging_db
path: /var/www/html/staging
The Script
This particular database has three very large tables that cache zip code information. These tables rarely change and ignoring them speeds the import significantly. In line 2 I specify that MySQL should not include these tables in the database dump and line 4 I drop all the tables on the staging site except the ignored tables.
If you want to export the entire database you can use this:
Lines 5-8 load the dumped production database into the staging database.
Lines 9-17 copy all of the production files to the staging files. It also clears out the cached CSS files and resets the permissions on all the copied files.
Line 19 updates staging to the latest code from the subversion repository. This script assumes that staging is already pointed to the correct path in the repository.
I like to clear the cache and watchdog table when recreating a staging site. I find that it's much easier to track down issues specific to the new build if you start with a clean slate.
Lines 24-27 is the equivalent of going to update.php in a browser and running all of the update scripts. I run it three times to ensure all updates are triggered. I've seen cases where updates are only available after running the update once.
In these final lines I set some variables to do the following:
You can use this last section to set anything you commonly need in a staging site. This section save me a lot of mouse clicks and form submissions.
Once the script is written and saved remember to grant execute privileges to the file. The following command will do it:
This script saves me an enormous amount of time and it enables us to easily test the deployment as many times as necessary. This leads to smoother deployments and happier clients.
Posted at 12:38 pm by Jeff Rigby
Contact DPCI to learn more about our Web content management systems,
digital asset management solutions,
online collaboration software,
custom development,
multi-channel publishing systems, and
workflow management solutions.