backup.provision.inc 2.82 KB
Newer Older
1
2
<?php

3
function drush_provision_mysql_pre_provision_backup($url = NULL) {
4
5
  # set the umask to 077 so that the dump itself is generated so it's non-readable by the webserver
  umask(0077);
6
  drush_log("Generating mysql dump for $url.", 'backup');
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
  # mixed copy-paste of drush_shell_exec and provision_shell_exec
  $cmd = sprintf("mysqldump --defaults-file=/dev/fd/3 -rsites/%s/database.sql %s", escapeshellcmd($url), escapeshellcmd(drush_get_option('db_name'))); 
  drush_log($cmd);
  if (drush_get_context('DRUSH_VERBOSE') || drush_get_context('DRUSH_SIMULATE')) {
      drush_print('Executing: ' . $cmd, $indent);
  }

  if (drush_get_context('DRUSH_SIMULATE')) {
    return true;
  }

  # pipe handling code
  # we go through all this trouble to hide the password from the commandline, it's the most secure way (apart from writing a temporary file, which would create conflicts in parallel runs)
  $mycnf = sprintf('[client]
host=%s
user=%s
password=%s
', drush_get_option('db_host'), drush_get_option('db_user'), drush_get_option('db_passwd'));

  $descriptorspec = array(
   // 0 => array("pipe", "r"),  // this would be stdin, but we don't need to input into mysqldump
   1 => array("pipe", "w"),  // stdout is a pipe that the child will write to
   2 => array("pipe", "w"),  // stderr is a file to write to
   3 => array("pipe", "r"),  // fd3 is our special file descriptor where we pass credentials
  );

  $process = proc_open($cmd, $descriptorspec, $pipes);

  $output = array();
  if (is_resource($process)) {
    fwrite($pipes[3], $mycnf);
    fclose($pipes[3]);

    $output = array_filter(array_merge(explode("\n", stream_get_contents($pipes[1])), explode("\n", stream_get_contents($pipes[2]))));
    // "It is important that you close any pipes before calling
    // proc_close in order to avoid a deadlock"
    fclose($pipes[1]);
    fclose($pipes[2]);
    $return_value = proc_close($process);
  } else {
    // XXX: failed to execute? unsure when this happens
    $return_value = -1;
  }

  # resuming drush_exec copy/paste
  $indent = 0;

  _drush_shell_exec_output_set($output);

  if (drush_get_context('DRUSH_VERBOSE')) {
    foreach ($output as $line) {
      drush_print($line, $indent + 2);
    }
  }

  $result = ($return_value == 0);
63
  if (!$result && !drush_get_option('force', false)) {
64
    drush_set_error('PROVISION_BACKUP_FAILED', dt("Could not generate database backup from mysqldump"));
65
  }
66
67
  # reset the umask to normal permissions
  umask(0022);
68
69
}

70
function drush_provision_mysql_pre_provision_backup_rollback($url = NULL) {
71
  provision_path("unlink", drush_get_option('sites_path') . "/$url/database.sql", TRUE, dt("Deleted mysql dump from sites directory"), 
72
  dt("Could not delete mysql dump from sites directory"));
73
74
}

75
function drush_provision_mysql_post_provision_backup($url = NULL) {
76
  drush_provision_mysql_pre_provision_backup_rollback($url);
77
}
78