Using Rsync to merge code differences
Posted on: Monday, October 17th 2011 by Brandon Tate

I just wanted to point out something that I use very frequently that allows me to sync websites that are on two separate servers. Rsync! Rsync finds files that need to be transferred using a "quick check" algorithm that looks for files that have changed in size or in last-modified time. Its a fast and efficient way to merge two different code bases without having to use ftp or any diff program.

Using Rsync is simple. First to install rsync you can use the following commands.

sudo apt-get install rsync

or

yum install rsync

Once Rsync is installed. You can type rsync -man to see all the options available to it.

Here is an example of Rsync:

rsync -r -v username@servername.com:/var/www/vhosts/site1/ /var/www/vhosts/site2

This example will merge the code from site1 on the remote server to site2 on my local. Using the -v specifies verbose and -r is recursive.

rsync -r -v --exclude “sites/default/files” username@servername.com:/var/www/vhosts/site1/ /var/www/vhosts/site2

This example will merge the code from site 1 to site2 but will also exclude the directory “sites/default/files”. This is handy if a production environment is loaded with gigs of image files you don’t need.

For more information on rsync, check “man rsync” or go to http://linux.die.net/man/1/rsync

If you have any questions, feel free to ask.

Comments

Hi Brandon,

This is an interesting topic and rsync is a great tool for jobs like this. I wonder whether you would consider using a revision control system like git to manage deployment of a site?

How do you think git would compare to rsync?

Hey Greggles,

I do use GIT and SVN to manage site but in this example, I didn't want to worry about revisions etc, plus i didn't want to commit any client changes yet.

I use rsync more for testing purposes if I want to quickly sync a site with a live site, pull down images or any files that we normally don't save into a repository.

In my opinion, I wouldn't compare git to rsync since git is a version control system and rsync is just a tool used to sync differences between folders, files, servers etc.

Brandon,

You might also want to take a look at drush, a nifty little project that will help keep track of your sites using "site alias" definitions. With drush, you can do something like this:

drush rsync @dev @live
drush @live updatedb

Depending on your definition of @dev and @live, this might exclude your 'files' directory or not.

Git is also a cool tool for moving files around. It uses rsync at its core, and is therefore very efficient. One difference between using git and rsync to push to the live site is that you'll end up using more disk space on live with git -- perhaps not an issue unless you're on a really small server.

You can also use drush to help you with git. In the drush-5 release, there is a bash function called 'gitd' available. You can use it with your site aliases like this:

gitd @live pull

This will ssh on over to whatever server your 'live' alias is pointing at, cd to the site root and run 'git pull'. Kind of handy.

Hey Greg,

Thanks for all the info. I use drush aliases etc but I've never brought drush and Git together... going to try this, thanks again!

I use rsync myself, and I started with a command similar to your post, but it's evolved over time. Your post inspired me to share mine: http://www.dave-cohen.com/node/1000005

Oh and I meant to say... you use the term 'merge', which I think implies more what source code control systems do, that is merge changes into a portion of a file. Rsync is a more blunt instrument, overwriting changed files rather than merging changes into them.

Post new comment

The content of this field is kept private and will not be shown publicly.
Type the characters you see in this picture. (verify using audio)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.