Updating a Drupal module in a subversion integrated filesystem using rsync

Version control is an essential tool when it comes to maintaining your code and properly tracking filesystem changes. In this quick tutorial, I’ll show you how to update a Drupal module in a subversion integrated environment using rsync. Since simply copying the contents of a new module update on top of your current directory structure is a bad idea (since it will NOT account for file deletions), rsync is a great solution for syncing module update changes. NOTE: it is a bad idea to update a module in a production environment without proper testing; this code assumes you are working in a development environment.

The first step is to download the latest (and in most cases, stable) package for the module you’d like to update to a directory outside your drupal path. For my Drupal installations, I browse to the Available updates page (admin/reports/updates) and copy the URL from this page. Next I go to my shell, use wget to fetch the package to my home directory, and unpack the file:

$ cd ~
$ mkdir Downloads
$ cd Downloads
$ wget http://ftp.drupal.org/files/projects/xmlsitemap-6.x-2.x-dev.tar.gz
$ tar -xzf xmlsitemap-6.x-2.x-dev.tar.gz

Now you can run the rsync command to apply the filesystem changes to your Drupal module directory. You’ll have to update the paths listed below to match your filesystem and Drupal installation.

$ rsync -avCz --delete ~/Downloads/xmlsitemap/ /var/www/vhosts/tdb.erl.dev/httpdocs/sites/all/modules/xmlsitemap/

If executed properly, you can change directory to the module you are trying to update and run an “svn stat” command to see what has been updated. NOTE: the below output is simulated to show common svn status changes.

$ cd /var/www/vhosts/tdb.erl.dev/httpdocs/sites/all/modules/xmlsitemap
$ svn stat
?      xmlsitemap.newfile.php
!      xmlsitemap_taxonomy
M      xmlsitemap.module

At this point you should see a list of all the files that have changed with this module update. You can now run the update.php script in your development environment and verify the changes are working properly. If everything is working properly, you can commit the module update changes…

# remove files that have been deleted
$ svn stat | grep ^! | awk {'print $2'} | xargs -i svn rm '{}'

# add files that have been added
$ svn stat | grep ^? | awk {'print $2'} | xargs -i svn add '{}'

# commit
$ svn commit -m "upgraded xmlsitemap module to version 6.x-2.x-dev"

Now you can safely deploy the module changes to your production environment (svn update), run the update.php script, and ensure everything is working properly.

Note: in Mac OSX:

svn stat | grep ^! | awk '{print $2}' | xargs -I{} svn rm '{}'

Updated: