backup.provision.inc 2.03 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 provision_drupal_provision_backup_validate($url = NULL, $backup_file = NULL) {
13 14 15 16
  _provision_drupal_url_required();
  _provision_drupal_valid_installed_site();

  // This is the actual drupal provisioning requirements. 
17
  if (!is_dir(drush_get_option('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
      drush_get_option('backup_file', $backup_file);
27 28
    }
  }
29

30
  if (!$backup_file) {
31
    $suggested = drush_get_option('backup_path') ."/$url-". date("Y-m-d", mktime()) .".tar";
32 33 34 35

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

39
    drush_set_option('backup_file', str_replace(".gz" , '', $suggested));
40 41 42 43 44 45
  }
}

/**
 * Implentation of hook_provision_backup()
 */
46 47
function provision_drupal_provision_backup($url) {
  $backup_file = drush_get_option('backup_file');
48
  // Adds the site directory into the backup file
49 50
  drush_log(dt("Adding sites directory to !backup_file", array('!backup_file' => $backup_file)), 'backup');
  $result = provision_shell_exec("cd %s; tar -rf %s * ", drush_get_option('sites_path') . "/$url",  $backup_file);
51 52

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


/**
 * Generate a backup tarbal for a site.
 */
61 62 63 64 65
function provision_drupal_provision_post_backup($url, $backup_file = NULL) {
  $backup_file = drush_get_option('backup_file');

  provision_shell_exec("gzip %s", $backup_file);
  drush_set_option('backup_file', $backup_file . '.gz');
66
}