Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 400 and 401] [Versions 400 and 402] [Versions 400 and 403]

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * This script allows to restore a course from CLI.
  19   *
  20   * @package    core
  21   * @subpackage cli
  22   * @copyright  2020 Catalyst IT
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  define('CLI_SCRIPT', 1);
  27  
  28  require(__DIR__ . '/../../config.php');
  29  require_once($CFG->libdir . '/clilib.php');
  30  require_once($CFG->dirroot . "/backup/util/includes/restore_includes.php");
  31  
  32  list($options, $unrecognized) = cli_get_params([
  33      'file' => '',
  34      'categoryid' => '',
  35      'showdebugging' => false,
  36      'help' => false,
  37  ], [
  38      'f' => 'file',
  39      'c' => 'categoryid',
  40      's' => 'showdebugging',
  41      'h' => 'help',
  42  ]);
  43  
  44  if ($unrecognized) {
  45      $unrecognized = implode("\n  ", $unrecognized);
  46      cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
  47  }
  48  
  49  if ($options['help'] || !($options['file']) || !($options['categoryid'])) {
  50      $help = <<<EOL
  51  Restore backup into provided category.
  52  
  53  Options:
  54  -f, --file=STRING           Path to the backup file.
  55  -c, --categoryid=INT        ID of the category to restore too.
  56  -s, --showdebugging         Show developer level debugging information
  57  -h, --help                  Print out this help.
  58  
  59  Example:
  60  \$sudo -u www-data /usr/bin/php admin/cli/restore_backup.php --file=/path/to/backup/file.mbz --categoryid=1\n
  61  EOL;
  62  
  63      echo $help;
  64      exit(0);
  65  }
  66  
  67  if ($options['showdebugging']) {
  68      set_debugging(DEBUG_DEVELOPER, true);
  69  }
  70  
  71  if (!$admin = get_admin()) {
  72      print_error('noadmins');
  73  }
  74  
  75  if (!file_exists($options['file'])) {
  76      print_error('filenotfound');
  77  }
  78  
  79  if (!$category = $DB->get_record('course_categories', ['id' => $options['categoryid']], 'id')) {
  80      print_error('invalidcategoryid');
  81  }
  82  
  83  $backupdir = "restore_" . uniqid();
  84  $path = $CFG->tempdir . DIRECTORY_SEPARATOR . "backup" . DIRECTORY_SEPARATOR . $backupdir;
  85  
  86  cli_heading(get_string('extractingbackupfileto', 'backup', $path));
  87  $fp = get_file_packer('application/vnd.moodle.backup');
  88  $fp->extract_to_pathname($options['file'], $path);
  89  
  90  cli_heading(get_string('preprocessingbackupfile'));
  91  try {
  92      list($fullname, $shortname) = restore_dbops::calculate_course_names(0, get_string('restoringcourse', 'backup'),
  93          get_string('restoringcourseshortname', 'backup'));
  94  
  95      $courseid = restore_dbops::create_new_course($fullname, $shortname, $category->id);
  96  
  97      $rc = new restore_controller($backupdir, $courseid, backup::INTERACTIVE_NO,
  98          backup::MODE_GENERAL, $admin->id, backup::TARGET_NEW_COURSE);
  99      $rc->execute_precheck();
 100      $rc->execute_plan();
 101      $rc->destroy();
 102  } catch (Exception $e) {
 103      cli_heading(get_string('cleaningtempdata'));
 104      fulldelete($path);
 105      print_error('generalexceptionmessage', 'error', '', $e->getMessage());
 106  }
 107  
 108  cli_heading(get_string('restoredcourseid', 'backup', $courseid));
 109  exit(0);