backup.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. * This script allows to do backup.
  18. *
  19. * @package core
  20. * @subpackage cli
  21. * @copyright 2013 Lancaster University
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. */
  24. define('CLI_SCRIPT', 1);
  25. require(__DIR__.'/../../config.php');
  26. require_once($CFG->libdir.'/clilib.php');
  27. require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
  28. // Now get cli options.
  29. list($options, $unrecognized) = cli_get_params(array(
  30. 'courseid' => false,
  31. 'courseshortname' => '',
  32. 'destination' => '',
  33. 'help' => false,
  34. ), array('h' => 'help'));
  35. if ($unrecognized) {
  36. $unrecognized = implode("\n ", $unrecognized);
  37. cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
  38. }
  39. if ($options['help'] || !($options['courseid'] || $options['courseshortname'])) {
  40. $help = <<<EOL
  41. Perform backup of the given course.
  42. Options:
  43. --courseid=INTEGER Course ID for backup.
  44. --courseshortname=STRING Course shortname for backup.
  45. --destination=STRING Path where to store backup file. If not set the backup
  46. will be stored within the course backup file area.
  47. -h, --help Print out this help.
  48. Example:
  49. \$sudo -u www-data /usr/bin/php admin/cli/backup.php --courseid=2 --destination=/moodle/backup/\n
  50. EOL;
  51. echo $help;
  52. die;
  53. }
  54. $admin = get_admin();
  55. if (!$admin) {
  56. mtrace("Error: No admin account was found");
  57. die;
  58. }
  59. // Do we need to store backup somewhere else?
  60. $dir = rtrim($options['destination'], '/');
  61. if (!empty($dir)) {
  62. if (!file_exists($dir) || !is_dir($dir) || !is_writable($dir)) {
  63. mtrace("Destination directory does not exists or not writable.");
  64. die;
  65. }
  66. }
  67. // Check that the course exists.
  68. if ($options['courseid']) {
  69. $course = $DB->get_record('course', array('id' => $options['courseid']), '*', MUST_EXIST);
  70. } else if ($options['courseshortname']) {
  71. $course = $DB->get_record('course', array('shortname' => $options['courseshortname']), '*', MUST_EXIST);
  72. }
  73. cli_heading('Performing backup...');
  74. $bc = new backup_controller(backup::TYPE_1COURSE, $course->id, backup::FORMAT_MOODLE,
  75. backup::INTERACTIVE_YES, backup::MODE_GENERAL, $admin->id);
  76. // Set the default filename.
  77. $format = $bc->get_format();
  78. $type = $bc->get_type();
  79. $id = $bc->get_id();
  80. $users = $bc->get_plan()->get_setting('users')->get_value();
  81. $anonymised = $bc->get_plan()->get_setting('anonymize')->get_value();
  82. $filename = backup_plan_dbops::get_default_backup_filename($format, $type, $id, $users, $anonymised);
  83. $bc->get_plan()->get_setting('filename')->set_value($filename);
  84. // Execution.
  85. $bc->finish_ui();
  86. $bc->execute_plan();
  87. $results = $bc->get_results();
  88. $file = $results['backup_destination']; // May be empty if file already moved to target location.
  89. // Do we need to store backup somewhere else?
  90. if (!empty($dir)) {
  91. if ($file) {
  92. mtrace("Writing " . $dir.'/'.$filename);
  93. if ($file->copy_content_to($dir.'/'.$filename)) {
  94. $file->delete();
  95. mtrace("Backup completed.");
  96. } else {
  97. mtrace("Destination directory does not exist or is not writable. Leaving the backup in the course backup file area.");
  98. }
  99. }
  100. } else {
  101. mtrace("Backup completed, the new file is listed in the backup area of the given course");
  102. }
  103. $bc->destroy();
  104. exit(0);