Skip to content

Commit

Permalink
MDL-60923 backup: Added $CFG->backuptempdir
Browse files Browse the repository at this point in the history
The new setting will allow to host the temporary backup files
into a specific target directory. Defaults to '$CFG->tempdir/backup'.

Calling make_backup_temp_directory() checks that the required sub-directory
will be properly created under the new target directory.
  • Loading branch information
scara committed Apr 16, 2018
1 parent 20bf0c4 commit ef84414
Show file tree
Hide file tree
Showing 36 changed files with 147 additions and 79 deletions.
1 change: 1 addition & 0 deletions admin/cli/install.php
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,7 @@
}
}
$CFG->tempdir = $CFG->dataroot.'/temp';
$CFG->backuptempdir = $CFG->tempdir.'/backup';
$CFG->cachedir = $CFG->dataroot.'/cache';
$CFG->localcachedir = $CFG->dataroot.'/localcache';

Expand Down
4 changes: 2 additions & 2 deletions admin/tool/recyclebin/classes/category_bin.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,9 @@ public function restore_item($item) {
// Get the backup file.
$file = reset($files);

// Get a temp directory name and create it.
// Get a backup temp directory name and create it.
$tempdir = \restore_controller::get_tempdir_name($context->id, $user->id);
$fulltempdir = make_temp_directory('/backup/' . $tempdir);
$fulltempdir = make_backup_temp_directory($tempdir);

// Extract the backup to tmpdir.
$fb = get_file_packer('application/vnd.moodle.backup');
Expand Down
4 changes: 2 additions & 2 deletions admin/tool/recyclebin/classes/course_bin.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,9 @@ public function restore_item($item) {
// Get the backup file.
$file = reset($files);

// Get a temp directory name and create it.
// Get a backup temp directory name and create it.
$tempdir = \restore_controller::get_tempdir_name($context->id, $user->id);
$fulltempdir = make_temp_directory('/backup/' . $tempdir);
$fulltempdir = make_backup_temp_directory($tempdir);

// Extract the backup to tempdir.
$fb = get_file_packer('application/vnd.moodle.backup');
Expand Down
2 changes: 1 addition & 1 deletion admin/tool/uploadcourse/classes/course.php
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ public function get_id() {
/**
* Get the directory of the object to restore.
*
* @return string|false|null subdirectory in $CFG->tempdir/backup/..., false when an error occured
* @return string|false|null subdirectory in $CFG->backuptempdir/..., false when an error occured
* and null when there is simply nothing.
*/
protected function get_restore_content_dir() {
Expand Down
4 changes: 2 additions & 2 deletions admin/tool/uploadcourse/classes/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ public static function get_restore_content_dir($backupfile = null, $shortname =
}

// If we don't use the cache, or if we do and not set, or the directory doesn't exist any more.
if (!$usecache || (($backupid = $cache->get($cachekey)) === false || !is_dir("$CFG->tempdir/backup/$backupid"))) {
if (!$usecache || (($backupid = $cache->get($cachekey)) === false || !is_dir(get_backup_temp_directory($backupid)))) {

// Use null instead of false because it would consider that the cache key has not been set.
$backupid = null;
Expand All @@ -236,7 +236,7 @@ public static function get_restore_content_dir($backupfile = null, $shortname =
// Extracting the backup file.
$packer = get_file_packer('application/vnd.moodle.backup');
$backupid = restore_controller::get_tempdir_name(SITEID, $USER->id);
$path = "$CFG->tempdir/backup/$backupid/";
$path = make_backup_temp_directory($backupid, false);
$result = $packer->extract_to_pathname($backupfile, $path);
if (!$result) {
$errors['invalidbackupfile'] = new lang_string('invalidbackupfile', 'tool_uploadcourse');
Expand Down
2 changes: 1 addition & 1 deletion admin/tool/uploadcourse/classes/processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ public function get_errors() {
/**
* Get the directory of the object to restore.
*
* @return string subdirectory in $CFG->tempdir/backup/...
* @return string subdirectory in $CFG->backuptempdir/...
*/
protected function get_restore_content_dir() {
$backupfile = null;
Expand Down
4 changes: 2 additions & 2 deletions backup/controller/restore_controller.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
*/
class restore_controller extends base_controller {

protected $tempdir; // Directory under tempdir/backup awaiting restore
protected $tempdir; // Directory under $CFG->backuptempdir awaiting restore
protected $restoreid; // Unique identificator for this restore

protected $courseid; // courseid where restore is going to happen
Expand Down Expand Up @@ -68,7 +68,7 @@ class restore_controller extends base_controller {
* while loading the plan, as well as for future use. (You can change it
* for a different one later using set_progress.)
*
* @param string $tempdir Directory under tempdir/backup awaiting restore
* @param string $tempdir Directory under $CFG->backuptempdir awaiting restore
* @param int $courseid Course id where restore is going to happen
* @param bool $interactive backup::INTERACTIVE_YES[true] or backup::INTERACTIVE_NO[false]
* @param int $mode backup::MODE_[ GENERAL | HUB | IMPORT | SAMESITE ]
Expand Down
4 changes: 2 additions & 2 deletions backup/controller/tests/controller_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public function test_restore_controller_is_executing() {
global $CFG;

// Make a backup.
check_dir_exists($CFG->tempdir . '/backup');
make_backup_temp_directory('');
$bc = new backup_controller(backup::TYPE_1ACTIVITY, $this->moduleid, backup::FORMAT_MOODLE,
backup::INTERACTIVE_NO, backup::MODE_IMPORT, $this->userid);
$backupid = $bc->get_backupid();
Expand Down Expand Up @@ -159,7 +159,7 @@ public function test_restore_of_deadlock_causing_backup() {

$foldername = 'deadlock';
$fp = get_file_packer('application/vnd.moodle.backup');
$tempdir = $CFG->dataroot . '/temp/backup/' . $foldername;
$tempdir = make_backup_temp_directory($foldername);
$files = $fp->extract_to_pathname($CFG->dirroot . '/backup/controller/tests/fixtures/deadlock.mbz', $tempdir);

$this->setAdminUser();
Expand Down
4 changes: 2 additions & 2 deletions backup/converter/convertlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public function convert() {
public function get_workdir_path() {
global $CFG;

return "$CFG->tempdir/backup/$this->workdir";
return make_backup_temp_directory($this->workdir);
}

/**
Expand All @@ -157,7 +157,7 @@ public function get_workdir_path() {
public function get_tempdir_path() {
global $CFG;

return "$CFG->tempdir/backup/$this->tempdir";
return make_backup_temp_directory($this->tempdir);
}

/// public static methods //////////////////////////////////////////////////
Expand Down
2 changes: 1 addition & 1 deletion backup/converter/imscc11/backuplib.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ protected function define_execution() {

require_once($CFG->dirroot . '/backup/cc/cc_includes.php');

$tempdir = $CFG->tempdir . '/backup/' . uniqid('', true);
$tempdir = $CFG->backuptempdir . '/' . uniqid('', true);

if (mkdir($tempdir, $CFG->directorypermissions, true)) {

Expand Down
3 changes: 2 additions & 1 deletion backup/converter/moodle1/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ public function log($message, $level, $a = null, $depth = null, $display = false
public static function detect_format($tempdir) {
global $CFG;

$filepath = $CFG->tempdir . '/backup/' . $tempdir . '/moodle.xml';
$tempdirpath = make_backup_temp_directory($tempdir, false);
$filepath = $tempdirpath . '/moodle.xml';
if (file_exists($filepath)) {
// looks promising, lets load some information
$handle = fopen($filepath, 'r');
Expand Down
26 changes: 15 additions & 11 deletions backup/converter/moodle1/tests/moodle1_converter_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,46 +35,50 @@ class core_backup_moodle1_converter_testcase extends advanced_testcase {
/** @var string the name of the directory containing the unpacked Moodle 1.9 backup */
protected $tempdir;

/** @var string the full name of the directory containing the unpacked Moodle 1.9 backup */
protected $tempdirpath;

/** @var string saved hash of an icon file used during testing */
protected $iconhash;

protected function setUp() {
global $CFG;

$this->tempdir = convert_helper::generate_id('unittest');
check_dir_exists("$CFG->tempdir/backup/$this->tempdir/course_files/sub1");
check_dir_exists("$CFG->tempdir/backup/$this->tempdir/moddata/unittest/4/7");
$this->tempdirpath = make_backup_temp_directory($this->tempdir);
check_dir_exists("$this->tempdirpath/course_files/sub1");
check_dir_exists("$this->tempdirpath/moddata/unittest/4/7");
copy(
"$CFG->dirroot/backup/converter/moodle1/tests/fixtures/moodle.xml",
"$CFG->tempdir/backup/$this->tempdir/moodle.xml"
"$this->tempdirpath/moodle.xml"
);
copy(
"$CFG->dirroot/backup/converter/moodle1/tests/fixtures/icon.gif",
"$CFG->tempdir/backup/$this->tempdir/course_files/file1.gif"
"$this->tempdirpath/course_files/file1.gif"
);
copy(
"$CFG->dirroot/backup/converter/moodle1/tests/fixtures/icon.gif",
"$CFG->tempdir/backup/$this->tempdir/course_files/sub1/file2.gif"
"$this->tempdirpath/course_files/sub1/file2.gif"
);
copy(
"$CFG->dirroot/backup/converter/moodle1/tests/fixtures/icon.gif",
"$CFG->tempdir/backup/$this->tempdir/moddata/unittest/4/file1.gif"
"$this->tempdirpath/moddata/unittest/4/file1.gif"
);
copy(
"$CFG->dirroot/backup/converter/moodle1/tests/fixtures/icon.gif",
"$CFG->tempdir/backup/$this->tempdir/moddata/unittest/4/icon.gif"
"$this->tempdirpath/moddata/unittest/4/icon.gif"
);
$this->iconhash = file_storage::hash_from_path($CFG->tempdir.'/backup/'.$this->tempdir.'/moddata/unittest/4/icon.gif');
$this->iconhash = file_storage::hash_from_path($this->tempdirpath.'/moddata/unittest/4/icon.gif');
copy(
"$CFG->dirroot/backup/converter/moodle1/tests/fixtures/icon.gif",
"$CFG->tempdir/backup/$this->tempdir/moddata/unittest/4/7/icon.gif"
"$this->tempdirpath/moddata/unittest/4/7/icon.gif"
);
}

protected function tearDown() {
global $CFG;
if (empty($CFG->keeptempdirectoriesonbackup)) {
fulldelete("$CFG->tempdir/backup/$this->tempdir");
fulldelete($this->tempdirpath);
}
}

Expand Down Expand Up @@ -563,7 +567,7 @@ public function test_question_bank_conversion() {

copy(
"$CFG->dirroot/backup/converter/moodle1/tests/fixtures/questions.xml",
"$CFG->tempdir/backup/$this->tempdir/moodle.xml"
"$this->tempdirpath/moodle.xml"
);
$converter = convert_factory::get_converter('moodle1', $this->tempdir);
$converter->convert();
Expand Down
2 changes: 1 addition & 1 deletion backup/import.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@
// Check whether the backup directory still exists. If missing, something
// went really wrong in backup, throw error. Note that backup::MODE_IMPORT
// backups don't store resulting files ever
$tempdestination = $CFG->tempdir . '/backup/' . $backupid;
$tempdestination = make_backup_temp_directory($backupid, false);
if (!file_exists($tempdestination) || !is_dir($tempdestination)) {
print_error('unknownbackupexporterror'); // shouldn't happen ever
}
Expand Down
6 changes: 2 additions & 4 deletions backup/moodle2/tests/moodle2_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,7 @@ public function test_restore_legacy_availability() {

// Extract backup file.
$backupid = 'abc';
$backuppath = $CFG->tempdir . '/backup/' . $backupid;
check_dir_exists($backuppath);
$backuppath = make_backup_temp_directory($backupid);
get_file_packer('application/vnd.moodle.backup')->extract_to_pathname(
__DIR__ . '/fixtures/availability_26_format.mbz', $backuppath);

Expand Down Expand Up @@ -972,8 +971,7 @@ public function test_restore_question_category_34_35() {
foreach ($backupfiles as $backupfile) {
// Extract backup file.
$backupid = $backupfile;
$backuppath = $CFG->tempdir . '/backup/' . $backupid;
check_dir_exists($backuppath);
$backuppath = make_backup_temp_directory($backupid);
get_file_packer('application/vnd.moodle.backup')->extract_to_pathname(
__DIR__ . "/fixtures/$backupfile.mbz", $backuppath);

Expand Down
2 changes: 1 addition & 1 deletion backup/restorefile.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
$browser = get_file_browser();

// check if tmp dir exists
$tmpdir = $CFG->tempdir . '/backup';
$tmpdir = make_backup_temp_directory('', false);
if (!check_dir_exists($tmpdir, true, true)) {
throw new restore_controller_exception('cannot_create_backup_temp_dir');
}
Expand Down
4 changes: 2 additions & 2 deletions backup/util/factories/backup_factory.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ public static function get_logger_chain($interactive, $execution, $backupid) {

// Create file_logger, observing $CFG->backup_file_logger_level
// defaulting to $dfltloglevel
check_dir_exists($CFG->tempdir . '/backup', true, true); // need to ensure that temp/backup already exists
$backuptempdir = make_backup_temp_directory(''); // Need to ensure that $CFG->backuptempdir already exists.
$fllevel = isset($CFG->backup_file_logger_level) ? $CFG->backup_file_logger_level : $dfltloglevel;
$enabledloggers[] = new file_logger($fllevel, true, true, $CFG->tempdir . '/backup/' . $backupid . '.log');
$enabledloggers[] = new file_logger($fllevel, true, true, $backuptempdir . '/' . $backupid . '.log');

// Create database_logger, observing $CFG->backup_database_logger_level and defaulting to LOG_WARNING
// and pointing to the backup_logs table
Expand Down
3 changes: 2 additions & 1 deletion backup/util/helper/backup_file_manager.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ class backup_file_manager {
public static function get_backup_storage_base_dir($backupid) {
global $CFG;

return $CFG->tempdir . '/backup/' . $backupid . '/files';
$backupiddir = make_backup_temp_directory($backupid);
return $backupiddir . '/files';
}

/**
Expand Down
5 changes: 3 additions & 2 deletions backup/util/helper/backup_general_helper.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ public static function get_backup_information($tempdir) {

$info = new stdclass(); // Final information goes here

$moodlefile = $CFG->tempdir . '/backup/' . $tempdir . '/moodle_backup.xml';
$backuptempdir = make_backup_temp_directory('', false);
$moodlefile = $backuptempdir . '/' . $tempdir . '/moodle_backup.xml';
if (!file_exists($moodlefile)) { // Shouldn't happen ever, but...
throw new backup_helper_exception('missing_moodle_backup_xml_file', $moodlefile);
}
Expand Down Expand Up @@ -267,7 +268,7 @@ public static function get_backup_information_from_mbz($filepath, file_progress

// Extract moodle_backup.xml.
$tmpname = 'info_from_mbz_' . time() . '_' . random_string(4);
$tmpdir = $CFG->tempdir . '/backup/' . $tmpname;
$tmpdir = make_backup_temp_directory($tmpname);
$fp = get_file_packer('application/vnd.moodle.backup');

$extracted = $fp->extract_to_pathname($filepath, $tmpdir, array('moodle_backup.xml'), $progress);
Expand Down
21 changes: 10 additions & 11 deletions backup/util/helper/backup_helper.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ abstract class backup_helper {
* Given one backupid, create all the needed dirs to have one backup temp dir available
*/
static public function check_and_create_backup_dir($backupid) {
global $CFG;
if (!check_dir_exists($CFG->tempdir . '/backup/' . $backupid, true, true)) {
$backupiddir = make_backup_temp_directory($backupid, false);
if (empty($backupiddir)) {
throw new backup_helper_exception('cannot_create_backup_temp_dir');
}
}
Expand All @@ -49,8 +49,8 @@ static public function check_and_create_backup_dir($backupid) {
* @param \core\progress\base $progress Optional progress reporting object
*/
static public function clear_backup_dir($backupid, \core\progress\base $progress = null) {
global $CFG;
if (!self::delete_dir_contents($CFG->tempdir . '/backup/' . $backupid, '', $progress)) {
$backupiddir = make_backup_temp_directory($backupid, false);
if (!self::delete_dir_contents($backupiddir, '', $progress)) {
throw new backup_helper_exception('cannot_empty_backup_temp_dir');
}
return true;
Expand All @@ -66,9 +66,9 @@ static public function clear_backup_dir($backupid, \core\progress\base $progress
* @param \core\progress\base $progress Optional progress reporting object
*/
static public function delete_backup_dir($backupid, \core\progress\base $progress = null) {
global $CFG;
$backupiddir = make_backup_temp_directory($backupid, false);
self::clear_backup_dir($backupid, $progress);
return rmdir($CFG->tempdir . '/backup/' . $backupid);
return rmdir($backupiddir);
}

/**
Expand Down Expand Up @@ -157,13 +157,12 @@ static public function delete_dir_contents($dir, $excludeddir='', \core\progress
* @param \core\progress\base $progress Optional progress reporting object
*/
static public function delete_old_backup_dirs($deletefrom, \core\progress\base $progress = null) {
global $CFG;

$status = true;
// Get files and directories in the temp backup dir witout descend
$list = get_directory_list($CFG->tempdir . '/backup', '', false, true, true);
// Get files and directories in the backup temp dir without descend.
$backuptempdir = make_backup_temp_directory('');
$list = get_directory_list($backuptempdir, '', false, true, true);
foreach ($list as $file) {
$file_path = $CFG->tempdir . '/backup/' . $file;
$file_path = $backuptempdir . '/' . $file;
$moddate = filemtime($file_path);
if ($status && $moddate < $deletefrom) {
//If directory, recurse
Expand Down
7 changes: 2 additions & 5 deletions backup/util/helper/convert_helper.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,15 +131,12 @@ public static function export_converter_dependencies($converter, $dependency) {
* @return boolean true if moodle2 format detected, false otherwise
*/
public static function detect_moodle2_format($tempdir) {
global $CFG;

$dirpath = $CFG->tempdir . '/backup/' . $tempdir;
$filepath = $dirpath . '/moodle_backup.xml';

$dirpath = make_backup_temp_directory($tempdir, false);
if (!is_dir($dirpath)) {
throw new convert_helper_exception('tmp_backup_directory_not_found', $dirpath);
}

$filepath = $dirpath . '/moodle_backup.xml';
if (!file_exists($filepath)) {
return false;
}
Expand Down
5 changes: 2 additions & 3 deletions backup/util/plan/backup_plan.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,12 @@ class backup_plan extends base_plan implements loggable {
* Constructor - instantiates one object of this class
*/
public function __construct($controller) {
global $CFG;

if (! $controller instanceof backup_controller) {
throw new backup_plan_exception('wrong_backup_controller_specified');
}
$backuptempdir = make_backup_temp_directory('');
$this->controller = $controller;
$this->basepath = $CFG->tempdir . '/backup/' . $controller->get_backupid();
$this->basepath = $backuptempdir . '/' . $controller->get_backupid();
$this->excludingdactivities = false;
parent::__construct('backup_plan');
}
Expand Down
3 changes: 2 additions & 1 deletion backup/util/plan/restore_plan.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ public function __construct($controller) {
if (! $controller instanceof restore_controller) {
throw new restore_plan_exception('wrong_restore_controller_specified');
}
$backuptempdir = make_backup_temp_directory('');
$this->controller = $controller;
$this->basepath = $CFG->tempdir . '/backup/' . $controller->get_tempdir();
$this->basepath = $backuptempdir . '/' . $controller->get_tempdir();
$this->preloaded = false;
$this->decoder = new restore_decode_processor($this->get_restoreid(), $this->get_info()->original_wwwroot, $CFG->wwwroot);
$this->missingmodules = false;
Expand Down
Loading

0 comments on commit ef84414

Please sign in to comment.