backup.provision.inc 2.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
<?php

/**
 * Provision backup command
 *
 * Back up an existing site
 */

/**
 * Make sure the site is installed and enabled, and that we have a valid target to back up to.
 */
12
function drush_provision_drupal_provision_backup_validate($backup_file = NULL) {
13
  if (!@drush_bootstrap(DRUSH_BOOTSTRAP_DRUPAL_CONFIGURATION)) {
14 15 16 17 18
    if (drush_get_option('force', false)) {
      drush_log("clearing error");
      drush_set_context('DRUSH_ERROR_CODE', DRUSH_SUCCESS);
    }
  }
19
  if (!drush_get_option('installed') && !drush_get_option('force', false)) {
20
     drush_set_error('PROVISION_DRUPAL_SITE_NOT_FOUND');
21
   }
22 23

  // This is the actual drupal provisioning requirements. 
drumm's avatar
drumm committed
24
  if (!is_dir(d()->platform->server->backup_path)) {
25
    drush_set_error('PROVISION_BACKUP_PATH_NOT_FOUND');
26
  }
27

28
  if ($backup_file) {
29
    if (provision_file()->exists($backup_file)->status()) {
30 31 32 33
      drush_set_error('PROVISION_BACKUP_ALREADY_EXISTS', dt('Back up file @path already exists.', array('@path' => $backup_file)));
    }
    else {
      drush_log(dt('Backing site up to @path.', array('@path' => $backup_file)), 'message');
34
      drush_set_option('backup_file', $backup_file);
35 36
    }
  }
37

38
  if (!$backup_file) {
Adrian Rossouw's avatar
Adrian Rossouw committed
39
    $suggested = d()->platform->server->backup_path . '/' . d()->uri . '-' . date("Ymd.His", mktime()) . '.tar.gz';
40 41

    // Use format of mysite.com-2008-01-02, if already existing, add number.
42
    while (is_file($suggested)) {
43
      $count++;
Adrian Rossouw's avatar
Adrian Rossouw committed
44
      $suggested = d()->platform->server->backup_path . '/' . d()->uri . '-' .  date('Ymd.His', mktime()) . '_' . $count . '.tar.gz';
45
    }
46
    drush_set_option('backup_file', $suggested);
47
  }
48

49 50 51 52 53
}

/**
 * Implentation of hook_provision_backup()
 */
54
function drush_provision_drupal_provision_backup() {
55
  $backup_file = drush_get_option('backup_file');
56
  // Adds the site directory into the backup file
57
  drush_log(dt("Adding sites directory to !backup_file", array('!backup_file' => $backup_file)), 'backup');
58 59
  $olddir = getcwd();
  // we need to do this because some retarded implementations of tar (e.g. SunOS) don't support -C
60
  chdir(d()->site_path);
61
  if (substr($backup_file, -2) == 'gz') {
62 63
    // same as above: some do not support -z
    $command = "tar cpf - . | gzip -c > %s";
64
  } else {
65
    $command = "tar cpf %s .";
66
  }
67
  $result = drush_shell_exec($command,  $backup_file);
68
  chdir($olddir);
69

70
  if (!$result && !drush_get_option('force', false)) {
71
    drush_set_error('PROVISION_BACKUP_FAILED', dt("Could not back up sites directory for drupal"));
72 73
  }
}
74 75 76 77

/**
 * Remove the backup file if something went wrong
 */
mig5's avatar
mig5 committed
78
function drush_provision_drupal_provision_backup_rollback() {
79 80
  $backup_file = drush_get_option('backup_file');
  if (file_exists($backup_file)) {
81
    provision_file()->unlink($backup_file)
82 83
      ->succeed('Removed stale backup file @path')
      ->fail('Failed deleting backup file @path');
84 85
  }
}