Gain productivity with Drush

Gain productivity with Drush - Emmanuel Milou

1
Introduction

Gain productivity with Drush - Emmanuel Milou

2
introduction

Emmanuel Milou
@manumilou
emmanuel.milou@savoirfairelinux.com
Drupal Technical Leader at Savoir-faire Linux
...
What is drush ?

Gain productivity with Drush - Emmanuel Milou

4
what is drush ?

“ Drush (DRUpal Shell) is a command
line shell and scripting interface for
Drupal “

Gain productivity wi...
what is drush ?



Running Drupal commands through the shell



3 main parts:
●

The drush project manager:
Manage modul...
what is drush ?

$ drush [options] command ... 

Gain productivity with Drush - Emmanuel Milou

7
what is drush ?



Short and long version for every command
●
●
●
●
●
●

site-install (si)
variable-get (vget)
variable-s...
what is drush ?



Example:
$ drush ­­yes vset maintenance_mode 1
$ drush vset ­­yes maintenance_mode 1
$ drush ­­yes var...
what is drush ?



The help command
$ drush help
$ drush help [command]

Gain productivity with Drush - Emmanuel Milou

1...
what is drush ?



Examples:
●
●
●
●
●
●
●

Installing sites
Updating sites
Downloading and enabling modules / themes
Cle...
Do I need to use drush ?

Gain productivity with Drush - Emmanuel Milou

12
Yes :-)

Gain productivity with Drush - Emmanuel Milou

13
why do I need to use drush



Swiss-knife



Work faster



Automate tasks



Handle multiple sites



Create your ow...
Installing drush

Gain productivity with Drush - Emmanuel Milou

15
installing drush

https://github.com/drush-ops/drush

Gain productivity with Drush - Emmanuel Milou

16
installing drush



Multi plateform:
●
●

Unix-like OS
Windows

Specific instructions on different platforms:
https://dru...
installing drush



Example on Ubuntu (need php-pear library)
pear channel­discover  
pear.drush.org
pear install drush/d...
Installing a site and
introspection

Gain productivity with Drush - Emmanuel Milou

19
site installation and instrospection



Download the latest stable release:
$ drush pm­download drupal­7.x



Run instal...
site installation and instrospection



More on site-install command:
$ drush site­install [profile] 
­­db­url=mysql://us...
site installation and instrospection



After a successful bootstrap
$ drush status



General view of the current insta...
Downloading and
managing modules

Gain productivity with Drush - Emmanuel Milou

23
downloading and managing modules



Available commands to:
●

●

●

List all modules:
$ drush pm­list
Download a module /...
downloading and managing modules

Installing and enabling a module used to look
like this:

Gain productivity with Drush -...
downloading and managing modules
1. Find the module's exact name
2. Look up for the module on drupal.org
3. Download the a...
downloading and managing modules

Now with drush, it is:

Gain productivity with Drush - Emmanuel Milou

27
downloading and managing modules



Find the module's exact name



Run:
$ drush dl <modulename>
$ drush enable <modulen...
downloading and managing modules



Download modules in sites/all/modules
­­destination=<path> to override



Specify sp...
downloading and managing modules

$ drush en views


Handles module's dependencies by:
●
●

Downloading the required modu...
downloading and managing modules



Disable a module:
$ drush pm­disable <modulename>



Uninstall a module:
$ drush pm­...
Updating a site

Gain productivity with Drush - Emmanuel Milou

32
updating a site



One of the most tedious task ever ...



Gets a lot easier with drush:
$ drush pm­update



Core, no...
Database management

Gain productivity with Drush - Emmanuel Milou

34
database management



Open a SQL cli, using Drupal's credentials
$ drush sql­cli



Database synchronisation between si...
database management



Database backup
$ drush sql­dump 
–result­file=dbname.sql –gzip 
●
●

Save to a file
Compress the ...
database management



Shortcut for accessing variables table:
$ drush variable­get clean_url



Write is supported:
$ d...
Controlling the cache
and cron

