-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathrestore.php
More file actions
104 lines (86 loc) · 3.26 KB
/
Copy pathrestore.php
File metadata and controls
104 lines (86 loc) · 3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
/**
* admin tool brcli
* Backup & restore command line interface
* @package admin
* @subpackage tool
* @author Paulo Júnior <pauloa.junior@ufla.br> based on https://github.com/mudrd8mz/moodle-toolbox/blob/master/mbz/mbz-restorecourses.php
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
define('CLI_SCRIPT', 1);
require(__DIR__.'/../../../config.php');
require_once($CFG->libdir.'/clilib.php');
require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
// Now get cli options.
list($options, $unrecognized) = cli_get_params(array(
'categoryid' => false,
'source' => '',
'help' => false,
), array('h' => 'help'));
if ($unrecognized) {
$unrecognized = implode("\n ", $unrecognized);
cli_error(get_string('unknowoption', 'tool_brcli', $unrecognized));
}
if ($options['help'] || !($options['categoryid']) || !($options['source'])) {
echo get_string('helpoptionres', 'tool_brcli');
die;
}
$admin = get_admin();
if (!$admin) {
cli_error(get_string('noadminaccount', 'tool_brcli'));
}
$dir = rtrim($options['source'], '/');
if (empty($dir) || !file_exists($dir) || !is_dir($dir)) {
cli_error(get_string('directoryerror', 'tool_brcli'));
}
// Check that the category exists.
if ($DB->count_records('course_categories', array('id'=>$options['categoryid'])) == 0) {
cli_error(get_string('nocategory', 'tool_brcli'));
}
$index = 1;
$sourcefiles = new FilesystemIterator($dir, FilesystemIterator::SKIP_DOTS);
$amount_of_courses = iterator_count($sourcefiles);
foreach ($sourcefiles as $sourcefile) {
if ($sourcefile->getExtension() !== 'mbz') {
continue;
}
mtrace(get_string('performingres', 'tool_brcli', $index . '/' . $amount_of_courses));
// Extract the file.
$packer = get_file_packer('application/vnd.moodle.backup');
$backupid = restore_controller::get_tempdir_name(SITEID, $admin->id);
$path = "$CFG->tempdir/backup/$backupid/";
if (!$packer->extract_to_pathname($sourcefile->getPathname(), $path)) {
mtrace(get_string('invalidbackupfile', 'tool_brcli', $sourcefile->getFilename()));
}
// Transaction.
$transaction = $DB->start_delegated_transaction();
// Create new course.
$folder = $backupid; // as found in: $CFG->dataroot . '/temp/backup/'
$categoryid = $options['categoryid']; // e.g. 1 == Miscellaneous
$userdoingrestore = $admin->id; // e.g. 2 == admin
$courseid = restore_dbops::create_new_course('', '', $categoryid);
// Restore backup into course.
$controller = new restore_controller($folder, $courseid,
backup::INTERACTIVE_NO, backup::MODE_GENERAL, $userdoingrestore,
backup::TARGET_NEW_COURSE);
if ($controller->execute_precheck()) {
$controller->execute_plan();
} else {
try {
$transaction->rollback(new Exception('Prechecked failed'));
} catch (Exception $e) {
unset($transaction);
$controller->destroy();
unset($controller);
continue;
}
}
$index = $index + 1;
// Commit and clean up.
$transaction->allow_commit();
unset($transaction);
$controller->destroy();
unset($controller);
}
mtrace(get_string('operationdone', 'tool_brcli'));
exit(0);