restoretest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. $_SERVER = array();
  3. $_SERVER['SERVER_NAME'] = 'gershwinklata1.avatarclassroom.com';
  4. define('CLI_SCRIPT', 1);
  5. require_once('config.php');
  6. require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
  7. require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
  8. $backup_types = array(
  9. 'course' => backup::TYPE_1COURSE,
  10. 'activity' => backup::TYPE_1ACTIVITY
  11. );
  12. if (count($argv) < 2) {
  13. print "Usage: ".$argv[0]." <user> <type> <course> [<backup_folder>]\n";
  14. exit;
  15. }
  16. $user_doing_the_backup = intval($argv[1]); //2; // Set this to the id of your admin accouun
  17. $backup_type_arg = $argv[2];
  18. if (!isset($backup_types[$backup_type_arg])) {
  19. print "Backup type $backup_type_arg not supported.\n";
  20. exit;
  21. }
  22. //$course_module_to_backup = 2; // Set this to one existing choice cmid in your dev site
  23. $courseid = intval($argv[3]); //27; //3; // Set this to one existing choice cmid in your dev site
  24. if (!$user_doing_the_backup) {
  25. print "Parameters wrong\n";
  26. exit;
  27. }
  28. if (!$courseid && ($backup_type == backup::TYPE_1ACTIVITY) ) {
  29. print "For a backup of type $backup_type_arg, you need to specify a course ID to restore to.\n";
  30. exit;
  31. }
  32. $backupdir = '../resources/moodledata/temp/backup';
  33. if (isset($argv[4])) {
  34. $folder = $argv[4];
  35. } else {
  36. $folder = null;
  37. $highestmtime = 0;
  38. foreach (glob($backupdir.'/*') as $f) {
  39. if (!is_dir($f)) {
  40. continue;
  41. }
  42. if ( !$folder || (filemtime($f) > $highestmtime) ) {
  43. $folder = $f;
  44. $highestmtime = filemtime($f);
  45. }
  46. }
  47. $folderbits = explode('/',$folder);
  48. $folder = array_pop($folderbits);
  49. }
  50. echo "About to start restore from backup folder $folder\n";
  51. if (!$courseid) {
  52. // Get or create category.
  53. $categoryname = 'Restore test';
  54. $categoryid = $DB->get_field( 'course_categories', 'id', array('name'=>$categoryname) );
  55. if (!$categoryid) {
  56. $categoryid = $DB->insert_record( 'course_categories', (object)array(
  57. 'name' => $categoryname,
  58. 'parent' => 0,
  59. 'visible' => 0
  60. ));
  61. $DB->set_field( 'course_categories', 'path', '/' . $categoryid, array('id'=>$categoryid) );
  62. }
  63. $shortname = 'Restore ' . date( 'His' );
  64. $fullname = 'Restore Test ' . date( 'Y-m-d H:i:s' );
  65. // Create new course.
  66. $courseid = restore_dbops::create_new_course( $fullname, $shortname, $categoryid );
  67. }
  68. // Transaction
  69. $transaction = $DB->start_delegated_transaction();
  70. // Create new course
  71. //$courseid = restore_dbops::create_new_course($fullname, $shortname, $categoryid);
  72. // Restore backup into course
  73. $controller = new restore_controller($folder, $courseid,
  74. backup::INTERACTIVE_NO, backup::MODE_SAMESITE, $user_doing_the_backup,
  75. backup::TARGET_NEW_COURSE);
  76. $controller->execute_precheck();
  77. $controller->execute_plan();
  78. // Commit
  79. $transaction->allow_commit();