Permalink
Browse files

MDL-32148 Backup: Adding config settings for skipping courses in auto…

…mated backups
  • Loading branch information...
1 parent ccd90e7 commit 914442ae295570db1c69b1fd678cffa41f6e54bb @gaudreaj gaudreaj committed Sep 19, 2012
Showing with 77 additions and 16 deletions.
  1. +19 −0 admin/settings/courses.php
  2. +52 −16 backup/util/helper/backup_cron_helper.class.php
  3. +6 −0 lang/en/backup.php
View
@@ -169,6 +169,25 @@
500 => '500');
$temp->add(new admin_setting_configselect('backup/backup_auto_keep', new lang_string('keep'), new lang_string('backupkeephelp'), 1, $keepoptoins));
$temp->add(new admin_setting_configcheckbox('backup/backup_shortname', new lang_string('backup_shortname', 'admin'), new lang_string('backup_shortnamehelp', 'admin'), 0));
+ $temp->add(new admin_setting_configcheckbox('backup/backup_auto_skip_hidden', new lang_string('skiphidden', 'backup'), new lang_string('skiphiddenhelp', 'backup'), 1));
+ $temp->add(new admin_setting_configselect('backup/backup_auto_skip_modif_days', new lang_string('skipmodifdays', 'backup'), new lang_string('skipmodifdayshelp', 'backup'), 30, array(
+ 0 => new lang_string('never'),
+ 1 => new lang_string('numdays', '', 1),
+ 2 => new lang_string('numdays', '', 2),
+ 3 => new lang_string('numdays', '', 3),
+ 5 => new lang_string('numdays', '', 5),
+ 7 => new lang_string('numdays', '', 7),
+ 10 => new lang_string('numdays', '', 10),
+ 14 => new lang_string('numdays', '', 14),
+ 20 => new lang_string('numdays', '', 20),
+ 30 => new lang_string('numdays', '', 30),
+ 60 => new lang_string('numdays', '', 60),
+ 90 => new lang_string('numdays', '', 90),
+ 120 => new lang_string('numdays', '', 120),
+ 180 => new lang_string('numdays', '', 180),
+ 365 => new lang_string('numdays', '', 365)
+ )));
+ $temp->add(new admin_setting_configcheckbox('backup/backup_auto_skip_modif_prev', new lang_string('skipmodifprev', 'backup'), new lang_string('skipmodifprevhelp', 'backup'), 0));
// Automated defaults section.
$temp->add(new admin_setting_heading('automatedsettings', new lang_string('automatedsettings','backup'), ''));
@@ -69,6 +69,7 @@ public static function run_automated_backup($rundirective = self::RUN_ON_SCHEDUL
$status = true;
$emailpending = false;
$now = time();
+ $config = get_config('backup');
mtrace("Checking automated backup status",'...');
$state = backup_cron_automated_helper::get_automated_backup_state($rundirective);
@@ -127,37 +128,72 @@ public static function run_automated_backup($rundirective = self::RUN_ON_SCHEDUL
// Skip courses that do not yet need backup
$skipped = !(($backupcourse->nextstarttime > 0 && $backupcourse->nextstarttime < $now) || $rundirective == self::RUN_IMMEDIATELY);
+ $skippedmessage = 'Do not yet need backup';
if ($skipped && $backupcourse->nextstarttime != $nextstarttime) {
$backupcourse->nextstarttime = $nextstarttime;
- $backupcourse->laststatus = backup_cron_automated_helper::BACKUP_STATUS_SKIPPED;
+ $backupcourse->laststatus = self::BACKUP_STATUS_SKIPPED;
$DB->update_record('backup_courses', $backupcourse);
mtrace('Backup of \'' . $course->fullname . '\' is scheduled on ' . $showtime);
}
- // Skip backup of unavailable courses that have remained unmodified in a month
- if (!$skipped && empty($course->visible) && ($now - $course->timemodified) > 31*24*60*60) { //Hidden + settings were unmodified last month
- //Check log if there were any modifications to the course content
- $sqlwhere = "course=:courseid AND time>:time AND ". $DB->sql_like('action', ':action', false, true, true);
- $params = array('courseid' => $course->id, 'time' => $now-31*24*60*60, 'action' => '%view%');
- $logexists = $DB->record_exists_select('log', $sqlwhere, $params);
- if (!$logexists) {
- $backupcourse->laststatus = self::BACKUP_STATUS_SKIPPED;
- $backupcourse->nextstarttime = $nextstarttime;
- $DB->update_record('backup_courses', $backupcourse);
- mtrace('Skipping unchanged course '.$course->fullname);
- $skipped = true;
+ // Check courses that must be skipped from settings.
+ if (!$skipped) {
+ // If config backup_auto_skip_hidden is set to true, skip courses that are not visible.
+ $skipped = ($config->backup_auto_skip_hidden && !$course->visible);
+ $skippedmessage = 'Not visible';
+
+ if (!$skipped) {
+ $sqlwhere = "course=:courseid AND time>:time AND ".$DB->sql_like('action', ':action', false, true, true);
+ // If config backup_auto_skip_modif_days is set to true, skip courses
+ // that have not been modified since the number of days defined.
+ if ($config->backup_auto_skip_modif_days) {
+ $timenotmodifsincedays = $now - ($config->backup_auto_skip_modif_days * DAYSECS);
+ //Check log if there were any modifications to the course content.
+ $params = array('courseid' => $course->id,
+ 'time' => $timenotmodifsincedays,
+ 'action' => '%view%');
+ $logexists = $DB->record_exists_select('log', $sqlwhere, $params);
+
+ $skipped = ($course->timemodified <= $timenotmodifsincedays && !$logexists);
+ $skippedmessage = 'Not modified since '.$config->backup_auto_skip_modif_days.' days';
+ }
+
+ // If config backup_auto_skip_modif_prev is set to true, skip courses
+ // that have not been modified since previous backup.
+ if (!$skipped && $config->backup_auto_skip_modif_prev) {
+ // Make sure that backup last status is OK or SKIPPED.
+ $hasrightstatus = $backupcourse->laststatus == self::BACKUP_STATUS_SKIPPED ||
+ $backupcourse->laststatus == self::BACKUP_STATUS_OK;
+ if ($hasrightstatus) {
+ //Check log if there were any modifications to the course content.
+ $params = array('courseid' => $course->id,
+ 'time' => $backupcourse->laststarttime,
+ 'action' => '%view%');
+ $logexists = $DB->record_exists_select('log', $sqlwhere, $params);
+
+ $skipped = ($course->timemodified <= $backupcourse->laststarttime && !$logexists);
+ $skippedmessage = 'Not modified since previous backup';
+ }
+ }
}
}
- //Now we backup every non-skipped course
- if (!$skipped) {
+ // Skip courses not needed for backup.
+ if ($skipped) {
+ $backupcourse->laststatus = self::BACKUP_STATUS_SKIPPED;
+ $backupcourse->nextstarttime = $nextstarttime;
+ $DB->update_record('backup_courses', $backupcourse);
+ mtrace('Skipping '.$course->fullname.' ('.$skippedmessage.')');
+ }
+ // Backup every non-skipped courses.
+ else {
mtrace('Backing up '.$course->fullname.'...');
//We have to send a email because we have included at least one backup
$emailpending = true;
//Only make the backup if laststatus isn't 2-UNFINISHED (uncontrolled error)
- if ($backupcourse->laststatus != 2) {
+ if ($backupcourse->laststatus != self::BACKUP_STATUS_UNFINISHED) {
//Set laststarttime
$starttime = time();
View
@@ -239,5 +239,11 @@
$string['setting_course_startdate'] = 'Course startdate';
$string['setting_keep_roles_and_enrolments'] = 'Keep current roles and enrolments';
$string['setting_keep_groups_and_groupings'] = 'Keep current groups and groupings';
+$string['skiphidden'] = 'Skip hidden courses';
+$string['skiphiddenhelp'] = 'Choose whether or not to skip hidden courses';
+$string['skipmodifdays'] = 'Skip courses not modified since';
+$string['skipmodifdayshelp'] = 'Choose to skip courses that have not been modified since a number of days';
+$string['skipmodifprev'] = 'Skip courses not modified since previous backup';
+$string['skipmodifprevhelp'] = 'Choose whether or not to skip courses that have not been modified since previous backup';
$string['totalcategorysearchresults'] = 'Total categories: {$a}';
$string['totalcoursesearchresults'] = 'Total courses: {$a}';

0 comments on commit 914442a

Please sign in to comment.