Ejemplo n.º 1
0
function generate_incrementals($courseid)
{
    global $CFG;
    //check XDELTA is installed.
    if (!check_xdelta_installed()) {
        mtrace(get_string('xdeltanotinstalled', 'local'));
        return false;
    }
    $incremental_config = backup_get_config();
    //set directory paths
    if (!empty($incremental_config->backup_inc_destination)) {
        $backuppath = $incremental_config->backup_inc_destination . '/' . $courseid . '/';
    } else {
        $backuppath = $CFG->dataroot . '/' . $courseid . '/backupdata/';
    }
    $backuplist = get_list_backups($courseid);
    $latestbackup = $backuplist[0];
    foreach ($backuplist as $id => $backup) {
        if ($id != 0) {
            if (!create_incremental($courseid, $backup, $latestbackup)) {
                //don't generate any more incrementals as the $backup is the same as $latestbackup and we don't need to keep this backup at all.
                unlink($backuppath . $latestbackup);
                //delete new backup as it isn't needed.
                mtrace(get_string('coursenotchanged', 'local'));
                break;
                //stop running more incrementals.
            }
        }
    }
    //now cleanup incremental files that are no longer needed.
    $incrementallist = get_list_backups($courseid, true);
    foreach ($incrementallist as $incremental) {
        $findstr = strpos($incremental, md5($latestbackup));
        if (!$findstr) {
            unlink($backuppath . 'incrementals/' . $incremental);
        }
    }
    return true;
}
Ejemplo n.º 2
0
function backup_execute(&$preferences, &$errorstr)
{
    global $CFG;
    $status = true;
    //Check for temp and backup and backup_unique_code directory
    //Create them as needed
    if (!defined('BACKUP_SILENTLY')) {
        echo "<li>" . get_string("creatingtemporarystructures") . '</li>';
    }
    $status = check_and_create_backup_dir($preferences->backup_unique_code);
    //Empty dir
    if ($status) {
        $status = clear_backup_dir($preferences->backup_unique_code);
    }
    //Delete old_entries from backup tables
    if (!defined('BACKUP_SILENTLY')) {
        echo "<li>" . get_string("deletingolddata") . '</li>';
    }
    $status = backup_delete_old_data();
    if (!$status) {
        $errorstr = "An error occurred deleting old backup data";
        backup_add_to_log(time(), $preferences->backup_course, $errorstr, 'manual');
        if (!defined('BACKUP_SILENTLY')) {
            notify($errorstr);
        }
    }
    //Create the moodle.xml file
    if ($status) {
        if (!defined('BACKUP_SILENTLY')) {
            echo "<li>" . get_string("creatingxmlfile");
            //Begin a new list to xml contents
            echo "<ul>";
            echo "<li>" . get_string("writingheader") . '</li>';
        }
        //Obtain the xml file (create and open) and print prolog information
        $backup_file = backup_open_xml($preferences->backup_unique_code);
        if (!defined('BACKUP_SILENTLY')) {
            echo "<li>" . get_string("writinggeneralinfo") . '</li>';
        }
        //Prints general info about backup to file
        if ($backup_file) {
            if (!($status = backup_general_info($backup_file, $preferences))) {
                $errorstr = "An error occurred while backing up general info";
                backup_add_to_log(time(), $preferences->backup_course, $errorstr, 'manual');
                if (!defined('BACKUP_SILENTLY')) {
                    notify($errorstr);
                } else {
                    return false;
                }
            }
        }
        if (!defined('BACKUP_SILENTLY')) {
            echo "<li>" . get_string("writingcoursedata");
            //Start new ul (for course)
            echo "<ul>";
            echo "<li>" . get_string("courseinfo") . '</li>';
        }
        //Prints course start (tag and general info)
        if ($status) {
            if (!($status = backup_course_start($backup_file, $preferences))) {
                $errorstr = "An error occurred while backing up course start";
                backup_add_to_log(time(), $preferences->backup_course, $errorstr, 'manual');
                if (!defined('BACKUP_SILENTLY')) {
                    notify($errorstr);
                } else {
                    return false;
                }
            }
        }
        //Metacourse information
        if ($status && $preferences->backup_metacourse) {
            if (!defined('BACKUP_SILENTLY')) {
                echo "<li>" . get_string("metacourse") . '</li>';
            }
            if (!($status = backup_course_metacourse($backup_file, $preferences))) {
                $errorstr = "An error occurred while backing up metacourse info";
                backup_add_to_log(time(), $preferences->backup_course, $errorstr, 'manual');
                if (!defined('BACKUP_SILENTLY')) {
                    notify($errorstr);
                } else {
                    return false;
                }
            }
        }
        if (!defined('BACKUP_SILENTLY')) {
            echo "<li>" . get_string("blocks") . '</li>';
        }
        //Blocks information
        if ($status) {
            if (!($status = backup_course_blocks($backup_file, $preferences))) {
                $errorstr = "An error occurred while backing up course blocks";
                backup_add_to_log(time(), $preferences->backup_course, $errorstr, 'manual');
                if (!defined('BACKUP_SILENTLY')) {
                    notify($errorstr);
                } else {
                    return false;
                }
            }
        }
        if (!defined('BACKUP_SILENTLY')) {
            echo "<li>" . get_string("sections") . '</li>';
        }
        //Section info
        if ($status) {
            if (!($status = backup_course_sections($backup_file, $preferences))) {
                $errorstr = "An error occurred while backing up course sections";
                backup_add_to_log(time(), $preferences->backup_course, $errorstr, 'manual');
                if (!defined('BACKUP_SILENTLY')) {
                    notify($errorstr);
                } else {
                    return false;
                }
            }
        }
        //End course contents (close ul)
        if (!defined('BACKUP_SILENTLY')) {
            echo "</ul></li>";
        }
        //User info
        if ($status) {
            if (!defined('BACKUP_SILENTLY')) {
                echo "<li>" . get_string("writinguserinfo") . '</li>';
            }
            if (!($status = backup_user_info($backup_file, $preferences))) {
                $errorstr = "An error occurred while backing up user info";
                backup_add_to_log(time(), $preferences->backup_course, $errorstr, 'manual');
                if (!defined('BACKUP_SILENTLY')) {
                    notify($errorstr);
                } else {
                    return false;
                }
            }
        }
        //If we have selected to backup messages and we are
        //doing a SITE backup, let's do it
        if ($status && $preferences->backup_messages && $preferences->backup_course == SITEID) {
            if (!defined('BACKUP_SILENTLY')) {
                echo "<li>" . get_string("writingmessagesinfo") . '</li>';
            }
            if (!($status = backup_messages($backup_file, $preferences))) {
                $errorstr = "An error occurred while backing up messages";
                backup_add_to_log(time(), $preferences->backup_course, $errorstr, 'manual');
                if (!defined('BACKUP_SILENTLY')) {
                    notify($errorstr);
                } else {
                    return false;
                }
            }
        }
        //If we have selected to backup blogs and we are
        //doing a SITE backup, let's do it
        if ($status && isset($preferences->backup_blogs) && $preferences->backup_blogs && $preferences->backup_course == SITEID) {
            if (!defined('BACKUP_SILENTLY')) {
                echo "<li>" . get_string("writingblogsinfo") . '</li>';
            }
            if (!($status = backup_blogs($backup_file, $preferences))) {
                $errorstr = "An error occurred while backing up blogs";
                backup_add_to_log(time(), $preferences->backup_course, $errorstr, 'manual');
                if (!defined('BACKUP_SILENTLY')) {
                    notify($errorstr);
                } else {
                    return false;
                }
            }
        }
        //If we have selected to backup quizzes or other modules that use questions
        //we've already added ids of categories and questions to backup to backup_ids table
        if ($status) {
            if (!defined('BACKUP_SILENTLY')) {
                echo "<li>" . get_string("writingcategoriesandquestions") . '</li>';
            }
            require_once $CFG->dirroot . '/question/backuplib.php';
            if (!($status = backup_question_categories($backup_file, $preferences))) {
                $errorstr = "An error occurred while backing up quiz categories";
                backup_add_to_log(time(), $preferences->backup_course, $errorstr, 'manual');
                if (!defined('BACKUP_SILENTLY')) {
                    notify($errorstr);
                } else {
                    return false;
                }
            }
        }
        //Print logs if selected
        if ($status) {
            if ($preferences->backup_logs) {
                if (!defined('BACKUP_SILENTLY')) {
                    echo "<li>" . get_string("writingloginfo") . '</li>';
                }
                if (!($status = backup_log_info($backup_file, $preferences))) {
                    $errorstr = "An error occurred while backing up log info";
                    backup_add_to_log(time(), $preferences->backup_course, $errorstr, 'manual');
                    if (!defined('BACKUP_SILENTLY')) {
                        notify($errorstr);
                    } else {
                        return false;
                    }
                }
            }
        }
        //Print scales info
        if ($status) {
            if (!defined('BACKUP_SILENTLY')) {
                echo "<li>" . get_string("writingscalesinfo") . '</li>';
            }
            if (!($status = backup_scales_info($backup_file, $preferences))) {
                $errorstr = "An error occurred while backing up scales";
                backup_add_to_log(time(), $preferences->backup_course, $errorstr, 'manual');
                if (!defined('BACKUP_SILENTLY')) {
                    notify($errorstr);
                } else {
                    return false;
                }
            }
        }
        //Print groups info
        if ($status) {
            if (!defined('BACKUP_SILENTLY')) {
                echo "<li>" . get_string("writinggroupsinfo") . '</li>';
            }
            if (!($status = backup_groups_info($backup_file, $preferences))) {
                $errostr = "An error occurred while backing up groups";
                backup_add_to_log(time(), $preferences->backup_course, $errorstr, 'manual');
                if (!defined('BACKUP_SILENTLY')) {
                    notify($errorstr);
                } else {
                    return false;
                }
            }
        }
        //Print groupings info
        if ($status) {
            if (!defined('BACKUP_SILENTLY')) {
                echo "<li>" . get_string("writinggroupingsinfo") . '</li>';
            }
            if (!($status = backup_groupings_info($backup_file, $preferences))) {
                $errorstr = "An error occurred while backing up groupings";
                backup_add_to_log(time(), $preferences->backup_course, $errorstr, 'manual');
                if (!defined('BACKUP_SILENTLY')) {
                    notify($errorstr);
                } else {
                    return false;
                }
            }
        }
        //Print groupings_groups info
        if ($status) {
            if (!defined('BACKUP_SILENTLY')) {
                echo "<li>" . get_string("writinggroupingsgroupsinfo") . '</li>';
            }
            if (!($status = backup_groupings_groups_info($backup_file, $preferences))) {
                $errorstr = "An error occurred while backing up groupings groups";
                backup_add_to_log(time(), $preferences->backup_course, $errorstr, 'manual');
                if (!defined('BACKUP_SILENTLY')) {
                    notify($errorstr);
                } else {
                    return false;
                }
            }
        }
        //Print events info
        if ($status) {
            if (!defined('BACKUP_SILENTLY')) {
                echo "<li>" . get_string("writingeventsinfo") . '</li>';
            }
            if (!($status = backup_events_info($backup_file, $preferences))) {
                $errorstr = "An error occurred while backing up events";
                backup_add_to_log(time(), $preferences->backup_course, $errorstr, 'manual');
                if (!defined('BACKUP_SILENTLY')) {
                    notify($errorstr);
                } else {
                    return false;
                }
            }
        }
        //Print gradebook info
        if ($status) {
            if (!defined('BACKUP_SILENTLY')) {
                echo "<li>" . get_string("writinggradebookinfo") . '</li>';
            }
            if (!($status = backup_gradebook_info($backup_file, $preferences))) {
                $errorstr = "An error occurred while backing up gradebook";
                backup_add_to_log(time(), $preferences->backup_course, $errorstr, 'manual');
                if (!defined('BACKUP_SILENTLY')) {
                    notify($errorstr);
                } else {
                    return false;
                }
            }
        }
        //Module info, this unique function makes all the work!!
        //db export and module fileis copy
        if ($status) {
            $mods_to_backup = false;
            //Check if we have any mod to backup
            if (!empty($preferences->mods)) {
                foreach ($preferences->mods as $module) {
                    if ($module->backup) {
                        $mods_to_backup = true;
                    }
                }
            }
            //If we have to backup some module
            if ($mods_to_backup) {
                if (!defined('BACKUP_SILENTLY')) {
                    echo "<li>" . get_string("writingmoduleinfo");
                }
                //Start modules tag
                if (!($status = backup_modules_start($backup_file, $preferences))) {
                    $errorstr = "An error occurred while backing up module info";
                    backup_add_to_log(time(), $preferences->backup_course, $errorstr, 'manual');
                    if (!defined('BACKUP_SILENTLY')) {
                        notify($errorstr);
                    } else {
                        return false;
                    }
                }
                //Open ul for module list
                if (!defined('BACKUP_SILENTLY')) {
                    echo "<ul>";
                }
                //Iterate over modules and call backup
                foreach ($preferences->mods as $module) {
                    if ($module->backup and $status) {
                        if (!defined('BACKUP_SILENTLY')) {
                            echo "<li>" . get_string("modulenameplural", $module->name) . '</li>';
                        }
                        if (!($status = backup_module($backup_file, $preferences, $module->name))) {
                            $errorstr = "An error occurred while backing up '{$module->name}'";
                            backup_add_to_log(time(), $preferences->backup_course, $errorstr, 'manual');
                            if (!defined('BACKUP_SILENTLY')) {
                                notify($errorstr);
                            } else {
                                return false;
                            }
                        }
                    }
                }
                //Close ul for module list
                if (!defined('BACKUP_SILENTLY')) {
                    echo "</ul></li>";
                }
                //Close modules tag
                if (!($status = backup_modules_end($backup_file, $preferences))) {
                    $errorstr = "An error occurred while finishing the module backups";
                    backup_add_to_log(time(), $preferences->backup_course, $errorstr, 'manual');
                    if (!defined('BACKUP_SILENTLY')) {
                        notify($errorstr);
                    } else {
                        return false;
                    }
                }
            }
        }
        //Backup course format data, if any.
        if (!defined('BACKUP_SILENTLY')) {
            echo '<li>' . get_string("courseformatdata") . '</li>';
        }
        if ($status) {
            if (!($status = backup_format_data($backup_file, $preferences))) {
                $errorstr = "An error occurred while backing up the course format data";
                backup_add_to_log(time(), $preferences->backup_course, $errorstr, 'manual');
                if (!defined('BACKUP_SILENTLY')) {
                    notify($errorstr);
                } else {
                    return false;
                }
            }
        }
        //Prints course end
        if ($status) {
            if (!($status = backup_course_end($backup_file, $preferences))) {
                $errorstr = "An error occurred while closing the course backup";
                backup_add_to_log(time(), $preferences->backup_course, $errorstr, 'manual');
                if (!defined('BACKUP_SILENTLY')) {
                    notify($errorstr);
                } else {
                    return false;
                }
            }
        }
        //Close the xml file and xml data
        if ($backup_file) {
            backup_close_xml($backup_file);
        }
        //End xml contents (close ul)
        if (!defined('BACKUP_SILENTLY')) {
            echo "</ul></li>";
        }
    }
    //Now, if selected, copy user files
    if ($status) {
        if ($preferences->backup_user_files) {
            if (!defined('BACKUP_SILENTLY')) {
                echo "<li>" . get_string("copyinguserfiles") . '</li>';
            }
            if (!($status = backup_copy_user_files($preferences))) {
                $errorstr = "An error occurred while copying user files";
                backup_add_to_log(time(), $preferences->backup_course, $errorstr, 'manual');
                if (!defined('BACKUP_SILENTLY')) {
                    notify($errorstr);
                } else {
                    return false;
                }
            }
        }
    }
    //Now, if selected, copy course files
    if ($status) {
        if ($preferences->backup_course_files) {
            if (!defined('BACKUP_SILENTLY')) {
                echo "<li>" . get_string("copyingcoursefiles") . '</li>';
            }
            if (!($status = backup_copy_course_files($preferences))) {
                $errorstr = "An error occurred while copying course files";
                backup_add_to_log(time(), $preferences->backup_course, $errorstr, 'manual');
                if (!defined('BACKUP_SILENTLY')) {
                    notify($errorstr);
                } else {
                    return false;
                }
            }
        }
    }
    //Now, if selected, copy site files
    if ($status) {
        if ($preferences->backup_site_files) {
            if (!defined('BACKUP_SILENTLY')) {
                echo "<li>" . get_string("copyingsitefiles") . '</li>';
            }
            if (!($status = backup_copy_site_files($preferences))) {
                $errorstr = "An error occurred while copying site files";
                backup_add_to_log(time(), $preferences->backup_course, $errorstr, 'manual');
                if (!defined('BACKUP_SILENTLY')) {
                    notify($errorstr);
                } else {
                    return false;
                }
            }
        }
    }
    //Now, zip all the backup directory contents
    if ($status) {
        if (!defined('BACKUP_SILENTLY')) {
            echo "<li>" . get_string("zippingbackup") . '</li>';
        }
        if (!($status = backup_zip($preferences))) {
            $errorstr = "An error occurred while zipping the backup";
            backup_add_to_log(time(), $preferences->backup_course, $errorstr, 'manual');
            if (!defined('BACKUP_SILENTLY')) {
                notify($errorstr);
            } else {
                return false;
            }
        }
    }
    //Now, copy the zip file to course directory
    if ($status) {
        if (!defined('BACKUP_SILENTLY')) {
            echo "<li>" . get_string("copyingzipfile") . '</li>';
        }
        if (!($status = copy_zip_to_course_dir($preferences))) {
            $errorstr = "An error occurred while copying the zip file to the course directory";
            backup_add_to_log(time(), $preferences->backup_course, $errorstr, 'manual');
            if (!defined('BACKUP_SILENTLY')) {
                notify($errorstr);
            } else {
                return false;
            }
        }
    }
    //Now, clean temporary data (db and filesystem)
    if ($status) {
        if (!defined('BACKUP_SILENTLY')) {
            echo "<li>" . get_string("cleaningtempdata") . '</li>';
        }
        if (!($status = clean_temp_data($preferences))) {
            $errorstr = "An error occurred while cleaning up temporary data";
            backup_add_to_log(time(), $preferences->backup_course, $errorstr, 'manual');
            if (!defined('BACKUP_SILENTLY')) {
                notify($errorstr);
            } else {
                return false;
            }
        }
    }
    //now call incremental backup if required.
    if (isset($preferences->backup_generate_incrementals) && $preferences->backup_generate_incrementals) {
        $backup_config = backup_get_config();
        require_once "incremental_backuplib.php";
        if (!check_xdelta_installed()) {
            $errorstr = "ERROR: XDELTA not installed - incrementals cannot run";
            backup_add_to_log(time(), $preferences->backup_course, $errorstr, 'manual');
            notify($errorstr);
        } elseif (isset($backup_config->backup_incrementals) && $backup_config->backup_incrementals) {
            require_once "{$CFG->libdir}/xdelta.class.php";
            echo '<li>' . get_string("generateincrementals", 'local') . '</li>';
            generate_incrementals($preferences->backup_course);
        }
    }
    return $status;
}
Ejemplo n.º 3
0
    }
}
$navlinks = array();
$navlinks[] = array('name' => get_string('updatecourse', 'local'), 'link' => '', 'type' => 'activity');
$navigation = build_navigation($navlinks);
print_header_simple(get_string('updatecourse', 'local'), get_string('updatecourse', 'local'), $navigation, "", "", true, '', '');
if (empty($currenthash)) {
    print_box(get_string('coursenotlinked', 'local'), 'generalbox', 'intro');
    incremental_manual_download_form($id);
    print_footer($course);
    exit;
}
$currenthash = str_ireplace('.zip', '', $currenthash->filename);
//strip out .zip from filename.
//check XDELTA is installed.
if (!check_xdelta_installed()) {
    error(get_string('xdeltanotinstalled', 'local'));
}
//get Xdelta path.
$incremental_config = backup_get_config();
$xdeltacmd = $incremental_config->backup_inc_pathtoxdelta;
if (strpos($xdeltacmd, '..') !== false) {
    //this is a relative path - convert it before using it!
    $xdeltacmd = realpath($CFG->dataroot . $xdeltacmd);
}
$restorefile = download_incremental($currenthash, $course->id);
if ($restorefile == 'autofailed') {
    $externalurl = get_field('config_plugins', 'value', 'name', 'backup_inc_server') . '?hash=' . $currenthash;
    print_box(get_string('autofailed', 'local') . '<br><a href="' . $externalurl . '&action=download" target="_blank">click here to download manually</a><br>', 'generalbox', 'intro');
    echo "TODO - must add upload form for incremental here!";
    //incremental_manual_download_form($id);