Merge branch 'MDL-56250-32-formvalidation' of https://github.com/roperto/moodle
[moodle.git] / admin / cli / backup.php
blobb8be31827c93f61d0f25e1122b4b8acb8db343f0
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * This script allows to do backup.
20 * @package core
21 * @subpackage cli
22 * @copyright 2013 Lancaster University
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 define('CLI_SCRIPT', 1);
28 require(__DIR__.'/../../config.php');
29 require_once($CFG->libdir.'/clilib.php');
30 require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
32 // Now get cli options.
33 list($options, $unrecognized) = cli_get_params(array(
34 'courseid' => false,
35 'courseshortname' => '',
36 'destination' => '',
37 'help' => false,
38 ), array('h' => 'help'));
40 if ($unrecognized) {
41 $unrecognized = implode("\n ", $unrecognized);
42 cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
45 if ($options['help'] || !($options['courseid'] || $options['courseshortname'])) {
46 $help = <<<EOL
47 Perform backup of the given course.
49 Options:
50 --courseid=INTEGER Course ID for backup.
51 --courseshortname=STRING Course shortname for backup.
52 --destination=STRING Path where to store backup file. If not set the backup
53 will be stored within the course backup file area.
54 -h, --help Print out this help.
56 Example:
57 \$sudo -u www-data /usr/bin/php admin/cli/backup.php --courseid=2 --destination=/moodle/backup/\n
58 EOL;
60 echo $help;
61 die;
64 $admin = get_admin();
65 if (!$admin) {
66 mtrace("Error: No admin account was found");
67 die;
70 // Do we need to store backup somewhere else?
71 $dir = rtrim($options['destination'], '/');
72 if (!empty($dir)) {
73 if (!file_exists($dir) || !is_dir($dir) || !is_writable($dir)) {
74 mtrace("Destination directory does not exists or not writable.");
75 die;
79 // Check that the course exists.
80 if ($options['courseid']) {
81 $course = $DB->get_record('course', array('id' => $options['courseid']), '*', MUST_EXIST);
82 } else if ($options['courseshortname']) {
83 $course = $DB->get_record('course', array('shortname' => $options['courseshortname']), '*', MUST_EXIST);
86 cli_heading('Performing backup...');
87 $bc = new backup_controller(backup::TYPE_1COURSE, $course->id, backup::FORMAT_MOODLE,
88 backup::INTERACTIVE_YES, backup::MODE_GENERAL, $admin->id);
89 // Set the default filename.
90 $format = $bc->get_format();
91 $type = $bc->get_type();
92 $id = $bc->get_id();
93 $users = $bc->get_plan()->get_setting('users')->get_value();
94 $anonymised = $bc->get_plan()->get_setting('anonymize')->get_value();
95 $filename = backup_plan_dbops::get_default_backup_filename($format, $type, $id, $users, $anonymised);
96 $bc->get_plan()->get_setting('filename')->set_value($filename);
98 // Execution.
99 $bc->finish_ui();
100 $bc->execute_plan();
101 $results = $bc->get_results();
102 $file = $results['backup_destination']; // May be empty if file already moved to target location.
104 // Do we need to store backup somewhere else?
105 if (!empty($dir)) {
106 if ($file) {
107 mtrace("Writing " . $dir.'/'.$filename);
108 if ($file->copy_content_to($dir.'/'.$filename)) {
109 $file->delete();
110 mtrace("Backup completed.");
111 } else {
112 mtrace("Destination directory does not exist or is not writable. Leaving the backup in the course backup file area.");
115 } else {
116 mtrace("Backup completed, the new file is listed in the backup area of the given course");
118 $bc->destroy();
119 exit(0);