Gain productivity with Drush - Emmanuel Milou

38
controlling the cache and cron



Clear database cache tables
$ drush cc all



Clear a specific cache
views, menu, bloc...
controlling the cache and cron



Run cron
$ drush cron

Gain productivity with Drush - Emmanuel Milou

40
Archiving and restoring site

Gain productivity with Drush - Emmanuel Milou

41
archiving and restoring site



Backup the code, files and database in a
single archive
$ drush archive­dump 
–tar­option...
archiving and restoring site



Restore an archive
$ drush archive­restore file       
                          

Gain p...
Drush aliases

Gain productivity with Drush - Emmanuel Milou

44
drush aliases





Run drush commands on other local or
remote Drupal installations
Site aliases:
●
●

@self: current in...
drush aliases



Shortcut:
$ drush 
­­uri=”camp2013.drushdemo.staging” 
­­root=”/home/manu/dev/drupal/camp
2013/staging/s...
drush aliases
<?php
/**
* Drush aliases definition
*/
$aliases['staging'] = array(
'root' => '/var/www/staging',
'uri' => ...
drush aliases

drush status == drush @self status


The simplest example:
$ drush @prod status



More advanced:
$ drush...
drush aliases

Documentation:
http://drupalcontrib.org/api/drupal/contribu
tions!drush!examples!example.aliases.drushrc
.p...
Third-party integration

Gain productivity with Drush - Emmanuel Milou

50
Third-party integration



3 easy steps to create a custom drush
command:
●

●

●

Create a command file called
COMMANDFI...
Third-party integration



Drush searches for commandfiles in the
following location:
●

The “/path/to/drush/command” fol...
Third-party integration



Complete documentation:
$ drush topic docs­commands

Gain productivity with Drush - Emmanuel M...
Third-party integration



Custom module integration
●

●

●

Create a file called
MODULENAME.drush.inc in the module
dir...
Third-party integration



devel_generate module
●



views module
●



Enable, disable, list, revert views, ...

featu...
Example: deploying a site

Gain productivity with Drush - Emmanuel Milou

56
Example: deploying a site

(1) Put production site in maintenance
(2) Sync files from the staging env to
production
(3) Ru...
Example: deploying a site



The perfect setup:






Continuous-integration server: Jenkins
Source code management:...
Example: deploying a site





Source code changes are pushed from dev
into git repository
Jenkins polls the git reposit...
Example: deploying a site

$ drush ­­yes @prod vset maintenance_mode 1
$ drush ­­yes rsync –verbose @staging @prod
$ drush...
Example: deploying a site



Code synchronisation:
✗

@prod -> @staging

✔

@staging -> @prod Files synchronisation:
✗

...
Thank you :-)
And don't forget to get a badge ...

Gain productivity with Drush - Emmanuel Milou

62
Gain productivity with Drush - Emmanuel Milou

63
Upcoming SlideShare
Loading in …5
×

Gain productivity with Drush (Drupal Camp, Montreal 2013)

498 views
364 views

Published on

