Drupal And Linux, A Deployment Script

Photo of Greg Harvey
Mon, 2010-01-18 17:51By greg

Quick post today, no politics *whatsoever*. ;-)

I just wrote another little batch script for deploying my Drupal sites from local to stage. Before I post the code, here's the workflow here in our office at CMS Professionals:

  • Code is stored in Subversion away on the Internet (svn.cmspros.co.uk)
  • Each developer has a working copy on their laptop
  • Database during development is held on central (dedicated) CentOS/MySQL machine on our office LAN
  • Unstable client preview/test versions of the code/database are kept on our CentOS test servers, again away on the Internet (testN.cmspros.co.uk)

The Linux shell script I've written, which I share below, is for deploying from our office to the test server. These are the steps it goes through:

  1. Dumps database to disk locally and bzips it (you must specify host - can be localhost, in our office it's usually - the dedicated MySQL box)
  2. Copies bz2 file to test server
  3. Deflates same bz2 file on test server, ready for importing
  4. Drops and re-creates the database on the test server
  5. Restores the database from the SQL file we copied up and deflated
  6. Deletes local and remote copies of the SQL file (we're done with it now)
  7. Does an SVN update of the application to latest HEAD code (relies on SSH key-based authentication to be properly configured for this)
  8. Switches to the application directory on the remote server and does a drush cache clear

Cool, huh? Here it is - just save it in a .sh file and make it executable:

if [ $1 = "--help" ];
echo "

Syntax for running this script is:
./database_push.sh sshuser sshdomain remotedatabaseuser databasename remotedatabasepwd

sshuser - string, username for ssh connection
sshdomain - string, the actual domain where the remote database is
scptargetdir - string, place on remote server to copy db dump to
databasehost - string, hostname or IP for database server (usually localhost)
databasename - string, name of database
remotedatabaseuser - string, name of remote database user
sshappdirectory - string, location of the web app for svn update
remotedatabasepwd - (optional) string, remote password for the database user
." &
echo "Creating db dump for $5 from $4 ..."
if [ $4 = 'localhost' ];
mysqldump -u root $5 | bzip2 > dump.sql.bz2
mysqldump -u root -h $4 $5 | bzip2 > dump.sql.bz2
echo "Copying database dump to $2:$3 ..."
scp dump.sql.bz2 $1@$2:$3
echo "Deflating remote database dump ..."
ssh $1@$2 "bzip2 -d $3/dump.sql.bz2"
if [ $8 ];
echo "Resetting remote db called $5 ..."
ssh $1@$2 "echo \"drop database $5;\" | mysql -u $6 -p$8"
ssh $1@$2 "echo \"create database $5;\" | mysql -u $6 -p$8"
echo "Restoring remote database $5 ..."
ssh $1@$2 "mysql -u $6 -p$8 $5 else
echo "Resetting remote db called $5 ..."
ssh $1@$2 "echo \"drop database $5;\" | mysql -u $6"
ssh $1@$2 "echo \"create database $5;\" | mysql -u $6"
echo "Restoring remote database $5 ..."
ssh $1@$2 "mysql -u $6 $5 fi
echo "Deleting local db dump ..."
rm dump.sql.bz2
echo "Deleting remote db dump ..."
ssh $1@$2 "rm $3/dump.sql"
echo "Updating remote application ..."
ssh $1@$2 "svn up $7"
echo "Clear Drupal cache on remote application ..."
ssh $1@$2 "cd $7"
ssh $1@$2 "drush cache clear"
echo "Done!"

My TODO for the script looks like this:

  • Put a mysql line in there to update the database and replace the Drupal temp directory path (if necessary)
  • Add code to rsync the files directory from local to remote

Edit: Another note, for me really. I might mount the MySQL server on everyone's workstation and symbolically link files directories to that machine, so not only is the database central but so is the matching set up Drupal files. Just weighing up if that's going to be more hassle than it's worth. Hmmm...