Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402] [Versions 401 and 402]

   1  <?php
   2  
   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.
  14  //
  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/>.
  17  
  18  /**
  19   * Automated backups CLI cron
  20   *
  21   * This script executes
  22   *
  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
  27   */
  28  
  29  define('CLI_SCRIPT', true);
  30  
  31  require(__DIR__.'/../../config.php');
  32  require_once($CFG->libdir.'/clilib.php');      // cli only functions
  33  
  34  // now get cli options
  35  list($options, $unrecognized) = cli_get_params(array('help'=>false),
  36                                                 array('h'=>'help'));
  37  
  38  if ($unrecognized) {
  39      $unrecognized = implode("\n  ", $unrecognized);
  40      cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
  41  }
  42  
  43  if ($options['help']) {
  44      $help =
  45  "Execute automated backups.
  46  
  47  This script executes automated backups completely and is designed to be
  48  called via cron.
  49  
  50  Options:
  51  -h, --help            Print out this help
  52  
  53  Example:
  54  \$sudo -u www-data /usr/bin/php admin/cli/automated_backups.php
  55  ";
  56  
  57      echo $help;
  58      die;
  59  }
  60  if (CLI_MAINTENANCE) {
  61      echo "CLI maintenance mode active, backup execution suspended.\n";
  62      exit(1);
  63  }
  64  
  65  if (moodle_needs_upgrading()) {
  66      echo "Moodle upgrade pending, backup execution suspended.\n";
  67      exit(1);
  68  }
  69  
  70  require_once($CFG->libdir.'/adminlib.php');
  71  require_once($CFG->libdir.'/gradelib.php');
  72  
  73  if (!empty($CFG->showcronsql)) {
  74      $DB->set_debug(true);
  75  }
  76  if (!empty($CFG->showcrondebugging)) {
  77      set_debugging(DEBUG_DEVELOPER, true);
  78  }
  79  
  80  $starttime = microtime();
  81  
  82  // Emulate normal session.
  83  \core\cron::setup_user();
  84  
  85  // Start output log.
  86  $timenow = time();
  87  
  88  mtrace("Server Time: ".date('r',$timenow)."\n\n");
  89  
  90  // Run automated backups if required.
  91  require_once($CFG->dirroot.'/backup/util/includes/backup_includes.php');
  92  require_once($CFG->dirroot.'/backup/util/helper/backup_cron_helper.class.php');
  93  backup_cron_automated_helper::run_automated_backup(backup_cron_automated_helper::RUN_IMMEDIATELY);
  94  
  95  mtrace("Automated cron backups completed correctly");
  96  
  97  $difftime = microtime_diff($starttime, microtime());
  98  mtrace("Execution took ".$difftime." seconds");