2 namespace Drush\Commands;
4 use Consolidation\AnnotatedCommand\CommandData;
5 use Drush\Commands\DrushCommands;
7 use Symfony\Component\Filesystem\Filesystem;
10 * Load this commandfile using the --include option - e.g. `drush --include=/path/to/drush/examples`
13 class SyncViaHttpCommands extends DrushCommands
17 * When a hook extends a command with additional options, it must
18 * implement declare those option(s) in a @hook option like this one. Doing so will add
19 * the option to the help text for the modified command, and will also
20 * allow the new option to be specified on the command line. Without
21 * this, Drush will fail with an error when a user attempts to use
24 * @hook option sql-sync
25 * @option http-sync Copy the database via http instead of rsync. Value is the url that the existing database dump can be found at.
26 * @option http-sync-user Username for the protected directory containing the sql dump.
27 * @option http-sync-password Password for the same directory.
29 public function optionsetSqlSync()
34 * During the pre hook, determine if the http-sync option has been
35 * specified. If it has been, then disable the normal ssh + rsync
36 * dump-and-transfer that sql-sync usually does, and transfer the
37 * database dump via an http download.
39 * @hook pre-command sql-sync
41 public function preSqlSync(CommandData $commandData)
43 $sql_dump_download_url = $commandData->input()->getOption('http-sync');
44 if (!empty($sql_dump_download_url)) {
45 $user = $commandData->input()->getOption('http-sync-user');
46 $password = $commandData->input()->getOption('http-sync-password');
47 $source_dump_file = $this->downloadFile($sql_dump_download_url, $user, $password);
48 $commandData->input()->setOption('target-dump', $source_dump_file);
49 $commandData->input()->setOption('no-dump', true);
50 $commandData->input()->setOption('no-sync', true);
57 * Optionally uses user authentication, using either wget or curl, as available.
59 protected function downloadFile($url, $user = false, $password = false, $destination = false, $overwrite = true)
62 if ($use_wget === null) {
63 $use_wget = drush_shell_exec('which wget');
66 $destination_tmp = drush_tempnam('download_file');
68 if ($user && $password) {
69 drush_shell_exec("wget -q --timeout=30 --user=%s --password=%s -O %s %s", $user, $password, $destination_tmp, $url);
71 drush_shell_exec("wget -q --timeout=30 -O %s %s", $destination_tmp, $url);
74 if ($user && $password) {
75 drush_shell_exec("curl -s -L --connect-timeout 30 --user %s:%s -o %s %s", $user, $password, $destination_tmp, $url);
77 drush_shell_exec("curl -s -L --connect-timeout 30 -o %s %s", $destination_tmp, $url);
80 if (!Drush::simulate()) {
81 if (!drush_file_not_empty($destination_tmp) && $file = @file_get_contents($url)) {
82 @file_put_contents($destination_tmp, $file);
84 if (!drush_file_not_empty($destination_tmp)) {
86 throw new \Exception(dt("The URL !url could not be downloaded.", ['!url' => $url]));
90 $fs = new Filesystem();
91 $fs->rename($destination_tmp, $destination, $overwrite);
94 return $destination_tmp;