Skip to content

Commit

Permalink
Scheduled backup improvements:
Browse files Browse the repository at this point in the history
- Old (>4 hour) directories are deleted at the beginning of the
  execution, so the disk full problem should be out!
- There are 2 types of errors:
    - Controlled errors: The backup process finish although something
        has been wrong.
    - Uncontrolled errors: The backup process dies (memory fault, apache
        problem, electric problem...:-)).
- Now scheduled backup is able to detect previous uncontrolled errors to
  avoid repeating the same course always. It'll be deferred to the next
  execution.
- Every time that a backup cycle has finished, admins will receive an email
  with the summary of the execution.
- The 'ERROR' text will be present in email subjects if something was wrong.

With this, all should be ok (I hope).

Merged from MOODLE_14_STABLE
  • Loading branch information
stronk7 committed Sep 24, 2004
1 parent c3cb1fd commit 70bbfd3
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 36 deletions.
93 changes: 61 additions & 32 deletions backup/backup_scheduled.php
Expand Up @@ -54,6 +54,12 @@ function schedule_backup_cron() {
$status = false;
}

//Delete old_entries from backup tables
if ($status) {
mtrace(" Deleting old data");
$status = backup_delete_old_data();
}

//Now we get a list of courses in the server
if ($status) {
mtrace(" Checking courses");
Expand All @@ -79,20 +85,25 @@ function schedule_backup_cron() {
}
//Now we backup every course with nextstarttime < now
if ($backup_course->nextstarttime > 0 && $backup_course->nextstarttime < $now) {
//Set laststarttime
$starttime = time();
set_field("backup_courses","laststarttime",$starttime,"courseid",$backup_course->courseid);
//Launch backup
$course_status = schedule_backup_launch_backup($course,$starttime);
//We have to send a email because we have included at least one backup
$emailpending = true;
//Set lastendtime
set_field("backup_courses","lastendtime",time(),"courseid",$backup_course->courseid);
//Set laststatus
if ($course_status) {
set_field("backup_courses","laststatus","1","courseid",$backup_course->courseid);
} else {
set_field("backup_courses","laststatus","0","courseid",$backup_course->courseid);
//Only make the backup if laststatus isn't 2-UNFINISHED (uncontrolled error)
if ($backup_course->laststatus != 2) {
//Set laststarttime
$starttime = time();
set_field("backup_courses","laststarttime",$starttime,"courseid",$backup_course->courseid);
//Set course status to unfinished, the process will reset it
set_field("backup_courses","laststatus","2","courseid",$backup_course->courseid);
//Launch backup
$course_status = schedule_backup_launch_backup($course,$starttime);
//Set lastendtime
set_field("backup_courses","lastendtime",time(),"courseid",$backup_course->courseid);
//Set laststatus
if ($course_status) {
set_field("backup_courses","laststatus","1","courseid",$backup_course->courseid);
} else {
set_field("backup_courses","laststatus","0","courseid",$backup_course->courseid);
}
}
}

Expand All @@ -117,25 +128,47 @@ function schedule_backup_cron() {
delete_records_select("backup_log", "laststarttime < '$loglifetime'");
}

//Send email to admin
//Send email to admin if necessary
if ($emailpending) {
mtrace(" Sending email to admin");
$message = "";
//Build the message text (future versions should handle html messages too!!)
$logs = get_records_select ("backup_log","laststarttime >= '$now'","id");
if ($logs) {
$currentcourse = 1;
foreach ($logs as $log) {
if ($currentcourse != $log->courseid) {
$message .= "\n==================================================\n\n";
$currentcourse = $log->courseid;
}
$message .= userdate($log->time,"%T",$admin->timezone)." ".$log->info."\n";
}

//Get info about the status of courses
$count_all = count_records('backup_courses');
$count_ok = count_records('backup_courses','laststatus','1');
$count_error = count_records('backup_courses','laststatus','0');
$count_unfinished = count_records('backup_courses','laststatus','2');

//Build the message text
//Summary
$message .= get_string('summary')."\n";
$message .= "==================================================\n";
$message .= " ".get_string('courses').": ".$count_all."\n";
$message .= " ".get_string('ok').": ".$count_ok."\n";
$message .= " ".get_string('error').": ".$count_error."\n";
$message .= " ".get_string('unfinished').": ".$count_unfinished."\n\n";

//Reference
if ($count_error != 0 || $count_unfinished != 0) {
$message .= " ".get_string('backupfailed')."\n\n";
$dest_url = $CFG->wwwroot.'/backup/log.php';
$message .= " ".get_string('backuptakealook','',$dest_url)."\n\n";
//Reset unfinished to error
set_field('backup_courses','laststatus','0','laststatus','2');
} else {
$message .= " ".get_string('backupfinished')."\n";
}
//Send the message

//Build the message subject
$site = get_site();
email_to_user($admin,$admin,"$site->shortname: ".get_string("scheduledbackupstatus"),$message);
$prefix = $site->shortname.": ";
if ($count_error != 0 || $count_unfinished != 0) {
$prefix .= "[".strtoupper(get_string('error'))."] ";
}
$subject = $prefix.get_string("scheduledbackupstatus");

//Send the message
email_to_user($admin,$admin,$subject,$message);
}


Expand All @@ -161,13 +194,15 @@ function schedule_backup_launch_backup($course,$starttime = 0) {
schedule_backup_log($starttime,$course->id," Phase 2: Executing and copying:");
$status = schedule_backup_course_execute($preferences,$starttime);
}

if ($status && $preferences) {
//Only if the backup_sche_keep is set
if ($preferences->backup_keep) {
schedule_backup_log($starttime,$course->id," Phase 3: Deleting old backup files:");
$status = schedule_backup_course_delete_old_files($preferences,$starttime);
}
}

if ($status && $preferences) {
mtrace(" End backup OK");
schedule_backup_log($starttime,$course->id,"End backup course $course->fullname - OK");
Expand Down Expand Up @@ -485,12 +520,6 @@ function schedule_backup_course_execute($preferences,$starttime = 0) {
$status = clear_backup_dir($preferences->backup_unique_code);
}

//Delete old_entries from backup tables
if ($status) {
schedule_backup_log($starttime,$preferences->backup_course," cleaning old data");
$status = backup_delete_old_data();
}

//Create the moodle.xml file
if ($status) {
schedule_backup_log($starttime,$preferences->backup_course," creating backup file");
Expand Down
11 changes: 7 additions & 4 deletions backup/log.php
@@ -1,4 +1,4 @@
<?PHP // $Id$
<?php // $Id$
// backup.php - allows admin to edit all configuration variables for scheduled backups

require_once("../config.php");
Expand Down Expand Up @@ -28,6 +28,7 @@
$strftimetime = get_string("strftimetime").":%S";
$strerror = get_string("error");
$strok = get_string("ok");
$strunfinished = get_string("unfinished");
$strcourse = get_string("course");
$strtimetaken = get_string("timetaken","quiz");
$strstatus = get_string("status");
Expand Down Expand Up @@ -66,10 +67,12 @@
echo "<td nowrap=\"nowrap\"><font size=\"2\">".userdate($course->laststarttime,$strftimedatetime)."</td>";
echo "<td nowrap=\"nowrap\"><font size=\"2\"> - </td>";
echo "<td nowrap=\"nowrap\"><font size=\"2\">".userdate($course->lastendtime,$strftimedatetime)."</td>";
if (!$course->laststatus) {
echo "<td nowrap=\"nowrap\" align=\"center\"><font size=\"2\" color=\"red\">".$strerror."</td>";
} else {
if ($course->laststatus == 1) {
echo "<td nowrap=\"nowrap\" align=\"center\"><font size=\"2\" color=\"green\">".$strok."</td>";
} else if ($course->laststatus == 2) {
echo "<td nowrap=\"nowrap\" align=\"center\"><font size=\"2\" color=\"red\">".$strunfinished."</td>";
} else {
echo "<td nowrap=\"nowrap\" align=\"center\"><font size=\"2\" color=\"red\">".$strerror."</td>";
}
echo "<td nowrap=\"nowrap\"><font size=\"2\">".userdate($course->nextstarttime,$strftimedatetime)."</td>";
echo "</tr>";
Expand Down

0 comments on commit 70bbfd3

Please sign in to comment.