2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * This script allows to do backup.
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(
35 'courseshortname' => '',
38 ), array('h' => 'help'));
41 $unrecognized = implode("\n ", $unrecognized);
42 cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
45 if ($options['help'] ||
!($options['courseid'] ||
$options['courseshortname'])) {
47 Perform backup of the given course.
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.
57 \$sudo -u www-data /usr/bin/php admin/cli/backup.php --courseid=2 --destination=/moodle/backup/\n
66 mtrace("Error: No admin account was found");
70 // Do we need to store backup somewhere else?
71 $dir = rtrim($options['destination'], '/');
73 if (!file_exists($dir) ||
!is_dir($dir) ||
!is_writable($dir)) {
74 mtrace("Destination directory does not exists or not writable.");
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();
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);
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?
107 mtrace("Writing " . $dir.'/'.$filename);
108 if ($file->copy_content_to($dir.'/'.$filename)) {
110 mtrace("Backup completed.");
112 mtrace("Destination directory does not exist or is not writable. Leaving the backup in the course backup file area.");
116 mtrace("Backup completed, the new file is listed in the backup area of the given course");