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