backup.provision.inc 2.01 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
  drush_bootstrap(DRUSH_BOOTSTRAP_DRUPAL_SITE);
14
  if (!drush_get_option('installed') && !drush_get_option('force', false)) {
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
  if ($backup_file) {
anarcat's avatar
anarcat committed
24
    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')) {
anarcat's avatar
anarcat committed
28
      drush_set_option('backup_file', $backup_file);
29 30
    }
  }
31

32
  if (!$backup_file) {
33
    $suggested = drush_get_option('backup_path') ."/$url-". date("Ymd.His", mktime()) .".tar.gz";
34 35

    // Use format of mysite.com-2008-01-02, if already existing, add number.
anarcat's avatar
anarcat committed
36
    while (is_file($suggested)) {
37
      $count++;
38
      $suggested = drush_get_option('backup_path') ."/$url-". date("Ymd.His", mktime()) ."_$count.tar.gz";
39
    }
anarcat's avatar
anarcat committed
40
    drush_set_option('backup_file', $suggested);
41
  }
anarcat's avatar
anarcat committed
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
  drush_log(dt("Adding sites directory to !backup_file", array('!backup_file' => $backup_file)), 'backup');
52 53 54 55 56 57
  if (substr($backup_file, -2) == 'gz') {
    $command = "tar -C %s -p -c -z -f %s .";
  } else {
    $command = "tar -C %s -p -c -f %s .";
  }
  $result = provision_shell_exec($command, drush_get_option('sites_path') . "/$url",  $backup_file);
58

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