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

Functions

Namesort descending Description
drush_sync_via_http_pre_sql_sync Implement hook pre sql sync.
sync_via_http_drush_help_alter Implement hook help alter.
_drush_sync_via_http_download_file Download a file, optionaly with user authentication, using either wget or curl, as available.

File

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