Example #1
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;
}
Example #2
0
function schedule_backup_cron()
{
    global $CFG, $DB;
    $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 backup status", '...');
    $backup_config = backup_get_config();
    if (!isset($backup_config->backup_sche_active) || !$backup_config->backup_sche_active) {
        mtrace("INACTIVE");
        return true;
    } else {
        if (isset($backup_config->backup_sche_running) && $backup_config->backup_sche_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;
            $numofrec = $DB->count_records_select("backup_log", "time > ? AND backuptype = ?", array($timeafter, 'scheduledbackup'));
            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_sche_running
            backup_set_config("backup_sche_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;
    }
    //Delete old_entries from backup tables
    if ($status) {
        mtrace("    Deleting old data");
        if (!backup_delete_old_data()) {
            $errorstr = "An error occurred deleting old backup data";
            add_to_backup_log(time(), $preferences->backup_course, $errorstr, 'scheduledbackup');
            mtrace("    " . $errorstr);
        }
    }
    //Now we get a list of courses in the server
    if ($status) {
        mtrace("    Checking courses");
        //First of all, we delete everything from backup tables related to deleted courses
        mtrace("        Skipping deleted courses");
        $skipped = 0;
        if ($bckcourses = $DB->get_records('backup_courses')) {
            foreach ($bckcourses as $bckcourse) {
                //Search if it exists
                if (!($exists = $DB->get_record('course', array('id' => $bckcourse->courseid)))) {
                    //Doesn't exist, so delete from backup tables
                    $DB->delete_records('backup_courses', array('courseid' => $bckcourse->courseid));
                    $DB->delete_records('backup_log', array('courseid' => $bckcourse->courseid));
                    $skipped++;
                }
            }
        }
        mtrace("            {$skipped} courses");
        //Now process existing courses
        $courses = $DB->get_records("course");
        //For each course, we check (insert, update) the backup_course table
        //with needed data
        foreach ($courses as $course) {
            if ($status) {
                mtrace("        {$course->fullname}");
                //We check if the course exists in backup_course
                $backup_course = $DB->get_record("backup_courses", array("courseid" => $course->id));
                //If it doesn't exist, create
                if (!$backup_course) {
                    $temp_backup_course->courseid = $course->id;
                    $newid = $DB->insert_record("backup_courses", $temp_backup_course);
                    //And get it from db
                    $backup_course = $DB->get_record("backup_courses", array("id" => $newid));
                }
                //If it doesn't exist now, error
                if (!$backup_course) {
                    mtrace("            ERROR (in backup_courses detection)");
                    $status = false;
                    continue;
                }
                // Skip backup of unavailable courses that have remained unmodified in a month
                $skipped = false;
                if (!$course->visible && $now - $course->timemodified > 31 * 24 * 60 * 60) {
                    //Hidden + unmodified last month
                    mtrace("            SKIPPING - hidden+unmodified");
                    $DB->set_field("backup_courses", "laststatus", "3", array("courseid" => $backup_course->courseid));
                    $skipped = true;
                }
                //Now we backup every non skipped course with nextstarttime < now
                if (!$skipped && $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();
                        $DB->set_field("backup_courses", "laststarttime", $starttime, array("courseid" => $backup_course->courseid));
                        //Set course status to unfinished, the process will reset it
                        $DB->set_field("backup_courses", "laststatus", "2", array("courseid" => $backup_course->courseid));
                        //Launch backup
                        $course_status = schedule_backup_launch_backup($course, $starttime);
                        //Set lastendtime
                        $DB->set_field("backup_courses", "lastendtime", time(), array("courseid" => $backup_course->courseid));
                        //Set laststatus
                        if ($course_status) {
                            $DB->set_field("backup_courses", "laststatus", "1", array("courseid" => $backup_course->courseid));
                        } else {
                            $DB->set_field("backup_courses", "laststatus", "0", array("courseid" => $backup_course->courseid));
                        }
                    }
                }
                //Now, calculate next execution of the course
                $nextstarttime = schedule_backup_next_execution($backup_course, $backup_config, $now, $admin->timezone);
                //Save it to db
                $DB->set_field("backup_courses", "nextstarttime", $nextstarttime, array("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;
        $DB->delete_records_select("backup_log", "laststarttime < ?", array($loglifetime));
    }
    //Send email to admin if necessary
    if ($emailpending) {
        mtrace("    Sending email to admin");
        $message = "";
        //Get info about the status of courses
        $count_all = $DB->count_records('backup_courses');
        $count_ok = $DB->count_records('backup_courses', array('laststatus' => '1'));
        $count_error = $DB->count_records('backup_courses', array('laststatus' => '0'));
        $count_unfinished = $DB->count_records('backup_courses', array('laststatus' => '2'));
        $count_skipped = $DB->count_records('backup_courses', array('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('backupfailed') . "\n\n";
            $dest_url = "{$CFG->wwwroot}/{$CFG->admin}/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 ($count_error != 0 || $count_unfinished != 0) {
            $prefix .= "[" . strtoupper(get_string('error')) . "] ";
        }
        $subject = $prefix . get_string("scheduledbackupstatus");
        //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 = '';
        message_send($eventdata);
    }
    //Everything is finished stop backup_sche_running
    backup_set_config("backup_sche_running", "0");
    return $status;
}
Example #3
0
 function config_write($name, $value)
 {
     global $CFG;
     if ($this->plugin === 'backup') {
         require_once $CFG->dirroot . '/backup/lib.php';
         return (bool) backup_set_config($name, $value);
     } else {
         return (bool) set_config($name, $value, $this->plugin);
     }
 }
Example #4
0
 function write_setting($data)
 {
     $week = 'umtwrfs';
     $result = array(0 => 0, 1 => 0, 2 => 0, 3 => 0, 4 => 0, 5 => 0, 6 => 0);
     if (!empty($data)) {
         foreach ($data as $key => $value) {
             if ($value == '1') {
                 $result[strpos($week, $key)] = 1;
             }
         }
     }
     return backup_set_config($this->name, implode('', $result)) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />';
 }
Example #5
0
 private function resetParams($config)
 {
     foreach ($config as $name => $value) {
         backup_set_config($name, $value);
     }
 }
function schedule_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 backup status", '...');
    $backup_config = backup_get_config();
    if (!isset($backup_config->backup_sche_active) || !$backup_config->backup_sche_active) {
        mtrace("INACTIVE");
        return true;
    } else {
        if (isset($backup_config->backup_sche_running) && $backup_config->backup_sche_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;
            $numofrec = count_records_select("backup_log", "time > {$timeafter}");
            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_sche_running
            backup_set_config("backup_sche_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;
    }
    //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");
        //First of all, we delete everything from backup tables related to deleted courses
        mtrace("        Skipping deleted courses");
        $skipped = 0;
        if ($bckcourses = get_records('backup_courses')) {
            foreach ($bckcourses as $bckcourse) {
                //Search if it exists
                if (!($exists = get_record('course', 'id', "{$bckcourse->courseid}"))) {
                    //Doesn't exist, so delete from backup tables
                    delete_records('backup_courses', 'courseid', "{$bckcourse->courseid}");
                    delete_records('backup_log', 'courseid', "{$bckcourse->courseid}");
                    $skipped++;
                }
            }
        }
        mtrace("            {$skipped} courses");
        //Now process existing courses
        $courses = get_records("course");
        //For each course, we check (insert, update) the backup_course table
        //with needed data
        foreach ($courses as $course) {
            if ($status) {
                mtrace("        {$course->fullname}");
                //We check if the course exists in backup_course
                $backup_course = get_record("backup_courses", "courseid", $course->id);
                //If it doesn't exist, create
                if (!$backup_course) {
                    $temp_backup_course->courseid = $course->id;
                    $newid = insert_record("backup_courses", $temp_backup_course);
                    //And get it from db
                    $backup_course = get_record("backup_courses", "id", $newid);
                }
                //If it doesn't exist now, error
                if (!$backup_course) {
                    mtrace("            ERROR (in backup_courses detection)");
                    $status = false;
                    continue;
                }
                // Skip courses that do not yet need backup
                $skipped = !($backup_course->nextstarttime > 0 && $backup_course->nextstarttime < $now);
                // Skip backup of unavailable courses that have remained unmodified in a month
                if (!$skipped && !$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
                    $sql = 'SELECT l.id FROM ' . $CFG->prefix . 'log l WHERE ' . 'l.course=' . $course->id . ' AND l.time>' . ($now - 31 * 24 * 60 * 60) . " AND lower(l.action) not like '%view%'";
                    $logexists = record_exists_sql($sql);
                    if (!$logexists) {
                        mtrace("            SKIPPING - hidden+unmodified");
                        set_field("backup_courses", "laststatus", "3", "courseid", $backup_course->courseid);
                        $skipped = true;
                    }
                }
                //Now we backup every non-skipped course
                if (!$skipped) {
                    //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("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);
                        }
                    }
                }
                //Now, calculate next execution of the course
                $nextstarttime = schedule_backup_next_execution($backup_course, $backup_config, $now, $admin->timezone);
                //Save it to db
                set_field("backup_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("backup_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('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');
        $count_skipped = count_records('backup_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('backupfailed') . "\n\n";
            $dest_url = "{$CFG->wwwroot}/{$CFG->admin}/report/backups/index.php";
            $message .= "  " . get_string('backuptakealook', '', $dest_url) . "\n\n";
            //Set message priority
            $admin->priority = 1;
            //Reset unfinished to error
            set_field('backup_courses', 'laststatus', '0', 'laststatus', '2');
        } else {
            $message .= "  " . get_string('backupfinished') . "\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("scheduledbackupstatus");
        //Send the message
        email_to_user($admin, $admin, $subject, $message);
    }
    //Everything is finished stop backup_sche_running
    backup_set_config("backup_sche_running", "0");
    return $status;
}