Skip to content

Commit

Permalink
MDL-21432 backup - clean temps after execution
Browse files Browse the repository at this point in the history
  • Loading branch information
stronk7 committed Apr 29, 2010
1 parent 760b2ed commit 2de3539
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 6 deletions.
2 changes: 2 additions & 0 deletions backup/controller/backup_controller.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -275,13 +275,15 @@ protected function calculate_backupid() {

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);
}

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;
Expand Down
3 changes: 3 additions & 0 deletions backup/moodle2/backup_final_task.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ public function build() {
// 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;
}

Expand Down
24 changes: 24 additions & 0 deletions backup/moodle2/backup_stepslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -35,6 +40,25 @@ protected function define_execution() {
}
}

/**
* 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
*/
Expand Down
18 changes: 12 additions & 6 deletions backup/util/helper/backup_helper.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,25 @@ static public function check_and_create_backup_dir($backupid) {
}

/**
* 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.
Expand Down Expand Up @@ -127,11 +137,7 @@ static public function delete_old_backup_dirs($deletefrom) {
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);
Expand Down
7 changes: 7 additions & 0 deletions config-dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,13 @@
// 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
Expand Down

0 comments on commit 2de3539

Please sign in to comment.