For those who haven't heard about drush before, i would recommend going through these videos before. For those who know about Drush, have you ever imagined how easy life would become if you could manage your staging sites without having to login to the server. Or , how awesome would it be to deploy changes to various instances i.e., dev/staging/production just using drush the right way!! I have been using drush to do all this since last 2 years.
The two things that need to be moved from our local instance to other instances for a basic Drupal deployment are: Files folder and db dump. Drush acts as a messiah over here and provides us with two very useful commands drush rsyncand drush sql-sync. In order to use these commands, we need to create drush aliases for different sites we working on. Location for these aliases should be typically inside ~/.drush folder. I have written a handy script which converts a template file into a drush alias files depending on the values entered by the end user. You can modify the script as per your need.
This scipt looks out for the template file at ~/.drush. So, place the template file inside ~/.drush directory.
Lets understand the terminologies mentioned in the above template file one by one.
%ALIAS_DEV: Alias name which you would like to keep for your dev instance. This is something which will be used as a reference to the dev site while using drush command to execute something there.Lets understand the terminologies mentioned in the above template file one by one.
uri: The uri of the dev instance. Should be something like dev.example.com.
remote-user: A valid user on the machine where the dev instance is hosted. Also, we need to make sure this user has the permission to execute drush commands on that machine.
remote-host: IP address/hostname for the machine. This is used by drush to connect to the dev machine while executing the commands there.
path-aliases: Directory paths which we want to be accessible using drush commands. %dump-dir would be used to store the db dumps temporarily when syncing databases. %files would be used to sync the files folder.
command-specific: drush commands comes with a lot of options. This configuration allows us to set some of the options that would automatically get applied when the specified command is fired for drush alias. e.g., in the template file defined above, we have sql-sync with sanitize set to true. Sanitize option would skip syncing users while importing databases. So, it would work in any of the examples below:
source-command-specific/target-command-specific: Same as command-specific. But gets triggered only if the alias is a source/target argument respectively.
P.S: Executing these commands on a remote server would require ssh access to the machine. So, drush while executing them, would ask you for the passwords every now and then. To avoid this, we can make set our public key as one of the authorized keys for the dev server.
Confused? Don't worry, its not as complicated as it sounds. Just follow the following steps and you should be done with it:
Copy your ssh key to the server
Login to the remote machine using ssh
Voila!! Now your terminal would never dare to ask you for a password while you tryin to login to dev.example.com.
Now since you are setup with ur drush alias, you must be excited to see them in action. So, yeah here is how to use them now:
This would take a db-dump of dev server, save it in %dump-dir temporarily. Then copy this to %dump-dir defined for @local instance. By default, it will merge the two databases on the local server. However, passing create-db option forces mysql to overwrite the current database with the dump from dev server.
Rsync Drupal root from dev to local
Rsync all files in the current directory to the 'img' directory in %files on dev.
Rsync all files in the %files defined on @dev into the %files on local.
Using general Drush commands
Basically any drush command accompanied with a valid drush alias would execute the command on that instance. These can also be used to create automated deployment tools when integrated with git-hooks. Thats all from me folks. Hope you enjoyed exploring usage of drush in these ways.