diff --git a/admin/cli/backup.php b/admin/cli/backup.php index b8be31827c9..4b094ec0cb2 100644 --- a/admin/cli/backup.php +++ b/admin/cli/backup.php @@ -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 = <<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); \ No newline at end of file diff --git a/admin/cli/restore_backup.php b/admin/cli/restore_backup.php index 46eccb2db4f..597c85f5dac 100644 --- a/admin/cli/restore_backup.php +++ b/admin/cli/restore_backup.php @@ -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 = <<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);