Converting an Existing Drupal Site to CVS

In an earlier post, I explained how to use CVS to install and update a Drupal website quickly and easily.  However, many Drupal sites are installed before the site developer is aware of the benefits of CVS site management.  As such, it would be useful to have a tutorial on how to convert a conventional Drupal install into a CVS-controlled version.  In this post I aim to explain how to do just that.

First Steps

Before you do anything to your site, you should completely backup your file system and database.  This will ensure that even if something goes horribly wrong with the transition process, you will be able to return your site back to its original working state.  After backing up, you should take careful note of your Drupal core version number, in addition to the version numbers of your contributed modules and themes.  These numbers will be necessary in future steps.  After doing this, you should put your site into maintenance mode.

Deleting the Old

At this point you should delete most of your site's file system from the server.  Everything should be deleted, with the notable exception of the 'files' directory.  This directory contains site-specific files, which CVS will not be able to manage.  For example, you obviously can not 'checkout' your site's photos from Drupal's CVS repository.  Deleting most of your site's files can seem a bit scary, but have no fear; you will be replacing these files very soon with a few CVS commands.

Checking Out the New

Now that you have deleted most of your site's files, you need to rebuild that file system with CVS.  You begin doing so by checking out Drupal core.  This is where you need to know which version of Drupal your site is using.  With that version number in hand you should run a command like this:

cvs -d:pserver:anonymous:anonymous@cvs.drupal.org:/cvs/drupal checkout -d path/to/site-root -r DRUPAL-X-X  drupal

See my previous post for more details.  After running this command, your site will have essentially the same core file system, with many directories named 'CVS' sprinkled throughout to manage file version information.  Now, we need to checkout the appropriate versions of contributed modules and themes to our 'sites/all/modules' and 'sites/all/themes' directories.  There is more information about this in my previous post.

Closing Steps

If your site uses any custom themes or modules, you will need to manually FTP those directories from your backed up files onto your server.  As with the 'files' directory, CVS will not be able to manage these files.  That is why you must manually load them onto the server.  Also, a very important final step is to alter the default 'sites/default/settings.php' file that was checked out with Drupal core.  This file has a line like:

$db_url = 'mysql://username:password@localhost/databasename';

This line is usually altered by Drupal to look something like:

$db_url = 'mysql://your-db-username:your-unique-password@yourdbserver/dbname';

You should edit the file to look something like this:

/* ORIGINAL DB LINE */
$db_url = 'mysql://username:password@localhost/databasename';
/* CUSTOM DB LINE */
$db_url = 'mysql://your-db-username:your-unique-password@yourdbserver/dbname';

I believe that if you simply corrected the default file's line, you would run into trouble with future updates via CVS.  Editing the code in the suggested manner will keep CVS happy, by keeping the original code intact and adding the correct information afterwards.

Now your site should be fully functional.  Since you have essentially recreated the file system with CVS (with the addition of many CVS-specific directories), there should be no problems.  You can now update the site quickly and easily, as explained in the previous post.  Congratulations!

Mike Goodwin

The Red Leaf Media blog is written by Mike Goodwin. Mike has been developing websites for three years using the Drupal content management system. He is experienced with XHTML, CSS, PHP, SQL and jQuery. If you would like to contact Mike, please use our contact form.