Simple deployments made easy using rsync with a Makefile

Automated methods for code deployment more than often make the developer happy. The deployment method described here uses rsync and a Makefile to easily sync a local site with a remote site, or the other way around.

Makefile.mysite

Before I get to far into this, I must say I can’t take credit for this Makefile script. I pulled it off of someone’s blog, awhile ago and I can’t find its true origination.
This script “can” be used on Windows with nmake and cwrsync… but I’m not going through the install setup for windows on this post.

The following assumes we have rsync (or cwrsync) and make (or nmake) installed and working.

First step, edit the Makefile:

# User-set makefile variables...
#
# localdir:    path to site files on personal machine (end with slash)
# remoteuser:  username for logging into server
# remotehost:  server host name
# remotedir:   path to site files on server (end with slash)
#
# switches:    rsync switches for both uploading and downloading files
# getswitches: rsync switches for downloading files only
# putswitches: rsync switches for uploading files only
#
# getmessage:  message to echo before downloading files
# putmessage:  message to echo before uploading files
localdir     = /home/fred/mysite
remoteuser   = fred
remotehost   = mysite.com
remotedir    = /var/www/mysite

switches     = --checksum -rlvzu --exclude ".*" --exclude "*.tgz*" --exclude "Makefile.*"
getswitches  = --exclude "logs"
putswitches  = --exclude "logs"
getmessage   = Updating local site mirror with newer files from server...
putmessage   = Updating server with newer files from local site mirror...

Next step, run it (linux syntax)!

This will show you what is on the remote site that is different than the local:

 make -f Makefile.mysite dryget

The above produces the following command:

rsync --update --dry-run --exclude "logs" --checksum -rlvzu --exclude ".*" --exclude "*.tgz*" --exclude "Makefile.*" fred@mysite.com:/var/www/mysite/ /home/fred/mysite/

I’ve added the tdir option, so that you can specify a target directory (must be begin with forward slash):

make -f Makefile.mysite tdir=/application

The above produces the following command:

rsync --update --exclude "logs" --checksum -rlvzu --exclude ".*" --exclude "*.tgz*" --exclude "Makefile.*" fred@mysite.com:/var/www/mysite/application/ /home/fred/mysite/application/

To check what you would deploy out:

make -f Makefile.mysite dryput

The above produces the following command:

rsync --dry-run --exclude "logs" --checksum -rlvzu --exclude ".*" --exclude "*.tgz*" --exclude "Makefile.*" /home/fred/mysite/ fred@mysite.com:/var/www/mysite/

Then, just change it to deploy:

make -f Makefile.mysite put

The above produces the following command:

rsync --exclude "logs" --checksum -rlvzu --exclude ".*" --exclude "*.tgz*" --exclude "Makefile.*" /home/fred/mysite/ fred@mysite.com:/var/www/mysite/

I’m not going through all the variations, but its very flexible and very simple deployment at its best. Beyond the simplicity, another advantage is that you can keep your deployment Makefiles under version control… which comes in really useful.

Happy Syncing!

~ by ityndall on March 17, 2011.

Leave a Reply