RogueWolves

Rogue Wolves is the personal site of .

I'm currently a research scientist with Oculus Info Inc. in Toronto, Ontario Canada.

My research interests include: adaptive user interfaces, machine learning, Bayesian reasoning and distributed artificial intelligence.

Using CVS to keep Drupal up to date

I've decided to use CVS to maintain my Drupal sites rather than manually downloading Drupal updates and upgrading by copying the updates into your drupal folder. Manually updating is time consuming. In order to do it correctly you shouldn't just copy the new files overtop of your existing install since this could leave old files in your folder that are no longer used and can cause problems or security holes. To do it right you need to rebuild your Drupal folder and then copy in your settings, themes, etc. Then you may also need to go and download all your modules again for the new version. It turns into a chore quickly. A much better way is to use CVS to handle all the grunt work.

Drupal like many open source projects uses CVS to maintain it's development and release code. CVS is a version control system that helps developers keep track of their code and all the changes that have occured. Also, like many open source projects the CVS repositories that store this code are open for anonymous access (read only). This means anyone can download the latest code directly from the CVS repository that hosts it. By using CVS you get all the nifty features of a version control system such as updates to out of date files/folders and removal of files no longer in the repository. This is exactly the functionality I want in order to eliminate the need to manually perform these actions...

First off let me stress the need to have a test site of your production Drupal site. This test site is a mirror image of your production Drupal site (database and all) and is for testing out updates before you apply them to your production site. Why is this important? Well if you apply an update directly to your production site it could break - particularly if you have custom themes or modules. You don't want any site downtime right? So by having a test site you can solve any problems before you take the plunge. I personally keep a mirror copy of my drupal site on my laptop that I can run locally. To figure out how to create a mirror copy of your drupal site refer to this great screencast by Lullabot for help on backing up your production site. You can then use the backup to create a test site.

Now you have a test site. Great. Make sure it works and then you are ready to setup CVS to get Drupal updates. If you are using OS X or a Unix/Linux based operating system CVS is most likely installed for you or is easy enough to get installed. These instructions won't be particularly Windows friendly unless you have windows command line cvs tools and a sh scripting engine installed. I'm sure there are ways to get this working on Windows but I'm a Mac/Linux user so I leave that up to you. Sorry!

To perform the cvs update I made use of the great script that Mark Constable posted on Drupal.org. I have modified his script a bit and it is attached to this post with the name cvsdrupal.sh. The script will connect to the Drupal CVS server, log in anonymously and then get the latest updates for the drupal version you specify and all the modules and themes you specify. These modules and themes of course must be maintained on the Drupal CVS server and not some third party system. To determine if it's in Drupal CVS check out this listing and see if your modules are present.

To use the script you need to change the CVS_WC variable to point to the directory location you wish to store the latest cvs version. This should be the directory of your test drupal site (or production if you are brave).

CVS_WC=/Users/scott/Sites/drupal

Next you need to specify the release you are updating. This is the version of drupal you want to update to. Leave off the minor numbers. For example if the latest is drupal 4.7.2 then the REL variable should be:

REL=DRUPAL-4-7

Here is a list of all the Drupal branches.

In addition to specifying the drupal version you can specify any additional modules you want that are not in core. To do this you can specify a list of modules with the MODULES variable. This is a space delimited list of module names. The names of the modules must be the CVS folder name for the module. You can find out the folder names for your modules by looking at this directory listing and typing the directory name into the MODULES variable. Here is an example:

MODULES=" cck notify pathauto tinymce views captcha "

You can also specify any contributed themes you want using the THEMES variable. You use it the same as the MODULES variable.

THEMES=" meta "

Once you have set all the variables you can execute the script and the directory specified will be updated to the latest version of the Drupal release specified. In order to finish the update make sure to visit update.php for your updated site to apply any database updates that are needed to complete the update.

Note: Be sure to log in to your site as user 1 before you run the update script. You need to be user 1 in order to run update.php and you may have complications logging in if you don't do it beforehand.

This script can be used to keep up to date with the version of Drupal you have installed, or with HEAD if you are doing development, but the magic begins when a new branch of Drupal is released such as Drupal 5. By changing the REL variable in the script you can upgrade instantly from version 4 to version 5. Be sure to check if the modules you use are supported in the new version before you upgrade.

Note: The cvs update script keeps the contrib modules and themes in contrib directories in the modules and themes directories instead of in the Sites directory as per Drupal 5. This is done for backwards compatibility so that previous versions of Drupal can use this script.

Here are some notes on steps I take when updating to a new major revision (4.6 -> 4.7 or 4.7 -> 5.0, etc):

  • Log in as user 1
  • Disable custom themes
  • Disable contrib modules (any module not in core)
  • Run the cvs update script
  • Visit update.php and perform any database updates necessary
  • Enable contrib modules
  • Visit update.php and perform any database updates necessary
  • Enable custom Themes
  • Test!

I tested this procedure for updating a Drupal 4.7 install to Drupal 5.1 and it worked perfectly for me. I only had a few snags with my custom theme but nothing too major.

After I have used CVS to update my test Drupal site and have verified that everything is in working order I want to push these updates to my production site. I use rsync for this functionality. In order to use rsync it must be installed on both the computer that hosts your test Drupal site and the computer that hosts your production site. (Note it could be the same computer - I personally use two different computers). I also run rsync over SSH so my script is setup that way. I've attached my rsync script and an rsync excludes file which I'll discuss shortly. The rsync script is called push_drupal.sh and if you use it you will have to modify some of it's variables such as your source directory and destination directory, the transport shell to use (SSH, RSH, etc) and the path to the excludes file. The excludes file is very important because I use the --delete option in rsync which tells rsync to remove any files or directories in the destination directory that are not present in the source directory. I do this to clean up files no longer in Drupal CVS. Doing this however means you have to take care not to delete files that may be in your production directory but not in your test directory. Also, you may not want certain files synchronized such as configuration files, .htaccess files, robots.txt, cron scripts, etc. At a bare minimum I suggest you have these in your rsync_excludes:

sites files favicon.ico .htaccess robots.txt scripts/cron-lynx.sh scripts/cron-curl.sh

WARNING: If you add anything new to your drupal directory make sure it's in your rsync_excludes so it's not deleted next rsync! This is especially important for those that don't maintain a separate directory for Drupal in their www root directory.

Here are a few resources I found on using CVS with Drupal you might find useful:

  1. How to update Drupal while preserving all your changes
  2. Using CVS to maintain a Drupal website