sync_via_http.drush.inc

  1. 8.0.x examples/sync_via_http.drush.inc
  2. 6.x examples/sync_via_http.drush.inc
  3. 7.x examples/sync_via_http.drush.inc
  4. 5.x examples/sync_via_http.drush.inc
  5. master examples/sync_via_http.drush.inc

Example "Sync via HTTP" sql-sync command alter.

Sync_via_http allows you to sql-sync your database using HTTP (e.g. wget or curl) instead of rsync. This is helpful for exporting your database to colaborators without shell access to the production or staging server.

For example:

$aliases['staging'] = array (
  'root' => '/srv/www/drupal',
  'uri' => 'staging.site.com',
  'source-command-specific' => array(
    'sql-sync'  => array(
      'http-sync'  => 'https://staging.site.com/protected-directory/site-database-dump.sql',
      'http-sync-user' => 'wwwadmin',
      'http-sync-password' => 'secretsecret',
    ),
  ),
);

To use this feature, copy the 'source-command-specific' item from the example alias above, place it in your staging site aliases, and custom the access credentials as necessary. You must also copy the sync_via_http.drush.inc file to a location where Drush will find it, such as $HOME/.drush. See `drush topic docs-commands` for more information.

IMPORTANT NOTE: This example does not cause the sql dump to be performed; it is presumed that the dump file already exists at the provided URL. For a full solution, a web page that initiated an sql-dump (or perhaps a local sql-sync followed by an sql-sanitize and then an sql-dump) would be necessary.

Functions

File

examples/sync_via_http.drush.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Example "Sync via HTTP" sql-sync command alter.
  5. *
  6. * Sync_via_http allows you to sql-sync your database using HTTP
  7. * (e.g. wget or curl) instead of rsync. This is helpful for
  8. * exporting your database to colaborators without shell access
  9. * to the production or staging server.
  10. *
  11. * For example:
  12. *
  13. * @code
  14. * $aliases['staging'] = array (
  15. * 'root' => '/srv/www/drupal',
  16. * 'uri' => 'staging.site.com',
  17. * 'source-command-specific' => array(
  18. * 'sql-sync' => array(
  19. * 'http-sync' => 'https://staging.site.com/protected-directory/site-database-dump.sql',
  20. * 'http-sync-user' => 'wwwadmin',
  21. * 'http-sync-password' => 'secretsecret',
  22. * ),
  23. * ),
  24. * );
  25. * @endcode
  26. *
  27. * To use this feature, copy the 'source-command-specific'
  28. * item from the example alias above, place it in your staging
  29. * site aliases, and custom the access credentials as
  30. * necessary. You must also copy the sync_via_http.drush.inc
  31. * file to a location where Drush will find it, such as
  32. * $HOME/.drush. See `drush topic docs-commands` for more
  33. * information.
  34. *
  35. * IMPORTANT NOTE: This example does not cause the sql dump
  36. * to be performed; it is presumed that the dump file already
  37. * exists at the provided URL. For a full solution, a web page
  38. * that initiated an sql-dump (or perhaps a local sql-sync followed
  39. * by an sql-sanitize and then an sql-dump) would be necessary.
  40. */
  41. /**
  42. * Implements hook_drush_help_alter().
  43. *
  44. * When a hook extends a command with additional options, it must
  45. * implement help alter and declare the option(s). Doing so will add
  46. * the option to the help text for the modified command, and will also
  47. * allow the new option to be specified on the command line. Without
  48. * this, Drush will fail with an error when a user attempts to use
  49. * the option.
  50. */
  51. function sync_via_http_drush_help_alter(&$command) {
  52. if ($command['command'] == 'sql-sync') {
  53. $command['options']['http-sync'] = "Copy the database via http instead of rsync. Value is the url that the existing database dump can be found at.";
  54. $command['sub-options']['http-sync']['http-sync-user'] = "Username for the protected directory containing the sql dump.";
  55. $command['sub-options']['http-sync']['http-sync-password'] = "Password for the same directory.";
  56. }
  57. }
  58. /**
  59. * Implements drush_hook_pre_COMMAND().
  60. *
  61. * During the pre hook, determine if the http-sync option has been
  62. * specified. If it has been, then disable the normal ssh + rsync
  63. * dump-and-transfer that sql-sync usually does, and transfer the
  64. * database dump via an http download.
  65. */
  66. function drush_sync_via_http_pre_sql_sync($source = NULL, $destination = NULL) {
  67. $sql_dump_download_url = drush_get_option('http-sync');
  68. if (!empty($sql_dump_download_url)) {
  69. $user = drush_get_option('http-sync-user', FALSE);
  70. $password = drush_get_option('http-sync-password', FALSE);
  71. $source_dump_file = _drush_sync_via_http_download_file($sql_dump_download_url, $user, $password);
  72. if ($source_dump_file === FALSE) {
  73. return drush_set_error('DRUSH_CANNOT_DOWNLOAD', dt("The URL !url could not be downloaded.", array('!url' => $sql_dump_download_url)));
  74. }
  75. drush_set_option('target-dump', $source_dump_file);
  76. drush_set_option('no-dump', TRUE);
  77. drush_set_option('no-sync', TRUE);
  78. }
  79. }
  80. /**
  81. * Downloads a files.
  82. *
  83. * Optionaly uses user authentication, using either wget or curl, as available.
  84. */
  85. function _drush_sync_via_http_download_file($url, $user = FALSE, $password = FALSE, $destination = FALSE, $overwrite = TRUE) {
  86. static $use_wget;
  87. if ($use_wget === NULL) {
  88. $use_wget = drush_shell_exec('which wget');
  89. }
  90. $destination_tmp = drush_tempnam('download_file');
  91. if ($use_wget) {
  92. if ($user && $password) {
  93. drush_shell_exec("wget -q --timeout=30 --user=%s --password=%s -O %s %s", $user, $password, $destination_tmp, $url);
  94. }
  95. else {
  96. drush_shell_exec("wget -q --timeout=30 -O %s %s", $destination_tmp, $url);
  97. }
  98. }
  99. else {
  100. if ($user && $password) {
  101. drush_shell_exec("curl -s -L --connect-timeout 30 --user %s:%s -o %s %s", $user, $password, $destination_tmp, $url);
  102. }
  103. else {
  104. drush_shell_exec("curl -s -L --connect-timeout 30 -o %s %s", $destination_tmp, $url);
  105. }
  106. }
  107. if (!drush_get_context('DRUSH_SIMULATE')) {
  108. if (!drush_file_not_empty($destination_tmp) && $file = @file_get_contents($url)) {
  109. @file_put_contents($destination_tmp, $file);
  110. }
  111. if (!drush_file_not_empty($destination_tmp)) {
  112. // Download failed.
  113. return FALSE;
  114. }
  115. }
  116. if ($destination) {
  117. drush_move_dir($destination_tmp, $destination, $overwrite);
  118. return $destination;
  119. }
  120. return $destination_tmp;
  121. }