Commit 2de3539b authored by Eloy Lafuente's avatar Eloy Lafuente
Browse files

MDL-21432 backup - clean temps after execution

parent 760b2edb
......@@ -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;
......
......@@ -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;
}
......
......@@ -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
*/
......
......@@ -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);
......
......@@ -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
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment