MDL-37761 Backup: Do not include files in backups when importing courses/activities

When we import courses or duplicate activities, the deduplicating nature of
the file_storage system means that we don't actually have to include the
real files in the backup - just their metadata from the file table.

The restoration process needs to know not to expect files from the backup
phase so a flag is set in moodle_backup metadata.
This commit is contained in:
Andrew Nicols 2013-05-12 16:53:57 +01:00
parent f1454424b5
commit b1850c12c1
8 changed files with 168 additions and 46 deletions

View file

@ -56,6 +56,7 @@ class backup_controller extends backup implements loggable {
protected $status; // Current status of the controller (created, planned, configured...)
protected $plan; // Backup execution plan
protected $includefiles; // Whether this backup includes files or not.
protected $execution; // inmediate/delayed
protected $executiontime; // epoch time when we want the backup to be executed (requires cron to run)
@ -239,6 +240,17 @@ class backup_controller extends backup implements loggable {
return $this->type;
}
/**
* Returns the current value of the include_files setting.
* This setting is intended to ensure that files are not included in
* generated backups.
*
* @return int Indicates whether files should be included in backups.
*/
public function get_include_files() {
return $this->includefiles;
}
public function get_operation() {
return $this->operation;
}
@ -362,6 +374,33 @@ class backup_controller extends backup implements loggable {
$this->log('applying plan defaults', backup::LOG_DEBUG);
backup_controller_dbops::apply_config_defaults($this);
$this->set_status(backup::STATUS_CONFIGURED);
$this->set_include_files();
}
/**
* Set the initial value for the include_files setting.
*
* @see backup_controller::get_include_files for further information on the purpose of this setting.
* @return int Indicates whether files should be included in backups.
*/
protected function set_include_files() {
// We normally include files.
$includefiles = true;
// In an import, we don't need to include files.
if ($this->get_mode() === backup::MODE_IMPORT) {
$includefiles = false;
}
// When a backup is intended for the same site, we don't need to include the files.
// Note, this setting is only used for duplication of an entire course.
if ($this->get_mode() === backup::MODE_SAMESITE) {
$includefiles = false;
}
$this->includefiles = (int) $includefiles;
$this->log("setting file inclusion to {$this->includefiles}", backup::LOG_DEBUG);
return $this->includefiles;
}
}

View file

@ -82,6 +82,25 @@ class backup_controller_testcase extends advanced_testcase {
$newbc = mock_backup_controller::load_controller($bc->get_backupid());
$this->assertTrue($newbc instanceof backup_controller); // This means checksum and load worked ok
}
public function test_backup_controller_include_files() {
// A MODE_GENERAL controller - this should include files
$bc = new mock_backup_controller(backup::TYPE_1ACTIVITY, $this->moduleid, backup::FORMAT_MOODLE,
backup::INTERACTIVE_NO, backup::MODE_GENERAL, $this->userid);
$this->assertEquals($bc->get_include_files(), 1);
// The MODE_IMPORT and MODE_SAMESITE should not include files in the backup.
// A MODE_IMPORT controller
$bc = new mock_backup_controller(backup::TYPE_1ACTIVITY, $this->moduleid, backup::FORMAT_MOODLE,
backup::INTERACTIVE_NO, backup::MODE_IMPORT, $this->userid);
$this->assertEquals($bc->get_include_files(), 0);
// A MODE_SAMESITE controller
$bc = new mock_backup_controller(backup::TYPE_1COURSE, $this->courseid, backup::FORMAT_MOODLE,
backup::INTERACTIVE_NO, backup::MODE_IMPORT, $this->userid);
$this->assertEquals($bc->get_include_files(), 0);
}
}