backup.provision.inc 2.18 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 29

      drush_set_option('backup_file', str_replace(".gz", '', $backup_file));
30 31
    }
  }
32

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

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

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

/**
 * Implentation of hook_provision_backup()
 */
49
function drush_provision_drupal_provision_backup($url) {
50
  $backup_file = drush_get_option('backup_file');
51
  // Adds the site directory into the backup file
52
  drush_log(dt("Adding sites directory to !backup_file", array('!backup_file' => $backup_file)), 'backup');
53
  $result = provision_shell_exec("tar -C %s -p -r -f %s .", drush_get_option('sites_path') . "/$url",  $backup_file);
54

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


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

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