MDL-54734 user: Add tests to demonstrate multi-user issues
[moodle.git] / admin / cli / automated_backups.php
blob0b03282fbcf65bee574d708fbf91126391e1323d
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 /**
19 * Automated backups CLI cron
21 * This script executes
23 * @package core
24 * @subpackage cli
25 * @copyright 2010 Sam Hemelryk
26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29 define('CLI_SCRIPT', true);
31 require(__DIR__.'/../../config.php');
32 require_once($CFG->libdir.'/clilib.php'); // cli only functions
33 require_once($CFG->libdir.'/cronlib.php');
35 // now get cli options
36 list($options, $unrecognized) = cli_get_params(array('help'=>false),
37 array('h'=>'help'));
39 if ($unrecognized) {
40 $unrecognized = implode("\n ", $unrecognized);
41 cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
44 if ($options['help']) {
45 $help =
46 "Execute automated backups.
48 This script executes automated backups completely and is designed to be
49 called via cron.
51 Options:
52 -h, --help Print out this help
54 Example:
55 \$sudo -u www-data /usr/bin/php admin/cli/automated_backups.php
58 echo $help;
59 die;
61 if (CLI_MAINTENANCE) {
62 echo "CLI maintenance mode active, backup execution suspended.\n";
63 exit(1);
66 if (moodle_needs_upgrading()) {
67 echo "Moodle upgrade pending, backup execution suspended.\n";
68 exit(1);
71 require_once($CFG->libdir.'/adminlib.php');
72 require_once($CFG->libdir.'/gradelib.php');
74 if (!empty($CFG->showcronsql)) {
75 $DB->set_debug(true);
77 if (!empty($CFG->showcrondebugging)) {
78 set_debugging(DEBUG_DEVELOPER, true);
81 $starttime = microtime();
83 /// emulate normal session
84 cron_setup_user();
86 /// Start output log
87 $timenow = time();
89 mtrace("Server Time: ".date('r',$timenow)."\n\n");
91 // Run automated backups if required.
92 require_once($CFG->dirroot.'/backup/util/includes/backup_includes.php');
93 require_once($CFG->dirroot.'/backup/util/helper/backup_cron_helper.class.php');
94 backup_cron_automated_helper::run_automated_backup(backup_cron_automated_helper::RUN_IMMEDIATELY);
96 mtrace("Automated cron backups completed correctly");
98 $difftime = microtime_diff($starttime, microtime());
99 mtrace("Execution took ".$difftime." seconds");