backup.provision.inc 1.89 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
<?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.
 */
function provision_drupal_provision_backup_validate(&$data, $url = NULL, $backup_file = NULL) {
  _provision_drupal_url_required();
  _provision_drupal_valid_installed_site();

  // This is the actual drupal provisioning requirements. 
  if (!is_dir(PROVISION_BACKUP_PATH)) {
18
    drush_set_error(PROVISION_BACKUP_PATH_NOT_FOUND);
19 20 21 22
  }
  
  if ($backup_file) {
    if ( provision_path("exists", $backup_file, FALSE,
23 24
            dt("Backing site up to @path."),
            dt("Back up file @path already exists."),
25
        PROVISION_BACKUP_ALREADY_EXISTS)) {
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
      $data['backup_file'] = $backup_file;
    }
  }
  if (!$backup_file) {
    $suggested = PROVISION_BACKUP_PATH ."/$url-". date("Y-m-d", mktime()) .".tar";

    // Use format of mysite.com-2008-01-02, if already existing, add number.
    while (is_file($suggested .'.gz')) {
      $count++;
      $suggested = PROVISION_BACKUP_PATH ."/$url-". date("Y-m-d", mktime()) ."_$count.tar";
    }

    $data['backup_file'] = (!empty($file)) ? ereg_replace('.gz$', '', $file) : $suggested;
  }
}

/**
 * Implentation of hook_provision_backup()
 */
function provision_drupal_provision_backup(&$data, $url, $backup_file) {
  // Adds the site directory into the backup file
47
  drush_log("Adding sites directory to $data[backup_file].gz", 'backup');
48 49 50
  $result = provision_shell_exec("cd %s; tar -rf %s * ", "sites/$url",  $data['backup_file']);

  if (!$result) {
51
    drush_set_error(PROVISION_BACKUP_FAILED, dt("Could not back up sites directory for drupal"));
52 53 54 55 56 57 58 59 60 61 62
  }
}


/**
 * Generate a backup tarbal for a site.
 */
function provision_drupal_provision_post_backup(&$data, $url, $backup_file = NULL) {
  provision_shell_exec("gzip %s", $data['backup_file']);
  $data['backup_file'] = $data['backup_file'] .'.gz';
}