Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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.
13
//
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/>.
16
 
17
/**
18
 * This script allows to do backup.
19
 *
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
24
 */
25
 
26
define('CLI_SCRIPT', 1);
27
 
28
require(__DIR__.'/../../config.php');
29
require_once($CFG->libdir.'/clilib.php');
30
require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
31
 
32
// Now get cli options.
33
list($options, $unrecognized) = cli_get_params(array(
34
    'courseid' => false,
35
    'courseshortname' => '',
36
    'cmid' => false,
37
    'destination' => '',
38
    'help' => false,
39
    ), array('h' => 'help'));
40
 
41
if ($unrecognized) {
42
    $unrecognized = implode("\n  ", $unrecognized);
43
    cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
44
}
45
 
46
if ($options['help'] || !($options['courseid'] || $options['courseshortname'] || $options['cmid'])) {
47
    $help = <<<EOL
48
Perform backup of the given course or course module.
49
 
50
Options:
51
--courseid=INTEGER          Course ID for backup.
52
--courseshortname=STRING    Course shortname for backup. This option is ignored if courseid is set.
53
--cmid=INTEGER              Course module ID for backup. This option is ignored if courseid or courseshortname is set.
54
--destination=STRING        Path where to store backup file. If not set the backup
55
                            will be stored within the course backup file area.
56
-h, --help                  Print out this help.
57
 
58
Example:
59
\$sudo -u www-data /usr/bin/php admin/cli/backup.php --courseid=2 --destination=/moodle/backup/\n
60
EOL;
61
 
62
    echo $help;
63
    die;
64
}
65
 
66
$admin = get_admin();
67
if (!$admin) {
68
    mtrace("Error: No admin account was found");
69
    die;
70
}
71
 
72
// Do we need to store backup somewhere else?
73
$dir = rtrim($options['destination'], '/');
74
if (!empty($dir)) {
75
    if (!file_exists($dir) || !is_dir($dir) || !is_writable($dir)) {
76
        mtrace("Destination directory does not exists or not writable.");
77
        die;
78
    }
79
}
80
 
81
// Check that the course or course module exists.
82
if ($options['courseid']) {
83
    $course = $DB->get_record('course', array('id' => $options['courseid']), '*', MUST_EXIST);
84
} else if ($options['courseshortname']) {
85
    $course = $DB->get_record('course', array('shortname' => $options['courseshortname']), '*', MUST_EXIST);
86
} else if ($options['cmid']) {
87
    $cm = $DB->get_record('course_modules', ['id' => $options['cmid']], '*', MUST_EXIST);
88
}
89
 
90
cli_heading('Performing backup...');
91
if (!empty($course)) {
92
    $bc = new backup_controller(backup::TYPE_1COURSE, $course->id, backup::FORMAT_MOODLE,
93
        backup::INTERACTIVE_YES, backup::MODE_GENERAL, $admin->id);
94
} else if (!empty($cm)) {
95
    $bc = new backup_controller(backup::TYPE_1ACTIVITY, $cm->id, backup::FORMAT_MOODLE,
96
        backup::INTERACTIVE_YES, backup::MODE_GENERAL, $admin->id);
97
} else {
98
    throw new \moodle_exception('invalidoption');
99
}
100
 
101
// Set the default filename.
102
$format = $bc->get_format();
103
$type = $bc->get_type();
104
$id = $bc->get_id();
105
$users = $bc->get_plan()->get_setting('users')->get_value();
106
$anonymised = $bc->get_plan()->get_setting('anonymize')->get_value();
107
$filename = backup_plan_dbops::get_default_backup_filename($format, $type, $id, $users, $anonymised);
108
$bc->get_plan()->get_setting('filename')->set_value($filename);
109
 
110
// Execution.
111
$bc->finish_ui();
112
$bc->execute_plan();
113
$results = $bc->get_results();
114
$file = $results['backup_destination']; // May be empty if file already moved to target location.
115
 
116
// Do we need to store backup somewhere else?
117
if (!empty($dir)) {
118
    if ($file) {
119
        mtrace("Writing " . $dir.'/'.$filename);
120
        if ($file->copy_content_to($dir.'/'.$filename)) {
121
            $file->delete();
122
            mtrace("Backup completed.");
123
        } else {
124
            mtrace("Destination directory does not exist or is not writable. Leaving the backup in the course backup file area.");
125
        }
126
    }
127
} else {
128
    if (!empty($course)) {
129
        mtrace("Backup completed, the new file is listed in the backup area of the given course");
130
    } else {
131
        mtrace("Backup completed, the new file is listed in the backup area of the given module");
132
    }
133
}
134
$bc->destroy();
135
exit(0);