Backup course by section via CLI

Backup course by section via CLI

by Carlo Ditan -
Number of replies: 4

Given the existing backup feature that uses CLI, is it possible to backup a course by its sections? For example, I will only backup Course A that has a section named 13 March - 17 March. 

Currently, the CLI backup feature backups the whole course (takes only two parameters -- course ID and destination of the backup file), so there's no cherry picking.

If it's possible to backup by sections via CLI, should I create my own script for this?  A detailed head-start would be nice.

Average of ratings: Useful (1)
In reply to Carlo Ditan

Re: Backup course by section via CLI

by Séverin Terrier -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Testers Picture of Translators

Hi,

I think that could be a good idea to be able to specify which section number you want to backup, in cli script.

You can vote comment on MDL-52050.

Séverin

In reply to Séverin Terrier

Re: Backup course by section via CLI

by Carlo Ditan -

I just managed to backup a section via CLI by section ID (did some minor tinkering on my local Moodle instance). The prerequisite is that the section and course ID pair should be present in the course_sections table. 

    $ php admin/cli/backup.php --courseid=2 --sectionid=17

Is there a way to fetch the course sections that have been modified recently (for example, all the course sections that have been modified today)?

Average of ratings: Useful (1)
In reply to Carlo Ditan

Re: Backup course by section via CLI

by Séverin Terrier -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Testers Picture of Translators

Hi Carlo,

I really hope that you're really talking about columns course and section in mdl_course_sections.

DESC mdl_course_sections;

+---------------+--------------+------+-----+---------+----------------+
| Field         | Type         | Null | Key | Default | Extra          |
+---------------+--------------+------+-----+---------+----------------+
| id            | bigint(10)   | NO   | PRI | NULL    | auto_increment |
| course        | bigint(10)   | NO   | MUL | 0       |                |
| section       | bigint(10)   | NO   |     | 0       |                |
| name          | varchar(255) | YES  |     | NULL    |                |
| summary       | longtext     | YES  |     | NULL    |                |
| summaryformat | tinyint(2)   | NO   |     | 0       |                |
| sequence      | longtext     | YES  |     | NULL    |                |
| visible       | tinyint(1)   | NO   |     | 1       |                |
| availability  | longtext     | YES  |     | NULL    |                |
+---------------+--------------+------+-----+---------+----------------+

Sorry I don't have an answer to your question.

It could be useful if you could share what you've done to allow making a backup about one section only.

Séverin

Average of ratings: Useful (1)
In reply to Séverin Terrier

Re: Backup course by section via CLI

by Carlo Ditan -

First, I added the sectionid as a parameter for the command. 

list($options, $unrecognized) = cli_get_params(array(
    'courseid' => false,
    'sectionid' => false,     // this is the added parameter
    'courseshortname' => '',
    'destination' => '',
    'help' => false,
    ), array('h' => 'help'));

Second, I added the sectionid as an additional condition to be queried in fetching a record from the course_sections table. Take note that both courseid and sectionid parameters should be present.  The command will throw an invalidrecord exception if the pair doesn't exist in the course_sections table.

if($options['sectionid'] && $options['courseid']) {
    $section = $DB->get_record('course_sections', array('id' => $options['sectionid'], 'course' => $options['courseid']), '*', MUST_EXIST);
} else if($options['courseid']) {
    // get course record by ID
} else {
    // get course record by shortname
}

Third, I added a condition wherein the backup controller will use the section type and ID, instead of course.

if($section) {
    $bc = new backup_controller(
        backup::TYPE_1SECTION, $section->id, 
        backup::FORMAT_MOODLE, backup::INTERACTIVE_YES, 
        backup::MODE_GENERAL, $admin->id);
}
else {
    $bc = new backup_controller(
        backup::TYPE_1COURSE, $course->id, 
        backup::FORMAT_MOODLE, backup::INTERACTIVE_YES, 
        backup::MODE_GENERAL, $admin->id);
}

That's basically what I did. The implementation can be improved though.

Average of ratings: Useful (2)