MDL-79563 cli: Add options for backup/restore CLI

This commit is contained in:
Tomo Tsuyuki 2023-11-10 10:16:31 +11:00
parent 859df15e73
commit 839bf730b9
2 changed files with 63 additions and 19 deletions

View File

@ -33,6 +33,7 @@ require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
list($options, $unrecognized) = cli_get_params(array(
'courseid' => false,
'courseshortname' => '',
'cmid' => false,
'destination' => '',
'help' => false,
), array('h' => 'help'));
@ -42,13 +43,14 @@ if ($unrecognized) {
cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
}
if ($options['help'] || !($options['courseid'] || $options['courseshortname'])) {
if ($options['help'] || !($options['courseid'] || $options['courseshortname'] || $options['cmid'])) {
$help = <<<EOL
Perform backup of the given course.
Perform backup of the given course or course module.
Options:
--courseid=INTEGER Course ID for backup.
--courseshortname=STRING Course shortname for backup.
--courseshortname=STRING Course shortname for backup. This option is ignored if courseid is set.
--cmid=INTEGER Course module ID for backup. This option is ignored if courseid or courseshortname is set.
--destination=STRING Path where to store backup file. If not set the backup
will be stored within the course backup file area.
-h, --help Print out this help.
@ -76,16 +78,26 @@ if (!empty($dir)) {
}
}
// Check that the course exists.
// Check that the course or course module exists.
if ($options['courseid']) {
$course = $DB->get_record('course', array('id' => $options['courseid']), '*', MUST_EXIST);
} else if ($options['courseshortname']) {
$course = $DB->get_record('course', array('shortname' => $options['courseshortname']), '*', MUST_EXIST);
} else if ($options['cmid']) {
$cm = $DB->get_record('course_modules', ['id' => $options['cmid']], '*', MUST_EXIST);
}
cli_heading('Performing backup...');
$bc = new backup_controller(backup::TYPE_1COURSE, $course->id, backup::FORMAT_MOODLE,
backup::INTERACTIVE_YES, backup::MODE_GENERAL, $admin->id);
if (!empty($course)) {
$bc = new backup_controller(backup::TYPE_1COURSE, $course->id, backup::FORMAT_MOODLE,
backup::INTERACTIVE_YES, backup::MODE_GENERAL, $admin->id);
} else if (!empty($cm)) {
$bc = new backup_controller(backup::TYPE_1ACTIVITY, $cm->id, backup::FORMAT_MOODLE,
backup::INTERACTIVE_YES, backup::MODE_GENERAL, $admin->id);
} else {
throw new \moodle_exception('invalidoption');
}
// Set the default filename.
$format = $bc->get_format();
$type = $bc->get_type();
@ -113,7 +125,11 @@ if (!empty($dir)) {
}
}
} else {
mtrace("Backup completed, the new file is listed in the backup area of the given course");
if (!empty($course)) {
mtrace("Backup completed, the new file is listed in the backup area of the given course");
} else {
mtrace("Backup completed, the new file is listed in the backup area of the given module");
}
}
$bc->destroy();
exit(0);

View File

@ -32,11 +32,13 @@ require_once($CFG->dirroot . "/backup/util/includes/restore_includes.php");
list($options, $unrecognized) = cli_get_params([
'file' => '',
'categoryid' => '',
'courseid' => '',
'showdebugging' => false,
'help' => false,
], [
'f' => 'file',
'c' => 'categoryid',
'C' => 'courseid',
's' => 'showdebugging',
'h' => 'help',
]);
@ -46,15 +48,17 @@ if ($unrecognized) {
cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
}
if ($options['help'] || !($options['file']) || !($options['categoryid'])) {
if ($options['help'] || !($options['file']) || !($options['categoryid'] || $options['courseid'])) {
$help = <<<EOL
Restore backup into provided category.
Restore backup into provided category or course.
If courseid is set, course module/s will be added into the course.
Options:
-f, --file=STRING Path to the backup file.
-c, --categoryid=INT ID of the category to restore too.
-s, --showdebugging Show developer level debugging information
-h, --help Print out this help.
-f, --file=STRING Path to the backup file.
-c, --categoryid=INT ID of the course category to restore to.
-C, --courseid=INT ID of the course to restore to. This option is ignored if categoryid is set.
-s, --showdebugging Show developer level debugging information
-h, --help Print out this help.
Example:
\$sudo -u www-data /usr/bin/php admin/cli/restore_backup.php --file=/path/to/backup/file.mbz --categoryid=1\n
@ -76,8 +80,16 @@ if (!file_exists($options['file'])) {
throw new \moodle_exception('filenotfound');
}
if (!$category = $DB->get_record('course_categories', ['id' => $options['categoryid']], 'id')) {
throw new \moodle_exception('invalidcategoryid');
if ($options['categoryid']) {
if (!$category = $DB->get_record('course_categories', ['id' => $options['categoryid']], 'id')) {
throw new \moodle_exception('invalidcategoryid');
}
} else if ($options['courseid']) {
if (!$course = $DB->get_record('course', ['id' => $options['courseid']], 'id')) {
throw new \moodle_exception('invalidcourseid');
}
} else {
throw new \moodle_exception('invalidoption');
}
$backupdir = "restore_" . uniqid();
@ -92,13 +104,29 @@ try {
list($fullname, $shortname) = restore_dbops::calculate_course_names(0, get_string('restoringcourse', 'backup'),
get_string('restoringcourseshortname', 'backup'));
$courseid = restore_dbops::create_new_course($fullname, $shortname, $category->id);
$rc = new restore_controller($backupdir, $courseid, backup::INTERACTIVE_NO,
backup::MODE_GENERAL, $admin->id, backup::TARGET_NEW_COURSE);
if (!empty($course)) {
$courseid = $course->id;
$rc = new restore_controller($backupdir, $courseid, backup::INTERACTIVE_NO,
backup::MODE_GENERAL, $admin->id, backup::TARGET_EXISTING_ADDING);
} else {
$courseid = restore_dbops::create_new_course($fullname, $shortname, $category->id);
$rc = new restore_controller($backupdir, $courseid, backup::INTERACTIVE_NO,
backup::MODE_GENERAL, $admin->id, backup::TARGET_NEW_COURSE);
}
$rc->execute_precheck();
$rc->execute_plan();
$rc->destroy();
// Rename course name if the backup is from course module and restore to category.
if (empty($course)) {
$course = get_course($courseid);
list($fullname, $shortname) = restore_dbops::calculate_course_names(0, get_string('restoretonewcourse', 'backup'),
get_string('newcourse'));
$course->fullname = $fullname;
$course->shortname = $shortname;
$course->visible = 1;
$DB->update_record('course', $course);
}
} catch (Exception $e) {
cli_heading(get_string('cleaningtempdata'));
fulldelete($path);