要通过我正在使用的自定义代码在 Moodle 中创建备份文件:
$bc = new backup_controller($type, $id, backup::FORMAT_MOODLE, backup::INTERACTIVE_YES, $backupmode, $userid, backup::RELEASESESSION_YES);
$backupid = $bc->get_backupid();
$backup = new backup_ui($bc);
// ...
// create filename, e.g. "backup-moodle2-course-35-map_2466-20211227-0925.mbz"
$filename = backup_plan_dbops::get_default_backup_filename($format, $type, $id, $users, $anonymised);
$bc->get_plan()->get_setting('filename')->set_value($filename);
$bc->finish_ui();
$bc->execute_plan();
$results = $bc->get_results();
$file = $results['backup_destination'];
echo $file->get_content();
$file->delete();
$backup->destroy();
它创建了一个 MBZ 文件,工作正常。
但是,我注意到备份文件堆积在服务器上(文件夹
/moodledata/filedir/
)并且未删除。使用几千兆字节的数据。
我尝试找出如何从文件系统中删除数据库和中的备份,但没有官方文档/解决方案。
如何从数据库和文件系统中删除创建的Moodle备份文件?
我尝试过但没有成功:
// CLEAN UP
// delete_course() will trigger the course_deleted event and the course_content_deleted
// see source in "moodlelib.php"
delete_course($course->id, false);
// REMOVE left over backups (were contextid == 3 in "mdl_files")
$contextid = 3;
// instance to file storage
$fs = get_file_storage();
// delete all course backup files (adds them to "tool_recyclebin" in table "mdl_files")
$fs->delete_area_files($contextid, 'backup');
// delete recycle element
$fs->delete_area_files($contextid, 'tool_recyclebin', 'recyclebin_coursecat');
// optional clear Moodle caches
purge_all_caches();
而这个没有成功:
// memo: "If you remove a row in mdl_files that pointed to a file
// you must also find that file via contenthash sluething and remove
// the file @ moodledata/filedir/xx/xx/contenthashname."
// instance to file storage
$fs = get_file_storage();
// delete all course backup files (adds them to "tool_recyclebin" in table "mdl_files")
$fs->delete_area_files($contextid, 'backup');
// delete recycle element
$fs->delete_area_files($contextid, 'tool_recyclebin', 'recyclebin_coursecat');
// should empty folders \filedir and \trashdir
Moodle 备份文件夹仍然不为空。
要在使用自定义代码创建 Moodle 备份文件后从数据库和文件系统中删除它们,您需要正确定位要删除的文件,并确保您在正确的上下文中工作并使用正确的文件存储处理在Moodle中。以下是如何构建代码来实现此目的的示例:
<?php
require_once('config.php'); // Ensure this path is correct
require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
require_once($CFG->libdir . '/filestorage/file_storage.php');
require_once($CFG->libdir . '/filestorage/stored_file.php');
// Assuming $file is the backup file you've created and want to delete
$file = $results['backup_destination']; // As per your provided code
// Get the context of the course to ensure we are deleting the files from the correct location
$context = context_course::instance($course->id);
// Initialize file storage
$fs = get_file_storage();
// Use the file storage to delete files based on component, filearea, and itemid
$fs->delete_area_files($context->id, 'backup', 'backupdata', $file->get_itemid());
// Optionally, to delete files by their contenthash directly from the moodledata/filedir,
// you need to be very cautious as this can lead to data loss if not handled properly.
// Here's an example of how you might approach it:
if ($file->get_contenthash()) {
$contenthash = $file->get_contenthash();
$path = $CFG->dataroot . '/filedir/' . substr($contenthash, 0, 2) . '/' . substr($contenthash, 2, 2) . '/' . $contenthash;
if (file_exists($path)) {
unlink($path); // Delete the file
}
}
// Note: Directly manipulating files within moodledata/filedir is not recommended
// as Moodle maintains a file pool and deleting files directly can lead to issues.
// Always prefer to use Moodle's file API to manage files.
// Clean up after backup
$backup->destroy();