backup.provision.inc 4.64 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
      drush_set_error('PROVISION_BACKUP_ALREADY_EXISTS', dt('Back up file @path already exists.', array('@path' => $backup_file)));
    }
    else {
33
      drush_log(dt('Backing site up to @path.', array('@path' => $backup_file)));
34
      drush_set_option('backup_file', $backup_file);
35 36
    }
  }
37

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

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

50 51 52 53 54
}

/**
 * Implentation of hook_provision_backup()
 */
55
function drush_provision_drupal_provision_backup() {
56
  $backup_file = drush_get_option('backup_file');
57
  // Adds the site directory into the backup file
58
  drush_log(dt("Adding sites directory to !backup_file", array('!backup_file' => $backup_file)), 'backup');
59 60 61


  // synch all filesystem changes back from the remote server.
62
  provision_drupal_fetch_site();
63

64
  // Check if we are currently cloaking credentials
65 66
  $cloaked = d()->service('http')->cloaked_db_creds();
  $cloaked = drush_get_option('provision_db_cloaking', $cloaked);
67 68 69 70 71 72 73 74 75

  if ($cloaked) {
    drush_set_option('cloaking_off_temp', TRUE);
    // Disable the cloaking of credentials temporarily
    drush_log(dt("Temporarily uncloaking database credentials for backup"));
    drush_set_option('provision_db_cloaking', FALSE);

    // Write the uncloaked credentials to the settings.php
    _provision_drupal_create_settings_file();
76
    provision_drupal_push_site();
77

78 79
  }

80 81
  $olddir = getcwd();
  // we need to do this because some retarded implementations of tar (e.g. SunOS) don't support -C
82

83 84 85
  if (!chdir(d()->site_path)) {
    return drush_set_error('PROVISION_BACKUP_PATH_NOT_FOUND', dt('cannot change directory to %dir', array('%dir' => d()->site_path)));
  }
86
  if (substr($backup_file, -2) == 'gz') {
87
    // same as above: some do not support -z
88
    $command = 'tar cpf - . | gzip -c > %s';
89
  } else {
90
    $command = 'tar cpf %s .';
91
  }
92
  $result = drush_shell_exec($command,  $backup_file);
93 94 95 96 97

  // Get the size of the backup
  $size = filesize($backup_file);
  drush_set_option('backup_file_size', $size);

98
  chdir($olddir);
99

100 101 102 103
  if (drush_get_option('cloaking_off_temp', FALSE)) {
    drush_log(dt("Re-cloaking database credentials after backup"));
    drush_set_option('provision_db_cloaking', TRUE);
    _provision_drupal_create_settings_file();
104
    provision_drupal_push_site();
105

106 107
  }

108
  if (!$result && !drush_get_option('force', false)) {
109
    drush_set_error('PROVISION_BACKUP_FAILED', dt("Could not back up sites directory for drupal"));
110 111
  }
}
112

113 114
function drush_provision_drupal_post_provision_backup() {
  drush_log(dt('Backed up site up to @path.', array('@path' => drush_get_option('backup_file'))), 'success');
115 116 117 118 119 120 121
  if (d()->client_name) {
    $backup_dir = d()->server->clients_path . '/' . d()->client_name . '/backups';
    provision_file()->create_dir($backup_dir, dt('Client backup directory for @client', array('@client' => d()->client_name)), 0750);
    provision_file()->symlink(drush_get_option('backup_file'), $backup_dir . '/' . basename(drush_get_option('backup_file')))
      ->succeed('Created symlink @path to @target')
      ->fail('Could not create symlink @path to @target: @reason');
  }
122 123
}

124 125 126
/**
 * Remove the backup file if something went wrong
 */
mig5's avatar
mig5 committed
127
function drush_provision_drupal_provision_backup_rollback() {
128
  $backup_file = drush_get_option('backup_file');
129
  if (is_readable($backup_file) && is_file($backup_file)) {
130
    provision_file()->unlink($backup_file)
131 132
      ->succeed('Removed stale backup file @path')
      ->fail('Failed deleting backup file @path');
133 134
  }
}