Exemplo n.º 1
0
 function test_incremental_backup()
 {
     // Do the test here/
     $course = get_record('course', 'id', '1');
     $course_status = schedule_backup_launch_inc_backup($course, time());
     $this->assertTrue($course_status);
 }
Exemplo n.º 2
0
function schedule_incr_backup_cron()
{
    global $CFG;
    $status = true;
    $emailpending = false;
    //Check for required functions...
    if (!function_exists('utf8_encode')) {
        mtrace("        ERROR: You need to add XML support to your PHP installation!");
        return true;
    }
    //Get now
    $now = time();
    //First of all, we have to see if the scheduled is active and detect
    //that there isn't another cron running
    mtrace("    Checking incremental status", '...');
    $incremental_config = backup_get_config();
    $xdeltapath = $incremental_config->backup_inc_pathtoxdelta;
    //check for relative path first.
    if (!empty($xdeltapath) && strpos($xdeltapath, '..') !== false) {
        //this is a relative path - convert it before using it!
        $xdeltapath = realpath($CFG->dataroot . $xdeltapath);
    }
    if (empty($xdeltapath) or !file_exists($xdeltapath)) {
        mtrace("  XDELTA not installed - incrementals cannot run");
        return false;
    }
    if (!isset($incremental_config->backup_sche_incrementals) || !$incremental_config->backup_sche_incrementals) {
        mtrace("INACTIVE");
        return true;
    } else {
        if (isset($incremental_config->backup_inc_running) && $incremental_config->backup_inc_running) {
            mtrace("RUNNING");
            //Now check if it's a really running task or something very old looking
            //for info in backup_logs to unlock status as necessary
            $timetosee = 1800;
            //Half an hour looking for activity
            $timeafter = time() - $timetosee;
            //TODO DAN - this needs to check the inc log to see if it is still going.
            $numofrec = count_records_select("backup_log", "time > {$timeafter} AND type='incrementalbackup'");
            if (!$numofrec) {
                $timetoseemin = $timetosee / 60;
                mtrace("    No activity in last " . $timetoseemin . " minutes. Unlocking status");
            } else {
                mtrace("    Scheduled backup seems to be running. Execution delayed");
                return true;
            }
        } else {
            mtrace("OK");
            //Mark backup_inc_running
            backup_set_config("backup_inc_running", "1");
        }
    }
    //Now we get the main admin user (we'll use his timezone, mail...)
    mtrace("    Getting admin info");
    $admin = get_admin();
    if (!$admin) {
        $status = false;
    }
    //Now we get a list of courses in the server
    if ($status) {
        mtrace("    Checking courses");
        //now pull courses out of incremental_courses
        $bkcourses = get_records('incremental_courses');
        //For each course, we check (insert, update) the backup_course table
        //with needed data
        if (empty($bkcourses)) {
            mtrace("    No courses configured for incremental backups");
        } else {
            foreach ($bkcourses as $backup_course) {
                //TODO - this should be tidied up to use a single sql query instead of 1 per course configured!
                $course = get_record('course', 'id', $backup_course->courseid);
                if ($status) {
                    mtrace("        {$course->fullname}");
                    //Now we backup every non skipped course with nextstarttime < now
                    if ($backup_course->nextstarttime > 0 && $backup_course->nextstarttime < $now) {
                        //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 ($backup_course->laststatus != 2) {
                            //Set laststarttime
                            $starttime = time();
                            set_field("incremental_courses", "laststarttime", $starttime, "courseid", $backup_course->courseid);
                            //Set course status to unfinished, the process will reset it
                            set_field("incremental_courses", "laststatus", "2", "courseid", $backup_course->courseid);
                            //Launch backup
                            $course_status = schedule_backup_launch_inc_backup($course, $starttime);
                            //Set lastendtime
                            set_field("incremental_courses", "lastendtime", time(), "courseid", $backup_course->courseid);
                            //Set laststatus
                            if ($course_status) {
                                set_field("incremental_courses", "laststatus", "1", "courseid", $backup_course->courseid);
                            } else {
                                set_field("incremental_courses", "laststatus", "0", "courseid", $backup_course->courseid);
                            }
                        }
                    }
                    //Now, calculate next execution of the course
                    $nextstarttime = schedule_inc_backup_next_execution($backup_course, $incremental_config, $now, $admin->timezone);
                    //Save it to db
                    set_field("incremental_courses", "nextstarttime", $nextstarttime, "courseid", $backup_course->courseid);
                    //Print it to screen as necessary
                    $showtime = "undefined";
                    if ($nextstarttime > 0) {
                        $showtime = userdate($nextstarttime, "", $admin->timezone);
                    }
                    mtrace("            Next execution: {$showtime}");
                }
            }
        }
    }
    //Delete old logs
    if (!empty($CFG->loglifetime)) {
        mtrace("    Deleting old logs");
        $loglifetime = $now - $CFG->loglifetime * 86400;
        delete_records_select("incremental_log", "laststarttime < '{$loglifetime}'");
    }
    //Send email to admin if necessary
    if ($emailpending) {
        mtrace("    Sending email to admin");
        $message = "";
        //Get info about the status of courses
        $count_all = count_records('incremental_courses');
        $count_ok = count_records('incremental_courses', 'laststatus', '1');
        $count_error = count_records('incremental_courses', 'laststatus', '0');
        $count_unfinished = count_records('incremental_courses', 'laststatus', '2');
        $count_skipped = count_records('incremental_courses', 'laststatus', '3');
        //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('skipped') . ": " . $count_skipped . "\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('incrementalfailed') . "\n\n";
            $dest_url = "{$CFG->wwwroot}/{$CFG->admin}/report/incrementals/index.php";
            $message .= "  " . get_string('incrementaltakealook', '', $dest_url) . "\n\n";
            //Set message priority
            $admin->priority = 1;
            //Reset unfinished to error
            set_field('incremental_courses', 'laststatus', '0', 'laststatus', '2');
        } else {
            $message .= "  " . get_string('incrementalfinished') . "\n";
        }
        //Build the message subject
        $site = get_site();
        $prefix = $site->shortname . ": ";
        if ($count_error != 0 || $count_unfinished != 0) {
            $prefix .= "[" . strtoupper(get_string('error')) . "] ";
        }
        $subject = $prefix . get_string("incrementalbackupstatus");
        //Send the message
        email_to_user($admin, $admin, $subject, $message);
    }
    //Everything is finished stop backup_sche_running
    backup_set_config("backup_inc_running", "0");
    return $status;
}
Exemplo n.º 3
0
        while (isset($currenthash->hash) && isset($currenthash->filename) && $currenthash->hash == $currenthash->filename) {
            //make sure the record is valid - if both hash and filename are exact - this is related to a client update - not the server backup.
            $currenthash = array_pop($incrementals);
            //get latest course hash.
        }
    }
}
if (empty($currenthash)) {
    //need to run backup for this course.
    // now do other stuff.!!!
    $navlinks = array();
    $navlinks[] = array('name' => get_string('exportcourse', 'local'), 'link' => '', 'type' => 'activity');
    $navigation = build_navigation($navlinks);
    print_header_simple(get_string('exportcourse', 'local'), get_string('exportcourse', 'local'), $navigation, "", "", true, '', '');
    print_box(get_string('errornobackup', 'local'), 'generalbox', 'intro');
    $course_status = schedule_backup_launch_inc_backup($course, time());
    if ($course_status) {
        redirect("export_incremental.php?id=" . $id);
    } else {
        error("please run a backup manually for this course");
    }
    exit;
}
$incremental_config = backup_get_config();
//set directory paths
if (!empty($incremental_config->backup_inc_destination)) {
    $backuppath = $incremental_config->backup_inc_destination . '/' . $crnt->courseid . '/';
} else {
    $backuppath = $CFG->dataroot . '/' . $course->id . '/backupdata/';
}
if (file_exists($backuppath . $currenthash->filename)) {