From 2de3539b43126de0567f80d9041ebdbcb9297d1f Mon Sep 17 00:00:00 2001 From: Eloy Lafuente Date: Thu, 29 Apr 2010 18:30:17 +0000 Subject: [PATCH] MDL-21432 backup - clean temps after execution --- backup/controller/backup_controller.class.php | 2 ++ backup/moodle2/backup_final_task.class.php | 3 +++ backup/moodle2/backup_stepslib.php | 24 +++++++++++++++++++ backup/util/helper/backup_helper.class.php | 18 +++++++++----- config-dist.php | 7 ++++++ 5 files changed, 48 insertions(+), 6 deletions(-) diff --git a/backup/controller/backup_controller.class.php b/backup/controller/backup_controller.class.php index ddb857f517c..1c774e75510 100644 --- a/backup/controller/backup_controller.class.php +++ b/backup/controller/backup_controller.class.php @@ -275,6 +275,7 @@ class backup_controller extends backup implements loggable { public function save_controller() { // Going to save controller to persistent storage, calculate checksum for later checks and save it + // TODO: flag the controller as NA. Any operation on it should be forbidden util loaded back $this->log('saving controller to db', backup::LOG_DEBUG); $this->checksum = $this->calculate_checksum(); backup_controller_dbops::save_controller($this, $this->checksum); @@ -282,6 +283,7 @@ class backup_controller extends backup implements loggable { public static function load_controller($backupid) { // Load controller from persistent storage + // TODO: flag the controller as available. Operations on it can continue $controller = backup_controller_dbops::load_controller($backupid); $controller->log('loading controller from db', backup::LOG_DEBUG); return $controller; diff --git a/backup/moodle2/backup_final_task.class.php b/backup/moodle2/backup_final_task.class.php index 52778ada9f2..ef660e57c3b 100644 --- a/backup/moodle2/backup_final_task.class.php +++ b/backup/moodle2/backup_final_task.class.php @@ -90,6 +90,9 @@ class backup_final_task extends backup_task { // Copy the generated zip file to final destination $this->add_step(new backup_store_backup_file('save_backupfile')); + // Clean the temp dir (conditionaly) and drop temp table + $this->add_step(new drop_and_clean_temp_stuff('drop_and_clean_temp_stuff')); + $this->built = true; } diff --git a/backup/moodle2/backup_stepslib.php b/backup/moodle2/backup_stepslib.php index bc34174f880..fe80f3fd456 100644 --- a/backup/moodle2/backup_stepslib.php +++ b/backup/moodle2/backup_stepslib.php @@ -25,6 +25,11 @@ /** * Define all the backup steps that will be used by common tasks in backup */ + +/** + * create the temp dir where backup/restore will happen, + * delete old directories and create temp ids table + */ class create_and_clean_temp_stuff extends backup_execution_step { protected function define_execution() { @@ -35,6 +40,25 @@ class create_and_clean_temp_stuff extends backup_execution_step { } } +/** + * delete the temp dir used by backup/restore (conditionally), + * delete old directories and drop tem ids table. Note we delete + * the directory but not the correspondig log file that will be + * there for, at least, 4 hours - only delete_old_backup_dirs() + * deletes log files (for easier access to them) + */ +class drop_and_clean_temp_stuff extends backup_execution_step { + + protected function define_execution() { + global $CFG; + backup_controller_dbops::drop_backup_ids_temp_table($this->get_backupid()); // Drop ids temp table + backup_helper::delete_old_backup_dirs(time() - (4 * 60 * 60)); // Delete > 4 hours temp dirs + if (empty($CFG->keeptempdirectoriesonbackup)) { // Conditionally + backup_helper::delete_backup_dir($this->get_backupid()); // Empty backup dir + } + } +} + /** * Create the directory where all the task (activity/block...) information will be stored */ diff --git a/backup/util/helper/backup_helper.class.php b/backup/util/helper/backup_helper.class.php index 11fb7f4348a..63ea5c4f517 100644 --- a/backup/util/helper/backup_helper.class.php +++ b/backup/util/helper/backup_helper.class.php @@ -40,15 +40,25 @@ abstract class backup_helper { } /** - * Given one backupid, ensure its temp dir is completelly empty + * Given one backupid, ensure its temp dir is completely empty */ static public function clear_backup_dir($backupid) { global $CFG; if (!self::delete_dir_contents($CFG->dataroot . '/temp/backup/' . $backupid)) { throw new backup_helper_exception('cannot_empty_backup_temp_dir'); } + return true; } + /** + * Given one backupid, delete completely its temp dir + */ + static public function delete_backup_dir($backupid) { + global $CFG; + self::clear_backup_dir($backupid); + return rmdir($CFG->dataroot . '/temp/backup/' . $backupid); + } + /** * Given one fullpath to directory, delete its contents recursively * Copied originally from somewhere in the net. @@ -127,11 +137,7 @@ abstract class backup_helper { if ($status && $moddate < $deletefrom) { //If directory, recurse if (is_dir($file_path)) { - $status = self::delete_dir_contents($file_path); - //There is nothing, delete the directory itself - if ($status) { - $status = rmdir($file_path); - } + $status = self::delete_backup_dir($file_path); //If file } else { unlink($file_path); diff --git a/config-dist.php b/config-dist.php index ceb835eb931..537495e0d1e 100644 --- a/config-dist.php +++ b/config-dist.php @@ -184,6 +184,13 @@ $CFG->admin = 'admin'; // course requiring users to be created. // $CFG->disableusercreationonrestore = true; // +// Keep the temporary directories used by backup and restore without being +// deleted at the end of the process. Use it if you want to debug / view +// all the information stored there after the process has ended. Note that +// those directories may be deleted (after some ttl) both by cron and / or +// by new backup / restore invocations. +// $CFG->keeptempdirectoriesonbackup = true; +// // Modify the restore process in order to force the "user checks" to assume // that the backup originated from a different site, so detection of matching // users is performed with different (more "relaxed") rules. Note that this is