MDL-50602 - Automated backup: Remove old backups associated to courses
[moodle.git] / backup / util / helper / backup_cron_helper.class.php
CommitLineData
bac233d3
SH
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
bac233d3
SH
18/**
19 * Utility helper for automated backups run through cron.
20 *
21 * @package core
22 * @subpackage backup
23 * @copyright 2010 Sam Hemelryk
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
26
db6c8b82
FM
27defined('MOODLE_INTERNAL') || die();
28
bac233d3
SH
29/**
30 * This class is an abstract class with methods that can be called to aid the
31 * running of automated backups over cron.
32 */
33abstract class backup_cron_automated_helper {
34
70f210c0 35 /** Automated backups are active and ready to run */
bac233d3 36 const STATE_OK = 0;
70f210c0 37 /** Automated backups are disabled and will not be run */
bac233d3 38 const STATE_DISABLED = 1;
70f210c0 39 /** Automated backups are all ready running! */
bac233d3
SH
40 const STATE_RUNNING = 2;
41
42 /** Course automated backup completed successfully */
43 const BACKUP_STATUS_OK = 1;
44 /** Course automated backup errored */
45 const BACKUP_STATUS_ERROR = 0;
46 /** Course automated backup never finished */
47 const BACKUP_STATUS_UNFINISHED = 2;
48 /** Course automated backup was skipped */
49 const BACKUP_STATUS_SKIPPED = 3;
05e521c9
FM
50 /** Course automated backup had warnings */
51 const BACKUP_STATUS_WARNING = 4;
70f210c0
MN
52 /** Course automated backup has yet to be run */
53 const BACKUP_STATUS_NOTYETRUN = 5;
bac233d3 54
081c914b
SH
55 /** Run if required by the schedule set in config. Default. **/
56 const RUN_ON_SCHEDULE = 0;
743fff98
PS
57 /** Run immediately. **/
58 const RUN_IMMEDIATELY = 1;
081c914b
SH
59
60 const AUTO_BACKUP_DISABLED = 0;
61 const AUTO_BACKUP_ENABLED = 1;
62 const AUTO_BACKUP_MANUAL = 2;
bac233d3
SH
63
64 /**
65 * Runs the automated backups if required
66 *
67 * @global moodle_database $DB
68 */
081c914b 69 public static function run_automated_backup($rundirective = self::RUN_ON_SCHEDULE) {
bac233d3
SH
70 global $CFG, $DB;
71
72 $status = true;
73 $emailpending = false;
74 $now = time();
914442ae 75 $config = get_config('backup');
bac233d3
SH
76
77 mtrace("Checking automated backup status",'...');
081c914b 78 $state = backup_cron_automated_helper::get_automated_backup_state($rundirective);
bac233d3
SH
79 if ($state === backup_cron_automated_helper::STATE_DISABLED) {
80 mtrace('INACTIVE');
081c914b 81 return $state;
bac233d3
SH
82 } else if ($state === backup_cron_automated_helper::STATE_RUNNING) {
83 mtrace('RUNNING');
743fff98 84 if ($rundirective == self::RUN_IMMEDIATELY) {
beae4a66 85 mtrace('Automated backups are already running. If this script is being run by cron this constitues an error. You will need to increase the time between executions within cron.');
081c914b
SH
86 } else {
87 mtrace("automated backup are already running. Execution delayed");
88 }
89 return $state;
bac233d3
SH
90 } else {
91 mtrace('OK');
92 }
93 backup_cron_automated_helper::set_state_running();
94
95 mtrace("Getting admin info");
96 $admin = get_admin();
97 if (!$admin) {
98 mtrace("Error: No admin account was found");
99 $state = false;
100 }
101
102 if ($status) {
103 mtrace("Checking courses");
104 mtrace("Skipping deleted courses", '...');
105 mtrace(sprintf("%d courses", backup_cron_automated_helper::remove_deleted_courses_from_schedule()));
106 }
107
108 if ($status) {
109
110 mtrace('Running required automated backups...');
658b9372 111 cron_trace_time_and_memory();
bac233d3
SH
112
113 // This could take a while!
3ef7279f 114 core_php_time_limit::raise();
bac233d3
SH
115 raise_memory_limit(MEMORY_EXTRA);
116
d6e7a63d 117 $nextstarttime = backup_cron_automated_helper::calculate_next_automated_backup(null, $now);
bac233d3
SH
118 $showtime = "undefined";
119 if ($nextstarttime > 0) {
4d91f735 120 $showtime = date('r', $nextstarttime);
bac233d3
SH
121 }
122
123 $rs = $DB->get_recordset('course');
124 foreach ($rs as $course) {
70f210c0 125 $backupcourse = $DB->get_record('backup_courses', array('courseid' => $course->id));
bac233d3
SH
126 if (!$backupcourse) {
127 $backupcourse = new stdClass;
128 $backupcourse->courseid = $course->id;
70f210c0
MN
129 $backupcourse->laststatus = self::BACKUP_STATUS_NOTYETRUN;
130 $DB->insert_record('backup_courses', $backupcourse);
131 $backupcourse = $DB->get_record('backup_courses', array('courseid' => $course->id));
bac233d3
SH
132 }
133
1c1ae7a7 134 // The last backup is considered as successful when OK or SKIPPED.
d3ea1327
JPG
135 $lastbackupwassuccessful = ($backupcourse->laststatus == self::BACKUP_STATUS_SKIPPED ||
136 $backupcourse->laststatus == self::BACKUP_STATUS_OK) && (
137 $backupcourse->laststarttime > 0 && $backupcourse->lastendtime > 0);
1c1ae7a7 138
5043bcfa
MN
139 // Assume that we are not skipping anything.
140 $skipped = false;
141 $skippedmessage = '';
142
143 // Check if we are going to be running the backup now.
144 $shouldrunnow = (($backupcourse->nextstarttime > 0 && $backupcourse->nextstarttime < $now)
145 || $rundirective == self::RUN_IMMEDIATELY);
4d91f735 146
1c1ae7a7 147 // If config backup_auto_skip_hidden is set to true, skip courses that are not visible.
5043bcfa 148 if ($shouldrunnow && $config->backup_auto_skip_hidden) {
914442ae
JPG
149 $skipped = ($config->backup_auto_skip_hidden && !$course->visible);
150 $skippedmessage = 'Not visible';
4d91f735
FM
151 }
152
1c1ae7a7
FM
153 // If config backup_auto_skip_modif_days is set to true, skip courses
154 // that have not been modified since the number of days defined.
5043bcfa 155 if ($shouldrunnow && !$skipped && $lastbackupwassuccessful && $config->backup_auto_skip_modif_days) {
1c1ae7a7
FM
156 $timenotmodifsincedays = $now - ($config->backup_auto_skip_modif_days * DAYSECS);
157 // Check log if there were any modifications to the course content.
25239798 158 $logexists = self::is_course_modified($course->id, $timenotmodifsincedays);
1c1ae7a7 159 $skipped = ($course->timemodified <= $timenotmodifsincedays && !$logexists);
58e03319 160 $skippedmessage = 'Not modified in the past '.$config->backup_auto_skip_modif_days.' days';
e2558cd3 161 }
4d91f735 162
1c1ae7a7
FM
163 // If config backup_auto_skip_modif_prev is set to true, skip courses
164 // that have not been modified since previous backup.
5043bcfa 165 if ($shouldrunnow && !$skipped && $lastbackupwassuccessful && $config->backup_auto_skip_modif_prev) {
1c1ae7a7 166 // Check log if there were any modifications to the course content.
25239798 167 $logexists = self::is_course_modified($course->id, $backupcourse->laststarttime);
1c1ae7a7
FM
168 $skipped = ($course->timemodified <= $backupcourse->laststarttime && !$logexists);
169 $skippedmessage = 'Not modified since previous backup';
e2558cd3 170 }
4d91f735 171
5043bcfa
MN
172 // Check if the course is not scheduled to run right now.
173 if (!$shouldrunnow) {
174 $backupcourse->nextstarttime = $nextstarttime;
175 $DB->update_record('backup_courses', $backupcourse);
176 mtrace('Skipping ' . $course->fullname . ' (Not scheduled for backup until ' . $showtime . ')');
1c1ae7a7 177 } else {
694f195d
SG
178 if ($skipped) { // Must have been skipped for a reason.
179 $backupcourse->laststatus = self::BACKUP_STATUS_SKIPPED;
bac233d3 180 $backupcourse->nextstarttime = $nextstarttime;
bac233d3 181 $DB->update_record('backup_courses', $backupcourse);
694f195d
SG
182 mtrace('Skipping ' . $course->fullname . ' (' . $skippedmessage . ')');
183 mtrace('Backup of \'' . $course->fullname . '\' is scheduled on ' . $showtime);
184 } else {
185 // Backup every non-skipped courses.
186 mtrace('Backing up '.$course->fullname.'...');
187
188 // We have to send an email because we have included at least one backup.
189 $emailpending = true;
190
191 // Only make the backup if laststatus isn't 2-UNFINISHED (uncontrolled error).
192 if ($backupcourse->laststatus != self::BACKUP_STATUS_UNFINISHED) {
193 // Set laststarttime.
194 $starttime = time();
195
196 $backupcourse->laststarttime = time();
197 $backupcourse->laststatus = self::BACKUP_STATUS_UNFINISHED;
198 $DB->update_record('backup_courses', $backupcourse);
199
200 $backupcourse->laststatus = self::launch_automated_backup($course, $backupcourse->laststarttime,
201 $admin->id);
202 $backupcourse->lastendtime = time();
203 $backupcourse->nextstarttime = $nextstarttime;
204
205 $DB->update_record('backup_courses', $backupcourse);
bac233d3
SH
206 }
207 }
208
694f195d
SG
209 // Remove excess backups.
210 $removedcount = self::remove_excess_backups($course, $now);
bac233d3
SH
211 mtrace("complete - next execution: $showtime");
212 }
213 }
214 $rs->close();
215 }
216
217 //Send email to admin if necessary
218 if ($emailpending) {
219 mtrace("Sending email to admin");
220 $message = "";
221
222 $count = backup_cron_automated_helper::get_backup_status_array();
05e521c9 223 $haserrors = ($count[self::BACKUP_STATUS_ERROR] != 0 || $count[self::BACKUP_STATUS_UNFINISHED] != 0);
bac233d3 224
70f210c0
MN
225 // Build the message text.
226 // Summary.
227 $message .= get_string('summary') . "\n";
bac233d3 228 $message .= "==================================================\n";
70f210c0
MN
229 $message .= ' ' . get_string('courses') . '; ' . array_sum($count) . "\n";
230 $message .= ' ' . get_string('ok') . '; ' . $count[self::BACKUP_STATUS_OK] . "\n";
231 $message .= ' ' . get_string('skipped') . '; ' . $count[self::BACKUP_STATUS_SKIPPED] . "\n";
232 $message .= ' ' . get_string('error') . '; ' . $count[self::BACKUP_STATUS_ERROR] . "\n";
233 $message .= ' ' . get_string('unfinished') . '; ' . $count[self::BACKUP_STATUS_UNFINISHED] . "\n";
234 $message .= ' ' . get_string('warning') . '; ' . $count[self::BACKUP_STATUS_WARNING] . "\n";
235 $message .= ' ' . get_string('backupnotyetrun') . '; ' . $count[self::BACKUP_STATUS_NOTYETRUN]."\n\n";
bac233d3
SH
236
237 //Reference
238 if ($haserrors) {
239 $message .= " ".get_string('backupfailed')."\n\n";
daca599f 240 $dest_url = "$CFG->wwwroot/report/backups/index.php";
bac233d3
SH
241 $message .= " ".get_string('backuptakealook','',$dest_url)."\n\n";
242 //Set message priority
243 $admin->priority = 1;
244 //Reset unfinished to error
245 $DB->set_field('backup_courses','laststatus','0', array('laststatus'=>'2'));
246 } else {
247 $message .= " ".get_string('backupfinished')."\n";
248 }
249
250 //Build the message subject
251 $site = get_site();
a689cd1d 252 $prefix = format_string($site->shortname, true, array('context' => context_course::instance(SITEID))).": ";
bac233d3
SH
253 if ($haserrors) {
254 $prefix .= "[".strtoupper(get_string('error'))."] ";
255 }
256 $subject = $prefix.get_string('automatedbackupstatus', 'backup');
257
258 //Send the message
259 $eventdata = new stdClass();
260 $eventdata->modulename = 'moodle';
261 $eventdata->userfrom = $admin;
262 $eventdata->userto = $admin;
263 $eventdata->subject = $subject;
264 $eventdata->fullmessage = $message;
265 $eventdata->fullmessageformat = FORMAT_PLAIN;
266 $eventdata->fullmessagehtml = '';
267 $eventdata->smallmessage = '';
268
269 $eventdata->component = 'moodle';
270 $eventdata->name = 'backup';
271
272 message_send($eventdata);
273 }
274
275 //Everything is finished stop backup_auto_running
276 backup_cron_automated_helper::set_state_running(false);
277
278 mtrace('Automated backups complete.');
279
280 return $status;
281 }
282
283 /**
284 * Gets the results from the last automated backup that was run based upon
285 * the statuses of the courses that were looked at.
286 *
287 * @global moodle_database $DB
288 * @return array
289 */
290 public static function get_backup_status_array() {
291 global $DB;
292
293 $result = array(
294 self::BACKUP_STATUS_ERROR => 0,
295 self::BACKUP_STATUS_OK => 0,
296 self::BACKUP_STATUS_UNFINISHED => 0,
297 self::BACKUP_STATUS_SKIPPED => 0,
70f210c0
MN
298 self::BACKUP_STATUS_WARNING => 0,
299 self::BACKUP_STATUS_NOTYETRUN => 0
bac233d3
SH
300 );
301
4cb18cb0 302 $statuses = $DB->get_records_sql('SELECT DISTINCT bc.laststatus, COUNT(bc.courseid) AS statuscount FROM {backup_courses} bc GROUP BY bc.laststatus');
bac233d3
SH
303
304 foreach ($statuses as $status) {
305 if (empty($status->statuscount)) {
306 $status->statuscount = 0;
307 }
308 $result[(int)$status->laststatus] += $status->statuscount;
309 }
310
311 return $result;
312 }
313
314 /**
315 * Works out the next time the automated backup should be run.
316 *
e49cb057 317 * @param mixed $ignoredtimezone all settings are in server timezone!
4d91f735
FM
318 * @param int $now timestamp, should not be in the past, most likely time()
319 * @return int timestamp of the next execution at server time
bac233d3 320 */
e49cb057 321 public static function calculate_next_automated_backup($ignoredtimezone, $now) {
bac233d3 322
bac233d3 323 $config = get_config('backup');
4d91f735 324
d6e7a63d
PS
325 $backuptime = new DateTime('@' . $now);
326 $backuptime->setTimezone(core_date::get_server_timezone_object());
327 $backuptime->setTime($config->backup_auto_hour, $config->backup_auto_minute);
743fff98 328
d6e7a63d
PS
329 while ($backuptime->getTimestamp() < $now) {
330 $backuptime->add(new DateInterval('P1D'));
331 }
332
333 // Get number of days from backup date to execute backups.
334 $automateddays = substr($config->backup_auto_weekdays, $backuptime->format('w')) . $config->backup_auto_weekdays;
4d91f735 335 $daysfromnow = strpos($automateddays, "1");
d4a49ba2 336
4d91f735
FM
337 // Error, there are no days to schedule the backup for.
338 if ($daysfromnow === false) {
339 return 0;
bac233d3
SH
340 }
341
d6e7a63d
PS
342 if ($daysfromnow > 0) {
343 $backuptime->add(new DateInterval('P' . $daysfromnow . 'D'));
bac233d3
SH
344 }
345
d6e7a63d 346 return $backuptime->getTimestamp();
bac233d3
SH
347 }
348
349 /**
350 * Launches a automated backup routine for the given course
351 *
352 * @param stdClass $course
353 * @param int $starttime
354 * @param int $userid
355 * @return bool
356 */
357 public static function launch_automated_backup($course, $starttime, $userid) {
358
05e521c9 359 $outcome = self::BACKUP_STATUS_OK;
bac233d3 360 $config = get_config('backup');
b2e791d1
AA
361 $dir = $config->backup_auto_destination;
362 $storage = (int)$config->backup_auto_storage;
363
364 $bc = new backup_controller(backup::TYPE_1COURSE, $course->id, backup::FORMAT_MOODLE, backup::INTERACTIVE_NO,
365 backup::MODE_AUTOMATED, $userid);
bac233d3
SH
366
367 try {
368
b2e791d1 369 // Set the default filename.
bac233d3
SH
370 $format = $bc->get_format();
371 $type = $bc->get_type();
372 $id = $bc->get_id();
373 $users = $bc->get_plan()->get_setting('users')->get_value();
374 $anonymised = $bc->get_plan()->get_setting('anonymize')->get_value();
b2e791d1
AA
375 $bc->get_plan()->get_setting('filename')->set_value(backup_plan_dbops::get_default_backup_filename($format, $type,
376 $id, $users, $anonymised));
743fff98 377
bac233d3
SH
378 $bc->set_status(backup::STATUS_AWAITING);
379
fde4c609 380 $bc->execute_plan();
bac233d3 381 $results = $bc->get_results();
05e521c9 382 $outcome = self::outcome_from_results($results);
b2e791d1 383 $file = $results['backup_destination']; // May be empty if file already moved to target location.
80a1dfe5 384
94b8c636
FM
385 // If we need to copy the backup file to an external dir and it is not writable, change status to error.
386 // This is a feature to prevent moodledata to be filled up and break a site when the admin misconfigured
387 // the automated backups storage type and destination directory.
388 if ($storage !== 0 && (empty($dir) || !file_exists($dir) || !is_dir($dir) || !is_writable($dir))) {
389 $bc->log('Specified backup directory is not writable - ', backup::LOG_ERROR, $dir);
bac233d3 390 $dir = null;
80a1dfe5 391 $outcome = self::BACKUP_STATUS_ERROR;
bac233d3 392 }
80a1dfe5 393
b2e791d1
AA
394 // Copy file only if there was no error.
395 if ($file && !empty($dir) && $storage !== 0 && $outcome != self::BACKUP_STATUS_ERROR) {
396 $filename = backup_plan_dbops::get_default_backup_filename($format, $type, $course->id, $users, $anonymised,
397 !$config->backup_shortname);
05e521c9 398 if (!$file->copy_content_to($dir.'/'.$filename)) {
80a1dfe5
AA
399 $bc->log('Attempt to copy backup file to the specified directory failed - ',
400 backup::LOG_ERROR, $dir);
05e521c9
FM
401 $outcome = self::BACKUP_STATUS_ERROR;
402 }
403 if ($outcome != self::BACKUP_STATUS_ERROR && $storage === 1) {
80a1dfe5
AA
404 if (!$file->delete()) {
405 $outcome = self::BACKUP_STATUS_WARNING;
406 $bc->log('Attempt to delete the backup file from course automated backup area failed - ',
407 backup::LOG_WARNING, $file->get_filename());
408 }
bac233d3
SH
409 }
410 }
411
fde4c609
EL
412 } catch (moodle_exception $e) {
413 $bc->log('backup_auto_failed_on_course', backup::LOG_ERROR, $course->shortname); // Log error header.
414 $bc->log('Exception: ' . $e->errorcode, backup::LOG_ERROR, $e->a, 1); // Log original exception problem.
415 $bc->log('Debug: ' . $e->debuginfo, backup::LOG_DEBUG, null, 1); // Log original debug information.
05e521c9 416 $outcome = self::BACKUP_STATUS_ERROR;
bac233d3
SH
417 }
418
b2e791d1
AA
419 // Delete the backup file immediately if something went wrong.
420 if ($outcome === self::BACKUP_STATUS_ERROR) {
421
422 // Delete the file from file area if exists.
423 if (!empty($file)) {
424 $file->delete();
425 }
426
427 // Delete file from external storage if exists.
428 if ($storage !== 0 && !empty($filename) && file_exists($dir.'/'.$filename)) {
429 @unlink($dir.'/'.$filename);
430 }
431 }
432
ca1292e0
SH
433 $bc->destroy();
434 unset($bc);
435
fde4c609 436 return $outcome;
bac233d3
SH
437 }
438
05e521c9
FM
439 /**
440 * Returns the backup outcome by analysing its results.
441 *
442 * @param array $results returned by a backup
443 * @return int {@link self::BACKUP_STATUS_OK} and other constants
444 */
445 public static function outcome_from_results($results) {
446 $outcome = self::BACKUP_STATUS_OK;
447 foreach ($results as $code => $value) {
448 // Each possible error and warning code has to be specified in this switch
449 // which basically analyses the results to return the correct backup status.
450 switch ($code) {
451 case 'missing_files_in_pool':
452 $outcome = self::BACKUP_STATUS_WARNING;
453 break;
454 }
455 // If we found the highest error level, we exit the loop.
456 if ($outcome == self::BACKUP_STATUS_ERROR) {
457 break;
458 }
459 }
460 return $outcome;
461 }
462
bac233d3
SH
463 /**
464 * Removes deleted courses fromn the backup_courses table so that we don't
465 * waste time backing them up.
466 *
467 * @global moodle_database $DB
468 * @return int
469 */
470 public static function remove_deleted_courses_from_schedule() {
471 global $DB;
472 $skipped = 0;
473 $sql = "SELECT bc.courseid FROM {backup_courses} bc WHERE bc.courseid NOT IN (SELECT c.id FROM {course} c)";
474 $rs = $DB->get_recordset_sql($sql);
475 foreach ($rs as $deletedcourse) {
476 //Doesn't exist, so delete from backup tables
477 $DB->delete_records('backup_courses', array('courseid'=>$deletedcourse->courseid));
478 $skipped++;
479 }
480 $rs->close();
481 return $skipped;
482 }
483
484 /**
485 * Gets the state of the automated backup system.
486 *
487 * @global moodle_database $DB
488 * @return int One of self::STATE_*
489 */
081c914b 490 public static function get_automated_backup_state($rundirective = self::RUN_ON_SCHEDULE) {
bac233d3
SH
491 global $DB;
492
493 $config = get_config('backup');
081c914b 494 $active = (int)$config->backup_auto_active;
4d91f735
FM
495 $weekdays = (string)$config->backup_auto_weekdays;
496
497 // In case of automated backup also check that it is scheduled for at least one weekday.
498 if ($active === self::AUTO_BACKUP_DISABLED ||
499 ($rundirective == self::RUN_ON_SCHEDULE && $active === self::AUTO_BACKUP_MANUAL) ||
500 ($rundirective == self::RUN_ON_SCHEDULE && strpos($weekdays, '1') === false)) {
bac233d3
SH
501 return self::STATE_DISABLED;
502 } else if (!empty($config->backup_auto_running)) {
beae4a66
EL
503 // Detect if the backup_auto_running semaphore is a valid one
504 // by looking for recent activity in the backup_controllers table
505 // for backups of type backup::MODE_AUTOMATED
506 $timetosee = 60 * 90; // Time to consider in order to clean the semaphore
507 $params = array( 'purpose' => backup::MODE_AUTOMATED, 'timetolook' => (time() - $timetosee));
508 if ($DB->record_exists_select('backup_controllers',
509 "operation = 'backup' AND type = 'course' AND purpose = :purpose AND timemodified > :timetolook", $params)) {
510 return self::STATE_RUNNING; // Recent activity found, still running
511 } else {
512 // No recent activity found, let's clean the semaphore
513 mtrace('Automated backups activity not found in last ' . (int)$timetosee/60 . ' minutes. Cleaning running status');
514 backup_cron_automated_helper::set_state_running(false);
515 }
bac233d3
SH
516 }
517 return self::STATE_OK;
518 }
519
520 /**
521 * Sets the state of the automated backup system.
522 *
523 * @param bool $running
524 * @return bool
525 */
526 public static function set_state_running($running = true) {
527 if ($running === true) {
528 if (self::get_automated_backup_state() === self::STATE_RUNNING) {
932f54e9 529 throw new backup_helper_exception('backup_automated_already_running');
bac233d3
SH
530 }
531 set_config('backup_auto_running', '1', 'backup');
532 } else {
533 unset_config('backup_auto_running', 'backup');
534 }
535 return true;
536 }
537
538 /**
539 * Removes excess backups from the external system and the local file system.
540 *
db6c8b82 541 * The number of backups keep comes from $config->backup_auto_keep.
bac233d3 542 *
db6c8b82 543 * @param stdClass $course object
694f195d 544 * @param int $now execution time
bac233d3
SH
545 * @return bool
546 */
694f195d 547 public static function remove_excess_backups($course, $now) {
bac233d3
SH
548 $config = get_config('backup');
549 $keep = (int)$config->backup_auto_keep;
550 $storage = $config->backup_auto_storage;
551 $dir = $config->backup_auto_destination;
694f195d 552 $histdays = (int)$config->backup_auto_keep_days;
bac233d3 553
694f195d
SG
554 if ($keep == 0 && $histdays == 0) {
555 // Means keep all backup files and never remove backup after x days.
dbf7784e
PS
556 return true;
557 }
558
bac233d3
SH
559 if (!file_exists($dir) || !is_dir($dir) || !is_writable($dir)) {
560 $dir = null;
561 }
562
db6c8b82 563 // Clean up excess backups in the course backup filearea.
bac233d3
SH
564 if ($storage == 0 || $storage == 2) {
565 $fs = get_file_storage();
a689cd1d 566 $context = context_course::instance($course->id);
bac233d3
SH
567 $component = 'backup';
568 $filearea = 'automated';
569 $itemid = 0;
570 $files = array();
db6c8b82 571 // Store all the matching files into timemodified => stored_file array.
bac233d3 572 foreach ($fs->get_area_files($context->id, $component, $filearea, $itemid) as $file) {
bac233d3
SH
573 $files[$file->get_timemodified()] = $file;
574 }
694f195d
SG
575
576 $backupremoved = self::remove_old_backups($files, true, $now, $course->idnumber);
577 if (!$backupremoved) {
8afbe44f
EL
578 return 0;
579 }
bac233d3
SH
580 }
581
db6c8b82 582 // Clean up excess backups in the specified external directory.
bac233d3 583 if (!empty($dir) && ($storage == 1 || $storage == 2)) {
8afbe44f 584 // Calculate backup filename regex, ignoring the date/time/info parts that can be
db6c8b82 585 // variable, depending of languages, formats and automated backup settings.
425dc0ed
MN
586 $filename = backup::FORMAT_MOODLE . '-' . backup::TYPE_1COURSE . '-' . $course->id . '-';
587 $regex = '#' . preg_quote($filename, '#') . '.*\.mbz$#';
bac233d3 588
db6c8b82 589 // Store all the matching files into filename => timemodified array.
bac233d3
SH
590 $files = array();
591 foreach (scandir($dir) as $file) {
db6c8b82
FM
592 // Skip files not matching the naming convention.
593 if (!preg_match($regex, $file, $matches)) {
594 continue;
595 }
596
597 // Read the information contained in the backup itself.
598 try {
599 $bcinfo = backup_general_helper::get_backup_information_from_mbz($dir . '/' . $file);
600 } catch (backup_helper_exception $e) {
601 mtrace('Error: ' . $file . ' does not appear to be a valid backup (' . $e->errorcode . ')');
602 continue;
603 }
604
605 // Make sure this backup concerns the course and site we are looking for.
606 if ($bcinfo->format === backup::FORMAT_MOODLE &&
607 $bcinfo->type === backup::TYPE_1COURSE &&
608 $bcinfo->original_course_id == $course->id &&
609 backup_general_helper::backup_is_samesite($bcinfo)) {
610 $files[$file] = $bcinfo->backup_date;
bac233d3
SH
611 }
612 }
694f195d
SG
613
614 $backupremoved = self::remove_old_backups($files, false, $now, $course->idnumber);
615 if (!$backupremoved) {
bac233d3
SH
616 return 0;
617 }
bac233d3
SH
618 }
619
620 return true;
621 }
25239798
AA
622
623 /**
624 * Check logs to find out if a course was modified since the given time.
625 *
626 * @param int $courseid course id to check
627 * @param int $since timestamp, from which to check
628 *
629 * @return bool true if the course was modified, false otherwise. This also returns false if no readers are enabled. This is
630 * intentional, since we cannot reliably determine if any modification was made or not.
631 */
632 protected static function is_course_modified($courseid, $since) {
633 $logmang = get_log_manager();
59aebbed 634 $readers = $logmang->get_readers('core\log\sql_reader');
25239798
AA
635 $where = "courseid = :courseid and timecreated > :since and crud <> 'r'";
636 $params = array('courseid' => $courseid, 'since' => $since);
637 foreach ($readers as $reader) {
638 if ($reader->get_events_select_count($where, $params)) {
639 return true;
640 }
641 }
642 return false;
643 }
694f195d
SG
644
645 /**
646 * Removes excess and old backups from the external system or the local file system.
647 *
648 * The number of backups to keep comes from $config->backup_auto_keep and
649 * $config->backup_auto_keep_copies.
650 *
651 * @param array $files existing backups
652 * @param bool $timeinkey endicate if the backup's time is in the key of the array
653 * @param int $now starting time of the process
654 * @param string $courseidnumber courseidnumer currently processed
655 * @return bool
656 */
657 protected static function remove_old_backups($files, $timeinkey, $now, $courseidnumber) {
658 $config = get_config('backup');
659 $dir = $config->backup_auto_destination;
660 $keep = (int)$config->backup_auto_keep;
661 $histkeep = (int)$config->backup_auto_keep_copies;
662 $histdays = (int)$config->backup_auto_keep_days;
663
664 if ($timeinkey) {
665 // Sort by keys descending (newer to older filemodified).
666 krsort($files);
667 } else {
668 // Sort by values descending (newer to older filemodified).
669 arsort($files);
670 }
671
672 $tokeep = $keep;
673 $oldbackup = false;
674 if ($histdays != 0) {
675 $keepcounter = 0;
676 foreach ($files as $key => $value) {
677 if ($timeinkey) {
678 $timemodified = $key;
679 } else {
680 $timemodified = $value;
681 }
682 if ($timemodified < ($now - ($histdays * DAYSECS)) || $keepcounter >= $keep) {
683 $oldbackup = true;
684 break;
685 }
686 $keepcounter++;
687 }
688
689 if ($keepcounter < $histkeep) {
690 $tokeep = $histkeep;
691 } else {
692 $tokeep = $keepcounter;
693 }
694 }
695
696 if (count($files) <= $tokeep) {
697 // There are less matching files than the desired number to keep there is nothing to clean up.
698 return false;
699 }
700
701 $remove = array_splice($files, $tokeep);
702 if ($timeinkey) {
703 foreach ($remove as $file) {
704 $file->delete();
705 }
706 } else {
707 foreach (array_keys($remove) as $file) {
708 unlink($dir . '/' . $file);
709 }
710 }
711
712 if ($oldbackup) {
713 mtrace('Course '. $courseidnumber. ' removed '.count($remove).' old backup file(s) from the automated filearea');
714 }
715
716 return true;
717 }
02cf6919 718}