/** * Runs the automated backups if required * * @global moodle_database $DB */ public static function run_automated_backup($rundirective = self::RUN_ON_SCHEDULE) { global $CFG, $DB; $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); if ($state === backup_cron_automated_helper::STATE_DISABLED) { mtrace('INACTIVE'); return $state; } else { if ($state === backup_cron_automated_helper::STATE_RUNNING) { mtrace('RUNNING'); if ($rundirective == self::RUN_IMMEDIATELY) { mtrace('Automated backups are already running. If this script is being run by cron this constitues an error. You will need to increase the time between executions within cron.'); } else { mtrace("automated backup are already running. Execution delayed"); } return $state; } else { mtrace('OK'); } } backup_cron_automated_helper::set_state_running(); mtrace("Getting admin info"); $admin = get_admin(); if (!$admin) { mtrace("Error: No admin account was found"); $state = false; } if ($status) { mtrace("Checking courses"); mtrace("Skipping deleted courses", '...'); mtrace(sprintf("%d courses", backup_cron_automated_helper::remove_deleted_courses_from_schedule())); } if ($status) { mtrace('Running required automated backups...'); cron_trace_time_and_memory(); // This could take a while! core_php_time_limit::raise(); raise_memory_limit(MEMORY_EXTRA); $nextstarttime = backup_cron_automated_helper::calculate_next_automated_backup($admin->timezone, $now); $showtime = "undefined"; if ($nextstarttime > 0) { $showtime = date('r', $nextstarttime); } $rs = $DB->get_recordset('course'); foreach ($rs as $course) { $backupcourse = $DB->get_record('backup_courses', array('courseid' => $course->id)); if (!$backupcourse) { $backupcourse = new stdClass(); $backupcourse->courseid = $course->id; $backupcourse->laststatus = self::BACKUP_STATUS_NOTYETRUN; $DB->insert_record('backup_courses', $backupcourse); $backupcourse = $DB->get_record('backup_courses', array('courseid' => $course->id)); } // The last backup is considered as successful when OK or SKIPPED. $lastbackupwassuccessful = ($backupcourse->laststatus == self::BACKUP_STATUS_SKIPPED || $backupcourse->laststatus == self::BACKUP_STATUS_OK) && ($backupcourse->laststarttime > 0 && $backupcourse->lastendtime > 0); // Assume that we are not skipping anything. $skipped = false; $skippedmessage = ''; // Check if we are going to be running the backup now. $shouldrunnow = $backupcourse->nextstarttime > 0 && $backupcourse->nextstarttime < $now || $rundirective == self::RUN_IMMEDIATELY; // If config backup_auto_skip_hidden is set to true, skip courses that are not visible. if ($shouldrunnow && $config->backup_auto_skip_hidden) { $skipped = $config->backup_auto_skip_hidden && !$course->visible; $skippedmessage = 'Not visible'; } // 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 ($shouldrunnow && !$skipped && $lastbackupwassuccessful && $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. $logexists = self::is_course_modified($course->id, $timenotmodifsincedays); $skipped = $course->timemodified <= $timenotmodifsincedays && !$logexists; $skippedmessage = 'Not modified in the past ' . $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 ($shouldrunnow && !$skipped && $lastbackupwassuccessful && $config->backup_auto_skip_modif_prev) { // Check log if there were any modifications to the course content. $logexists = self::is_course_modified($course->id, $backupcourse->laststarttime); $skipped = $course->timemodified <= $backupcourse->laststarttime && !$logexists; $skippedmessage = 'Not modified since previous backup'; } // Check if the course is not scheduled to run right now. if (!$shouldrunnow) { $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. $backupcourse->laststatus = self::BACKUP_STATUS_SKIPPED; $backupcourse->nextstarttime = $nextstarttime; $DB->update_record('backup_courses', $backupcourse); mtrace('Skipping ' . $course->fullname . ' (' . $skippedmessage . ')'); mtrace('Backup of \'' . $course->fullname . '\' is scheduled on ' . $showtime); } else { // Backup every non-skipped courses. mtrace('Backing up ' . $course->fullname . '...'); // We have to send an 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 != self::BACKUP_STATUS_UNFINISHED) { // Set laststarttime. $starttime = time(); $backupcourse->laststarttime = time(); $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->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); } } mtrace("complete - next execution: {$showtime}"); } } } $rs->close(); } //Send email to admin if necessary if ($emailpending) { mtrace("Sending email to admin"); $message = ""; $count = backup_cron_automated_helper::get_backup_status_array(); $haserrors = $count[self::BACKUP_STATUS_ERROR] != 0 || $count[self::BACKUP_STATUS_UNFINISHED] != 0; // Build the message text. // Summary. $message .= get_string('summary') . "\n"; $message .= "==================================================\n"; $message .= ' ' . get_string('courses') . '; ' . array_sum($count) . "\n"; $message .= ' ' . get_string('ok') . '; ' . $count[self::BACKUP_STATUS_OK] . "\n"; $message .= ' ' . get_string('skipped') . '; ' . $count[self::BACKUP_STATUS_SKIPPED] . "\n"; $message .= ' ' . get_string('error') . '; ' . $count[self::BACKUP_STATUS_ERROR] . "\n"; $message .= ' ' . get_string('unfinished') . '; ' . $count[self::BACKUP_STATUS_UNFINISHED] . "\n"; $message .= ' ' . get_string('warning') . '; ' . $count[self::BACKUP_STATUS_WARNING] . "\n"; $message .= ' ' . get_string('backupnotyetrun') . '; ' . $count[self::BACKUP_STATUS_NOTYETRUN] . "\n\n"; //Reference if ($haserrors) { $message .= " " . get_string('backupfailed') . "\n\n"; $dest_url = "{$CFG->wwwroot}/report/backups/index.php"; $message .= " " . get_string('backuptakealook', '', $dest_url) . "\n\n"; //Set message priority $admin->priority = 1; //Reset unfinished to error $DB->set_field('backup_courses', 'laststatus', '0', array('laststatus' => '2')); } else { $message .= " " . get_string('backupfinished') . "\n"; } //Build the message subject $site = get_site(); $prefix = format_string($site->shortname, true, array('context' => context_course::instance(SITEID))) . ": "; if ($haserrors) { $prefix .= "[" . strtoupper(get_string('error')) . "] "; } $subject = $prefix . get_string('automatedbackupstatus', 'backup'); //Send the message $eventdata = new stdClass(); $eventdata->modulename = 'moodle'; $eventdata->userfrom = $admin; $eventdata->userto = $admin; $eventdata->subject = $subject; $eventdata->fullmessage = $message; $eventdata->fullmessageformat = FORMAT_PLAIN; $eventdata->fullmessagehtml = ''; $eventdata->smallmessage = ''; $eventdata->component = 'moodle'; $eventdata->name = 'backup'; message_send($eventdata); } //Everything is finished stop backup_auto_running backup_cron_automated_helper::set_state_running(false); mtrace('Automated backups complete.'); return $status; }
/** * Runs the automated backups if required * * @global moodle_database $DB */ public static function run_automated_backup($rundirective = self::RUN_ON_SCHEDULE) { global $CFG, $DB; $status = true; $emailpending = false; $now = time(); mtrace("Checking automated backup status", '...'); $state = backup_cron_automated_helper::get_automated_backup_state($rundirective); if ($state === backup_cron_automated_helper::STATE_DISABLED) { mtrace('INACTIVE'); return $state; } else { if ($state === backup_cron_automated_helper::STATE_RUNNING) { mtrace('RUNNING'); if ($rundirective == self::RUN_IMMEDIATELY) { mtrace('Automated backups are already running. If this script is being run by cron this constitues an error. You will need to increase the time between executions within cron.'); } else { mtrace("automated backup are already running. Execution delayed"); } return $state; } else { mtrace('OK'); } } backup_cron_automated_helper::set_state_running(); mtrace("Getting admin info"); $admin = get_admin(); if (!$admin) { mtrace("Error: No admin account was found"); $state = false; } if ($status) { mtrace("Checking courses"); mtrace("Skipping deleted courses", '...'); mtrace(sprintf("%d courses", backup_cron_automated_helper::remove_deleted_courses_from_schedule())); } if ($status) { mtrace('Running required automated backups...'); // This could take a while! @set_time_limit(0); raise_memory_limit(MEMORY_EXTRA); $nextstarttime = backup_cron_automated_helper::calculate_next_automated_backup($admin->timezone, $now); $showtime = "undefined"; if ($nextstarttime > 0) { $showtime = userdate($nextstarttime, "", $admin->timezone); } $rs = $DB->get_recordset('course'); foreach ($rs as $course) { $backupcourse = $DB->get_record('backup_courses', array('courseid' => $course->id)); if (!$backupcourse) { $backupcourse = new stdClass(); $backupcourse->courseid = $course->id; $DB->insert_record('backup_courses', $backupcourse); $backupcourse = $DB->get_record('backup_courses', array('courseid' => $course->id)); } // Skip courses that do not yet need backup $skipped = !($backupcourse->nextstarttime >= 0 && $backupcourse->nextstarttime < $now || $rundirective == self::RUN_IMMEDIATELY); // 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 = backup_cron_automated_helper::BACKUP_STATUS_SKIPPED; $backupcourse->nextstarttime = $nextstarttime; $DB->update_record('backup_courses', $backupcourse); mtrace('Skipping unchanged course ' . $course->fullname); $skipped = true; } } //Now we backup every non-skipped course if (!$skipped) { 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) { //Set laststarttime $starttime = time(); $backupcourse->laststarttime = time(); $backupcourse->laststatus = backup_cron_automated_helper::BACKUP_STATUS_UNFINISHED; $DB->update_record('backup_courses', $backupcourse); $backupcourse->laststatus = backup_cron_automated_helper::launch_automated_backup($course, $backupcourse->laststarttime, $admin->id); $backupcourse->lastendtime = time(); $backupcourse->nextstarttime = $nextstarttime; $DB->update_record('backup_courses', $backupcourse); if ($backupcourse->laststatus) { // Clean up any excess course backups now that we have // taken a successful backup. $removedcount = backup_cron_automated_helper::remove_excess_backups($course); } } mtrace("complete - next execution: {$showtime}"); } } $rs->close(); } //Send email to admin if necessary if ($emailpending) { mtrace("Sending email to admin"); $message = ""; $count = backup_cron_automated_helper::get_backup_status_array(); $haserrors = $count[backup_cron_automated_helper::BACKUP_STATUS_ERROR] != 0 || $count[backup_cron_automated_helper::BACKUP_STATUS_UNFINISHED] != 0; //Build the message text //Summary $message .= get_string('summary') . "\n"; $message .= "==================================================\n"; $message .= " " . get_string('courses') . ": " . array_sum($count) . "\n"; $message .= " " . get_string('ok') . ": " . $count[backup_cron_automated_helper::BACKUP_STATUS_OK] . "\n"; $message .= " " . get_string('skipped') . ": " . $count[backup_cron_automated_helper::BACKUP_STATUS_SKIPPED] . "\n"; $message .= " " . get_string('error') . ": " . $count[backup_cron_automated_helper::BACKUP_STATUS_ERROR] . "\n"; $message .= " " . get_string('unfinished') . ": " . $count[backup_cron_automated_helper::BACKUP_STATUS_UNFINISHED] . "\n\n"; //Reference if ($haserrors) { $message .= " " . get_string('backupfailed') . "\n\n"; $dest_url = "{$CFG->wwwroot}/report/backups/index.php"; $message .= " " . get_string('backuptakealook', '', $dest_url) . "\n\n"; //Set message priority $admin->priority = 1; //Reset unfinished to error $DB->set_field('backup_courses', 'laststatus', '0', array('laststatus' => '2')); } else { $message .= " " . get_string('backupfinished') . "\n"; } //Build the message subject $site = get_site(); $prefix = format_string($site->shortname, true, array('context' => get_context_instance(CONTEXT_COURSE, SITEID))) . ": "; if ($haserrors) { $prefix .= "[" . strtoupper(get_string('error')) . "] "; } $subject = $prefix . get_string('automatedbackupstatus', 'backup'); //Send the message $eventdata = new stdClass(); $eventdata->modulename = 'moodle'; $eventdata->userfrom = $admin; $eventdata->userto = $admin; $eventdata->subject = $subject; $eventdata->fullmessage = $message; $eventdata->fullmessageformat = FORMAT_PLAIN; $eventdata->fullmessagehtml = ''; $eventdata->smallmessage = ''; $eventdata->component = 'moodle'; $eventdata->name = 'backup'; message_send($eventdata); } //Everything is finished stop backup_auto_running backup_cron_automated_helper::set_state_running(false); mtrace('Automated backups complete.'); return $status; }