Skip to content

Instantly share code, notes, and snippets.

@stronk7
Created November 14, 2014 10:58
Show Gist options
  • Save stronk7/a2d49eec2ee38f5bcebb to your computer and use it in GitHub Desktop.
Save stronk7/a2d49eec2ee38f5bcebb to your computer and use it in GitHub Desktop.
Simple scripts to quickly test backup & restore from terminal
<?php
define('CLI_SCRIPT', true);
require_once('config.php');
require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
$course_module_to_backup = 1;
$section_to_backup = 2;
$course_to_backup = 2;
$user_doing_the_backup = 2; //Admin
//$user_doing_the_backup = 3; //Teacher
$inittime = microtime(true);
$initmem = memory_get_usage(true);
for ($i = 0;$i<1;$i++) {
// activity backup
//$bc = new backup_controller(backup::TYPE_1ACTIVITY, $course_module_to_backup, backup::FORMAT_MOODLE,
// backup::INTERACTIVE_YES, backup::MODE_GENERAL, $user_doing_the_backup);
// section backup
//$bc = new backup_controller(backup::TYPE_1SECTION, $section_to_backup, backup::FORMAT_MOODLE,
// backup::INTERACTIVE_YES, backup::MODE_GENERAL, $user_doing_the_backup);
// course backup
$bc = new backup_controller(backup::TYPE_1COURSE, $course_to_backup, backup::FORMAT_MOODLE,
backup::INTERACTIVE_YES, backup::MODE_GENERAL, $user_doing_the_backup);
$users = $bc->get_plan()->get_setting('users');
$logs = $bc->get_plan()->get_setting('logs');
$users->set_value(true);
$logs->set_value(true);
$bc->finish_ui();
print_object('Before execute plan: ' . display_size(memory_get_usage(true)));
$bc->execute_plan();
print_object('After execute plan: ' . display_size(memory_get_usage(true)));
print_object($bc->get_results());
$bc->destroy();
}
print_object('Time elapsed: ' . (microtime(true) - $inittime));
print_object('Memory init: ' . display_size($initmem));
print_object('Memory peak: ' . display_size(memory_get_peak_usage(true)));
print_object('Memory end: ' . display_size(memory_get_usage(true)));
///print_object($DB->get_records('backup_ids_temp', array('backupid' => $bc->get_backupid(), 'itemname' => 'filefinal')));
<?php
define('CLI_SCRIPT', true);
require_once('config.php');
require_once($CFG->dirroot . '/lib/pagelib.php');
require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
$tempdir = '1cb36986adee863a51012bc3ced80e99';
$course_to_restore_to = 3;
$user_doing_the_restore = 2; //Admin
//$user_doing_the_restore = 3; //Teacher
// Force cleaning of the whole course
remove_course_contents($course_to_restore_to, false);
$inittime = microtime(true);
$initmem = memory_get_usage(true);
// restore contents in tempdir into existing course
$rc = new restore_controller($tempdir, $course_to_restore_to, backup::INTERACTIVE_YES, backup::MODE_GENERAL, $user_doing_the_restore, backup::TARGET_NEW_COURSE);
if ($rc->get_status() == backup::STATUS_REQUIRE_CONV) {
echo "format: {$rc->get_format()} detected. Conversion needed";
$rc->convert();
}
$rc->finish_ui();
print_object('Memory after ui: ' . display_size(memory_get_usage(true)));
// Advance the startdate by one year
$rc->get_plan()->get_setting('course_startdate')->set_value(
$rc->get_plan()->get_setting('course_startdate')->get_value() + (365 * 24 * 60 * 60));
$rc->save_controller();
$rc=restore_controller::load_controller($rc->get_restoreid());
print_object('Memory after load controller 1: ' . display_size(memory_get_usage(true)));
if (!$rc->execute_precheck(false)) {
$results = $rc->get_precheck_results();
print_object($results);
}
print_object('Memory after prechecks: ' . display_size(memory_get_usage(true)));
$rc->save_controller();
$rc=restore_controller::load_controller($rc->get_restoreid());
print_object('Memory after load controller 2: ' . display_size(memory_get_usage(true)));
// Printout all the settings
/**
foreach ($rc->get_plan()->get_tasks() as $index => $task) {
echo "Task " . get_class($task) . ": $index => {$task->get_name()} => ";
foreach ($task->get_settings() as $index => $setting) {
echo "{$setting->get_name()} [{$setting->get_value()}], ";
}
echo "<br/>";
foreach ($task->get_steps() as $index => $step) {
echo "&nbsp;&nbsp;&nbsp;&nbsp;Step: $index => {$step->get_name()}<br/>";
}
}
*/
if (empty($results['errors'])) {
$rc->execute_plan();
} else {
print_object('Restore stopped due to errors in prechecks');
}
print_object($rc->get_results());
$rc->destroy();
print_object('Time elapsed: ' . (microtime(true) - $inittime));
print_object('Memory init: ' . display_size($initmem));
print_object('Memory peak: ' . display_size(memory_get_peak_usage(true)));
print_object('Memory end: ' . display_size(memory_get_usage(true)));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment