backup.provision.inc 2.71 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($url = NULL, $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. 
24
  if (!is_dir(drush_get_option('backup_path'))) {
25
    drush_set_error('PROVISION_BACKUP_PATH_NOT_FOUND');
26
  }
27

28
  if ($backup_file) {
anarcat's avatar
anarcat committed
29
    if (provision_path("exists", $backup_file, FALSE,
30 31
            dt("Backing site up to @path."),
            dt("Back up file @path already exists."),
32
        'PROVISION_BACKUP_ALREADY_EXISTS')) {
anarcat's avatar
anarcat committed
33
      drush_set_option('backup_file', $backup_file);
34 35
    }
  }
36

37
  if (!$backup_file) {
38
    $suggested = drush_get_option('backup_path') ."/$url-". date("Ymd.His", mktime()) .".tar.gz";
39 40

    // Use format of mysite.com-2008-01-02, if already existing, add number.
anarcat's avatar
anarcat committed
41
    while (is_file($suggested)) {
42
      $count++;
43
      $suggested = drush_get_option('backup_path') ."/$url-". date("Ymd.His", mktime()) ."_$count.tar.gz";
44
    }
anarcat's avatar
anarcat committed
45
    drush_set_option('backup_file', $suggested);
46
  }
anarcat's avatar
anarcat committed
47

48 49 50 51 52
}

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

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

/**
 * Remove the backup file if something went wrong
 */
mig5's avatar
mig5 committed
77
function drush_provision_drupal_provision_backup_rollback() {
78 79 80 81 82 83
  $backup_file = drush_get_option('backup_file');
  if (file_exists($backup_file)) {
    provision_path('unlink', $backup_file, TRUE, 
          dt("Removed stale backup file $backup_file"), dt("Failed deleting backup file $backup_file"));
  }
}