-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbackup.php
88 lines (73 loc) · 2.81 KB
/
backup.php
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
<?php
/**
* admin tool brcli
* Backup & restore command line interface
* @package admin
* @subpackage tool
* @author Paulo Júnior <[email protected]> based on /admin/cli/backup.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/backup_includes.php');
// Now get cli options.
list($options, $unrecognized) = cli_get_params(array(
'categoryid' => false,
'destination' => '',
'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['destination'])) {
echo get_string('helpoptionbck', 'tool_brcli');
die;
}
$admin = get_admin();
if (!$admin) {
cli_error(get_string('noadminaccount', 'tool_brcli'));
}
// Do we need to store backup somewhere else?
$dir = rtrim($options['destination'], '/');
if (empty($dir) || !file_exists($dir) || !is_dir($dir) || !is_writable($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'));
}
$courses = $DB->get_records('course', array('category'=>$options['categoryid']));
$amount_of_courses = count($courses);
$index = 1;
foreach ($courses as $cs) {
$bc = new backup_controller(backup::TYPE_1COURSE, $cs->id, backup::FORMAT_MOODLE,
backup::INTERACTIVE_YES, backup::MODE_GENERAL, $admin->id);
mtrace(get_string('performingbck', 'tool_brcli', $index . '/' . $amount_of_courses));
// Set the default filename.
$format = $bc->get_format();
$type = $bc->get_type();
$id = $bc->get_id();
$users = $bc->get_plan()->get_setting('users')->get_value();
$anonymised = $bc->get_plan()->get_setting('anonymize')->get_value();
$filename = backup_plan_dbops::get_default_backup_filename($format, $type, $id, $users, $anonymised);
$bc->get_plan()->get_setting('filename')->set_value($filename);
// Execution.
$bc->finish_ui();
$bc->execute_plan();
$results = $bc->get_results();
$file = $results['backup_destination']; // May be empty if file already moved to target location.
// Do we need to store backup somewhere else?
if ($file) {
if ($file->copy_content_to($dir.'/'.$filename)) {
$file->delete();
} else {
mtrace(get_string('directoryerror', 'tool_brcli'));
}
}
$bc->destroy();
$index = $index + 1;
}
mtrace(get_string('operationdone', 'tool_brcli'));
exit(0);