Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
SlideShare a Scribd company logo
A Drush Primer
DrupalCamp Chattanooga 2013
Chris Hales
DevOps Manager
Twitter: @chris_hales
IRC: chales
@Mediacurrent

Not on the Agenda
●
●
●
●

General Drupal administration
How to use the terminal
How to install Drush (RTFM)
Really deep dive examples (well, just a few)
@Mediacurrent

Agenda
●
●
●
●
●
●
●
●

What is Drush?
Some history
Basic use
Simple commands to make your life easier
Drush aliases
Advanced commands
Custom combinations
Drush Make
Drush
Well over 889,000 downloads
Integration with 276+ modules
Packages available for most systems
What is

Drush?
“A command line shell and
scripting interface for
Drupal.”
www.drush.org
@Mediacurrent

What is it?
The main focus of Drush is to take complex commands that you could
perform within the terminal and shorten them into simplified and easy to
remember commands.
rsync -vldkogDtprz ssh root@192.168.1.128:
/var/www/drupal/sites/default/files
/local/www/drupal/sites/default/files
Or if you use Drush with an alias setup for your dev site you can use the
following command.
$ drush rsync @mysite.dev:%files @mysite.local:%files
@Mediacurrent

History
Drush is a command line utility to assist in Drupal site building,
maintenance and administration tasks.
It is a Drupal contrib project but it’s not a module. On *nix systems it’s run
as a shell script or a bat on Windows. The guts are written in PHP so most
Drupal developers should understand the inner workings.
The first commit to the project was made on Nov 12, 2006 by Arto
Bendiken. He made the first few commits that started the 4.x release.
Drupal legend Moshe Weitzman has been the
primary maintainer for the past few years but
he’s had a lot of help. At this time there are
165 contributors to the project.
@Mediacurrent

History
The current version of Drush is version 7 which is compatible with
Drupal 6, 7 and 8.
The Drush project moved to Github in August 2013 however you can still
find general info on the original Drupal hosted project page.
http://drupal.org/project/drush
Look for Drush at it’s new home on Github
https://github.com/drush-ops/drush
A Drush Primer - DrupalCamp Chattanooga 2013
The

Basics
@Mediacurrent

Getting Help
Review the Drush documentation online
http://www.drush.org/help/docs
Resources on Drupal.org
https://drupal.org/node/477684
https://drupal.org/drush-faq
http://www.drush.org/resources
https://groups.drupal.org/node/28088
Drush itself has a very extensive help system
$ drush --help
$ drush help <command>

And don’t forget Google
is your friend!
@Mediacurrent

The Basics
To use Drush with remote hosts you must use SSH keys!
Drush can perform some commands against remote sites without the
need for Drush to be installed on the remote.
You must be within the context of a fully functional Drupal site to use
most commands. For multisites drill into the site directory with the
settings.php file of the site you wish to work with. If you see the
following error you are not within a functional site.
Command <command> needs a higher bootstrap level to run - you
will need to invoke drush from a more functional Drupal
environment to run this command.
[error]
A Drupal installation directory could not be found
@Mediacurrent

Commands
The main command structure usually looks like this.
$ drush <cmd> [[arg1] [arg2]...] [[option1] [option2]...]
Drush commands are usually in the form of
$ drush <command> <from site> <to site>
Common options that remove interaction.
-y(es) -n(o)
$ drush <command> <from site> <to site> -y
@Mediacurrent

Common Tasks
Here are a few of the common admin tasks.

●
●
●
●
●
●
●

Clear caches
Install modules
Update modules
Run update.php
Update configuration settings
Manual cron runs
Updating users
@Mediacurrent

Site Status
Check your site status.
$ cd /path/to/drupal/docroot
$ drush st
Drupal version
Site URI
Database driver
Database username
Database password
Database name
Database
Drupal bootstrap
Drupal user

:
:
:
:
:
:
:
:
:

6.28
mysite.dev
mysqli
user
pass
ag_www
Connected
Successful
Anonymous
@Mediacurrent

Site Status
Default theme
: mytheme
Administration theme : seven
Administration theme : seven
PHP executable
: /path/to/php
PHP configuration
: /path/to/conf/php.ini
PHP OS
: Darwin
Drush version
: 6.0
Drush configuration : "/path/to/drush/drushrc.php"
Drush alias files
: ~/.drush/my.drushrc.php
Drupal root
: /path/to/docroot
Site path
: sites/mysite.dev
File directory path : sites/default/files
Temporary file directory path
: /tmp
@Mediacurrent

