MDL-26333 dml - fix test (AS not in tables/views)
[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;
52 /** Run immediatly. **/
53 const RUN_IMMEDIATLY = 1;
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');
081c914b
SH
78 if ($rundirective == self::RUN_IMMEDIATLY) {
79 mtrace('automated backups are already. If this script is being run by cron this constitues an error. You will need to increase the time between executions within cron.');
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) {
113 $showtime = userdate($nextstarttime,"",$admin->timezone);
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
126 // Skip backup of unavailable courses that have remained unmodified in a month
127 $skipped = false;
128 if (empty($course->visible) && ($now - $course->timemodified) > 31*24*60*60) { //Hidden + unmodified last month
129 $backupcourse->laststatus = backup_cron_automated_helper::BACKUP_STATUS_SKIPPED;
130 $DB->update_record('backup_courses', $backupcourse);
081c914b 131 mtrace('Skipping unchanged course '.$course->fullname);
bac233d3 132 $skipped = true;
081c914b 133 } else if (($backupcourse->nextstarttime >= 0 && $backupcourse->nextstarttime < $now) || $rundirective == self::RUN_IMMEDIATLY) {
bac233d3
SH
134 mtrace('Backing up '.$course->fullname, '...');
135
136 //We have to send a email because we have included at least one backup
137 $emailpending = true;
138
139 //Only make the backup if laststatus isn't 2-UNFINISHED (uncontrolled error)
140 if ($backupcourse->laststatus != 2) {
141 //Set laststarttime
142 $starttime = time();
143
144 $backupcourse->laststarttime = time();
145 $backupcourse->laststatus = backup_cron_automated_helper::BACKUP_STATUS_UNFINISHED;
146 $DB->update_record('backup_courses', $backupcourse);
147
148 $backupcourse->laststatus = backup_cron_automated_helper::launch_automated_backup($course, $backupcourse->laststarttime, $admin->id);
149 $backupcourse->lastendtime = time();
150 $backupcourse->nextstarttime = $nextstarttime;
151
152 $DB->update_record('backup_courses', $backupcourse);
153
154 if ($backupcourse->laststatus) {
155 // Clean up any excess course backups now that we have
156 // taken a successful backup.
157 $removedcount = backup_cron_automated_helper::remove_excess_backups($course);
158 }
159 }
160
161 mtrace("complete - next execution: $showtime");
162 }
163 }
164 $rs->close();
165 }
166
167 //Send email to admin if necessary
168 if ($emailpending) {
169 mtrace("Sending email to admin");
170 $message = "";
171
172 $count = backup_cron_automated_helper::get_backup_status_array();
173 $haserrors = ($count[backup_cron_automated_helper::BACKUP_STATUS_ERROR] != 0 || $count[backup_cron_automated_helper::BACKUP_STATUS_UNFINISHED] != 0);
174
175 //Build the message text
176 //Summary
177 $message .= get_string('summary')."\n";
178 $message .= "==================================================\n";
179 $message .= " ".get_string('courses').": ".array_sum($count)."\n";
180 $message .= " ".get_string('ok').": ".$count[backup_cron_automated_helper::BACKUP_STATUS_OK]."\n";
181 $message .= " ".get_string('skipped').": ".$count[backup_cron_automated_helper::BACKUP_STATUS_SKIPPED]."\n";
182 $message .= " ".get_string('error').": ".$count[backup_cron_automated_helper::BACKUP_STATUS_ERROR]."\n";
183 $message .= " ".get_string('unfinished').": ".$count[backup_cron_automated_helper::BACKUP_STATUS_UNFINISHED]."\n\n";
184
185 //Reference
186 if ($haserrors) {
187 $message .= " ".get_string('backupfailed')."\n\n";
188 $dest_url = "$CFG->wwwroot/$CFG->admin/report/backups/index.php";
189 $message .= " ".get_string('backuptakealook','',$dest_url)."\n\n";
190 //Set message priority
191 $admin->priority = 1;
192 //Reset unfinished to error
193 $DB->set_field('backup_courses','laststatus','0', array('laststatus'=>'2'));
194 } else {
195 $message .= " ".get_string('backupfinished')."\n";
196 }
197
198 //Build the message subject
199 $site = get_site();
200 $prefix = $site->shortname.": ";
201 if ($haserrors) {
202 $prefix .= "[".strtoupper(get_string('error'))."] ";
203 }
204 $subject = $prefix.get_string('automatedbackupstatus', 'backup');
205
206 //Send the message
207 $eventdata = new stdClass();
208 $eventdata->modulename = 'moodle';
209 $eventdata->userfrom = $admin;
210 $eventdata->userto = $admin;
211 $eventdata->subject = $subject;
212 $eventdata->fullmessage = $message;
213 $eventdata->fullmessageformat = FORMAT_PLAIN;
214 $eventdata->fullmessagehtml = '';
215 $eventdata->smallmessage = '';
216
217 $eventdata->component = 'moodle';
218 $eventdata->name = 'backup';
219
220 message_send($eventdata);
221 }
222
223 //Everything is finished stop backup_auto_running
224 backup_cron_automated_helper::set_state_running(false);
225
226 mtrace('Automated backups complete.');
227
228 return $status;
229 }
230
231 /**
232 * Gets the results from the last automated backup that was run based upon
233 * the statuses of the courses that were looked at.
234 *
235 * @global moodle_database $DB
236 * @return array
237 */
238 public static function get_backup_status_array() {
239 global $DB;
240
241 $result = array(
242 self::BACKUP_STATUS_ERROR => 0,
243 self::BACKUP_STATUS_OK => 0,
244 self::BACKUP_STATUS_UNFINISHED => 0,
245 self::BACKUP_STATUS_SKIPPED => 0,
246 );
247
248 $statuses = $DB->get_records_sql('SELECT DISTINCT bc.laststatus, COUNT(bc.courseid) statuscount FROM {backup_courses} bc GROUP BY bc.laststatus');
249
250 foreach ($statuses as $status) {
251 if (empty($status->statuscount)) {
252 $status->statuscount = 0;
253 }
254 $result[(int)$status->laststatus] += $status->statuscount;
255 }
256
257 return $result;
258 }
259
260 /**
261 * Works out the next time the automated backup should be run.
262 *
263 * @param mixed $timezone
264 * @param int $now
265 * @return int
266 */
267 public static function calculate_next_automated_backup($timezone, $now) {
268
269 $result = -1;
270 $config = get_config('backup');
271 $midnight = usergetmidnight($now, $timezone);
272 $date = usergetdate($now, $timezone);
273
274 //Get number of days (from today) to execute backups
275 $automateddays = substr($config->backup_auto_weekdays,$date['wday']) . $config->backup_auto_weekdays;
276 $daysfromtoday = strpos($automateddays, "1");
277 if (empty($daysfromtoday)) {
278 $daysfromtoday = 1;
279 }
280
281 //If some day has been found
282 if ($daysfromtoday !== false) {
283 //Calculate distance
284 $dist = ($daysfromtoday * 86400) + //Days distance
285 ($config->backup_auto_hour * 3600) + //Hours distance
286 ($config->backup_auto_minute * 60); //Minutes distance
287 $result = $midnight + $dist;
288 }
289
290 //If that time is past, call the function recursively to obtain the next valid day
291 if ($result > 0 && $result < time()) {
292 $result = self::calculate_next_automated_backup($timezone, $result);
293 }
294
295 return $result;
296 }
297
298 /**
299 * Launches a automated backup routine for the given course
300 *
301 * @param stdClass $course
302 * @param int $starttime
303 * @param int $userid
304 * @return bool
305 */
306 public static function launch_automated_backup($course, $starttime, $userid) {
307
308 $config = get_config('backup');
309 $bc = new backup_controller(backup::TYPE_1COURSE, $course->id, backup::FORMAT_MOODLE, backup::INTERACTIVE_NO, backup::MODE_AUTOMATED, $userid);
310
311 try {
312
313 $settings = array(
314 'users' => 'backup_auto_users',
315 'role_assignments' => 'backup_auto_users',
316 'user_files' => 'backup_auto_user_files',
317 'activities' => 'backup_auto_activities',
318 'blocks' => 'backup_auto_blocks',
319 'filters' => 'backup_auto_filters',
320 'comments' => 'backup_auto_comments',
321 'completion_information' => 'backup_auto_userscompletion',
322 'logs' => 'backup_auto_logs',
323 'histories' => 'backup_auto_histories'
324 );
325 foreach ($settings as $setting => $configsetting) {
326 if ($bc->get_plan()->setting_exists($setting)) {
327 $bc->get_plan()->get_setting($setting)->set_value($config->{$configsetting});
328 }
329 }
330
331 // Set the default filename
332 $format = $bc->get_format();
333 $type = $bc->get_type();
334 $id = $bc->get_id();
335 $users = $bc->get_plan()->get_setting('users')->get_value();
336 $anonymised = $bc->get_plan()->get_setting('anonymize')->get_value();
337 $bc->get_plan()->get_setting('filename')->set_value(backup_plan_dbops::get_default_backup_filename($format, $type, $id, $users, $anonymised));
338
339 $bc->set_status(backup::STATUS_AWAITING);
340
341 $outcome = $bc->execute_plan();
342 $results = $bc->get_results();
343 $file = $results['backup_destination'];
344 $dir = $config->backup_auto_destination;
345 $storage = (int)$config->backup_auto_storage;
346 if (!file_exists($dir) || !is_dir($dir) || !is_writable($dir)) {
347 $dir = null;
348 }
349 if (!empty($dir) && $storage !== 0) {
350 $filename = self::get_external_filename($course->id, $format, $type, $users, $anonymised);
351 $outcome = $file->copy_content_to($dir.'/'.$filename);
352 if ($outcome && $storage === 1) {
353 $file->delete();
354 }
355 }
356
ca1292e0 357 $outcome = true;
bac233d3
SH
358 } catch (backup_exception $e) {
359 $bc->log('backup_auto_failed_on_course', backup::LOG_WARNING, $course->shortname);
ca1292e0 360 $outcome = false;
bac233d3
SH
361 }
362
ca1292e0
SH
363 $bc->destroy();
364 unset($bc);
365
bac233d3
SH
366 return true;
367 }
368
369 /**
370 * Gets the filename to use for the backup when it is being moved to an
371 * external location.
372 *
373 * Note: we use the course id in the filename rather than the course shortname
374 * because it may contain UTF-8 characters that could cause problems for the
375 * recieving filesystem.
376 *
377 * @param int $courseid
378 * @param string $format One of backup::FORMAT_
379 * @param string $type One of backup::TYPE_
380 * @param bool $users Should be true is users were included in the backup
381 * @param bool $anonymised Should be true is user information was anonymized.
382 * @return string The filename to use
383 */
384 public static function get_external_filename($courseid, $format, $type, $users, $anonymised) {
385 $backupword = str_replace(' ', '_', moodle_strtolower(get_string('backupfilename')));
386 $backupword = trim(clean_filename($backupword), '_');
387 // Calculate date
388 $backupdateformat = str_replace(' ', '_', get_string('backupnameformat', 'langconfig'));
389 $date = userdate(time(), $backupdateformat, 99, false);
390 $date = moodle_strtolower(trim(clean_filename($date), '_'));
391 // Calculate info
392 $info = '';
393 if (!$users) {
394 $info = 'nu';
395 } else if ($anonymised) {
396 $info = 'an';
397 }
398 return $backupword.'-'.$format.'-'.$type.'-'.$courseid.'-'.$date.'-'.$info.'.mbz';
399 }
400
401 /**
402 * Removes deleted courses fromn the backup_courses table so that we don't
403 * waste time backing them up.
404 *
405 * @global moodle_database $DB
406 * @return int
407 */
408 public static function remove_deleted_courses_from_schedule() {
409 global $DB;
410 $skipped = 0;
411 $sql = "SELECT bc.courseid FROM {backup_courses} bc WHERE bc.courseid NOT IN (SELECT c.id FROM {course} c)";
412 $rs = $DB->get_recordset_sql($sql);
413 foreach ($rs as $deletedcourse) {
414 //Doesn't exist, so delete from backup tables
415 $DB->delete_records('backup_courses', array('courseid'=>$deletedcourse->courseid));
416 $skipped++;
417 }
418 $rs->close();
419 return $skipped;
420 }
421
422 /**
423 * Gets the state of the automated backup system.
424 *
425 * @global moodle_database $DB
426 * @return int One of self::STATE_*
427 */
081c914b 428 public static function get_automated_backup_state($rundirective = self::RUN_ON_SCHEDULE) {
bac233d3
SH
429 global $DB;
430
431 $config = get_config('backup');
081c914b
SH
432 $active = (int)$config->backup_auto_active;
433 if ($active === self::AUTO_BACKUP_DISABLED || ($rundirective == self::RUN_ON_SCHEDULE && $active === self::AUTO_BACKUP_MANUAL)) {
bac233d3
SH
434 return self::STATE_DISABLED;
435 } else if (!empty($config->backup_auto_running)) {
436 // TODO: We should find some way of checking whether the automated
437 // backup has infact finished. In 1.9 this was being done by checking
438 // the log entries.
439 return self::STATE_RUNNING;
440 }
441 return self::STATE_OK;
442 }
443
444 /**
445 * Sets the state of the automated backup system.
446 *
447 * @param bool $running
448 * @return bool
449 */
450 public static function set_state_running($running = true) {
451 if ($running === true) {
452 if (self::get_automated_backup_state() === self::STATE_RUNNING) {
453 throw new backup_exception('backup_automated_already_running');
454 }
455 set_config('backup_auto_running', '1', 'backup');
456 } else {
457 unset_config('backup_auto_running', 'backup');
458 }
459 return true;
460 }
461
462 /**
463 * Removes excess backups from the external system and the local file system.
464 *
465 * The number of backups keep comes from $config->backup_auto_keep
466 *
467 * @param stdClass $course
468 * @return bool
469 */
470 public static function remove_excess_backups($course) {
471 $config = get_config('backup');
472 $keep = (int)$config->backup_auto_keep;
473 $storage = $config->backup_auto_storage;
474 $dir = $config->backup_auto_destination;
475
476 $backupword = str_replace(' ', '_', moodle_strtolower(get_string('backupfilename')));
477 $backupword = trim(clean_filename($backupword), '_');
478
479 if (!file_exists($dir) || !is_dir($dir) || !is_writable($dir)) {
480 $dir = null;
481 }
482
483 // Clean up excess backups in the course backup filearea
484 if ($storage == 0 || $storage == 2) {
485 $fs = get_file_storage();
486 $context = get_context_instance(CONTEXT_COURSE, $course->id);
487 $component = 'backup';
488 $filearea = 'automated';
489 $itemid = 0;
490 $files = array();
491 foreach ($fs->get_area_files($context->id, $component, $filearea, $itemid) as $file) {
492 if (strpos($file->get_filename(), $backupword) !== 0) {
493 continue;
494 }
495 $files[$file->get_timemodified()] = $file;
496 }
497 arsort($files);
498 $remove = array_splice($files, $keep);
499 foreach ($remove as $file) {
500 $file->delete();
501 }
502 //mtrace('Removed '.count($remove).' old backup file(s) from the data directory');
503 }
504
505 // Clean up excess backups in the specified external directory
506 if (!empty($dir) && ($storage == 1 || $storage == 2)) {
507 // Calculate backup filename regex
508
509 $filename = $backupword . '-' . backup::FORMAT_MOODLE . '-' . backup::TYPE_1COURSE . '-' .$course->id . '-';
510
511 $regex = '#^'.preg_quote($filename, '#').'(\d{8})\-(\d{4})\-[a-z]{2}\.mbz$#S';
512
513 $files = array();
514 foreach (scandir($dir) as $file) {
515 if (preg_match($regex, $file, $matches)) {
516 $files[$file] = $matches[1].$matches[2];
517 }
518 }
519 if (count($files) <= $keep) {
520 // There are less matching files than the desired number to keep
521 // do there is nothing to clean up.
522 return 0;
523 }
524 arsort($files);
525 $remove = array_splice($files, $keep);
526 foreach (array_keys($remove) as $file) {
527 unlink($dir.'/'.$file);
528 }
529 //mtrace('Removed '.count($remove).' old backup file(s) from external directory');
530 }
531
532 return true;
533 }
534}