MDL-35773 Backup: API should have option to not backup files

Allow both UI and automated backups to be created without
including files.  Instead include only file references.
This is essentially implementing "SAMESITE" to backup files
instead of only for import and export functionality.
A new backup setting to include files (defaults to yes)
has been included.

The restore process will also look for and attempt to
restore files from the trashdir as part of restoring
backups.  Additionally to support this process the
ammount of time files are kept in trashdir before they
are cleaned up via cron is also adjustable via admin
setting.
This commit is contained in:
Matt Porritt 2018-12-10 12:27:26 +11:00 committed by Mark Nelson
parent f622ee97e3
commit d7e4481e98
17 changed files with 190 additions and 18 deletions

View file

@ -234,13 +234,46 @@ class file_system_filedir extends file_system {
return copy($source, $target);
}
/**
* Create a file record from a stored file object.
*
* This is required in cases where we are recoverying a file
* from the trash and we need to also recrete the file record
* in the database.
*
* @param stored_file $file
* @return stdClass
*/
protected function create_recovery_record(stored_file $file) {
$filerecord = new stdClass();
$filerecord->contextid = $file->get_contextid();
$filerecord->component = $file->get_component();
$filerecord->filearea = $file->get_filearea();
$filerecord->itemid = $file->get_itemid();
$filerecord->filepath = $file->get_filepath();
$filerecord->filename = $file->get_filename();
$filerecord->timecreated = $file->get_timecreated();
$filerecord->timemodified = $file->get_timemodified();
$filerecord->userid = empty($file->get_userid()) ? null : $file->get_userid();
$filerecord->source = empty($file->get_source()) ? null : $file->get_source();
$filerecord->author = empty($file->get_author()) ? null : $file->get_author();
$filerecord->license = empty($file->get_license()) ? null : $file->get_license();
$filerecord->status = empty($file->get_status()) ? 0 : $file->get_status();
$filerecord->sortorder = $file->get_sortorder();
$filerecord->contenthash = $file->get_contenthash();
return $filerecord;
}
/**
* Tries to recover missing content of file from trash.
*
* @param stored_file $file stored_file instance
* @param bool $createrecord Create file record for stored file.
* @return bool success
*/
protected function recover_file(stored_file $file) {
public function recover_file(stored_file $file, $createrecord=false) {
$contentfile = $this->get_local_path_from_storedfile($file, false);
if (file_exists($contentfile)) {
@ -274,9 +307,25 @@ class file_system_filedir extends file_system {
}
}
// Perform a rename - these are generally atomic which gives us big
// performance wins, especially for large files.
return rename($trashfile, $contentfile);
// Restore file from trash and create file record in database if needed.
if ($createrecord) {
$recoveryrecord = $this->create_recovery_record($file);
$fs = new file_storage();
$fs->create_file_from_pathname($recoveryrecord, $trashfile);
// Remove copy of file still in trash.
// There are no references to this file anywhere so we just unlink it.
unlink($trashfile);
} else {
// If record exists in database then perform a rename.
// These are generally atomic which gives us big
// performance wins, especially for large files.
return rename($trashfile, $contentfile);
}
return true;
}
/**