MDL-13872:
[moodle-linuxchix.git] / backup / backup_scheduled.php
blob9addef8e7a230e8ee7ead25b96b6768e6d3cb651
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;
101 continue;
103 // Skip backup of unavailable courses that have remained unmodified in a month
104 $skipped = false;
105 if (!$course->visible && ($now - $course->timemodified) > 31*24*60*60) { //Hidden + unmodified last month
106 mtrace(" SKIPPING - hidden+unmodified");
107 set_field("backup_courses","laststatus","3","courseid",$backup_course->courseid);
108 $skipped = true;
110 //Now we backup every non skipped course with nextstarttime < now
111 if (!$skipped && $backup_course->nextstarttime > 0 && $backup_course->nextstarttime < $now) {
112 //We have to send a email because we have included at least one backup
113 $emailpending = true;
114 //Only make the backup if laststatus isn't 2-UNFINISHED (uncontrolled error)
115 if ($backup_course->laststatus != 2) {
116 //Set laststarttime
117 $starttime = time();
118 set_field("backup_courses","laststarttime",$starttime,"courseid",$backup_course->courseid);
119 //Set course status to unfinished, the process will reset it
120 set_field("backup_courses","laststatus","2","courseid",$backup_course->courseid);
121 //Launch backup
122 $course_status = schedule_backup_launch_backup($course,$starttime);
123 //Set lastendtime
124 set_field("backup_courses","lastendtime",time(),"courseid",$backup_course->courseid);
125 //Set laststatus
126 if ($course_status) {
127 set_field("backup_courses","laststatus","1","courseid",$backup_course->courseid);
128 } else {
129 set_field("backup_courses","laststatus","0","courseid",$backup_course->courseid);
134 //Now, calculate next execution of the course
135 $nextstarttime = schedule_backup_next_execution ($backup_course,$backup_config,$now,$admin->timezone);
136 //Save it to db
137 set_field("backup_courses","nextstarttime",$nextstarttime,"courseid",$backup_course->courseid);
138 //Print it to screen as necessary
139 $showtime = "undefined";
140 if ($nextstarttime > 0) {
141 $showtime = userdate($nextstarttime,"",$admin->timezone);
143 mtrace(" Next execution: $showtime");
148 //Delete old logs
149 if (!empty($CFG->loglifetime)) {
150 mtrace(" Deleting old logs");
151 $loglifetime = $now - ($CFG->loglifetime * 86400);
152 delete_records_select("backup_log", "laststarttime < '$loglifetime'");
155 //Send email to admin if necessary
156 if ($emailpending) {
157 mtrace(" Sending email to admin");
158 $message = "";
160 //Get info about the status of courses
161 $count_all = count_records('backup_courses');
162 $count_ok = count_records('backup_courses','laststatus','1');
163 $count_error = count_records('backup_courses','laststatus','0');
164 $count_unfinished = count_records('backup_courses','laststatus','2');
165 $count_skipped = count_records('backup_courses','laststatus','3');
167 //Build the message text
168 //Summary
169 $message .= get_string('summary')."\n";
170 $message .= "==================================================\n";
171 $message .= " ".get_string('courses').": ".$count_all."\n";
172 $message .= " ".get_string('ok').": ".$count_ok."\n";
173 $message .= " ".get_string('skipped').": ".$count_skipped."\n";
174 $message .= " ".get_string('error').": ".$count_error."\n";
175 $message .= " ".get_string('unfinished').": ".$count_unfinished."\n\n";
177 //Reference
178 if ($count_error != 0 || $count_unfinished != 0) {
179 $message .= " ".get_string('backupfailed')."\n\n";
180 $dest_url = "$CFG->wwwroot/$CFG->admin/report/backups/index.php";
181 $message .= " ".get_string('backuptakealook','',$dest_url)."\n\n";
182 //Set message priority
183 $admin->priority = 1;
184 //Reset unfinished to error
185 set_field('backup_courses','laststatus','0','laststatus','2');
186 } else {
187 $message .= " ".get_string('backupfinished')."\n";
190 //Build the message subject
191 $site = get_site();
192 $prefix = $site->shortname.": ";
193 if ($count_error != 0 || $count_unfinished != 0) {
194 $prefix .= "[".strtoupper(get_string('error'))."] ";
196 $subject = $prefix.get_string("scheduledbackupstatus");
198 //Send the message
199 email_to_user($admin,$admin,$subject,$message);
203 //Everything is finished stop backup_sche_running
204 backup_set_config("backup_sche_running","0");
206 return $status;
209 //This function executes the ENTIRE backup of a course (passed as parameter)
210 //using all the scheduled backup preferences
211 function schedule_backup_launch_backup($course,$starttime = 0) {
213 $preferences = false;
214 $status = false;
216 mtrace(" Executing backup");
217 schedule_backup_log($starttime,$course->id,"Start backup course $course->fullname");
218 schedule_backup_log($starttime,$course->id," Phase 1: Checking and counting:");
219 $preferences = schedule_backup_course_configure($course,$starttime);
221 if ($preferences) {
222 schedule_backup_log($starttime,$course->id," Phase 2: Executing and copying:");
223 $status = schedule_backup_course_execute($preferences,$starttime);
226 if ($status && $preferences) {
227 //Only if the backup_sche_keep is set
228 if ($preferences->backup_keep) {
229 schedule_backup_log($starttime,$course->id," Phase 3: Deleting old backup files:");
230 $status = schedule_backup_course_delete_old_files($preferences,$starttime);
234 if ($status && $preferences) {
235 mtrace(" End backup OK");
236 schedule_backup_log($starttime,$course->id,"End backup course $course->fullname - OK");
237 } else {
238 mtrace(" End backup with ERROR");
239 schedule_backup_log($starttime,$course->id,"End backup course $course->fullname - ERROR!!");
242 return $status && $preferences;
245 //This function saves to backup_log all the needed process info
246 //to use it later. NOTE: If $starttime = 0 no info in saved
247 function schedule_backup_log($starttime,$courseid,$message) {
249 if ($starttime) {
250 $log->courseid = $courseid;
251 $log->time = time();
252 $log->laststarttime = $starttime;
253 $log->info = addslashes($message);
255 insert_record ("backup_log",$log);
260 //This function returns the next future GMT time to execute the course based in the
261 //configuration of the scheduled backups
262 function schedule_backup_next_execution ($backup_course,$backup_config,$now,$timezone) {
264 $result = -1;
266 //Get today's midnight GMT
267 $midnight = usergetmidnight($now,$timezone);
269 //Get today's day of week (0=Sunday...6=Saturday)
270 $date = usergetdate($now,$timezone);
271 $dayofweek = $date['wday'];
273 //Get number of days (from today) to execute backups
274 $scheduled_days = substr($backup_config->backup_sche_weekdays,$dayofweek).
275 $backup_config->backup_sche_weekdays;
276 $daysfromtoday = strpos($scheduled_days, "1");
278 //If some day has been found
279 if ($daysfromtoday !== false) {
280 //Calculate distance
281 $dist = ($daysfromtoday * 86400) + //Days distance
282 ($backup_config->backup_sche_hour*3600) + //Hours distance
283 ($backup_config->backup_sche_minute*60); //Minutes distance
284 $result = $midnight + $dist;
287 //If that time is past, call the function recursively to obtain the next valid day
288 if ($result > 0 && $result < time()) {
289 $result = schedule_backup_next_execution ($backup_course,$backup_config,$now + 86400,$timezone);
292 return $result;
297 //This function implements all the needed code to prepare a course
298 //to be in backup (insert temp info into backup temp tables).
299 function schedule_backup_course_configure($course,$starttime = 0) {
301 global $CFG;
303 $status = true;
305 schedule_backup_log($starttime,$course->id," checking parameters");
307 //Check the required variable
308 if (empty($course->id)) {
309 $status = false;
311 //Get scheduled backup preferences
312 $backup_config = backup_get_config();
314 //Checks backup_config pairs exist
315 if ($status) {
316 if (!isset($backup_config->backup_sche_modules)) {
317 $backup_config->backup_sche_modules = 1;
319 if (!isset($backup_config->backup_sche_withuserdata)) {
320 $backup_config->backup_sche_withuserdata = 1;
322 if (!isset($backup_config->backup_sche_metacourse)) {
323 $backup_config->backup_sche_metacourse = 1;
325 if (!isset($backup_config->backup_sche_users)) {
326 $backup_config->backup_sche_users = 1;
328 if (!isset($backup_config->backup_sche_logs)) {
329 $backup_config->backup_sche_logs = 0;
331 if (!isset($backup_config->backup_sche_userfiles)) {
332 $backup_config->backup_sche_userfiles = 1;
334 if (!isset($backup_config->backup_sche_coursefiles)) {
335 $backup_config->backup_sche_coursefiles = 1;
337 if (!isset($backup_config->backup_sche_sitefiles)) {
338 $backup_config->backup_sche_sitefiles = 1;
340 if (!isset($backup_config->backup_sche_messages)) {
341 $backup_config->backup_sche_messages = 0;
343 if (!isset($backup_config->backup_sche_active)) {
344 $backup_config->backup_sche_active = 0;
346 if (!isset($backup_config->backup_sche_weekdays)) {
347 $backup_config->backup_sche_weekdays = "0000000";
349 if (!isset($backup_config->backup_sche_hour)) {
350 $backup_config->backup_sche_hour = 00;
352 if (!isset($backup_config->backup_sche_minute)) {
353 $backup_config->backup_sche_minute = 00;
355 if (!isset($backup_config->backup_sche_destination)) {
356 $backup_config->backup_sche_destination = "";
358 if (!isset($backup_config->backup_sche_keep)) {
359 $backup_config->backup_sche_keep = 1;
363 if ($status) {
364 //Checks for the required files/functions to backup every mod
365 //And check if there is data about it
366 $count = 0;
367 if ($allmods = get_records("modules") ) {
368 foreach ($allmods as $mod) {
369 $modname = $mod->name;
370 $modfile = "$CFG->dirroot/mod/$modname/backuplib.php";
371 $modbackup = $modname."_backup_mods";
372 $modcheckbackup = $modname."_check_backup_mods";
373 if (file_exists($modfile)) {
374 include_once($modfile);
375 if (function_exists($modbackup) and function_exists($modcheckbackup)) {
376 $var = "exists_".$modname;
377 $$var = true;
378 $count++;
380 // PENNY NOTES: I have moved from here to the closing brace inside
381 // by two sets of ifs()
382 // to avoid the backup failing on a non existant backup.
383 // If the file/function/whatever doesn't exist, we don't want to set this
384 // this module in backup preferences at all.
385 //Check data
386 //Check module info
387 $var = "backup_".$modname;
388 if (!isset($$var)) {
389 $$var = $backup_config->backup_sche_modules;
391 //Now stores all the mods preferences into an array into preferences
392 $preferences->mods[$modname]->backup = $$var;
394 //Check include user info
395 $var = "backup_user_info_".$modname;
396 if (!isset($$var)) {
397 $$var = $backup_config->backup_sche_withuserdata;
399 //Now stores all the mods preferences into an array into preferences
400 $preferences->mods[$modname]->userinfo = $$var;
401 //And the name of the mod
402 $preferences->mods[$modname]->name = $modname;
408 // now set instances
409 if ($coursemods = get_course_mods($course->id)) {
410 foreach ($coursemods as $mod) {
411 if (array_key_exists($mod->modname,$preferences->mods)) { // we are to backup this module
412 if (empty($preferences->mods[$mod->modname]->instances)) {
413 $preferences->mods[$mod->modname]->instances = array(); // avoid warnings
415 $preferences->mods[$mod->modname]->instances[$mod->instance]->backup = $preferences->mods[$mod->modname]->backup;
416 $preferences->mods[$mod->modname]->instances[$mod->instance]->userinfo = $preferences->mods[$mod->modname]->userinfo;
417 // there isn't really a nice way to do this...
418 $preferences->mods[$mod->modname]->instances[$mod->instance]->name = get_field($mod->modname,'name','id',$mod->instance);
423 // finally, clean all the $preferences->mods[] not having instances. Nothing to backup about them
424 foreach ($preferences->mods as $modname => $mod) {
425 if (!isset($mod->instances)) {
426 unset($preferences->mods[$modname]);
431 //Convert other parameters
432 if ($status) {
433 $preferences->backup_metacourse = $backup_config->backup_sche_metacourse;
434 $preferences->backup_users = $backup_config->backup_sche_users;
435 $preferences->backup_logs = $backup_config->backup_sche_logs;
436 $preferences->backup_user_files = $backup_config->backup_sche_userfiles;
437 $preferences->backup_course_files = $backup_config->backup_sche_coursefiles;
438 $preferences->backup_site_files = $backup_config->backup_sche_sitefiles;
439 $preferences->backup_messages = $backup_config->backup_sche_messages;
440 $preferences->backup_course = $course->id;
441 $preferences->backup_destination = $backup_config->backup_sche_destination;
442 $preferences->backup_keep = $backup_config->backup_sche_keep;
445 //Calculate various backup preferences
446 if ($status) {
447 schedule_backup_log($starttime,$course->id," calculating backup name");
449 //Calculate the backup file name
450 $backup_name = backup_get_zipfile_name($course);
452 //Calculate the string to match the keep preference
453 $keep_name = backup_get_keep_name($course);
455 //Set them
456 $preferences->backup_name = $backup_name;
457 $preferences->keep_name = $keep_name;
459 //Roleasignments
460 $roles = get_records('role', '', '', 'sortorder');
461 foreach ($roles as $role) {
462 $preferences->backuproleassignments[$role->id] = $role;
465 //Another Info
466 backup_add_static_preferences($preferences);
469 //Calculate the backup unique code to allow simultaneus backups (to define
470 //the temp-directory name and records in backup temp tables
471 if ($status) {
472 $backup_unique_code = time();
473 $preferences->backup_unique_code = $backup_unique_code;
476 //Calculate necesary info to backup modules
477 if ($status) {
478 schedule_backup_log($starttime,$course->id," calculating modules data");
479 if ($allmods = get_records("modules") ) {
480 foreach ($allmods as $mod) {
481 $modname = $mod->name;
482 $modbackup = $modname."_backup_mods";
483 //If exists the lib & function
484 $var = "exists_".$modname;
485 if (isset($$var) && $$var) {
486 //Add hidden fields
487 $var = "backup_".$modname;
488 //Only if selected
489 if ($$var == 1) {
490 $var = "backup_user_info_".$modname;
491 //Call the check function to show more info
492 $modcheckbackup = $modname."_check_backup_mods";
493 schedule_backup_log($starttime,$course->id," $modname");
494 $modcheckbackup($course->id,$$var,$backup_unique_code);
501 //Now calculate the users
502 if ($status) {
503 schedule_backup_log($starttime,$course->id," calculating users");
504 //Decide about include users with messages, based on SITEID
505 if ($preferences->backup_messages && $preferences->backup_course == SITEID) {
506 $include_message_users = true;
507 } else {
508 $include_message_users = false;
510 user_check_backup($course->id,$backup_unique_code,$preferences->backup_users,$include_message_users);
513 //Now calculate the logs
514 if ($status) {
515 if ($preferences->backup_logs) {
516 schedule_backup_log($starttime,$course->id," calculating logs");
517 log_check_backup($course->id);
521 //Now calculate the userfiles
522 if ($status) {
523 if ($preferences->backup_user_files) {
524 schedule_backup_log($starttime,$course->id," calculating user files");
525 user_files_check_backup($course->id,$preferences->backup_unique_code);
529 //Now calculate the coursefiles
530 if ($status) {
531 if ($preferences->backup_course_files) {
532 schedule_backup_log($starttime,$course->id," calculating course files");
533 course_files_check_backup($course->id,$preferences->backup_unique_code);
537 //Now calculate the sitefiles
538 if ($status) {
539 if ($preferences->backup_site_files) {
540 schedule_backup_log($starttime,$course->id," calculating site files");
541 site_files_check_backup($course->id,$preferences->backup_unique_code);
545 //If everything is ok, return calculated preferences
546 if ($status) {
547 $status = $preferences;
550 return $status;
553 //TODO: Unify this function with backup_execute() to have both backups 100% equivalent. Moodle 2.0
555 //This function implements all the needed code to backup a course
556 //copying it to the desired destination (default if not specified)
557 function schedule_backup_course_execute($preferences,$starttime = 0) {
559 global $CFG;
561 $status = true;
563 //Some parts of the backup doesn't know about $preferences, so we
564 //put a copy of it inside that CFG (always global) to be able to
565 //use it. Then, when needed I search for preferences inside CFG
566 //Used to avoid some problems in full_tag() when preferences isn't
567 //set globally (i.e. in scheduled backups)
568 $CFG->backup_preferences = $preferences;
570 //Check for temp and backup and backup_unique_code directory
571 //Create them as needed
572 schedule_backup_log($starttime,$preferences->backup_course," checking temp structures");
573 $status = check_and_create_backup_dir($preferences->backup_unique_code);
574 //Empty backup dir
575 if ($status) {
576 schedule_backup_log($starttime,$preferences->backup_course," cleaning current dir");
577 $status = clear_backup_dir($preferences->backup_unique_code);
580 //Create the moodle.xml file
581 if ($status) {
582 schedule_backup_log($starttime,$preferences->backup_course," creating backup file");
583 //Obtain the xml file (create and open) and print prolog information
584 $backup_file = backup_open_xml($preferences->backup_unique_code);
585 //Prints general info about backup to file
586 if ($backup_file) {
587 schedule_backup_log($starttime,$preferences->backup_course," general info");
588 $status = backup_general_info($backup_file,$preferences);
589 } else {
590 $status = false;
593 //Prints course start (tag and general info)
594 if ($status) {
595 $status = backup_course_start($backup_file,$preferences);
598 //Metacourse information
599 if ($status && $preferences->backup_metacourse) {
600 schedule_backup_log($starttime,$preferences->backup_course," metacourse info");
601 $status = backup_course_metacourse($backup_file,$preferences);
604 //Block info
605 if ($status) {
606 schedule_backup_log($starttime,$preferences->backup_course," blocks info");
607 $status = backup_course_blocks($backup_file,$preferences);
610 //Section info
611 if ($status) {
612 schedule_backup_log($starttime,$preferences->backup_course," sections info");
613 $status = backup_course_sections($backup_file,$preferences);
616 //User info
617 if ($status) {
618 schedule_backup_log($starttime,$preferences->backup_course," user info");
619 $status = backup_user_info($backup_file,$preferences);
622 //If we have selected to backup messages and we are
623 //doing a SITE backup, let's do it
624 if ($status && $preferences->backup_messages && $preferences->backup_course == SITEID) {
625 schedule_backup_log($starttime,$preferences->backup_course," messages");
626 if (!$status = backup_messages($backup_file,$preferences)) {
627 notify("An error occurred while backing up messages");
631 //If we have selected to backup quizzes, backup categories and
632 //questions structure (step 1). See notes on mod/quiz/backuplib.php
633 if ($status and $preferences->mods['quiz']->backup) {
634 schedule_backup_log($starttime,$preferences->backup_course," categories & questions");
635 $status = backup_question_categories($backup_file,$preferences);
638 //Print logs if selected
639 if ($status) {
640 if ($preferences->backup_logs) {
641 schedule_backup_log($starttime,$preferences->backup_course," logs");
642 $status = backup_log_info($backup_file,$preferences);
646 //Print scales info
647 if ($status) {
648 schedule_backup_log($starttime,$preferences->backup_course," scales");
649 $status = backup_scales_info($backup_file,$preferences);
652 //Print groups info
653 if ($status) {
654 schedule_backup_log($starttime,$preferences->backup_course," groups");
655 $status = backup_groups_info($backup_file,$preferences);
658 //Print groupings info
659 if ($status) {
660 schedule_backup_log($starttime,$preferences->backup_course," groupings");
661 $status = backup_groupings_info($backup_file,$preferences);
664 //Print groupings_groups info
665 if ($status) {
666 schedule_backup_log($starttime,$preferences->backup_course," groupings_groups");
667 $status = backup_groupings_groups_info($backup_file,$preferences);
670 //Print events info
671 if ($status) {
672 schedule_backup_log($starttime,$preferences->backup_course," events");
673 $status = backup_events_info($backup_file,$preferences);
676 //Print gradebook info
677 if ($status) {
678 schedule_backup_log($starttime,$preferences->backup_course," gradebook");
679 $status = backup_gradebook_info($backup_file,$preferences);
682 //Module info, this unique function makes all the work!!
683 //db export and module fileis copy
684 if ($status) {
685 $mods_to_backup = false;
686 //Check if we have any mod to backup
687 foreach ($preferences->mods as $module) {
688 if ($module->backup) {
689 $mods_to_backup = true;
692 //If we have to backup some module
693 if ($mods_to_backup) {
694 schedule_backup_log($starttime,$preferences->backup_course," modules");
695 //Start modules tag
696 $status = backup_modules_start ($backup_file,$preferences);
697 //Iterate over modules and call backup
698 foreach ($preferences->mods as $module) {
699 if ($module->backup and $status) {
700 schedule_backup_log($starttime,$preferences->backup_course," $module->name");
701 $status = backup_module($backup_file,$preferences,$module->name);
704 //Close modules tag
705 $status = backup_modules_end ($backup_file,$preferences);
709 //Backup course format data, if any.
710 if ($status) {
711 schedule_backup_log($starttime,$preferences->backup_course," course format data");
712 $status = backup_format_data($backup_file,$preferences);
715 //Prints course end
716 if ($status) {
717 $status = backup_course_end($backup_file,$preferences);
720 //Close the xml file and xml data
721 if ($backup_file) {
722 backup_close_xml($backup_file);
726 //Now, if selected, copy user files
727 if ($status) {
728 if ($preferences->backup_user_files) {
729 schedule_backup_log($starttime,$preferences->backup_course," copying user files");
730 $status = backup_copy_user_files ($preferences);
734 //Now, if selected, copy course files
735 if ($status) {
736 if ($preferences->backup_course_files) {
737 schedule_backup_log($starttime,$preferences->backup_course," copying course files");
738 $status = backup_copy_course_files ($preferences);
742 //Now, if selected, copy site files
743 if ($status) {
744 if ($preferences->backup_site_files) {
745 schedule_backup_log($starttime,$preferences->backup_course," copying site files");
746 $status = backup_copy_site_files ($preferences);
750 //Now, zip all the backup directory contents
751 if ($status) {
752 schedule_backup_log($starttime,$preferences->backup_course," zipping files");
753 $status = backup_zip ($preferences);
756 //Now, copy the zip file to course directory
757 if ($status) {
758 schedule_backup_log($starttime,$preferences->backup_course," copying backup");
759 $status = copy_zip_to_course_dir ($preferences);
762 //Now, clean temporary data (db and filesystem)
763 if ($status) {
764 schedule_backup_log($starttime,$preferences->backup_course," cleaning temp data");
765 $status = clean_temp_data ($preferences);
768 //Unset CFG->backup_preferences only needed in scheduled backups
769 unset ($CFG->backup_preferences);
771 return $status;
774 //This function deletes old backup files when the "keep" limit has been reached
775 //in the destination directory.
776 function schedule_backup_course_delete_old_files($preferences,$starttime=0) {
778 global $CFG;
780 $status = true;
782 //Calculate the directory to check
783 $dirtocheck = "";
784 //if $preferences->backup_destination isn't empty, then check that directory
785 if (!empty($preferences->backup_destination)) {
786 $dirtocheck = $preferences->backup_destination;
787 //else calculate standard backup directory location
788 } else {
789 $dirtocheck = $CFG->dataroot."/".$preferences->backup_course."/backupdata";
791 schedule_backup_log($starttime,$preferences->backup_course," checking $dirtocheck");
792 if ($CFG->debug > 7) {
793 mtrace(" Keeping backup files in $dirtocheck");
796 //Get all the files in $dirtocheck
797 $files = get_directory_list($dirtocheck,"",false);
798 //Get all matching files ($preferences->keep_name) from $files
799 $matchingfiles = array();
800 foreach ($files as $file) {
801 if (substr($file, 0, strlen($preferences->keep_name)) == $preferences->keep_name) {
802 $modifieddate = filemtime($dirtocheck."/".$file);
803 $matchingfiles[$modifieddate] = $file;
806 //Sort by key (modified date) to get the oldest first (instead of doing that by name
807 //because it could give us problems in some languages with different format names).
808 ksort($matchingfiles);
810 //Count matching files
811 $countmatching = count($matchingfiles);
812 schedule_backup_log($starttime,$preferences->backup_course," found $countmatching backup files");
813 mtrace(" found $countmatching backup files");
814 if ($preferences->backup_keep < $countmatching) {
815 schedule_backup_log($starttime,$preferences->backup_course," keep limit ($preferences->backup_keep) reached. Deleting old files");
816 mtrace(" keep limit ($preferences->backup_keep) reached. Deleting old files");
817 $filestodelete = $countmatching - $preferences->backup_keep;
818 $filesdeleted = 0;
819 foreach ($matchingfiles as $matchfile) {
820 if ($filesdeleted < $filestodelete) {
821 schedule_backup_log($starttime,$preferences->backup_course," $matchfile deleted");
822 mtrace(" $matchfile deleted");
823 $filetodelete = $dirtocheck."/".$matchfile;
824 unlink($filetodelete);
825 $filesdeleted++;
829 return $status;