restore.provision.inc 3.43 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
<?php

/**
 * Restore command implementation
 *
 * This command when called will 
 *   1. Make a backup of the current site, before modifications are made.
 *   2. Temporarily disable the site by causing apache to redirect to a help page. Restarting apache is required.
 *   3. Extract the backup that is being restored to to a temporary folder in the sites directory.
 *   4. Create a new database, belonging to the site's user, and switch it around with the current site's database.
 *   5. Import the old database and site.php details.
 *   6. Switch around the sites directory of the current site and the backup being restored.
 *   7. Regenerate configuration files.
 *   8. TODO: diagnostic to test that everything is ok?
 *   9. Remove the temporary redirect and restart apache so the previous site is available again.
 *   10. Remove the extranuous db and duplicate site directory.
 *
 * If at any time an error occurs, before step 9. It should reverse all the changes it has made,
 * and leave the current site directory and database in the right place, and remove all cruft that
 * was created by this process.
 */

/**
 * Make sure we have a valid site being restored, and that the file being restored from exists
 */
26
function drush_provision_drupal_provision_restore_validate($url = null, $restore_file = null) {
27
  drush_bootstrap(DRUSH_BOOTSTRAP_DRUPAL_SITE);
28

29 30 31 32
  $exists = provision_service('file')->exists($restore_file)
    ->succeed('Restoring site from @path')
    ->fail('Could not find backup file @path', 'PROVISION_BACKUP_NOT_FOUND')
    ->status();
33
  if ($exists) {
34
    drush_set_option('restore_file', $restore_file);
35 36 37 38 39 40
  }
}

/**
 * Make a backup before making any changes, and add extract the file we are restoring from
 */
41
function drush_provision_drupal_pre_provision_restore($url, $restore_file) {
42
  drush_invoke('provision-backup', $url);
43

44 45 46
  provision_service('file')->extract(drush_get_option('restore_file'), drush_get_option('sites_path') . '/' . $url . '.restore') 
    ->succeed('Successfully extracted the contents of @path')
    ->fail('Failed to extract the contents of @path', 'PROVISION_BACKUP_EXTRACTION_FAILED');
47 48 49 50 51
}

/**
 * Remove the extracted site directory
 */
52
function drush_provision_drupal_pre_provision_restore_rollback($url) {
53
  _provision_recursive_delete(drush_get_option('sites_path') ."/$url.restore");
54 55 56 57 58
}

/**
 * Switch the restore directories around now that we have the new db installed
 */
59
function drush_provision_drupal_provision_restore($url) {
60 61 62 63 64
  $old = drush_get_option('sites_path') . '/' . $url . '.restore';
  $new = drush_get_option('sites_path') . '/' . $url;
  provision_service('file')->switch_paths($old, $new)
    ->succeed('Swapping out the @path1 and @path2 directories was successful.')
    ->fail('Swapping the @path1 and @path2 directories has failed.', 'DRUSH_PERM_ERROR');
65
  // make sure it has the latest site data available
66 67
  _provision_drupal_create_settings_file($url);
  provision_save_site_data($url);
68 69 70
}

// Luckily this is reversable =)
71
function drush_provision_drupal_provision_restore_rollback($url) {
72
  drush_provision_drupal_provision_restore($url);
73 74 75 76 77
}

/**
 * Delete the old site directory and recreate the settings file
 */
78
function drush_provision_drupal_post_provision_restore($url) {
79
  _provision_recursive_delete(drush_get_option('sites_path') ."/$url.restore");
80 81
  // This is the actual drupal provisioning requirements. 
  _provision_drupal_create_directories($url);
82
  _provision_drupal_create_settings_file($url);
83 84 85
}