automated_backups.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. * Automated backups CLI cron
  18. *
  19. * This script executes
  20. *
  21. * @package core
  22. * @subpackage cli
  23. * @copyright 2010 Sam Hemelryk
  24. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25. */
  26. define('CLI_SCRIPT', true);
  27. require(__DIR__.'/../../config.php');
  28. require_once($CFG->libdir.'/clilib.php'); // cli only functions
  29. require_once($CFG->libdir.'/cronlib.php');
  30. // now get cli options
  31. list($options, $unrecognized) = cli_get_params(array('help'=>false),
  32. array('h'=>'help'));
  33. if ($unrecognized) {
  34. $unrecognized = implode("\n ", $unrecognized);
  35. cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
  36. }
  37. if ($options['help']) {
  38. $help =
  39. "Execute automated backups.
  40. This script executes automated backups completely and is designed to be
  41. called via cron.
  42. Options:
  43. -h, --help Print out this help
  44. Example:
  45. \$sudo -u www-data /usr/bin/php admin/cli/automated_backups.php
  46. ";
  47. echo $help;
  48. die;
  49. }
  50. if (CLI_MAINTENANCE) {
  51. echo "CLI maintenance mode active, backup execution suspended.\n";
  52. exit(1);
  53. }
  54. if (moodle_needs_upgrading()) {
  55. echo "Moodle upgrade pending, backup execution suspended.\n";
  56. exit(1);
  57. }
  58. require_once($CFG->libdir.'/adminlib.php');
  59. require_once($CFG->libdir.'/gradelib.php');
  60. if (!empty($CFG->showcronsql)) {
  61. $DB->set_debug(true);
  62. }
  63. if (!empty($CFG->showcrondebugging)) {
  64. set_debugging(DEBUG_DEVELOPER, true);
  65. }
  66. $starttime = microtime();
  67. /// emulate normal session
  68. cron_setup_user();
  69. /// Start output log
  70. $timenow = time();
  71. mtrace("Server Time: ".date('r',$timenow)."\n\n");
  72. // Run automated backups if required.
  73. require_once($CFG->dirroot.'/backup/util/includes/backup_includes.php');
  74. require_once($CFG->dirroot.'/backup/util/helper/backup_cron_helper.class.php');
  75. backup_cron_automated_helper::run_automated_backup(backup_cron_automated_helper::RUN_IMMEDIATELY);
  76. mtrace("Automated cron backups completed correctly");
  77. $difftime = microtime_diff($starttime, microtime());
  78. mtrace("Execution took ".$difftime." seconds");