Using Bazaar to Manage Your Multi-site Drupal Environment

I've found a solution to my own needs for version control of my drupal multi-site environment. I'm sure there is room for improvement as I'm far from a master at any version control system, but this is now working for me. I welcome your advice on ways to change or enhance this environment, but first a few words on my goals and why I chose Bazaar over the other options.

My intention was to setup a central server that is available by SFTP to my MacBook and Linux desktop. One major requirement that made something like subversion insufficient was that I wanted to be able to work on project while sitting on a plane or other places where Internet access wasn't available and still make commits. The reason I avoided git is because I didn't want to lose the option to easily work with Windows users. It isn't an immediate need, but I don't like knocking options off. I know Windows users can get git to work, but it requires a bunch of extra programs, or so I've read.

My Environment

Three different repositories exist for my drupal environment. The first is a current drupal core with the sites directory ignored by bazaar. I'll refer to this as the "drupal-core" repository. The second is the "all" directory in sites and contains all the contrib modules and themes. I call this the "drupal-sites-all" repository. The third is the template site repository it contains typical modules I use and a backup of the database. This one goes by "drupal-sites-template". Any other project is a bzr branch of the drupal-sites-template repository. I assume that the three repositories will always work together. In other words, the "drupal-sites-template" and "drupal-sites-all" repository will always be in the sites directory of drupal-core. Breaking it apart like this simply eases administration. I can upgrade drupal by deleting the sites directory in a new tarball and copying it over the current repository. This makes sure I don't wipe out the contents of the sites directory.

Assumptions

I assume that you have already installed bazaar on all the computers that will be involved in this process including the central bazaar server. You may have a web server that does not have bazaar installed. We can still work with that also. I'm also assuming that all your computers are unix based and will take typical unix commands. Setting up a PC is beyond my intended scope.

Setting Up The drupal-core Repository

Setting this up first requires unzipping a drupal tarball in the location of your pleasing on one of the remote computers, not the server. We'll be doing all our local work in that directory. After that, follow these commands to get your the first of the three repositories setup. This is not yet putting it on the server.

cd drupal-* #enter the drupal directory
bzr init #initiate the repository
bzr ignore sites #ignore the sites directory
bzr ignore sites/* #ignore anything in the sites directory
bzr add ./* #add all the files to the repository, except those previously ignored
bzr commit -m "initial commit" #commit those additions

Setting Up the drupal-sites-all Repository

cd sites/all #enter the sites/all directory
#initiate the repository
bzr init
#make sure you have at the very least, the backup_migrate module
wget http://ftp.drupal.org/files/projects/backup_migrate-*.tar.gz
#add the contents of the repository
bzr add ./*
bzr commit -m "Initial Commit"

Setting Up the drupal-sites-template Repository

Before you start this you'll want to setup a database. I'm assuming you've done this before. I call the database template. You'll also want to add 'template.localhost' to your /etc/hosts file on the line starting with 127.0.0.1 with a space between the last word on the line.

To make this work as a multi-site on MAMP, you'll need to go into the MAMP, Preferences and the Apache tab. Then set this to the drupal directory we are working from as shown here. I also have setup Apache to work on port 80 to save me a couple of key strokes.

#enter the sites directory
cd ..
#copy the default site to template.localhost
cp -r default template.localhost

Next run the install from http://template.localhost/install.php to setup a base site for all other sites. You can enable and configure any modules you like. The database will be used later to build up a new site. Be sure backup the database when you are done. It should be somewhere in the sites/template.localhost/files directory. This assures that bazaar can version control your database backup. If you are a themer, you may want to put a basic starter theme or sub-theme in the sites/template.localhost/themes directory to give you a head start.

Next is the actual creation of the repository. I won't comment the commands. I expect you've figured it out by now.


cd template.localhost
bzr init
bzr add ./*
bzr commit -m "Initial Commit"

Congratulations!

With that, you've successfully setup the environment. Read on for instructions on making the best use of this setup.

Working with the Central Server

Having everything centralized can be very helpful in working as a group or working on more than one computer. Here are some helpful hints on accomplishing this.

Pushing Your Repositories to the Central Server

The final thing is to move each of these repositories up to the server. I'll show the commands once. You'll want to replace PROJECTNAME with drupal-core, drupal-sites-all and drupal-sites-template. Later, you'll do the same thing if you want to push your other sites up to teh server

# Create the remote repository
bzr init-repo --no-trees sftp://user@server/home/bzr-repos/PROJECTNAME

# Enter the repository you want to upload and run this command to push the files up to your server
bzr push sftp://user@server/home/bzr-repos/PROJECTNAME

# After the initial push, no longer need to include the location. Bazaar kindly remembers it.
bzr push

Checking The Repositories Out on Another Workstation

So you've set it all up and now want to connect another machine to it. Simply us the 'bzr checkout' command like this. I like to keep the projects on the server in a standard name to make it easy to remember what they are for. When I pull them down to a workstation, I may use any number of different names. Typically project.localhost is what I used in the MAMP setup. I might also have a project-test.localhost for another branch I want to try out without knocking the other one out. You'll want to make DESTLOCATION whatever the name of the folder you want on the workstation you're working with.

bzr checkout sftp://user@server/home/bzr-repos/PROJECTNAME DESTLOCATION

Pushing Your Site to to the Web Server

A plugin for Bazaar exists to upload your site to the server. It even tracks changes so that it can upload only the changes. Naturally it is called the Upload plugin. Some excellent and compact instructions for its install were written up on the Ubuntu Switch blog.

A few things should be kept in mind. Changes made on the web server will not be in the repository. You should not permit any changes on the web server outside of bzr. Also, don't forget to do a database backup so that you can restore it on the other side.

Once the plug is installed, you have an upload command in bzr that does the job. You'll want to make DESTLOCATION the domain of the site you are uploading.

bzr upload sftp://user@server/home/USER/public_html/sites/DESTLOCATION

# like the push command, you can leave the location off
bzr upload

Running a Project in Bazaar

With your setup, especially the drupal-sites-template repository, it becomes pretty easy to get new site up and running quickly.

Setup the Project

Setup the project by running these commands and replacing PROJECTNAME with the name you'd like to use for your project.

cd sites/
bzr branch template.localhost PROJECTNAME.localhost

Now setup the database in the settings.php file and upload the backup in the files directory to the new database. At that point, we're ready to create the repository. Finally, add PROJECTNAME.localhost to your /etc/hosts that we edited before. (That's the one starting with 127.0.0.1)

cp ../template.localhost .
bzr ignore settings.php
bzr commit -m "initial commit"

Committing Your Changes as You Go

As you make changes you can watch what files have been edited or added by using the 'bzr status'. You will want to frequently make commits after certain objectives have been completed. There are a couple steps to this. You'll want to backup the database to the files directory and then run 'bzr commit -m "some comment about the changes"'. You can safely remove the timestamp from your database backups. Bazaar will make sure it keeps track of the database versions so you don't have to do it at the file system level.

Committing Changes While Off the Internet

If you are working away from the Internet, you can still make commits, just use the 'bzr commit --local' command. If you will be working off grid for awhile, use the 'bzr unbind' command to disconnect and then 'bzr bind' to reconnect when you return. Bazaar will remember where you were connected previously.

Getting back in sync localy after being away will be handled by 'bzr update'. Once you have synced your local branch and everything seems okay, you will still need to use 'bzr commit' to push your changes up to the server.

EDIT: Added information on the upload plugin and some other minor edits.