Clearing Cache
Clearing caches.
$ cd /path/to/drupal/docroot
$ drush cc
Enter a number to choose which cache to clear.
[0]
: Cancel
[1]
: all
[2]
: drush
[3]
: theme-registry
[4]
: menu
[5]
: css-js
[6]
: block
[7]
: module-list
[8]
: theme-list
[9]
: registry
[10] : views
@Mediacurrent

Clearing Cache
Clear all caches.
$ cd /path/to/drupal/docroot
$ drush cc all
'all' cache was cleared in mysite.local [success]
Clear just a specific cache such as the CSS/JS cache
$ cd /path/to/drupal/docroot
$ drush cc css-js
'css-js' cache was cleared in mysite.local [success]
@Mediacurrent

Install Modules
Download the module.
$ cd /path/to/drupal/docroot
$ drush dl schema
Project schema (6.x-1.7) downloaded to
sites/all/modules/contrib/schema.
[success]
Then enable the module.
$ drush en schema
schema was enabled successfully.
You can also combine the commands.
$ drush dl scheme && drush en schema -y

[ok]
@Mediacurrent

Update Modules
View a list of available module updates.
(output abbreviated)
$ cd /path/to/drupal/docroot
$ drush up
Name
context
ctools
features
filefield

Installed Version
6.x-3.1
6.x-1.10+2-dev
6.x-1.x-dev
6.x-3.10

Proposed version Message
6.x-3.3
SECURITY UPDATE available
6.x-1.0
Update available
6.x-1.2
Update available
6.x-3.11
Update available

Do you really want to continue with the update process? (y/n):
@Mediacurrent

Update Modules
Update one or more modules by specifying them. The new version will be
downloaded, any database updates executed and caches cleared.
(output abbreviated)
$ cd /path/to/drupal/docroot
$ drush up context features
Do you really want to continue with the update process? (y/n): y
Project context was updated successfully. Installed version is
now 6.x-3.3.
Backups were saved into the directory /backups/context. [ok]
'all' cache was cleared in mysite.local
[success]
No database updates required
[success]
'all' cache was cleared in mysite.local
[success]
Finished performing updates.
[ok]
Advanced
@Mediacurrent

Drush Aliases
Drush aliases allow your to target the context of a site much easier.
$ cd /path/to/drupal/docroot
$ drush sa --full --with-optional @self
$aliases["self"] = array (
'root' => '/path/to/site/docroot',
'uri' => 'http://default',
'#name' => 'self',
'path-aliases' =>
array (
'%drush' => '/path/to/php/drush',
'%site' => 'sites/default/',
),
);
@Mediacurrent

Drush Aliases
Once you create an alias file and name the sites you wish to interact with
you can target the sites’ context and run any command against it.
For a full featured alais template you can use mine as a starting point,
https://gist.github.com/chales/2635760
Download a module to the local site.
$ drush @mysite.local dl admin_menu
Check production status.
$ drush @mysite.prod st
Clear caches on the staging site.
$ drush @mysite.stage cc all
@Mediacurrent

Drush Aliases
Now you can use alias shortcuts to perform commands such as syncing
your local site to the production site.
Sync the files in sites/default/files using the files path alias.
$ drush rsync @mysite.prod:%files @mysite.local:%files
Sync the database using sql-sync.
$ drush sql-sync @mysite.prod @mysite.local
@Mediacurrent

Database Dumps
Backup a database.
$ drush sql-dump --result-file=dump.sql
Backup a database using gzip compression.
$ drush sql-dump --gzip --result-file=dump.sql.gz
Similar but you can just use the command output and write it to a file.
$ drush sql-dump > /path/to/dump/file.sql
To restore this dump file you can use the Drush sql-query command.
$ drush sqlq < /path/to/dump/file.sql
@Mediacurrent

Site Install
You can install a full Drupal site.
$ cd /path/to/drupal/docroot
$ drush si --db-url=mysql://root:root@127.0.0.1/test_d7
--account-pass=abc123
(assumes you have a database setup already)
If you need to add the database you can also use the superuser options
which will then create the database as well.
--db-su=name --db-su-pw=password --accountpass=myadminpass
@Mediacurrent

