MDL-50602 - Automated backup: Remove old backups associated to courses

This commit is contained in:
Serge Gauthier 2015-06-30 09:24:18 -04:00 committed by Jean-Philippe Gaudreau
parent fd57d685c7
commit 694f195da0
3 changed files with 156 additions and 49 deletions

View File

@ -262,6 +262,45 @@ if ($hassiteconfig or has_any_capability($capabilities, $systemcontext)) {
)));
$temp->add(new admin_setting_configcheckbox('backup/backup_auto_skip_modif_prev', new lang_string('skipmodifprev', 'backup'), new lang_string('skipmodifprevhelp', 'backup'), 0));
// Automated backup removal section.
$temp->add(new admin_setting_heading('automatedbackupremovalsettings',
new lang_string('automatedbackupremovalsettings', 'backup'), ''));
$temp->add(new admin_setting_configselect('backup/backup_auto_keep_days', new lang_string('automatedbackupkeepdays', 'backup'),
new lang_string('automatedbackupkeepdayshelp', 'backup'), 0, array(
0 => new lang_string('never'),
1000 => new lang_string('numdays', '', 1000),
365 => new lang_string('numdays', '', 365),
180 => new lang_string('numdays', '', 180),
150 => new lang_string('numdays', '', 150),
120 => new lang_string('numdays', '', 120),
90 => new lang_string('numdays', '', 90),
60 => new lang_string('numdays', '', 60),
35 => new lang_string('numdays', '', 35),
10 => new lang_string('numdays', '', 10),
5 => new lang_string('numdays', '', 5),
2 => new lang_string('numdays', '', 2)
)));
$temp->add(new admin_setting_configselect('backup/backup_auto_keep_copies',
new lang_string('automatedbackupkeepcopies', 'backup'), new lang_string('automatedbackupkeepcopieshelp', 'backup'),
0, array(
0 => new lang_string('none'),
1 => '1',
2 => '2',
5 => '5',
10 => '10',
20 => '20',
30 => '30',
40 => '40',
50 => '50',
100 => '100',
200 => '200',
300 => '300',
400 => '400',
500 => '500'
)));
// Automated defaults section.
$temp->add(new admin_setting_heading('automatedsettings', new lang_string('automatedsettings','backup'), ''));
$temp->add(new admin_setting_configcheckbox('backup/backup_auto_users', new lang_string('generalusers', 'backup'), new lang_string('configgeneralusers', 'backup'), 1));

View File

