Synchronize one drupal site with another using Drush

Synchronize one drupal site with another using Drush

I'm late to the Drush party. I've been using it for what seems like forever, but I haven't taken advantage of some of the newer and really cool features. Earlier this year Drush got support for aliasing, sql syncing and file syncing. And with that the last of my reasons for continuing to use my scripts instead of Drush disappeared. Here is a recipe to sync one drupal site with another in three simple commands. When set up, the commands will be something like this: //update the codebase using whatever source control system you prefer. git pull //import the database using drush drush sql-sync @dev.mysite @local.mysite --sanitize //sync the files directory using drush drush rsync @dev.mysite:%files/ @local.mysite:%files Files are synced using rsync command. Rsync is much smarter than FTP. It will only move over the parts of files have have changed. So if your development server has a files directory of 1.7GB but you synced the day before, it may only take a few seconds to get all the updated files. rsync splits files into chunks and then compares checksums. When the checksums don't match, those chunks are moved from the source to the target. Of course if you are creating a new environment, you'll have to pull all of the files. If you choose to use drush rsync for the entire site, not just the files directory, you can. You'll really only want to do that if you aren't using any form of source control. And Drush is smart enough to not pull your settings.php file. Just run drush rsync @dev.mysite @local.mysite Also of note, the --sanitize option for sql-sync is fantastic. It will obscure all email addresses in the user table so you won't have someone accidentally mail all your users. It will also reset all users to password "password" so you can do user and role testing. The sanitize option requires Drush 4. Aliases are used to define site settings in your .drush directory. @dev.mysite in the examples above refer to aliases defined in files that reside in what is usually your ~/.drush directory. Use this recipe to set up your alias files.
  1. Create a directory called ~/.drush
  2. Create a file called mysite.aliases.drushrc.php in .drush I prefer to have an alias file for each site I use.
  3. Modify and add the code below.
'path/to/drupal/site', 'uri' => 'mysite.local', 'db_url' => 'mysql://user:password@localhost/database', 'path-aliases' => array( '%dump' => 'path/to/dump/mysite.sql', '%files' => 'path/to/drupal/site/file/dir', ) ); //this is the alias for the remote development site $aliases['dev.mysite'] = array( 'root' => 'path/to/drupal/site', 'uri' => 'www.example.com', 'remote-host' => 'www.example.com', 'remote-user' => 'sshuser', 'db_url' => 'mysql://user:password@localhost/database', 'path-aliases' => array( '%dump' => 'path/to/dumpfile.sql', '%files' => 'path/to/drupal/site/file/dir', ) ); ?>

Update - Apparently in newer versions of drush you should use db-url instead of db_url.

Photo by Tim Briggs