At Commercial Progression we rely heavily on Drush. We use it to download modules, enable them, update/revert our features, clear the cache, and make our coffee in the morning (feature request). It’s hard to imagine life as a Drupal developer without it and I would like to share a feature of Drush that I find very useful. Drush provides an easy way to keep your environment's files and database in sync. I will show you how to set this up and make your life just a little bit easier.
At minimum we have three environments for our sites; development, staging, and production. Sometimes it is necessary to pull down the files and/or the database from production or staging to your local development environment. This can become pretty tedious and Drush has a way to make this process much less time consuming.
The way this could be accomplished without Drush is to dump the database on staging or production and copy it by some means to your local machine. You could then import that database locally. The files would probably need to be tarred/zipped up from prod/stage and then copied back to you local machine and untarred/unzipped.
With a little setup you can have Drush handle this and all you need to do is enter a couple of commands.
Step One: Create a drush alias file
Let’s start with the setup. We will need to create a drush alias file for your site. This can be created in a few places. The two that I would normally use are either ~/.drush or sites/all/drush. I prefer the latter because it allows me to version control the alias file and other developers can take advantage of the aliases already created and add their own entry for their local development environment. The name of the file would be sitename.aliases.drushrc.php regardless of where you decide to create it.
Step Two: Add your local environment
In that file we need to add some PHP code, let’s create an entry for our local development environment first.
The alias name can be whatever you choose, but try to make it relevant to the environment you are targeting with the entry. The path aliases array is used for telling Drush where to rsync our files.
Try a drush command from inside your site directory to make sure this worked. drush @dev status
This should print out the status of your site.
Step 3: Add your remote environment
Now to set up a remote environment. We will need a couple of extra options in our entry.
In addition to the options we had in our dev environment we need a remote host and a remote user so Drush knows where to look for your staging/production site.
Try the same drush command as before but replace @dev with @remote. drush @remote status
If everything went well and Drush can access your server with the remote host and remote user you provided, you should be able to see the status of your staging server from your local machine!
Step 4: Ensure your environments share an SSH key pair
Can you sync your database and files yet? Not quite, we’re pretty close though. You need to make sure your local machine’s public ssh key is in your remote server’s authorized_keys file. Huh? It’s ok, we’ll walk through it.
There are a lot of resources online for learning about ssh keys so I am not going to go into great detail here. If you haven’t already, you need to create an ssh key pair on your local machine. This can be done pretty easily with this command: ssh-keygen -t rsa
Press enter to save the file in the default location and feel free to secure your key pair with a password (not required, but suggested). This will create two files called idrsa and idrsa.pub. The content of the idrsa.pub file needs to be added to your remote server’s authorizedkeys file. First, print out the contents of the file and copy it. cat ~/.ssh/id_rsa.pub
Second, log into your remote server with the appropriate user and edit the autorizedkeys file. nano ~/.ssh/authorizedkeys
Paste in the key from your local machine and save and exit (ctrl-x, y, enter).
You can test that this worked by trying to SSH into your remote server. It should no longer prompt you for a password unless you protected your ssh key pair with one. If that is the case, enter that password when prompted.
Step 5: Sync it up!
Now we can sync the database and files! drush sql-sync @remote @dev
This command will pull down the remote database and overwrite your local database. Be careful with this, if you mix up the order you could very well overwrite your production database with your local development database. That would be bad, don’t do that.
Now for the files drush rsync @remote:%files @dev:%files
This will sync your local files with your remote files. Again, don’t mix up the order of this unless you are trying to overwrite the files on your staging or production environment. But again, don’t do that.
These two commands will save you a lot of time. They can also cause a lot of pain if used incorrectly. Be careful and enjoy!