@ -174,7 +174,8 @@ abstract class backup_cron_automated_helper {
$backupcourse->nextstarttime = $nextstarttime;
$DB->update_record('backup_courses', $backupcourse);
mtrace('Skipping ' . $course->fullname . ' (Not scheduled for backup until ' . $showtime . ')');
} else if ($skipped) { // Must have been skipped for a reason.
} else {
if ($skipped) { // Must have been skipped for a reason.
$backupcourse->laststatus = self::BACKUP_STATUS_SKIPPED;
$backupcourse->nextstarttime = $nextstarttime;
$DB->update_record('backup_courses', $backupcourse);
@ -196,19 +197,17 @@ abstract class backup_cron_automated_helper {
$backupcourse->laststatus = self::BACKUP_STATUS_UNFINISHED;
$DB->update_record('backup_courses', $backupcourse);
$backupcourse->laststatus = backup_cron_automated_helper::launch_automated_backup($course, $backupcourse->laststarttime, $admin->id);
$backupcourse->laststatus = self::launch_automated_backup($course, $backupcourse->laststarttime,
$admin->id);
$backupcourse->lastendtime = time();
$backupcourse->nextstarttime = $nextstarttime;
$DB->update_record('backup_courses', $backupcourse);
if ($backupcourse->laststatus === self::BACKUP_STATUS_OK) {
// Clean up any excess course backups now that we have
// taken a successful backup.
$removedcount = backup_cron_automated_helper::remove_excess_backups($course);
}
}
// Remove excess backups.
$removedcount = self::remove_excess_backups($course, $now);
mtrace("complete - next execution: $showtime");
}
}
@ -542,16 +541,18 @@ abstract class backup_cron_automated_helper {
* The number of backups keep comes from $config->backup_auto_keep.
*
* @param stdClass $course object
* @param int $now execution time
* @return bool
*/
public static function remove_excess_backups($course) {
public static function remove_excess_backups($course, $now) {
$config = get_config('backup');
$keep = (int)$config->backup_auto_keep;
$storage = $config->backup_auto_storage;
$dir = $config->backup_auto_destination;
$histdays = (int)$config->backup_auto_keep_days;
if ($keep == 0) {
// Means keep all backup files.
if ($keep == 0 && $histdays == 0) {
// Means keep all backup files and never remove backup after x days.
return true;
}
@ -571,17 +572,11 @@ abstract class backup_cron_automated_helper {
foreach ($fs->get_area_files($context->id, $component, $filearea, $itemid) as $file) {
$files[$file->get_timemodified()] = $file;
}
if (count($files) <= $keep) {
// There are less matching files than the desired number to keep there is nothing to clean up.
$backupremoved = self::remove_old_backups($files, true, $now, $course->idnumber);
if (!$backupremoved) {
return 0;
}
// Sort by keys descending (newer to older filemodified).
krsort($files);
$remove = array_splice($files, $keep);
foreach ($remove as $file) {
$file->delete();
}
//mtrace('Removed '.count($remove).' old backup file(s) from the automated filearea');
}
// Clean up excess backups in the specified external directory.
@ -615,17 +610,11 @@ abstract class backup_cron_automated_helper {
$files[$file] = $bcinfo->backup_date;
}
}
if (count($files) <= $keep) {
// There are less matching files than the desired number to keep there is nothing to clean up.
$backupremoved = self::remove_old_backups($files, false, $now, $course->idnumber);
if (!$backupremoved) {
return 0;
}
// Sort by values descending (newer to older filemodified).
arsort($files);
$remove = array_splice($files, $keep);
foreach (array_keys($remove) as $file) {
unlink($dir . '/' . $file);
}
//mtrace('Removed '.count($remove).' old backup file(s) from external directory');
}
return true;
@ -652,4 +641,78 @@ abstract class backup_cron_automated_helper {
}
return false;
}
/**
* Removes excess and old backups from the external system or the local file system.
*
* The number of backups to keep comes from $config->backup_auto_keep and
* $config->backup_auto_keep_copies.
*
* @param array $files existing backups
* @param bool $timeinkey endicate if the backup's time is in the key of the array
* @param int $now starting time of the process
* @param string $courseidnumber courseidnumer currently processed
* @return bool
*/
protected static function remove_old_backups($files, $timeinkey, $now, $courseidnumber) {
$config = get_config('backup');
$dir = $config->backup_auto_destination;
$keep = (int)$config->backup_auto_keep;
$histkeep = (int)$config->backup_auto_keep_copies;
$histdays = (int)$config->backup_auto_keep_days;
if ($timeinkey) {
// Sort by keys descending (newer to older filemodified).
krsort($files);
} else {
// Sort by values descending (newer to older filemodified).
arsort($files);
}
$tokeep = $keep;
$oldbackup = false;
if ($histdays != 0) {
$keepcounter = 0;
foreach ($files as $key => $value) {
if ($timeinkey) {
$timemodified = $key;
} else {
$timemodified = $value;
}
if ($timemodified < ($now - ($histdays * DAYSECS)) || $keepcounter >= $keep) {
$oldbackup = true;
break;
}
$keepcounter++;
}
if ($keepcounter < $histkeep) {
$tokeep = $histkeep;
} else {
$tokeep = $keepcounter;
}
}
if (count($files) <= $tokeep) {
// There are less matching files than the desired number to keep there is nothing to clean up.
return false;
}
$remove = array_splice($files, $tokeep);
if ($timeinkey) {
foreach ($remove as $file) {
$file->delete();
}
} else {
foreach (array_keys($remove) as $file) {
unlink($dir . '/' . $file);
}
}
if ($oldbackup) {
mtrace('Course '. $courseidnumber. ' removed '.count($remove).' old backup file(s) from the automated filearea');
}
return true;
}
}

View File

@ -26,6 +26,11 @@ $string['autoactivedisabled'] = 'Disabled';
$string['autoactiveenabled'] = 'Enabled';
$string['autoactivemanual'] = 'Manual';
$string['autoactivedescription'] = 'Choose whether or not to do automated backups. If manual is selected automated backups will be possible only by through the automated backups CLI script. This can be done either manually on the command line or through cron.';
$string['automatedbackupkeepcopies'] = 'Number of backups to keep';
$string['automatedbackupkeepcopieshelp'] = 'Choose the minimum number of automated backups to keep.';
$string['automatedbackupkeepdays'] = 'Remove backups after';
$string['automatedbackupkeepdayshelp'] = 'Indicate after how many days you want to remove automated backups.';
$string['automatedbackupremovalsettings'] = 'Automated backup removal settings';
$string['automatedbackupschedule'] = 'Schedule';
$string['automatedbackupschedulehelp'] = 'Choose which days of the week to perform automated backups.';
$string['automatedbackupsinactive'] = 'Automated backups haven\'t been enabled by the site admin';