Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

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

   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   * Utility helper for automated backups run through cron.
  19   *
  20   * @package    core
  21   * @subpackage backup
  22   * @copyright  2010 Sam Hemelryk
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  /**
  29   * This class is an abstract class with methods that can be called to aid the
  30   * running of automated backups over cron.
  31   */
  32  abstract class backup_cron_automated_helper {
  33  
  34      /** Automated backups are active and ready to run */
  35      const STATE_OK = 0;
  36      /** Automated backups are disabled and will not be run */
  37      const STATE_DISABLED = 1;
  38      /** Automated backups are all ready running! */
  39      const STATE_RUNNING = 2;
  40  
  41      /** Course automated backup completed successfully */
  42      const BACKUP_STATUS_OK = 1;
  43      /** Course automated backup errored */
  44      const BACKUP_STATUS_ERROR = 0;
  45      /** Course automated backup never finished */
  46      const BACKUP_STATUS_UNFINISHED = 2;
  47      /** Course automated backup was skipped */
  48      const BACKUP_STATUS_SKIPPED = 3;
  49      /** Course automated backup had warnings */
  50      const BACKUP_STATUS_WARNING = 4;
  51      /** Course automated backup has yet to be run */
  52      const BACKUP_STATUS_NOTYETRUN = 5;
  53      /** Course automated backup has been added to adhoc task queue */
  54      const BACKUP_STATUS_QUEUED = 6;
  55  
  56      /** Run if required by the schedule set in config. Default. **/
  57      const RUN_ON_SCHEDULE = 0;
  58      /** Run immediately. **/
  59      const RUN_IMMEDIATELY = 1;
  60  
  61      const AUTO_BACKUP_DISABLED = 0;
  62      const AUTO_BACKUP_ENABLED = 1;
  63      const AUTO_BACKUP_MANUAL = 2;
  64  
  65      /** Automated backup storage in course backup filearea */
  66      const STORAGE_COURSE = 0;
  67      /** Automated backup storage in specified directory */
  68      const STORAGE_DIRECTORY = 1;
  69      /** Automated backup storage in course backup filearea and specified directory */
  70      const STORAGE_COURSE_AND_DIRECTORY = 2;
  71  
  72      /**
  73       * Get the courses to backup.
  74       *
  75       * When there are multiple courses to backup enforce some order to the record set.
  76       * The following is the preference order.
  77       * First backup courses that do not have an entry in backup_courses first,
  78       * as they are likely new and never been backed up. Do the oldest modified courses first.
  79       * Then backup courses that have previously been backed up starting with the oldest next start time.
  80       *
  81       * @param null|int $now timestamp to use in course selection.
  82       * @return moodle_recordset The recordset of matching courses.
  83       */
  84      protected static function get_courses($now = null) {
  85          global $DB;
  86          if ($now == null) {
  87              $now = time();
  88          }
  89  
  90          $sql = 'SELECT c.*,
  91                         COALESCE(bc.nextstarttime, 1) nextstarttime
  92                    FROM {course} c
  93               LEFT JOIN {backup_courses} bc ON bc.courseid = c.id
  94                   WHERE bc.nextstarttime IS NULL OR bc.nextstarttime < ?
  95                ORDER BY nextstarttime ASC,
  96                         c.timemodified DESC';
  97  
  98          $params = array(
  99              $now,  // Only get courses where the backup start time is in the past.
 100          );
 101          $rs = $DB->get_recordset_sql($sql, $params);
 102  
 103          return $rs;
 104      }
 105  
 106      /**
 107       * Runs the automated backups if required
 108       *
 109       * @param bool $rundirective
 110       */
 111      public static function run_automated_backup($rundirective = self::RUN_ON_SCHEDULE) {
 112          $now = time();
 113  
 114          $lock = self::get_automated_backup_lock($rundirective);
 115          if (!$lock) {
 116              return;
 117          }
 118  
 119          try {
 120              mtrace("Checking courses");
 121              mtrace("Skipping deleted courses", '...');
 122              mtrace(sprintf("%d courses", self::remove_deleted_courses_from_schedule()));
 123              mtrace('Running required automated backups...');
 124              cron_trace_time_and_memory();
 125  
 126              mtrace("Getting admin info");
 127              $admin = get_admin();
 128              if (!$admin) {
 129                  mtrace("Error: No admin account was found");
 130                  return;
 131              }
 132  
 133              $rs = self::get_courses($now); // Get courses to backup.
 134              $emailpending = self::check_and_push_automated_backups($rs, $admin);
 135              $rs->close();
 136  
 137              // Send email to admin if necessary.
 138              if ($emailpending) {
 139                  self::send_backup_status_to_admin($admin);
 140              }
 141          } finally {
 142              // Everything is finished release lock.
 143              $lock->release();
 144              mtrace('Automated backups complete.');
 145          }
 146      }
 147  
 148      /**
 149       * Gets the results from the last automated backup that was run based upon
 150       * the statuses of the courses that were looked at.
 151       *
 152       * @return array
 153       */
 154      public static function get_backup_status_array() {
 155          global $DB;
 156  
 157          $result = array(
 158              self::BACKUP_STATUS_ERROR => 0,
 159              self::BACKUP_STATUS_OK => 0,
 160              self::BACKUP_STATUS_UNFINISHED => 0,
 161              self::BACKUP_STATUS_SKIPPED => 0,
 162              self::BACKUP_STATUS_WARNING => 0,
 163              self::BACKUP_STATUS_NOTYETRUN => 0,
 164              self::BACKUP_STATUS_QUEUED => 0,
 165          );
 166  
 167          $statuses = $DB->get_records_sql('SELECT DISTINCT bc.laststatus,
 168                                              COUNT(bc.courseid) AS statuscount
 169                                              FROM {backup_courses} bc
 170                                              GROUP BY bc.laststatus');
 171  
 172          foreach ($statuses as $status) {
 173              if (empty($status->statuscount)) {
 174                  $status->statuscount = 0;
 175              }
 176              $result[(int)$status->laststatus] += $status->statuscount;
 177          }
 178  
 179          return $result;
 180      }
 181  
 182      /**
 183       * Collect details for all statuses of the courses
 184       * and send report to admin.
 185       *
 186       * @param stdClass $admin
 187       * @return array
 188       */
 189      private static function send_backup_status_to_admin($admin) {
 190          global $DB, $CFG;
 191  
 192          mtrace("Sending email to admin");
 193          $message = "";
 194  
 195          $count = self::get_backup_status_array();
 196          $haserrors = ($count[self::BACKUP_STATUS_ERROR] != 0 || $count[self::BACKUP_STATUS_UNFINISHED] != 0);
 197  
 198          // Build the message text.
 199          // Summary.
 200          $message .= get_string('summary') . "\n";
 201          $message .= "==================================================\n";
 202          $message .= '  ' . get_string('courses') . ': ' . array_sum($count) . "\n";
 203          $message .= '  ' . get_string('ok') . ': ' . $count[self::BACKUP_STATUS_OK] . "\n";
 204          $message .= '  ' . get_string('skipped') . ': ' . $count[self::BACKUP_STATUS_SKIPPED] . "\n";
 205          $message .= '  ' . get_string('error') . ': ' . $count[self::BACKUP_STATUS_ERROR] . "\n";
 206          $message .= '  ' . get_string('unfinished') . ': ' . $count[self::BACKUP_STATUS_UNFINISHED] . "\n";
 207          $message .= '  ' . get_string('backupadhocpending') . ': ' . $count[self::BACKUP_STATUS_QUEUED] . "\n";
 208          $message .= '  ' . get_string('warning') . ': ' . $count[self::BACKUP_STATUS_WARNING] . "\n";
 209          $message .= '  ' . get_string('backupnotyetrun') . ': ' . $count[self::BACKUP_STATUS_NOTYETRUN]."\n\n";
 210  
 211          // Reference.
 212          if ($haserrors) {
 213              $message .= "  ".get_string('backupfailed')."\n\n";
 214              $desturl = "$CFG->wwwroot/report/backups/index.php";
 215              $message .= "  ".get_string('backuptakealook', '', $desturl)."\n\n";
 216              // Set message priority.
 217              $admin->priority = 1;
 218              // Reset error and unfinished statuses to ok if longer than 24 hours.
 219              $sql = "laststatus IN (:statuserror,:statusunfinished) AND laststarttime < :yesterday";
 220              $params = [
 221                  'statuserror' => self::BACKUP_STATUS_ERROR,
 222                  'statusunfinished' => self::BACKUP_STATUS_UNFINISHED,
 223                  'yesterday' => time() - 86400,
 224              ];
 225              $DB->set_field_select('backup_courses', 'laststatus', self::BACKUP_STATUS_OK, $sql, $params);
 226          } else {
 227              $message .= "  ".get_string('backupfinished')."\n";
 228          }
 229  
 230          // Build the message subject.
 231          $site = get_site();
 232          $prefix = format_string($site->shortname, true, array('context' => context_course::instance(SITEID))).": ";
 233          if ($haserrors) {
 234              $prefix .= "[".strtoupper(get_string('error'))."] ";
 235          }
 236          $subject = $prefix.get_string('automatedbackupstatus', 'backup');
 237  
 238          // Send the message.
 239          $eventdata = new \core\message\message();
 240          $eventdata->courseid          = SITEID;
 241          $eventdata->modulename        = 'moodle';
 242          $eventdata->userfrom          = $admin;
 243          $eventdata->userto            = $admin;
 244          $eventdata->subject           = $subject;
 245          $eventdata->fullmessage       = $message;
 246          $eventdata->fullmessageformat = FORMAT_PLAIN;
 247          $eventdata->fullmessagehtml   = '';
 248          $eventdata->smallmessage      = '';
 249  
 250          $eventdata->component         = 'moodle';
 251          $eventdata->name         = 'backup';
 252  
 253          return message_send($eventdata);
 254      }
 255  
 256      /**
 257       * Loop through courses and push to course ad-hoc task if required
 258       *
 259       * @param \record_set $courses
 260       * @param stdClass $admin
 261       * @return boolean
 262       */
 263      private static function check_and_push_automated_backups($courses, $admin) {
 264          global $DB;
 265  
 266          $now = time();
 267          $emailpending = false;
 268  
 269          $nextstarttime = self::calculate_next_automated_backup(null, $now);
 270          $showtime = "undefined";
 271          if ($nextstarttime > 0) {
 272              $showtime = date('r', $nextstarttime);
 273          }
 274  
 275          foreach ($courses as $course) {
 276              $backupcourse = $DB->get_record('backup_courses', array('courseid' => $course->id));
 277              if (!$backupcourse) {
 278                  $backupcourse = new stdClass;
 279                  $backupcourse->courseid = $course->id;
 280                  $backupcourse->laststatus = self::BACKUP_STATUS_NOTYETRUN;
 281                  $DB->insert_record('backup_courses', $backupcourse);
 282                  $backupcourse = $DB->get_record('backup_courses', array('courseid' => $course->id));
 283              }
 284  
 285              // Check if we are going to be running the backup now.
 286              $shouldrunnow = ($backupcourse->nextstarttime > 0 && $backupcourse->nextstarttime < $now);
 287  
 288              // Check if the course is not scheduled to run right now, or it has been put in queue.
 289              if (!$shouldrunnow || $backupcourse->laststatus == self::BACKUP_STATUS_QUEUED) {
 290                  $backupcourse->nextstarttime = $nextstarttime;
 291                  $DB->update_record('backup_courses', $backupcourse);
 292                  mtrace('Skipping ' . $course->fullname . ' (Not scheduled for backup until ' . $showtime . ')');
 293              } else {
 294                      $skipped = self::should_skip_course_backup($backupcourse, $course, $nextstarttime);
 295                  if (!$skipped) { // If it should not be skipped.
 296  
 297                      // Only make the backup if laststatus isn't 2-UNFINISHED (uncontrolled error or being backed up).
 298                      if ($backupcourse->laststatus != self::BACKUP_STATUS_UNFINISHED) {
 299                          // Add every non-skipped courses to backup adhoc task queue.
 300                          mtrace('Putting backup of ' . $course->fullname . ' in adhoc task queue ...');
 301  
 302                          // We have to send an email because we have included at least one backup.
 303                          $emailpending = true;
 304                          // Create adhoc task for backup.
 305                          self::push_course_backup_adhoc_task($backupcourse, $admin);
 306                          mtrace("complete - next execution: $showtime");
 307                      }
 308                  }
 309              }
 310          }
 311  
 312          return $emailpending;
 313      }
 314  
 315      /**
 316       * Check if we can skip this course backup.
 317       *
 318       * @param stdClass $backupcourse
 319       * @param stdClass $course
 320       * @param int $nextstarttime
 321       * @return boolean
 322       */
 323      private static function should_skip_course_backup($backupcourse, $course, $nextstarttime) {
 324          global $DB;
 325  
 326          $config = get_config('backup');
 327          $now = time();
 328           // Assume that we are not skipping anything.
 329           $skipped = false;
 330           $skippedmessage = '';
 331  
 332          // The last backup is considered as successful when OK or SKIPPED.
 333          $lastbackupwassuccessful = ($backupcourse->laststatus == self::BACKUP_STATUS_SKIPPED ||
 334          $backupcourse->laststatus == self::BACKUP_STATUS_OK) && (
 335          $backupcourse->laststarttime > 0 && $backupcourse->lastendtime > 0);
 336  
 337          // If config backup_auto_skip_hidden is set to true, skip courses that are not visible.
 338          if ($config->backup_auto_skip_hidden) {
 339              $skipped = ($config->backup_auto_skip_hidden && !$course->visible);
 340              $skippedmessage = 'Not visible';
 341          }
 342  
 343          // If config backup_auto_skip_modif_days is set to true, skip courses
 344          // that have not been modified since the number of days defined.
 345          if (!$skipped && $lastbackupwassuccessful && $config->backup_auto_skip_modif_days) {
 346              $timenotmodifsincedays = $now - ($config->backup_auto_skip_modif_days * DAYSECS);
 347              // Check log if there were any modifications to the course content.
 348              $logexists = self::is_course_modified($course->id, $timenotmodifsincedays);
 349              $skipped = ($course->timemodified <= $timenotmodifsincedays && !$logexists);
 350              $skippedmessage = 'Not modified in the past '.$config->backup_auto_skip_modif_days.' days';
 351          }
 352  
 353          // If config backup_auto_skip_modif_prev is set to true, skip courses
 354          // that have not been modified since previous backup.
 355          if (!$skipped && $lastbackupwassuccessful && $config->backup_auto_skip_modif_prev) {
 356              // Check log if there were any modifications to the course content.
 357              $logexists = self::is_course_modified($course->id, $backupcourse->laststarttime);
 358              $skipped = ($course->timemodified <= $backupcourse->laststarttime && !$logexists);
 359              $skippedmessage = 'Not modified since previous backup';
 360          }
 361  
 362          if ($skipped) { // Must have been skipped for a reason.
 363              $backupcourse->laststatus = self::BACKUP_STATUS_SKIPPED;
 364              $backupcourse->nextstarttime = $nextstarttime;
 365              $DB->update_record('backup_courses', $backupcourse);
 366              mtrace('Skipping ' . $course->fullname . ' (' . $skippedmessage . ')');
 367              mtrace('Backup of \'' . $course->fullname . '\' is scheduled on ' . date('r', $nextstarttime));
 368          }
 369  
 370          return $skipped;
 371      }
 372  
 373      /**
 374       * Create course backup adhoc task
 375       *
 376       * @param stdClass $backupcourse
 377       * @param stdClass $admin
 378       * @return void
 379       */
 380      private static function push_course_backup_adhoc_task($backupcourse, $admin) {
 381          global $DB;
 382  
 383          $asynctask = new \core\task\course_backup_task();
 384          $asynctask->set_blocking(false);
 385          $asynctask->set_custom_data(array(
 386              'courseid' => $backupcourse->courseid,
 387              'adminid' => $admin->id
 388          ));
 389          \core\task\manager::queue_adhoc_task($asynctask);
 390  
 391          $backupcourse->laststatus = self::BACKUP_STATUS_QUEUED;
 392          $DB->update_record('backup_courses', $backupcourse);
 393      }
 394  
 395      /**
 396       * Works out the next time the automated backup should be run.
 397       *
 398       * @param mixed $ignoredtimezone all settings are in server timezone!
 399       * @param int $now timestamp, should not be in the past, most likely time()
 400       * @return int timestamp of the next execution at server time
 401       */
 402      public static function calculate_next_automated_backup($ignoredtimezone, $now) {
 403  
 404          $config = get_config('backup');
 405  
 406          $backuptime = new DateTime('@' . $now);
 407          $backuptime->setTimezone(core_date::get_server_timezone_object());
 408          $backuptime->setTime($config->backup_auto_hour, $config->backup_auto_minute);
 409  
 410          while ($backuptime->getTimestamp() < $now) {
 411              $backuptime->add(new DateInterval('P1D'));
 412          }
 413  
 414          // Get number of days from backup date to execute backups.
 415          $automateddays = substr($config->backup_auto_weekdays, $backuptime->format('w')) . $config->backup_auto_weekdays;
 416          $daysfromnow = strpos($automateddays, "1");
 417  
 418          // Error, there are no days to schedule the backup for.
 419          if ($daysfromnow === false) {
 420              return 0;
 421          }
 422  
 423          if ($daysfromnow > 0) {
 424              $backuptime->add(new DateInterval('P' . $daysfromnow . 'D'));
 425          }
 426  
 427          return $backuptime->getTimestamp();
 428      }
 429  
 430      /**
 431       * Launches a automated backup routine for the given course
 432       *
 433       * @param stdClass $course
 434       * @param int $starttime
 435       * @param int $userid
 436       * @return bool
 437       */
 438      public static function launch_automated_backup($course, $starttime, $userid) {
 439  
 440          $outcome = self::BACKUP_STATUS_OK;
 441          $config = get_config('backup');
 442          $dir = $config->backup_auto_destination;
 443          $storage = (int)$config->backup_auto_storage;
 444  
 445          $bc = new backup_controller(backup::TYPE_1COURSE, $course->id, backup::FORMAT_MOODLE, backup::INTERACTIVE_NO,
 446                  backup::MODE_AUTOMATED, $userid);
 447  
 448          try {
 449  
 450              // Set the default filename.
 451              $format = $bc->get_format();
 452              $type = $bc->get_type();
 453              $id = $bc->get_id();
 454              $users = $bc->get_plan()->get_setting('users')->get_value();
 455              $anonymised = $bc->get_plan()->get_setting('anonymize')->get_value();
 456              $incfiles = (bool)$config->backup_auto_files;
 457              $bc->get_plan()->get_setting('filename')->set_value(backup_plan_dbops::get_default_backup_filename($format, $type,
 458                      $id, $users, $anonymised, false, $incfiles));
 459  
 460              $bc->set_status(backup::STATUS_AWAITING);
 461  
 462              $bc->execute_plan();
 463              $results = $bc->get_results();
 464              $outcome = self::outcome_from_results($results);
 465              $file = $results['backup_destination']; // May be empty if file already moved to target location.
 466  
 467              // If we need to copy the backup file to an external dir and it is not writable, change status to error.
 468              // This is a feature to prevent moodledata to be filled up and break a site when the admin misconfigured
 469              // the automated backups storage type and destination directory.
 470              if ($storage !== 0 && (empty($dir) || !file_exists($dir) || !is_dir($dir) || !is_writable($dir))) {
 471                  $bc->log('Specified backup directory is not writable - ', backup::LOG_ERROR, $dir);
 472                  $dir = null;
 473                  $outcome = self::BACKUP_STATUS_ERROR;
 474              }
 475  
 476              // Copy file only if there was no error.
 477              if ($file && !empty($dir) && $storage !== 0 && $outcome != self::BACKUP_STATUS_ERROR) {
 478                  $filename = backup_plan_dbops::get_default_backup_filename($format, $type, $course->id, $users, $anonymised,
 479                          !$config->backup_shortname);
 480                  if (!$file->copy_content_to($dir.'/'.$filename)) {
 481                      $bc->log('Attempt to copy backup file to the specified directory failed - ',
 482                              backup::LOG_ERROR, $dir);
 483                      $outcome = self::BACKUP_STATUS_ERROR;
 484                  }
 485                  if ($outcome != self::BACKUP_STATUS_ERROR && $storage === 1) {
 486                      if (!$file->delete()) {
 487                          $outcome = self::BACKUP_STATUS_WARNING;
 488                          $bc->log('Attempt to delete the backup file from course automated backup area failed - ',
 489                                  backup::LOG_WARNING, $file->get_filename());
 490                      }
 491                  }
 492              }
 493  
 494          } catch (moodle_exception $e) {
 495              $bc->log('backup_auto_failed_on_course', backup::LOG_ERROR, $course->shortname); // Log error header.
 496              $bc->log('Exception: ' . $e->errorcode, backup::LOG_ERROR, $e->a, 1); // Log original exception problem.
 497              $bc->log('Debug: ' . $e->debuginfo, backup::LOG_DEBUG, null, 1); // Log original debug information.
 498              $outcome = self::BACKUP_STATUS_ERROR;
 499          }
 500  
 501          // Delete the backup file immediately if something went wrong.
 502          if ($outcome === self::BACKUP_STATUS_ERROR) {
 503  
 504              // Delete the file from file area if exists.
 505              if (!empty($file)) {
 506                  $file->delete();
 507              }
 508  
 509              // Delete file from external storage if exists.
 510              if ($storage !== 0 && !empty($filename) && file_exists($dir.'/'.$filename)) {
 511                  @unlink($dir.'/'.$filename);
 512              }
 513          }
 514  
 515          $bc->destroy();
 516          unset($bc);
 517  
 518          return $outcome;
 519      }
 520  
 521      /**
 522       * Returns the backup outcome by analysing its results.
 523       *
 524       * @param array $results returned by a backup
 525       * @return int {@link self::BACKUP_STATUS_OK} and other constants
 526       */
 527      public static function outcome_from_results($results) {
 528          $outcome = self::BACKUP_STATUS_OK;
 529          foreach ($results as $code => $value) {
 530              // Each possible error and warning code has to be specified in this switch
 531              // which basically analyses the results to return the correct backup status.
 532              switch ($code) {
 533                  case 'missing_files_in_pool':
 534                      $outcome = self::BACKUP_STATUS_WARNING;
 535                      break;
 536              }
 537              // If we found the highest error level, we exit the loop.
 538              if ($outcome == self::BACKUP_STATUS_ERROR) {
 539                  break;
 540              }
 541          }
 542          return $outcome;
 543      }
 544  
 545      /**
 546       * Removes deleted courses fromn the backup_courses table so that we don't
 547       * waste time backing them up.
 548       *
 549       * @return int
 550       */
 551      public static function remove_deleted_courses_from_schedule() {
 552          global $DB;
 553          $skipped = 0;
 554          $sql = "SELECT bc.courseid FROM {backup_courses} bc WHERE bc.courseid NOT IN (SELECT c.id FROM {course} c)";
 555          $rs = $DB->get_recordset_sql($sql);
 556          foreach ($rs as $deletedcourse) {
 557              // Doesn't exist, so delete from backup tables.
 558              $DB->delete_records('backup_courses', array('courseid' => $deletedcourse->courseid));
 559              $skipped++;
 560          }
 561          $rs->close();
 562          return $skipped;
 563      }
 564  
 565      /**
 566       * Try to get lock for automated backup.
 567       * @param int $rundirective
 568       *
 569       * @return \core\lock\lock|boolean - An instance of \core\lock\lock if the lock was obtained, or false.
 570       */
 571      public static function get_automated_backup_lock($rundirective = self::RUN_ON_SCHEDULE) {
 572          $config = get_config('backup');
 573          $active = (int)$config->backup_auto_active;
 574          $weekdays = (string)$config->backup_auto_weekdays;
 575  
 576          mtrace("Checking automated backup status", '...');
 577          $locktype = 'automated_backup';
 578          $resource = 'queue_backup_jobs_running';
 579          $lockfactory = \core\lock\lock_config::get_lock_factory($locktype);
 580  
 581          // In case of automated backup also check that it is scheduled for at least one weekday.
 582          if ($active === self::AUTO_BACKUP_DISABLED ||
 583                  ($rundirective == self::RUN_ON_SCHEDULE && $active === self::AUTO_BACKUP_MANUAL) ||
 584                  ($rundirective == self::RUN_ON_SCHEDULE && strpos($weekdays, '1') === false)) {
 585              mtrace('INACTIVE');
 586              return false;
 587          }
 588  
 589          if (!$lock = $lockfactory->get_lock($resource, 10)) {
 590              return false;
 591          }
 592  
 593          mtrace('OK');
 594          return $lock;
 595      }
 596  
 597      /**
 598       * Removes excess backups from a specified course.
 599       *
 600       * @param stdClass $course Course object
 601       * @param int $now Starting time of the process
 602       * @return bool Whether or not backups is being removed
 603       */
 604      public static function remove_excess_backups($course, $now = null) {
 605          $config = get_config('backup');
 606          $maxkept = (int)$config->backup_auto_max_kept;
 607          $storage = $config->backup_auto_storage;
 608          $deletedays = (int)$config->backup_auto_delete_days;
 609  
 610          if ($maxkept == 0 && $deletedays == 0) {
 611              // Means keep all backup files and never delete backup after x days.
 612              return true;
 613          }
 614  
 615          if (!isset($now)) {
 616              $now = time();
 617          }
 618  
 619          // Clean up excess backups in the course backup filearea.
 620          $deletedcoursebackups = false;
 621          if ($storage == self::STORAGE_COURSE || $storage == self::STORAGE_COURSE_AND_DIRECTORY) {
 622              $deletedcoursebackups = self::remove_excess_backups_from_course($course, $now);
 623          }
 624  
 625          // Clean up excess backups in the specified external directory.
 626          $deleteddirectorybackups = false;
 627          if ($storage == self::STORAGE_DIRECTORY || $storage == self::STORAGE_COURSE_AND_DIRECTORY) {
 628              $deleteddirectorybackups = self::remove_excess_backups_from_directory($course, $now);
 629          }
 630  
 631          if ($deletedcoursebackups || $deleteddirectorybackups) {
 632              return true;
 633          } else {
 634              return false;
 635          }
 636      }
 637  
 638      /**
 639       * Removes excess backups in the course backup filearea from a specified course.
 640       *
 641       * @param stdClass $course Course object
 642       * @param int $now Starting time of the process
 643       * @return bool Whether or not backups are being removed
 644       */
 645      protected static function remove_excess_backups_from_course($course, $now) {
 646          $fs = get_file_storage();
 647          $context = context_course::instance($course->id);
 648          $component = 'backup';
 649          $filearea = 'automated';
 650          $itemid = 0;
 651          $backupfiles = array();
 652          $backupfilesarea = $fs->get_area_files($context->id, $component, $filearea, $itemid, 'timemodified DESC', false);
 653          // Store all the matching files into timemodified => stored_file array.
 654          foreach ($backupfilesarea as $backupfile) {
 655              $backupfiles[$backupfile->get_timemodified()] = $backupfile;
 656          }
 657  
 658          $backupstodelete = self::get_backups_to_delete($backupfiles, $now);
 659          if ($backupstodelete) {
 660              foreach ($backupstodelete as $backuptodelete) {
 661                  $backuptodelete->delete();
 662              }
 663              mtrace('Deleted ' . count($backupstodelete) . ' old backup file(s) from the automated filearea');
 664              return true;
 665          } else {
 666              return false;
 667          }
 668      }
 669  
 670      /**
 671       * Removes excess backups in the specified external directory from a specified course.
 672       *
 673       * @param stdClass $course Course object
 674       * @param int $now Starting time of the process
 675       * @return bool Whether or not backups are being removed
 676       */
 677      protected static function remove_excess_backups_from_directory($course, $now) {
 678          $config = get_config('backup');
 679          $dir = $config->backup_auto_destination;
 680  
 681          $isnotvaliddir = !file_exists($dir) || !is_dir($dir) || !is_writable($dir);
 682          if ($isnotvaliddir) {
 683              mtrace('Error: ' . $dir . ' does not appear to be a valid directory');
 684              return false;
 685          }
 686  
 687          // Calculate backup filename regex, ignoring the date/time/info parts that can be
 688          // variable, depending of languages, formats and automated backup settings.
 689          $filename = backup::FORMAT_MOODLE . '-' . backup::TYPE_1COURSE . '-' . $course->id . '-';
 690          $regex = '#' . preg_quote($filename, '#') . '.*\.mbz$#';
 691  
 692          // Store all the matching files into filename => timemodified array.
 693          $backupfiles = array();
 694          foreach (scandir($dir) as $backupfile) {
 695              // Skip files not matching the naming convention.
 696              if (!preg_match($regex, $backupfile)) {
 697                  continue;
 698              }
 699  
 700              // Read the information contained in the backup itself.
 701              try {
 702                  $bcinfo = backup_general_helper::get_backup_information_from_mbz($dir . '/' . $backupfile);
 703              } catch (backup_helper_exception $e) {
 704                  mtrace('Error: ' . $backupfile . ' does not appear to be a valid backup (' . $e->errorcode . ')');
 705                  continue;
 706              }
 707  
 708              // Make sure this backup concerns the course and site we are looking for.
 709              if ($bcinfo->format === backup::FORMAT_MOODLE &&
 710                      $bcinfo->type === backup::TYPE_1COURSE &&
 711                      $bcinfo->original_course_id == $course->id &&
 712                      backup_general_helper::backup_is_samesite($bcinfo)) {
 713                  $backupfiles[$bcinfo->backup_date] = $backupfile;
 714              }
 715          }
 716  
 717          $backupstodelete = self::get_backups_to_delete($backupfiles, $now);
 718          if ($backupstodelete) {
 719              foreach ($backupstodelete as $backuptodelete) {
 720                  unlink($dir . '/' . $backuptodelete);
 721              }
 722              mtrace('Deleted ' . count($backupstodelete) . ' old backup file(s) from external directory');
 723              return true;
 724          } else {
 725              return false;
 726          }
 727      }
 728  
 729      /**
 730       * Get the list of backup files to delete depending on the automated backup settings.
 731       *
 732       * @param array $backupfiles Existing backup files
 733       * @param int $now Starting time of the process
 734       * @return array Backup files to delete
 735       */
 736      protected static function get_backups_to_delete($backupfiles, $now) {
 737          $config = get_config('backup');
 738          $maxkept = (int)$config->backup_auto_max_kept;
 739          $deletedays = (int)$config->backup_auto_delete_days;
 740          $minkept = (int)$config->backup_auto_min_kept;
 741  
 742          // Sort by keys descending (newer to older filemodified).
 743          krsort($backupfiles);
 744          $tokeep = $maxkept;
 745          if ($deletedays > 0) {
 746              $deletedayssecs = $deletedays * DAYSECS;
 747              $tokeep = 0;
 748              $backupfileskeys = array_keys($backupfiles);
 749              foreach ($backupfileskeys as $timemodified) {
 750                  $mustdeletebackup = $timemodified < ($now - $deletedayssecs);
 751                  if ($mustdeletebackup || $tokeep >= $maxkept) {
 752                      break;
 753                  }
 754                  $tokeep++;
 755              }
 756  
 757              if ($tokeep < $minkept) {
 758                  $tokeep = $minkept;
 759              }
 760          }
 761  
 762          if (count($backupfiles) <= $tokeep) {
 763              // There are less or equal matching files than the desired number to keep, there is nothing to clean up.
 764              return false;
 765          } else {
 766              $backupstodelete = array_splice($backupfiles, $tokeep);
 767              return $backupstodelete;
 768          }
 769      }
 770  
 771      /**
 772       * Check logs to find out if a course was modified since the given time.
 773       *
 774       * @param int $courseid course id to check
 775       * @param int $since timestamp, from which to check
 776       *
 777       * @return bool true if the course was modified, false otherwise. This also returns false if no readers are enabled. This is
 778       * intentional, since we cannot reliably determine if any modification was made or not.
 779       */
 780      protected static function is_course_modified($courseid, $since) {
 781          $logmang = get_log_manager();
 782          $readers = $logmang->get_readers('core\log\sql_reader');
 783          $params = array('courseid' => $courseid, 'since' => $since);
 784  
 785          foreach ($readers as $readerpluginname => $reader) {
 786              $where = "courseid = :courseid and timecreated > :since and crud <> 'r'";
 787  
 788              // Prevent logs of prevous backups causing a false positive.
 789              if ($readerpluginname != 'logstore_legacy') {
 790                  $where .= " and target <> 'course_backup'";
 791              }
 792  
 793              if ($reader->get_events_select_count($where, $params)) {
 794                  return true;
 795              }
 796          }
 797          return false;
 798      }
 799  }