backup.provision.inc 2.12 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 14
  drush_bootstrap(DRUSH_BOOTSTRAP_DRUPAL_SITE);
  if (!drush_get_option('installed')) {
15
     drush_set_error('PROVISION_DRUPAL_SITE_NOT_FOUND');
16
   }
17 18

  // This is the actual drupal provisioning requirements. 
19
  if (!is_dir(drush_get_option('backup_path'))) {
20
    drush_set_error('PROVISION_BACKUP_PATH_NOT_FOUND');
21
  }
22

23 24
  if ($backup_file) {
    if ( provision_path("exists", $backup_file, FALSE,
25 26
            dt("Backing site up to @path."),
            dt("Back up file @path already exists."),
27
        'PROVISION_BACKUP_ALREADY_EXISTS')) {
28
      drush_get_option('backup_file', $backup_file);
29 30
    }
  }
31

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

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

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

/**
 * Implentation of hook_provision_backup()
 */
48
function drush_provision_drupal_provision_backup($url) {
49
  $backup_file = drush_get_option('backup_file');
50
  // Adds the site directory into the backup file
51 52
  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);
53 54

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


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

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