More logical wording on button to import from local file
[moodle-linuxchix.git] / backup / backup_scheduled.php
blob204b19b2170578914f856b2a3dee4660d02c38c3
1 <?php //$Id$
2 //This file contains all the code needed to execute scheduled backups
4 //This function is executed via moodle cron
5 //It prepares all the info and execute backups as necessary
6 function schedule_backup_cron() {
8 global $CFG;
10 $status = true;
12 $emailpending = false;
14 //Check for required functions...
15 if(!function_exists('utf8_encode')) {
16 mtrace(" ERROR: You need to add XML support to your PHP installation!");
17 return true;
20 //Get now
21 $now = time();
23 //First of all, we have to see if the scheduled is active and detect
24 //that there isn't another cron running
25 mtrace(" Checking backup status",'...');
26 $backup_config = backup_get_config();
27 if(!isset($backup_config->backup_sche_active) || !$backup_config->backup_sche_active) {
28 mtrace("INACTIVE");
29 return true;
30 } else if (isset($backup_config->backup_sche_running) && $backup_config->backup_sche_running) {
31 mtrace("RUNNING");
32 //Now check if it's a really running task or something very old looking
33 //for info in backup_logs to unlock status as necessary
34 $timetosee = 1800; //Half an hour looking for activity
35 $timeafter = time() - $timetosee;
36 $numofrec = count_records_select ("backup_log","time > $timeafter");
37 if (!$numofrec) {
38 $timetoseemin = $timetosee/60;
39 mtrace(" No activity in last ".$timetoseemin." minutes. Unlocking status");
40 } else {
41 mtrace(" Scheduled backup seems to be running. Execution delayed");
42 return true;
44 } else {
45 mtrace("OK");
46 //Mark backup_sche_running
47 backup_set_config("backup_sche_running","1");
50 //Now we get the main admin user (we'll use his timezone, mail...)
51 mtrace(" Getting admin info");
52 $admin = get_admin();
53 if (!$admin) {
54 $status = false;
57 //Delete old_entries from backup tables
58 if ($status) {
59 mtrace(" Deleting old data");
60 $status = backup_delete_old_data();
63 //Now we get a list of courses in the server
64 if ($status) {
65 mtrace(" Checking courses");
66 //First of all, we delete everything from backup tables related to deleted courses
67 mtrace(" Skipping deleted courses");
68 $skipped = 0;
69 if ($bckcourses = get_records('backup_courses')) {
70 foreach($bckcourses as $bckcourse) {
71 //Search if it exists
72 if (!$exists = get_record('course', 'id', "$bckcourse->courseid")) {
73 //Doesn't exist, so delete from backup tables
74 delete_records('backup_courses', 'courseid', "$bckcourse->courseid");
75 delete_records('backup_log', 'courseid', "$bckcourse->courseid");
76 $skipped++;
80 mtrace(" $skipped courses");
81 //Now process existing courses
82 $courses = get_records("course");
83 //For each course, we check (insert, update) the backup_course table
84 //with needed data
85 foreach ($courses as $course) {
86 if ($status) {
87 mtrace(" $course->fullname");
88 //We check if the course exists in backup_course
89 $backup_course = get_record("backup_courses","courseid",$course->id);
90 //If it doesn't exist, create
91 if (!$backup_course) {
92 $temp_backup_course->courseid = $course->id;
93 $newid = insert_record("backup_courses",$temp_backup_course);
94 //And get it from db
95 $backup_course = get_record("backup_courses","id",$newid);
97 //If it doesn't exist now, error
98 if (!$backup_course) {
99 mtrace(" ERROR (in backup_courses detection)");
100 $status = false;
102 //Now we backup every course with nextstarttime < now
103 if ($backup_course->nextstarttime > 0 && $backup_course->nextstarttime < $now) {
104 //We have to send a email because we have included at least one backup
105 $emailpending = true;
106 //Only make the backup if laststatus isn't 2-UNFINISHED (uncontrolled error)
107 if ($backup_course->laststatus != 2) {
108 //Set laststarttime
109 $starttime = time();
110 set_field("backup_courses","laststarttime",$starttime,"courseid",$backup_course->courseid);
111 //Set course status to unfinished, the process will reset it
112 set_field("backup_courses","laststatus","2","courseid",$backup_course->courseid);
113 //Launch backup
114 $course_status = schedule_backup_launch_backup($course,$starttime);
115 //Set lastendtime
116 set_field("backup_courses","lastendtime",time(),"courseid",$backup_course->courseid);
117 //Set laststatus
118 if ($course_status) {
119 set_field("backup_courses","laststatus","1","courseid",$backup_course->courseid);
120 } else {
121 set_field("backup_courses","laststatus","0","courseid",$backup_course->courseid);
126 //Now, calculate next execution of the course
127 $nextstarttime = schedule_backup_next_execution ($backup_course,$backup_config,$now,$admin->timezone);
128 //Save it to db
129 set_field("backup_courses","nextstarttime",$nextstarttime,"courseid",$backup_course->courseid);
130 //Print it to screen as necessary
131 $showtime = "undefined";
132 if ($nextstarttime > 0) {
133 $showtime = userdate($nextstarttime,"",$admin->timezone);
135 mtrace(" Next execution: $showtime");
140 //Delete old logs
141 if (!empty($CFG->loglifetime)) {
142 mtrace(" Deleting old logs");
143 $loglifetime = $now - ($CFG->loglifetime * 86400);
144 delete_records_select("backup_log", "laststarttime < '$loglifetime'");
147 //Send email to admin if necessary
148 if ($emailpending) {
149 mtrace(" Sending email to admin");
150 $message = "";
152 //Get info about the status of courses
153 $count_all = count_records('backup_courses');
154 $count_ok = count_records('backup_courses','laststatus','1');
155 $count_error = count_records('backup_courses','laststatus','0');
156 $count_unfinished = count_records('backup_courses','laststatus','2');
158 //Build the message text
159 //Summary
160 $message .= get_string('summary')."\n";
161 $message .= "==================================================\n";
162 $message .= " ".get_string('courses').": ".$count_all."\n";
163 $message .= " ".get_string('ok').": ".$count_ok."\n";
164 $message .= " ".get_string('error').": ".$count_error."\n";
165 $message .= " ".get_string('unfinished').": ".$count_unfinished."\n\n";
167 //Reference
168 if ($count_error != 0 || $count_unfinished != 0) {
169 $message .= " ".get_string('backupfailed')."\n\n";
170 $dest_url = $CFG->wwwroot.'/backup/log.php';
171 $message .= " ".get_string('backuptakealook','',$dest_url)."\n\n";
172 //Set message priority
173 $admin->priority = 1;
174 //Reset unfinished to error
175 set_field('backup_courses','laststatus','0','laststatus','2');
176 } else {
177 $message .= " ".get_string('backupfinished')."\n";
180 //Build the message subject
181 $site = get_site();
182 $prefix = $site->shortname.": ";
183 if ($count_error != 0 || $count_unfinished != 0) {
184 $prefix .= "[".strtoupper(get_string('error'))."] ";
186 $subject = $prefix.get_string("scheduledbackupstatus");
188 //Send the message
189 email_to_user($admin,$admin,$subject,$message);
193 //Everything is finished stop backup_sche_running
194 backup_set_config("backup_sche_running","0");
196 return $status;
199 //This function executes the ENTIRE backup of a course (passed as parameter)
200 //using all the scheduled backup preferences
201 function schedule_backup_launch_backup($course,$starttime = 0) {
203 $preferences = false;
204 $status = false;
206 mtrace(" Executing backup");
207 schedule_backup_log($starttime,$course->id,"Start backup course $course->fullname");
208 schedule_backup_log($starttime,$course->id," Phase 1: Checking and counting:");
209 $preferences = schedule_backup_course_configure($course,$starttime);
211 if ($preferences) {
212 schedule_backup_log($starttime,$course->id," Phase 2: Executing and copying:");
213 $status = schedule_backup_course_execute($preferences,$starttime);
216 if ($status && $preferences) {
217 //Only if the backup_sche_keep is set
218 if ($preferences->backup_keep) {
219 schedule_backup_log($starttime,$course->id," Phase 3: Deleting old backup files:");
220 $status = schedule_backup_course_delete_old_files($preferences,$starttime);
224 if ($status && $preferences) {
225 mtrace(" End backup OK");
226 schedule_backup_log($starttime,$course->id,"End backup course $course->fullname - OK");
227 } else {
228 mtrace(" End backup with ERROR");
229 schedule_backup_log($starttime,$course->id,"End backup course $course->fullname - ERROR!!");
232 return $status && $preferences;
235 //This function saves to backup_log all the needed process info
236 //to use it later. NOTE: If $starttime = 0 no info in saved
237 function schedule_backup_log($starttime,$courseid,$message) {
239 if ($starttime) {
240 $log->courseid = $courseid;
241 $log->time = time();
242 $log->laststarttime = $starttime;
243 $log->info = addslashes($message);
245 insert_record ("backup_log",$log);
250 //This function returns the next future GMT time to execute the course based in the
251 //configuration of the scheduled backups
252 function schedule_backup_next_execution ($backup_course,$backup_config,$now,$timezone) {
254 $result = -1;
256 //Get today's midnight GMT
257 $midnight = usergetmidnight($now,$timezone);
259 //Get today's day of week (0=Sunday...6=Saturday)
260 $date = usergetdate($now,$timezone);
261 $dayofweek = $date['wday'];
263 //Get number of days (from today) to execute backups
264 $scheduled_days = substr($backup_config->backup_sche_weekdays,$dayofweek).
265 $backup_config->backup_sche_weekdays;
266 $daysfromtoday = strpos($scheduled_days, "1");
268 //If some day has been found
269 if ($daysfromtoday !== false) {
270 //Calculate distance
271 $dist = ($daysfromtoday * 86400) + //Days distance
272 ($backup_config->backup_sche_hour*3600) + //Hours distance
273 ($backup_config->backup_sche_minute*60); //Minutes distance
274 $result = $midnight + $dist;
277 //If that time is past, call the function recursively to obtain the next valid day
278 if ($result > 0 && $result < time()) {
279 $result = schedule_backup_next_execution ($backup_course,$backup_config,$now + 86400,$timezone);
282 return $result;
287 //This function implements all the needed code to prepare a course
288 //to be in backup (insert temp info into backup temp tables).
289 function schedule_backup_course_configure($course,$starttime = 0) {
291 global $CFG;
293 $status = true;
295 schedule_backup_log($starttime,$course->id," checking parameters");
297 //Check the required variable
298 if (empty($course->id)) {
299 $status = false;
301 //Get scheduled backup preferences
302 $backup_config = backup_get_config();
304 //Checks backup_config pairs exist
305 if ($status) {
306 if (!isset($backup_config->backup_sche_modules)) {
307 $backup_config->backup_sche_modules = 1;
309 if (!isset($backup_config->backup_sche_withuserdata)) {
310 $backup_config->backup_sche_withuserdata = 1;
312 if (!isset($backup_config->backup_sche_metacourse)) {
313 $backup_config->backup_sche_metacourse = 1;
315 if (!isset($backup_config->backup_sche_users)) {
316 $backup_config->backup_sche_users = 1;
318 if (!isset($backup_config->backup_sche_logs)) {
319 $backup_config->backup_sche_logs = 0;
321 if (!isset($backup_config->backup_sche_userfiles)) {
322 $backup_config->backup_sche_userfiles = 1;
324 if (!isset($backup_config->backup_sche_coursefiles)) {
325 $backup_config->backup_sche_coursefiles = 1;
327 if (!isset($backup_config->backup_sche_messages)) {
328 $backup_config->backup_sche_messages = 1;
330 if (!isset($backup_config->backup_sche_active)) {
331 $backup_config->backup_sche_active = 0;
333 if (!isset($backup_config->backup_sche_weekdays)) {
334 $backup_config->backup_sche_weekdays = "0000000";
336 if (!isset($backup_config->backup_sche_hour)) {
337 $backup_config->backup_sche_hour = 00;
339 if (!isset($backup_config->backup_sche_minute)) {
340 $backup_config->backup_sche_minute = 00;
342 if (!isset($backup_config->backup_sche_destination)) {
343 $backup_config->backup_sche_destination = "";
345 if (!isset($backup_config->backup_sche_keep)) {
346 $backup_config->backup_sche_keep = 1;
350 if ($status) {
351 //Checks for the required files/functions to backup every mod
352 //And check if there is data about it
353 $count = 0;
354 if ($allmods = get_records("modules") ) {
355 foreach ($allmods as $mod) {
356 $modname = $mod->name;
357 $modfile = "$CFG->dirroot/mod/$modname/backuplib.php";
358 $modbackup = $modname."_backup_mods";
359 $modcheckbackup = $modname."_check_backup_mods";
360 if (file_exists($modfile)) {
361 include_once($modfile);
362 if (function_exists($modbackup) and function_exists($modcheckbackup)) {
363 $var = "exists_".$modname;
364 $$var = true;
365 $count++;
367 // PENNY NOTES: I have moved from here to the closing brace inside
368 // by two sets of ifs()
369 // to avoid the backup failing on a non existant backup.
370 // If the file/function/whatever doesn't exist, we don't want to set this
371 // this module in backup preferences at all.
372 //Check data
373 //Check module info
374 $var = "backup_".$modname;
375 if (!isset($$var)) {
376 $$var = $backup_config->backup_sche_modules;
378 //Now stores all the mods preferences into an array into preferences
379 $preferences->mods[$modname]->backup = $$var;
381 //Check include user info
382 $var = "backup_user_info_".$modname;
383 if (!isset($$var)) {
384 $$var = $backup_config->backup_sche_withuserdata;
386 //Now stores all the mods preferences into an array into preferences
387 $preferences->mods[$modname]->userinfo = $$var;
388 //And the name of the mod
389 $preferences->mods[$modname]->name = $modname;
394 // now set instances
395 if ($coursemods = get_course_mods($course->id)) {
396 foreach ($coursemods as $mod) {
397 if (array_key_exists($mod->modname,$preferences->mods)) { // we are to backup this module
398 if (empty($preferences->mods[$mod->modname]->instances)) {
399 $preferences->mods[$mod->modname]->instances = array(); // avoid warnings
401 $preferences->mods[$mod->modname]->instances[$mod->instance]->backup = $preferences->mods[$modname]->backup;
402 $preferences->mods[$mod->modname]->instances[$mod->instance]->userinfo = $preferences->mods[$modname]->userinfo;
403 // there isn't really a nice way to do this...
404 $preferences->mods[$mod->modname]->instances[$mod->instance]->name = get_field($mod->modname,'name','id',$mod->instance);
410 //Convert other parameters
411 if ($status) {
412 $preferences->backup_metacourse = $backup_config->backup_sche_metacourse;
413 $preferences->backup_users = $backup_config->backup_sche_users;
414 $preferences->backup_logs = $backup_config->backup_sche_logs;
415 $preferences->backup_user_files = $backup_config->backup_sche_userfiles;
416 $preferences->backup_course_files = $backup_config->backup_sche_coursefiles;
417 $preferences->backup_messages = $backup_config->backup_sche_messages;
418 $preferences->backup_course = $course->id;
419 $preferences->backup_destination = $backup_config->backup_sche_destination;
420 $preferences->backup_keep = $backup_config->backup_sche_keep;
423 //Calculate the backup string
424 if ($status) {
425 schedule_backup_log($starttime,$course->id," calculating backup name");
427 //Calculate the backup word
428 //Take off some characters in the filename !!
429 $takeoff = array(" ", ":", "/", "\\", "|");
430 $backup_word = str_replace($takeoff,"_",strtolower(get_string("backupfilename")));
431 //If non-translated, use "backup"
432 if (substr($backup_word,0,1) == "[") {
433 $backup_word= "backup";
436 //Calculate the date format string
437 $backup_date_format = str_replace(" ","_",get_string("backupnameformat"));
438 //If non-translated, use "%Y%m%d-%H%M"
439 if (substr($backup_date_format,0,1) == "[") {
440 $backup_date_format = "%%Y%%m%%d-%%H%%M";
443 //Calculate the shortname
444 $backup_shortname = clean_filename($course->shortname);
445 if (empty($backup_shortname) or $backup_shortname == '_' ) {
446 $backup_shortname = $course->id;
449 //Calculate the final backup filename
450 //The backup word
451 $backup_name = $backup_word."-";
452 //The shortname
453 $backup_name .= strtolower($backup_shortname)."-";
454 //The date format
455 $backup_name .= userdate(time(),$backup_date_format,99,false);
456 //The extension
457 $backup_name .= ".zip";
458 //And finally, clean everything
459 $backup_name = clean_filename($backup_name);
461 //Calculate the string to match the keep preference
462 $keep_name = $backup_word."-";
463 //The shortname
464 $keep_name .= strtolower($backup_shortname)."-";
465 //And finally, clean everything
466 $keep_name = clean_filename($keep_name);
468 $preferences->backup_name = $backup_name;
469 $preferences->keep_name = $keep_name;
472 //Calculate the backup unique code to allow simultaneus backups (to define
473 //the temp-directory name and records in backup temp tables
474 if ($status) {
475 $backup_unique_code = time();
476 $preferences->backup_unique_code = $backup_unique_code;
479 //Calculate necesary info to backup modules
480 if ($status) {
481 schedule_backup_log($starttime,$course->id," calculating modules data");
482 if ($allmods = get_records("modules") ) {
483 foreach ($allmods as $mod) {
484 $modname = $mod->name;
485 $modbackup = $modname."_backup_mods";
486 //If exists the lib & function
487 $var = "exists_".$modname;
488 if (isset($$var) && $$var) {
489 //Add hidden fields
490 $var = "backup_".$modname;
491 //Only if selected
492 if ($$var == 1) {
493 $var = "backup_user_info_".$modname;
494 //Call the check function to show more info
495 $modcheckbackup = $modname."_check_backup_mods";
496 schedule_backup_log($starttime,$course->id," $modname");
497 $modcheckbackup($course->id,$$var,$backup_unique_code);
504 //Now calculate the users
505 if ($status) {
506 schedule_backup_log($starttime,$course->id," calculating users");
507 user_check_backup($course->id,$backup_unique_code,$preferences->backup_users,$preferences->backup_messages);
510 //Now calculate the logs
511 if ($status) {
512 if ($preferences->backup_logs) {
513 schedule_backup_log($starttime,$course->id," calculating logs");
514 log_check_backup($course->id);
518 //Now calculate the userfiles
519 if ($status) {
520 if ($preferences->backup_user_files) {
521 schedule_backup_log($starttime,$course->id," calculating user files");
522 user_files_check_backup($course->id,$preferences->backup_unique_code);
526 //Now calculate the coursefiles
527 if ($status) {
528 if ($preferences->backup_course_files) {
529 schedule_backup_log($starttime,$course->id," calculating course files");
530 course_files_check_backup($course->id,$preferences->backup_unique_code);
534 //If everything is ok, return calculated preferences
535 if ($status) {
536 $status = $preferences;
539 return $status;
542 //This function implements all the needed code to backup a course
543 //copying it to the desired destination (default if not specified)
544 function schedule_backup_course_execute($preferences,$starttime = 0) {
546 global $CFG;
548 $status = true;
550 //Another Info to add
551 $preferences->moodle_version = $CFG->version;
552 $preferences->moodle_release = $CFG->release;
553 $preferences->backup_version = $CFG->backup_version;
554 $preferences->backup_release = $CFG->backup_release;
556 //Some parts of the backup doesn't know about $preferences, so we
557 //put a copy of it inside that CFG (always global) to be able to
558 //use it. Then, when needed I search for preferences inside CFG
559 //Used to avoid some problems in full_tag() when preferences isn't
560 //set globally (i.e. in scheduled backups)
561 $CFG->backup_preferences = $preferences;
563 //Check for temp and backup and backup_unique_code directory
564 //Create them as needed
565 schedule_backup_log($starttime,$preferences->backup_course," checking temp structures");
566 $status = check_and_create_backup_dir($preferences->backup_unique_code);
567 //Empty backup dir
568 if ($status) {
569 schedule_backup_log($starttime,$preferences->backup_course," cleaning current dir");
570 $status = clear_backup_dir($preferences->backup_unique_code);
573 //Create the moodle.xml file
574 if ($status) {
575 schedule_backup_log($starttime,$preferences->backup_course," creating backup file");
576 //Obtain the xml file (create and open) and print prolog information
577 $backup_file = backup_open_xml($preferences->backup_unique_code);
578 //Prints general info about backup to file
579 if ($backup_file) {
580 schedule_backup_log($starttime,$preferences->backup_course," general info");
581 $status = backup_general_info($backup_file,$preferences);
582 } else {
583 $status = false;
586 //Prints course start (tag and general info)
587 if ($status) {
588 $status = backup_course_start($backup_file,$preferences);
591 //Metacourse information
592 if ($status && $preferences->backup_metacourse) {
593 schedule_backup_log($starttime,$preferences->backup_course," metacourse info");
594 $status = backup_course_metacourse($backup_file,$preferences);
597 //Block info
598 if ($status) {
599 schedule_backup_log($starttime,$preferences->backup_course," blocks info");
600 $status = backup_course_blocks($backup_file,$preferences);
603 //Section info
604 if ($status) {
605 schedule_backup_log($starttime,$preferences->backup_course," sections info");
606 $status = backup_course_sections($backup_file,$preferences);
609 //User info
610 if ($status) {
611 schedule_backup_log($starttime,$preferences->backup_course," user info");
612 $status = backup_user_info($backup_file,$preferences);
615 //If we have selected to backup messages and we are
616 //doing a SITE backup, let's do it
617 if ($status && $preferences->backup_messages && $preferences->backup_course == SITEID) {
618 schedule_backup_log($starttime,$preferences->backup_course," messages");
619 if (!$status = backup_messages($backup_file,$preferences)) {
620 notify("An error occurred while backing up messages");
624 //If we have selected to backup quizzes, backup categories and
625 //questions structure (step 1). See notes on mod/quiz/backuplib.php
626 if ($status and $preferences->mods['quiz']->backup) {
627 schedule_backup_log($starttime,$preferences->backup_course," categories & questions");
628 $status = backup_question_categories($backup_file,$preferences);
631 //Print logs if selected
632 if ($status) {
633 if ($preferences->backup_logs) {
634 schedule_backup_log($starttime,$preferences->backup_course," logs");
635 $status = backup_log_info($backup_file,$preferences);
639 //Print scales info
640 if ($status) {
641 schedule_backup_log($starttime,$preferences->backup_course," scales");
642 $status = backup_scales_info($backup_file,$preferences);
645 //Print groups info
646 if ($status) {
647 schedule_backup_log($starttime,$preferences->backup_course," groups");
648 $status = backup_groups_info($backup_file,$preferences);
651 //Print events info
652 if ($status) {
653 schedule_backup_log($starttime,$preferences->backup_course," events");
654 $status = backup_events_info($backup_file,$preferences);
657 //Print gradebook info
658 if ($status) {
659 schedule_backup_log($starttime,$preferences->backup_course," gradebook");
660 $status = backup_gradebook_info($backup_file,$preferences);
663 //Module info, this unique function makes all the work!!
664 //db export and module fileis copy
665 if ($status) {
666 $mods_to_backup = false;
667 //Check if we have any mod to backup
668 foreach ($preferences->mods as $module) {
669 if ($module->backup) {
670 $mods_to_backup = true;
673 //If we have to backup some module
674 if ($mods_to_backup) {
675 schedule_backup_log($starttime,$preferences->backup_course," modules");
676 //Start modules tag
677 $status = backup_modules_start ($backup_file,$preferences);
678 //Iterate over modules and call backup
679 foreach ($preferences->mods as $module) {
680 if ($module->backup and $status) {
681 schedule_backup_log($starttime,$preferences->backup_course," $module->name");
682 $status = backup_module($backup_file,$preferences,$module->name);
685 //Close modules tag
686 $status = backup_modules_end ($backup_file,$preferences);
690 //Prints course end
691 if ($status) {
692 $status = backup_course_end($backup_file,$preferences);
695 //Close the xml file and xml data
696 if ($backup_file) {
697 backup_close_xml($backup_file);
701 //Now, if selected, copy user files
702 if ($status) {
703 if ($preferences->backup_user_files) {
704 schedule_backup_log($starttime,$preferences->backup_course," copying user files");
705 $status = backup_copy_user_files ($preferences);
709 //Now, if selected, copy course files
710 if ($status) {
711 if ($preferences->backup_course_files) {
712 schedule_backup_log($starttime,$preferences->backup_course," copying course files");
713 $status = backup_copy_course_files ($preferences);
717 //Now, zip all the backup directory contents
718 if ($status) {
719 schedule_backup_log($starttime,$preferences->backup_course," zipping files");
720 $status = backup_zip ($preferences);
723 //Now, copy the zip file to course directory
724 if ($status) {
725 schedule_backup_log($starttime,$preferences->backup_course," copying backup");
726 $status = copy_zip_to_course_dir ($preferences);
729 //Now, clean temporary data (db and filesystem)
730 if ($status) {
731 schedule_backup_log($starttime,$preferences->backup_course," cleaning temp data");
732 $status = clean_temp_data ($preferences);
735 //Unset CFG->backup_preferences only needed in scheduled backups
736 unset ($CFG->backup_preferences);
738 return $status;
741 //This function deletes old backup files when the "keep" limit has been reached
742 //in the destination directory.
743 function schedule_backup_course_delete_old_files($preferences,$starttime=0) {
745 global $CFG;
747 $status = true;
749 //Calculate the directory to check
750 $dirtocheck = "";
751 //if $preferences->backup_destination isn't empty, then check that directory
752 if (!empty($preferences->backup_destination)) {
753 $dirtocheck = $preferences->backup_destination;
754 //else calculate standard backup directory location
755 } else {
756 $dirtocheck = $CFG->dataroot."/".$preferences->backup_course."/backupdata";
758 schedule_backup_log($starttime,$preferences->backup_course," checking $dirtocheck");
759 mtrace(" Keeping backup files in $dirtocheck");
761 //Get all the files in $dirtocheck
762 $files = get_directory_list($dirtocheck,"",false);
763 //Get all matching files ($preferences->keep_name) from $files
764 $matchingfiles = array();
765 foreach ($files as $file) {
766 if (substr($file, 0, strlen($preferences->keep_name)) == $preferences->keep_name) {
767 $modifieddate = filemtime($dirtocheck."/".$file);
768 $matchingfiles[$modifieddate] = $file;
771 //Sort by key (modified date) to get the oldest first (instead of doing that by name
772 //because it could give us problems in some languages with different format names).
773 ksort($matchingfiles);
775 //Count matching files
776 $countmatching = count($matchingfiles);
777 schedule_backup_log($starttime,$preferences->backup_course," found $countmatching backup files");
778 mtrace(" found $countmatching backup files");
779 if ($preferences->backup_keep < $countmatching) {
780 schedule_backup_log($starttime,$preferences->backup_course," keep limit ($preferences->backup_keep) reached. Deleting old files");
781 mtrace(" keep limit ($preferences->backup_keep) reached. Deleting old files");
782 $filestodelete = $countmatching - $preferences->backup_keep;
783 $filesdeleted = 0;
784 foreach ($matchingfiles as $matchfile) {
785 if ($filesdeleted < $filestodelete) {
786 schedule_backup_log($starttime,$preferences->backup_course," $matchfile deleted");
787 mtrace(" $matchfile deleted");
788 $filetodelete = $dirtocheck."/".$matchfile;
789 unlink($filetodelete);
790 $filesdeleted++;
794 return $status;