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