如何从数据库和文件系统中删除Moodle备份文件?

问题描述 投票:0回答:1

要通过我正在使用的自定义代码在 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
1个回答
0
投票

要在使用自定义代码创建 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();

重要注意事项:

  • 上下文:确保您在正确的上下文中工作。对于课程备份,这通常是 context_course::instance($courseid)。
  • 文件 API:始终使用 Moodle 的文件 API 与文件交互,以确保完整性并防止 Moodle 文件池系统内出现问题。
  • 直接删除:不建议直接从moodledata/filedir删除文件。如果必须,请确保您对 Moodle 的文件存储系统有透彻的了解,以避免意外的数据丢失。
  • 测试:彻底测试在安全的非生产环境中删除数据的任何代码,以确保其按预期运行,不会产生意外的副作用。
© www.soinside.com 2019 - 2024. All rights reserved.