Variables
List all available variables.
$ drush variable-get
Set maintenance mode.
$ drush variable-set maintenance_mode 1
maintenance_mode was set to "1".
[success]
Disable maintenance mode.
$ drush variable-set maintenance_mode 0
maintenance_mode was set to "0".
[success]
Thank You!

Questions?
@chris_hales

Mediacurrent.com

slideshare.net/chris_hales

More Related Content

A Drush Primer - DrupalCamp Chattanooga 2013

  • 1. A Drush Primer DrupalCamp Chattanooga 2013
  • 2. Chris Hales DevOps Manager Twitter: @chris_hales IRC: chales
  • 3. @Mediacurrent Not on the Agenda ● ● ● ● General Drupal administration How to use the terminal How to install Drush (RTFM) Really deep dive examples (well, just a few)
  • 4. @Mediacurrent Agenda ● ● ● ● ● ● ● ● What is Drush? Some history Basic use Simple commands to make your life easier Drush aliases Advanced commands Custom combinations Drush Make
  • 5. Drush Well over 889,000 downloads Integration with 276+ modules Packages available for most systems
  • 7. “A command line shell and scripting interface for Drupal.” www.drush.org
  • 8. @Mediacurrent What is it? The main focus of Drush is to take complex commands that you could perform within the terminal and shorten them into simplified and easy to remember commands. rsync -vldkogDtprz ssh root@192.168.1.128: /var/www/drupal/sites/default/files /local/www/drupal/sites/default/files Or if you use Drush with an alias setup for your dev site you can use the following command. $ drush rsync @mysite.dev:%files @mysite.local:%files
  • 9. @Mediacurrent History Drush is a command line utility to assist in Drupal site building, maintenance and administration tasks. It is a Drupal contrib project but it’s not a module. On *nix systems it’s run as a shell script or a bat on Windows. The guts are written in PHP so most Drupal developers should understand the inner workings. The first commit to the project was made on Nov 12, 2006 by Arto Bendiken. He made the first few commits that started the 4.x release. Drupal legend Moshe Weitzman has been the primary maintainer for the past few years but he’s had a lot of help. At this time there are 165 contributors to the project.
  • 10. @Mediacurrent History The current version of Drush is version 7 which is compatible with Drupal 6, 7 and 8. The Drush project moved to Github in August 2013 however you can still find general info on the original Drupal hosted project page. http://drupal.org/project/drush Look for Drush at it’s new home on Github https://github.com/drush-ops/drush
  • 13. @Mediacurrent Getting Help Review the Drush documentation online http://www.drush.org/help/docs Resources on Drupal.org https://drupal.org/node/477684 https://drupal.org/drush-faq http://www.drush.org/resources https://groups.drupal.org/node/28088 Drush itself has a very extensive help system $ drush --help $ drush help <command> And don’t forget Google is your friend!
  • 14. @Mediacurrent The Basics To use Drush with remote hosts you must use SSH keys! Drush can perform some commands against remote sites without the need for Drush to be installed on the remote. You must be within the context of a fully functional Drupal site to use most commands. For multisites drill into the site directory with the settings.php file of the site you wish to work with. If you see the following error you are not within a functional site. Command <command> needs a higher bootstrap level to run - you will need to invoke drush from a more functional Drupal environment to run this command. [error] A Drupal installation directory could not be found
  • 15. @Mediacurrent Commands The main command structure usually looks like this. $ drush <cmd> [[arg1] [arg2]...] [[option1] [option2]...] Drush commands are usually in the form of $ drush <command> <from site> <to site> Common options that remove interaction. -y(es) -n(o) $ drush <command> <from site> <to site> -y
  • 16. @Mediacurrent Common Tasks Here are a few of the common admin tasks. ● ● ● ● ● ● ● Clear caches Install modules Update modules Run update.php Update configuration settings Manual cron runs Updating users
  • 17. @Mediacurrent Site Status Check your site status. $ cd /path/to/drupal/docroot $ drush st Drupal version Site URI Database driver Database username Database password Database name Database Drupal bootstrap Drupal user : : : : : : : : : 6.28 mysite.dev mysqli user pass ag_www Connected Successful Anonymous
  • 18. @Mediacurrent Site Status Default theme : mytheme Administration theme : seven Administration theme : seven PHP executable : /path/to/php PHP configuration : /path/to/conf/php.ini PHP OS : Darwin Drush version : 6.0 Drush configuration : "/path/to/drush/drushrc.php" Drush alias files : ~/.drush/my.drushrc.php Drupal root : /path/to/docroot Site path : sites/mysite.dev File directory path : sites/default/files Temporary file directory path : /tmp
  • 19. @Mediacurrent Clearing Cache Clearing caches. $ cd /path/to/drupal/docroot $ drush cc Enter a number to choose which cache to clear. [0] : Cancel [1] : all [2] : drush [3] : theme-registry [4] : menu [5] : css-js [6] : block [7] : module-list [8] : theme-list [9] : registry [10] : views
  • 20. @Mediacurrent Clearing Cache Clear all caches. $ cd /path/to/drupal/docroot $ drush cc all 'all' cache was cleared in mysite.local [success] Clear just a specific cache such as the CSS/JS cache $ cd /path/to/drupal/docroot $ drush cc css-js 'css-js' cache was cleared in mysite.local [success]
  • 21. @Mediacurrent Install Modules Download the module. $ cd /path/to/drupal/docroot $ drush dl schema Project schema (6.x-1.7) downloaded to sites/all/modules/contrib/schema. [success] Then enable the module. $ drush en schema schema was enabled successfully. You can also combine the commands. $ drush dl scheme && drush en schema -y [ok]
  • 22. @Mediacurrent Update Modules View a list of available module updates. (output abbreviated) $ cd /path/to/drupal/docroot $ drush up Name context ctools features filefield Installed Version 6.x-3.1 6.x-1.10+2-dev 6.x-1.x-dev 6.x-3.10 Proposed version Message 6.x-3.3 SECURITY UPDATE available 6.x-1.0 Update available 6.x-1.2 Update available 6.x-3.11 Update available Do you really want to continue with the update process? (y/n):
  • 23. @Mediacurrent Update Modules Update one or more modules by specifying them. The new version will be downloaded, any database updates executed and caches cleared. (output abbreviated) $ cd /path/to/drupal/docroot $ drush up context features Do you really want to continue with the update process? (y/n): y Project context was updated successfully. Installed version is now 6.x-3.3. Backups were saved into the directory /backups/context. [ok] 'all' cache was cleared in mysite.local [success] No database updates required [success] 'all' cache was cleared in mysite.local [success] Finished performing updates. [ok]
  • 25. @Mediacurrent Drush Aliases Drush aliases allow your to target the context of a site much easier. $ cd /path/to/drupal/docroot $ drush sa --full --with-optional @self $aliases["self"] = array ( 'root' => '/path/to/site/docroot', 'uri' => 'http://default', '#name' => 'self', 'path-aliases' => array ( '%drush' => '/path/to/php/drush', '%site' => 'sites/default/', ), );
  • 26. @Mediacurrent Drush Aliases Once you create an alias file and name the sites you wish to interact with you can target the sites’ context and run any command against it. For a full featured alais template you can use mine as a starting point, https://gist.github.com/chales/2635760 Download a module to the local site. $ drush @mysite.local dl admin_menu Check production status. $ drush @mysite.prod st Clear caches on the staging site. $ drush @mysite.stage cc all
  • 27. @Mediacurrent Drush Aliases Now you can use alias shortcuts to perform commands such as syncing your local site to the production site. Sync the files in sites/default/files using the files path alias. $ drush rsync @mysite.prod:%files @mysite.local:%files Sync the database using sql-sync. $ drush sql-sync @mysite.prod @mysite.local
  • 28. @Mediacurrent Database Dumps Backup a database. $ drush sql-dump --result-file=dump.sql Backup a database using gzip compression. $ drush sql-dump --gzip --result-file=dump.sql.gz Similar but you can just use the command output and write it to a file. $ drush sql-dump > /path/to/dump/file.sql To restore this dump file you can use the Drush sql-query command. $ drush sqlq < /path/to/dump/file.sql
  • 29. @Mediacurrent Site Install You can install a full Drupal site. $ cd /path/to/drupal/docroot $ drush si --db-url=mysql://root:root@127.0.0.1/test_d7 --account-pass=abc123 (assumes you have a database setup already) If you need to add the database you can also use the superuser options which will then create the database as well. --db-su=name --db-su-pw=password --accountpass=myadminpass
  • 30. @Mediacurrent Variables List all available variables. $ drush variable-get Set maintenance mode. $ drush variable-set maintenance_mode 1 maintenance_mode was set to "1". [success] Disable maintenance mode. $ drush variable-set maintenance_mode 0 maintenance_mode was set to "0". [success]