Automatic Drush site aliases

Public

Automatically create Drush site aliases for your Drupal multi-site environment.
Inspired by: http://lcadvanced.com/blog/drush-pro-tip-automatic-site-aliases

Place the following in your aliases.drushrc.php file (see: https://github.com/drush-ops/drush/blob/master/examples/example.aliases....).

</> CopyGet raw version
php
  1. $drupal = '/var/www/drupal';
  2. $aliases = array();
  3.  
  4. // Automatic alias for each Drupal site
  5. $site = new DirectoryIterator($drupal . '/sites');
  6. while ($site->valid()) {
  7. // Look for directories containing a 'settings.php' file
  8. if ($site->isDir() && !$site->isDot() && !$site->isLink()) {
  9. if (file_exists($site->getPathname() . '/settings.php')) {
  10. // Add site alias
  11. $basename = $site->getBasename();
  12. $aliases[$basename] = array(
  13. 'uri' => $basename,
  14. 'root' => $drupal,
  15. );
  16. }
  17. }
  18. $site->next();
  19. }
  20.  
  21. // Get all site aliases
  22. $all = array();
  23. foreach ($aliases as $name => $definition) {
  24. $all[] = '@' . $name;
  25. }
  26.  
  27. // 'All' alias group
  28. $aliases['all'] = array(
  29. 'site-list' => $all,
  30. );