backup.provision.inc 1.97 KB
Newer Older
1 2 3 4 5 6 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 63 64
<?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)) {
    provision_log("Backup directory does not exist.");
    provision_set_error(PROVISION_PERM_ERROR);
  }
  
  if ($backup_file) {
    if ( provision_path("exists", $backup_file, FALSE,
            t("Backing site up to @path."),
            t("Back up file @path already exists."),
        PROVISION_FRAMEWORK_ERROR)) {
      $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
  provision_log("backup", "Adding sites directory to $data[backup_file].gz");
  $result = provision_shell_exec("cd %s; tar -rf %s * ", "sites/$url",  $data['backup_file']);

  if (!$result) {
    provision_log("error", "Could not back up sites directory for drupal");
    provision_set_error(PROVISION_FRAMEWORK_ERROR);
  }
}


/**
 * 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';
}