Merge branch 'MDL-28531-master' of git://github.com/FMCorz/moodle
[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
18
19/**
20 * Utility helper for automated backups run through cron.
21 *
22 * @package core
23 * @subpackage backup
24 * @copyright 2010 Sam Hemelryk
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 */
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 */
32abstract 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
081c914b
SH
50 /** Run if required by the schedule set in config. Default. **/
51 const RUN_ON_SCHEDULE = 0;
743fff98
PS
52 /** Run immediately. **/
53 const RUN_IMMEDIATELY = 1;
081c914b
SH
54
55 const AUTO_BACKUP_DISABLED = 0;
56 const AUTO_BACKUP_ENABLED = 1;
57 const AUTO_BACKUP_MANUAL = 2;
bac233d3
SH
58
59 /**
60 * Runs the automated backups if required
61 *
62 * @global moodle_database $DB
63 */
081c914b 64 public static function run_automated_backup($rundirective = self::RUN_ON_SCHEDULE) {
bac233d3
SH
65 global $CFG, $DB;
66
67 $status = true;
68 $emailpending = false;
69 $now = time();
70
71 mtrace("Checking automated backup status",'...');
081c914b 72 $state = backup_cron_automated_helper::get_automated_backup_state($rundirective);
bac233d3
SH
73 if ($state === backup_cron_automated_helper::STATE_DISABLED) {
74 mtrace('INACTIVE');
081c914b 75 return $state;
bac233d3
SH
76 } else if ($state === backup_cron_automated_helper::STATE_RUNNING) {
77 mtrace('RUNNING');
743fff98 78 if ($rundirective == self::RUN_IMMEDIATELY) {
beae4a66 79 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
80 } else {
81 mtrace("automated backup are already running. Execution delayed");
82 }
83 return $state;
bac233d3
SH
84 } else {
85 mtrace('OK');
86 }
87 backup_cron_automated_helper::set_state_running();
88
89 mtrace("Getting admin info");
90 $admin = get_admin();
91 if (!$admin) {
92 mtrace("Error: No admin account was found");
93 $state = false;
94 }
95
96 if ($status) {
97 mtrace("Checking courses");
98 mtrace("Skipping deleted courses", '...');
99 mtrace(sprintf("%d courses", backup_cron_automated_helper::remove_deleted_courses_from_schedule()));
100 }
101
102 if ($status) {
103
104 mtrace('Running required automated backups...');
105
106 // This could take a while!
107 @set_time_limit(0);
108 raise_memory_limit(MEMORY_EXTRA);
109
110 $nextstarttime = backup_cron_automated_helper::calculate_next_automated_backup($admin->timezone, $now);
111 $showtime = "undefined";
112 if ($nextstarttime > 0) {
3d515f48 113 $showtime = date('r', $nextstarttime);
bac233d3
SH
114 }
115
116 $rs = $DB->get_recordset('course');
117 foreach ($rs as $course) {
118 $backupcourse = $DB->get_record('backup_courses', array('courseid'=>$course->id));
119 if (!$backupcourse) {
120 $backupcourse = new stdClass;
121 $backupcourse->courseid = $course->id;
122 $DB->insert_record('backup_courses',$backupcourse);
123 $backupcourse = $DB->get_record('backup_courses', array('courseid'=>$course->id));
124 }
125
e2558cd3 126 // Skip courses that do not yet need backup
3d515f48
FM
127 $skipped = !(($backupcourse->nextstarttime > 0 && $backupcourse->nextstarttime < $now) || $rundirective == self::RUN_IMMEDIATELY);
128 if ($skipped && $backupcourse->nextstarttime != $nextstarttime) {
129 $backupcourse->nextstarttime = $nextstarttime;
130 $backupcourse->laststatus = backup_cron_automated_helper::BACKUP_STATUS_SKIPPED;
131 $DB->update_record('backup_courses', $backupcourse);
132 mtrace('Backup of \'' . $course->fullname . '\' is scheduled on ' . $showtime);
133 }
134
bac233d3 135 // Skip backup of unavailable courses that have remained unmodified in a month
e2558cd3
MG
136 if (!$skipped && empty($course->visible) && ($now - $course->timemodified) > 31*24*60*60) { //Hidden + settings were unmodified last month
137 //Check log if there were any modifications to the course content
a6c124f5 138 $sqlwhere = "course=:courseid AND time>:time AND ". $DB->sql_like('action', ':action', false, true, true);
e2558cd3 139 $params = array('courseid' => $course->id, 'time' => $now-31*24*60*60, 'action' => '%view%');
a6c124f5
MG
140 $logexists = $DB->record_exists_select('log', $sqlwhere, $params);
141 if (!$logexists) {
e2558cd3
MG
142 $backupcourse->laststatus = backup_cron_automated_helper::BACKUP_STATUS_SKIPPED;
143 $backupcourse->nextstarttime = $nextstarttime;
144 $DB->update_record('backup_courses', $backupcourse);
145 mtrace('Skipping unchanged course '.$course->fullname);
146 $skipped = true;
147 }
148 }
3d515f48 149
e2558cd3
MG
150 //Now we backup every non-skipped course
151 if (!$skipped) {
3d515f48 152 mtrace('Backing up '.$course->fullname.'...');
bac233d3
SH
153
154 //We have to send a email because we have included at least one backup
155 $emailpending = true;
743fff98 156
bac233d3
SH
157 //Only make the backup if laststatus isn't 2-UNFINISHED (uncontrolled error)
158 if ($backupcourse->laststatus != 2) {
159 //Set laststarttime
160 $starttime = time();
161
162 $backupcourse->laststarttime = time();
163 $backupcourse->laststatus = backup_cron_automated_helper::BACKUP_STATUS_UNFINISHED;
164 $DB->update_record('backup_courses', $backupcourse);
165
166 $backupcourse->laststatus = backup_cron_automated_helper::launch_automated_backup($course, $backupcourse->laststarttime, $admin->id);
167 $backupcourse->lastendtime = time();
168 $backupcourse->nextstarttime = $nextstarttime;
169
170 $DB->update_record('backup_courses', $backupcourse);
171
172 if ($backupcourse->laststatus) {
173 // Clean up any excess course backups now that we have
174 // taken a successful backup.
175 $removedcount = backup_cron_automated_helper::remove_excess_backups($course);
176 }
177 }
178
179 mtrace("complete - next execution: $showtime");
180 }
181 }
182 $rs->close();
183 }
184
185 //Send email to admin if necessary
186 if ($emailpending) {
187 mtrace("Sending email to admin");
188 $message = "";
189
190 $count = backup_cron_automated_helper::get_backup_status_array();
191 $haserrors = ($count[backup_cron_automated_helper::BACKUP_STATUS_ERROR] != 0 || $count[backup_cron_automated_helper::BACKUP_STATUS_UNFINISHED] != 0);
192
193 //Build the message text
194 //Summary
195 $message .= get_string('summary')."\n";
196 $message .= "==================================================\n";
197 $message .= " ".get_string('courses').": ".array_sum($count)."\n";
198 $message .= " ".get_string('ok').": ".$count[backup_cron_automated_helper::BACKUP_STATUS_OK]."\n";
199 $message .= " ".get_string('skipped').": ".$count[backup_cron_automated_helper::BACKUP_STATUS_SKIPPED]."\n";
200 $message .= " ".get_string('error').": ".$count[backup_cron_automated_helper::BACKUP_STATUS_ERROR]."\n";
201 $message .= " ".get_string('unfinished').": ".$count[backup_cron_automated_helper::BACKUP_STATUS_UNFINISHED]."\n\n";
202
203 //Reference
204 if ($haserrors) {
205 $message .= " ".get_string('backupfailed')."\n\n";
daca599f 206 $dest_url = "$CFG->wwwroot/report/backups/index.php";
bac233d3
SH
207 $message .= " ".get_string('backuptakealook','',$dest_url)."\n\n";
208 //Set message priority
209 $admin->priority = 1;
210 //Reset unfinished to error
211 $DB->set_field('backup_courses','laststatus','0', array('laststatus'=>'2'));
212 } else {
213 $message .= " ".get_string('backupfinished')."\n";
214 }
215
216 //Build the message subject
217 $site = get_site();
a689cd1d 218 $prefix = format_string($site->shortname, true, array('context' => context_course::instance(SITEID))).": ";
bac233d3
SH
219 if ($haserrors) {
220 $prefix .= "[".strtoupper(get_string('error'))."] ";
221 }
222 $subject = $prefix.get_string('automatedbackupstatus', 'backup');
223
224 //Send the message
225 $eventdata = new stdClass();
226 $eventdata->modulename = 'moodle';
227 $eventdata->userfrom = $admin;
228 $eventdata->userto = $admin;
229 $eventdata->subject = $subject;
230 $eventdata->fullmessage = $message;
231 $eventdata->fullmessageformat = FORMAT_PLAIN;
232 $eventdata->fullmessagehtml = '';
233 $eventdata->smallmessage = '';
234
235 $eventdata->component = 'moodle';
236 $eventdata->name = 'backup';
237
238 message_send($eventdata);
239 }
240
241 //Everything is finished stop backup_auto_running
242 backup_cron_automated_helper::set_state_running(false);
243
244 mtrace('Automated backups complete.');
245
246 return $status;
247 }
248
249 /**
250 * Gets the results from the last automated backup that was run based upon
251 * the statuses of the courses that were looked at.
252 *
253 * @global moodle_database $DB
254 * @return array
255 */
256 public static function get_backup_status_array() {
257 global $DB;
258
259 $result = array(
260 self::BACKUP_STATUS_ERROR => 0,
261 self::BACKUP_STATUS_OK => 0,
262 self::BACKUP_STATUS_UNFINISHED => 0,
263 self::BACKUP_STATUS_SKIPPED => 0,
264 );
265
266 $statuses = $DB->get_records_sql('SELECT DISTINCT bc.laststatus, COUNT(bc.courseid) statuscount FROM {backup_courses} bc GROUP BY bc.laststatus');
267
268 foreach ($statuses as $status) {
269 if (empty($status->statuscount)) {
270 $status->statuscount = 0;
271 }
272 $result[(int)$status->laststatus] += $status->statuscount;
273 }
274
275 return $result;
276 }
277
278 /**
279 * Works out the next time the automated backup should be run.
280 *
3d515f48
FM
281 * @param mixed $timezone user timezone
282 * @param int $now timestamp, should not be in the past, most likely time()
283 * @return int timestamp of the next execution at server time
bac233d3
SH
284 */
285 public static function calculate_next_automated_backup($timezone, $now) {
286
3d515f48 287 $result = 0;
bac233d3 288 $config = get_config('backup');
3d515f48
FM
289 $autohour = $config->backup_auto_hour;
290 $automin = $config->backup_auto_minute;
291
292 // Gets the user time relatively to the server time.
bac233d3 293 $date = usergetdate($now, $timezone);
3d515f48
FM
294 $usertime = mktime($date['hours'], $date['minutes'], $date['seconds'], $date['mon'], $date['mday'], $date['year']);
295 $diff = $now - $usertime;
743fff98 296
3d515f48 297 // Get number of days (from user's today) to execute backups.
d4a49ba2 298 $automateddays = substr($config->backup_auto_weekdays, $date['wday']) . $config->backup_auto_weekdays;
3d515f48 299 $daysfromnow = strpos($automateddays, "1");
d4a49ba2 300
3d515f48
FM
301 // Error, there are no days to schedule the backup for.
302 if ($daysfromnow === false) {
303 return 0;
bac233d3
SH
304 }
305
3d515f48
FM
306 // Checks if the date would happen in the future (of the user).
307 $userresult = mktime($autohour, $automin, 0, $date['mon'], $date['mday'] + $daysfromnow, $date['year']);
308 if ($userresult <= $usertime) {
309 // If not, we skip the first scheduled day, that should fix it.
310 $daysfromnow = strpos($automateddays, "1", 1);
311 $userresult = mktime($autohour, $automin, 0, $date['mon'], $date['mday'] + $daysfromnow, $date['year']);
bac233d3
SH
312 }
313
3d515f48
FM
314 // Now we generate the time relative to the server.
315 $result = $userresult + $diff;
316
317 // If that time is past, call the function recursively to obtain the next valid day.
318 if ($result <= $now) {
319 // Checking time() in here works, but makes PHPUnit Tests extremely hard to predict.
320 // $now should never be earlier than time() anyway...
321 $result = self::calculate_next_automated_backup($timezone, $now + DAYSECS);
bac233d3
SH
322 }
323
324 return $result;
325 }
326
327 /**
328 * Launches a automated backup routine for the given course
329 *
330 * @param stdClass $course
331 * @param int $starttime
332 * @param int $userid
333 * @return bool
334 */
335 public static function launch_automated_backup($course, $starttime, $userid) {
336
fde4c609 337 $outcome = true;
bac233d3
SH
338 $config = get_config('backup');
339 $bc = new backup_controller(backup::TYPE_1COURSE, $course->id, backup::FORMAT_MOODLE, backup::INTERACTIVE_NO, backup::MODE_AUTOMATED, $userid);
340
341 try {
342
343 $settings = array(
344 'users' => 'backup_auto_users',
0d5e5644 345 'role_assignments' => 'backup_auto_role_assignments',
bac233d3
SH
346 'activities' => 'backup_auto_activities',
347 'blocks' => 'backup_auto_blocks',
348 'filters' => 'backup_auto_filters',
349 'comments' => 'backup_auto_comments',
350 'completion_information' => 'backup_auto_userscompletion',
351 'logs' => 'backup_auto_logs',
352 'histories' => 'backup_auto_histories'
353 );
354 foreach ($settings as $setting => $configsetting) {
355 if ($bc->get_plan()->setting_exists($setting)) {
356 $bc->get_plan()->get_setting($setting)->set_value($config->{$configsetting});
357 }
358 }
359
360 // Set the default filename
361 $format = $bc->get_format();
362 $type = $bc->get_type();
363 $id = $bc->get_id();
364 $users = $bc->get_plan()->get_setting('users')->get_value();
365 $anonymised = $bc->get_plan()->get_setting('anonymize')->get_value();
366 $bc->get_plan()->get_setting('filename')->set_value(backup_plan_dbops::get_default_backup_filename($format, $type, $id, $users, $anonymised));
743fff98 367
bac233d3
SH
368 $bc->set_status(backup::STATUS_AWAITING);
369
fde4c609 370 $bc->execute_plan();
bac233d3 371 $results = $bc->get_results();
096702f6 372 $file = $results['backup_destination']; // may be empty if file already moved to target location
bac233d3
SH
373 $dir = $config->backup_auto_destination;
374 $storage = (int)$config->backup_auto_storage;
375 if (!file_exists($dir) || !is_dir($dir) || !is_writable($dir)) {
376 $dir = null;
377 }
096702f6 378 if ($file && !empty($dir) && $storage !== 0) {
66150286 379 $filename = backup_plan_dbops::get_default_backup_filename($format, $type, $course->id, $users, $anonymised, !$config->backup_shortname);
bac233d3
SH
380 $outcome = $file->copy_content_to($dir.'/'.$filename);
381 if ($outcome && $storage === 1) {
382 $file->delete();
383 }
384 }
385
fde4c609
EL
386 } catch (moodle_exception $e) {
387 $bc->log('backup_auto_failed_on_course', backup::LOG_ERROR, $course->shortname); // Log error header.
388 $bc->log('Exception: ' . $e->errorcode, backup::LOG_ERROR, $e->a, 1); // Log original exception problem.
389 $bc->log('Debug: ' . $e->debuginfo, backup::LOG_DEBUG, null, 1); // Log original debug information.
ca1292e0 390 $outcome = false;
bac233d3
SH
391 }
392
ca1292e0
SH
393 $bc->destroy();
394 unset($bc);
395
fde4c609 396 return $outcome;
bac233d3
SH
397 }
398
bac233d3
SH
399 /**
400 * Removes deleted courses fromn the backup_courses table so that we don't
401 * waste time backing them up.
402 *
403 * @global moodle_database $DB
404 * @return int
405 */
406 public static function remove_deleted_courses_from_schedule() {
407 global $DB;
408 $skipped = 0;
409 $sql = "SELECT bc.courseid FROM {backup_courses} bc WHERE bc.courseid NOT IN (SELECT c.id FROM {course} c)";
410 $rs = $DB->get_recordset_sql($sql);
411 foreach ($rs as $deletedcourse) {
412 //Doesn't exist, so delete from backup tables
413 $DB->delete_records('backup_courses', array('courseid'=>$deletedcourse->courseid));
414 $skipped++;
415 }
416 $rs->close();
417 return $skipped;
418 }
419
420 /**
421 * Gets the state of the automated backup system.
422 *
423 * @global moodle_database $DB
424 * @return int One of self::STATE_*
425 */
081c914b 426 public static function get_automated_backup_state($rundirective = self::RUN_ON_SCHEDULE) {
bac233d3
SH
427 global $DB;
428
429 $config = get_config('backup');
081c914b 430 $active = (int)$config->backup_auto_active;
3d515f48
FM
431 $weekdays = (string)$config->backup_auto_weekdays;
432
433 // In case of automated backup also check that it is scheduled for at least one weekday.
434 if ($active === self::AUTO_BACKUP_DISABLED ||
435 ($rundirective == self::RUN_ON_SCHEDULE && $active === self::AUTO_BACKUP_MANUAL) ||
436 ($rundirective == self::RUN_ON_SCHEDULE && strpos($weekdays, '1') === false)) {
bac233d3
SH
437 return self::STATE_DISABLED;
438 } else if (!empty($config->backup_auto_running)) {
beae4a66
EL
439 // Detect if the backup_auto_running semaphore is a valid one
440 // by looking for recent activity in the backup_controllers table
441 // for backups of type backup::MODE_AUTOMATED
442 $timetosee = 60 * 90; // Time to consider in order to clean the semaphore
443 $params = array( 'purpose' => backup::MODE_AUTOMATED, 'timetolook' => (time() - $timetosee));
444 if ($DB->record_exists_select('backup_controllers',
445 "operation = 'backup' AND type = 'course' AND purpose = :purpose AND timemodified > :timetolook", $params)) {
446 return self::STATE_RUNNING; // Recent activity found, still running
447 } else {
448 // No recent activity found, let's clean the semaphore
449 mtrace('Automated backups activity not found in last ' . (int)$timetosee/60 . ' minutes. Cleaning running status');
450 backup_cron_automated_helper::set_state_running(false);
451 }
bac233d3
SH
452 }
453 return self::STATE_OK;
454 }
455
456 /**
457 * Sets the state of the automated backup system.
458 *
459 * @param bool $running
460 * @return bool
461 */
462 public static function set_state_running($running = true) {
463 if ($running === true) {
464 if (self::get_automated_backup_state() === self::STATE_RUNNING) {
465 throw new backup_exception('backup_automated_already_running');
466 }
467 set_config('backup_auto_running', '1', 'backup');
468 } else {
469 unset_config('backup_auto_running', 'backup');
470 }
471 return true;
472 }
473
474 /**
475 * Removes excess backups from the external system and the local file system.
476 *
477 * The number of backups keep comes from $config->backup_auto_keep
478 *
479 * @param stdClass $course
480 * @return bool
481 */
482 public static function remove_excess_backups($course) {
483 $config = get_config('backup');
484 $keep = (int)$config->backup_auto_keep;
485 $storage = $config->backup_auto_storage;
486 $dir = $config->backup_auto_destination;
487
dbf7784e
PS
488 if ($keep == 0) {
489 // means keep all backup files
490 return true;
491 }
492
6f3451e5 493 $backupword = str_replace(' ', '_', textlib::strtolower(get_string('backupfilename')));
bac233d3
SH
494 $backupword = trim(clean_filename($backupword), '_');
495
496 if (!file_exists($dir) || !is_dir($dir) || !is_writable($dir)) {
497 $dir = null;
498 }
499
500 // Clean up excess backups in the course backup filearea
501 if ($storage == 0 || $storage == 2) {
502 $fs = get_file_storage();
a689cd1d 503 $context = context_course::instance($course->id);
bac233d3
SH
504 $component = 'backup';
505 $filearea = 'automated';
506 $itemid = 0;
507 $files = array();
8afbe44f 508 // Store all the matching files into timemodified => stored_file array
bac233d3
SH
509 foreach ($fs->get_area_files($context->id, $component, $filearea, $itemid) as $file) {
510 if (strpos($file->get_filename(), $backupword) !== 0) {
511 continue;
512 }
513 $files[$file->get_timemodified()] = $file;
514 }
8afbe44f
EL
515 if (count($files) <= $keep) {
516 // There are less matching files than the desired number to keep
517 // do there is nothing to clean up.
518 return 0;
519 }
520 // Sort by keys descending (newer to older filemodified)
521 krsort($files);
bac233d3
SH
522 $remove = array_splice($files, $keep);
523 foreach ($remove as $file) {
524 $file->delete();
525 }
8afbe44f 526 //mtrace('Removed '.count($remove).' old backup file(s) from the automated filearea');
bac233d3
SH
527 }
528
529 // Clean up excess backups in the specified external directory
530 if (!empty($dir) && ($storage == 1 || $storage == 2)) {
8afbe44f
EL
531 // Calculate backup filename regex, ignoring the date/time/info parts that can be
532 // variable, depending of languages, formats and automated backup settings
bac233d3 533 $filename = $backupword . '-' . backup::FORMAT_MOODLE . '-' . backup::TYPE_1COURSE . '-' .$course->id . '-';
8afbe44f 534 $regex = '#^'.preg_quote($filename, '#').'.*\.mbz$#';
bac233d3 535
8afbe44f 536 // Store all the matching files into fullpath => timemodified array
bac233d3
SH
537 $files = array();
538 foreach (scandir($dir) as $file) {
539 if (preg_match($regex, $file, $matches)) {
8afbe44f 540 $files[$file] = filemtime($dir . '/' . $file);
bac233d3
SH
541 }
542 }
543 if (count($files) <= $keep) {
544 // There are less matching files than the desired number to keep
545 // do there is nothing to clean up.
546 return 0;
547 }
8afbe44f 548 // Sort by values descending (newer to older filemodified)
bac233d3
SH
549 arsort($files);
550 $remove = array_splice($files, $keep);
551 foreach (array_keys($remove) as $file) {
8afbe44f 552 unlink($dir . '/' . $file);
bac233d3
SH
553 }
554 //mtrace('Removed '.count($remove).' old backup file(s) from external directory');
555 }
556
557 return true;
558 }
02cf6919 559}