This presentation is an overview on how using the command-line tool Drush to gain productivity working with Drupal.

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
498
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
9
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Gain productivity with Drush (Drupal Camp, Montreal 2013)

  1. 1. Gain productivity with Drush Gain productivity with Drush - Emmanuel Milou 1
  2. 2. Introduction Gain productivity with Drush - Emmanuel Milou 2
  3. 3. introduction Emmanuel Milou @manumilou emmanuel.milou@savoirfairelinux.com Drupal Technical Leader at Savoir-faire Linux Based in Montreal http://sflx.ca/love Gain productivity with Drush - Emmanuel Milou 3
  4. 4. What is drush ? Gain productivity with Drush - Emmanuel Milou 4
  5. 5. what is drush ? “ Drush (DRUpal Shell) is a command line shell and scripting interface for Drupal “ Gain productivity with Drush - Emmanuel Milou 5
  6. 6. what is drush ?  Running Drupal commands through the shell  3 main parts: ● The drush project manager: Manage modules and themes Drush core: • ● Manage site configuration Drush SQL • ● • Issue query to database Gain productivity with Drush - Emmanuel Milou 6
  7. 7. what is drush ? $ drush [options] command ...  Gain productivity with Drush - Emmanuel Milou 7
  8. 8. what is drush ?  Short and long version for every command ● ● ● ● ● ● site-install (si) variable-get (vget) variable-set (vset) pm-download (dl) enable (en) ... Gain productivity with Drush - Emmanuel Milou 8
  9. 9. what is drush ?  Example: $ drush ­­yes vset maintenance_mode 1 $ drush vset ­­yes maintenance_mode 1 $ drush ­­yes variable­set  maintenance_mode 1 Gain productivity with Drush - Emmanuel Milou 9
  10. 10. what is drush ?  The help command $ drush help $ drush help [command] Gain productivity with Drush - Emmanuel Milou 10
  11. 11. what is drush ?  Examples: ● ● ● ● ● ● ● Installing sites Updating sites Downloading and enabling modules / themes Clearing caches Administring sites configuration Archiving sites ... Gain productivity with Drush - Emmanuel Milou 11
  12. 12. Do I need to use drush ? Gain productivity with Drush - Emmanuel Milou 12
  13. 13. Yes :-) Gain productivity with Drush - Emmanuel Milou 13
  14. 14. why do I need to use drush  Swiss-knife  Work faster  Automate tasks  Handle multiple sites  Create your own command Gain productivity with Drush - Emmanuel Milou 14
  15. 15. Installing drush Gain productivity with Drush - Emmanuel Milou 15
  16. 16. installing drush https://github.com/drush-ops/drush Gain productivity with Drush - Emmanuel Milou 16
  17. 17. installing drush  Multi plateform: ● ● Unix-like OS Windows Specific instructions on different platforms: https://drupal.org/node/1791676 Gain productivity with Drush - Emmanuel Milou 17
  18. 18. installing drush  Example on Ubuntu (need php-pear library) pear channel­discover   pear.drush.org pear install drush/drush drush version Gain productivity with Drush - Emmanuel Milou 18
  19. 19. Installing a site and introspection Gain productivity with Drush - Emmanuel Milou 19
  20. 20. site installation and instrospection  Download the latest stable release: $ drush pm­download drupal­7.x  Run installation procedure: $ drush site­install  ­­db­url=mysql://user:pass@localho st:port/dbname Gain productivity with Drush - Emmanuel Milou 20
  21. 21. site installation and instrospection  More on site-install command: $ drush site­install [profile]  ­­db­url=mysql://user:pass@hostname [:port]/database ­­site­name=”Dev site” ­­account­mail=admin@mysite.com ­­account­name=admin ­­account­pass=admin Gain productivity with Drush - Emmanuel Milou 21
  22. 22. site installation and instrospection  After a successful bootstrap $ drush status  General view of the current installation: ● ● ● ● Database credentials Drupal root Files directory path Admin theme Gain productivity with Drush - Emmanuel Milou 22
  23. 23. Downloading and managing modules Gain productivity with Drush - Emmanuel Milou 23
  24. 24. downloading and managing modules  Available commands to: ● ● ● List all modules: $ drush pm­list Download a module / theme: $ drush pm­download <modulename> Enable a module / theme: $ drush pm­enable <modulename> Gain productivity with Drush - Emmanuel Milou 24
  25. 25. downloading and managing modules Installing and enabling a module used to look like this: Gain productivity with Drush - Emmanuel Milou 25
  26. 26. downloading and managing modules 1. Find the module's exact name 2. Look up for the module on drupal.org 3. Download the archive module from project page 4. Go on your disk and extract the archive 5. Copy the module directory in the drupal installation 6. Go to module admin page 7. Realize the module needs an other module to be enabled 8. Repeat from step 2 Gain productivity with Drush - Emmanuel Milou 26
  27. 27. downloading and managing modules Now with drush, it is: Gain productivity with Drush - Emmanuel Milou 27
  28. 28. downloading and managing modules  Find the module's exact name  Run: $ drush dl <modulename> $ drush enable <modulename> Gain productivity with Drush - Emmanuel Milou 28
  29. 29. downloading and managing modules  Download modules in sites/all/modules ­­destination=<path> to override  Specify specific version to download: ● ● ● Latest dev release: $ drush dl views ­­dev List of recent releases:  $ drush dl views ­­select Specific version: $ drush dl 7.x­3.x­dev Gain productivity with Drush - Emmanuel Milou 29
  30. 30. downloading and managing modules $ drush en views  Handles module's dependencies by: ● ● Downloading the required modules Enabling them Gain productivity with Drush - Emmanuel Milou 30
  31. 31. downloading and managing modules  Disable a module: $ drush pm­disable <modulename>  Uninstall a module: $ drush pm­uninstall <modulename> Gain productivity with Drush - Emmanuel Milou 31
  32. 32. Updating a site Gain productivity with Drush - Emmanuel Milou 32
  33. 33. updating a site  One of the most tedious task ever ...  Gets a lot easier with drush: $ drush pm­update  Core, non-core, modules, security, ... Gain productivity with Drush - Emmanuel Milou 33
  34. 34. Database management Gain productivity with Drush - Emmanuel Milou 34
  35. 35. database management  Open a SQL cli, using Drupal's credentials $ drush sql­cli  Database synchronisation between sites  Run a custom query   $ drush sql­query “SELECT title from  node where nid=1” Import SQL statements from a file $ drush sql­query –file=example.sql Gain productivity with Drush - Emmanuel Milou 35
  36. 36. database management  Database backup $ drush sql­dump  –result­file=dbname.sql –gzip  ● ● Save to a file Compress the dump Gain productivity with Drush - Emmanuel Milou 36
  37. 37. database management  Shortcut for accessing variables table: $ drush variable­get clean_url  Write is supported: $ drush variable­set clean_url 0 Gain productivity with Drush - Emmanuel Milou 37
  38. 38. Controlling the cache and cron Gain productivity with Drush - Emmanuel Milou 38
  39. 39. controlling the cache and cron  Clear database cache tables $ drush cc all  Clear a specific cache views, menu, block, theme-registry, ... $ drush cc views ● Gain productivity with Drush - Emmanuel Milou 39
  40. 40. controlling the cache and cron  Run cron $ drush cron Gain productivity with Drush - Emmanuel Milou 40
  41. 41. Archiving and restoring site Gain productivity with Drush - Emmanuel Milou 41
  42. 42. archiving and restoring site  Backup the code, files and database in a single archive $ drush archive­dump  –tar­options="­­exclude=.git  –exclude=sites/default/files"                                   Gain productivity with Drush - Emmanuel Milou 42
  43. 43. archiving and restoring site  Restore an archive $ drush archive­restore file                                   Gain productivity with Drush - Emmanuel Milou 43
  44. 44. Drush aliases Gain productivity with Drush - Emmanuel Milou 44
  45. 45. drush aliases   Run drush commands on other local or remote Drupal installations Site aliases: ● ● @self: current installation Custom: • @dev • @staging • @prod Gain productivity with Drush - Emmanuel Milou 45
  46. 46. drush aliases  Shortcut: $ drush  ­­uri=”camp2013.drushdemo.staging”  ­­root=”/home/manu/dev/drupal/camp 2013/staging/src/drupal” status becomes: $ drush @dev status Gain productivity with Drush - Emmanuel Milou 46
  47. 47. drush aliases <?php /** * Drush aliases definition */ $aliases['staging'] = array( 'root' => '/var/www/staging', 'uri' => 'staging.bibliopoly.sfl.net', 'remote-host' =>'staging.bibliopoly.sfl.net', 'remote-user' => 'webmaster', 'path-aliases' => array( '%files' => 'sites/default/files', ), ); $aliases['prod'] = array( 'root' => '/var/www/prod', 'uri' => 'live.bibliopoly.sfl.net', 'remote-host' =>'live.bibliopoly.sfl.net', 'remote-user' => 'webmaster', 'path-aliases' => array( '%files' => 'sites/default/files', ), ); Gain productivity with Drush - Emmanuel Milou 47
  48. 48. drush aliases drush status == drush @self status  The simplest example: $ drush @prod status  More advanced: $ drush sql­sync @prod @dev $ drush rsync @staging @prod $ drush rsync @prod:%files         @staging:%files Gain productivity with Drush - Emmanuel Milou 48
  49. 49. drush aliases Documentation: http://drupalcontrib.org/api/drupal/contribu tions!drush!examples!example.aliases.drushrc .php/7 Gain productivity with Drush - Emmanuel Milou 49
  50. 50. Third-party integration Gain productivity with Drush - Emmanuel Milou 50
  51. 51. Third-party integration  3 easy steps to create a custom drush command: ● ● ● Create a command file called COMMANDFILE.drush.inc Implement the function COMMANDFILE_drush_command() Implement the functions that your command will call Gain productivity with Drush - Emmanuel Milou 51
  52. 52. Third-party integration  Drush searches for commandfiles in the following location: ● The “/path/to/drush/command” folder ● Folders listed in the “include” folder ● ● ● ● The system-wide drush commands folder, e.g. /usr/share/drush/commands The ".drush" folder in the user's HOME folder /drush and /sites/all/drush in the current Drupal installation All enabled modules in the current Drupal installation Gain productivity with Drush - Emmanuel Milou 52
  53. 53. Third-party integration  Complete documentation: $ drush topic docs­commands Gain productivity with Drush - Emmanuel Milou 53
  54. 54. Third-party integration  Custom module integration ● ● ● Create a file called MODULENAME.drush.inc in the module directory Add the reference in MODULENAME.info • files[] = MODULENAME.drush.inc Implement hook_drush_command() • MODULENAME_drush_command() Gain productivity with Drush - Emmanuel Milou 54
  55. 55. Third-party integration  devel_generate module ●  views module ●  Enable, disable, list, revert views, ... features module ●  Generate content, users, terms, ... Create, export, revert features, ... coder_review module ● Run code reviews Gain productivity with Drush - Emmanuel Milou 55
  56. 56. Example: deploying a site Gain productivity with Drush - Emmanuel Milou 56
  57. 57. Example: deploying a site (1) Put production site in maintenance (2) Sync files from the staging env to production (3) Run any pending database updates on the production site (4) Clear the cache on production (5) Put production site back online Gain productivity with Drush - Emmanuel Milou 57
  58. 58. Example: deploying a site  The perfect setup:      Continuous-integration server: Jenkins Source code management: git A development environment A staging environment A production environment Gain productivity with Drush - Emmanuel Milou 58
  59. 59. Example: deploying a site   Source code changes are pushed from dev into git repository Jenkins polls the git repository for new commits  Jenkins checkout latest revision  Jenkins runs automatic tests  Jenkins deploys latest revision to the staging environment if tests passed Gain productivity with Drush - Emmanuel Milou 59
  60. 60. Example: deploying a site $ drush ­­yes @prod vset maintenance_mode 1 $ drush ­­yes rsync –verbose @staging @prod $ drush ­­yes @prod updatedb $ drush @prod clear­cache all $ drush ­­yes @prod vset maintenance_mode 0 Gain productivity with Drush - Emmanuel Milou 60
  61. 61. Example: deploying a site  Code synchronisation: ✗ @prod -> @staging ✔ @staging -> @prod Files synchronisation: ✗  Database synchronisation: ✗ ✔ @prod -> @staging @staging -> @prod ✔ @staging -> @prod @prod -> @staging Gain productivity with Drush - Emmanuel Milou 61
  62. 62. Thank you :-) And don't forget to get a badge ... Gain productivity with Drush - Emmanuel Milou 62
  63. 63. Gain productivity with Drush - Emmanuel Milou 63

×