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() {
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!");
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
) {
30 } else if (isset($backup_config->backup_sche_running
) && $backup_config->backup_sche_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");
38 $timetoseemin = $timetosee/60;
39 mtrace(" No activity in last ".$timetoseemin." minutes. Unlocking status");
41 mtrace(" Scheduled backup seems to be running. Execution delayed");
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");
57 //Delete old_entries from backup tables
59 mtrace(" Deleting old data");
60 $status = backup_delete_old_data();
63 //Now we get a list of courses in the server
65 mtrace(" Checking courses");
66 //First of all, we delete everything from backup tables related to deleted courses
67 mtrace(" Skipping deleted courses");
69 if ($bckcourses = get_records('backup_courses')) {
70 foreach($bckcourses as $bckcourse) {
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");
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
85 foreach ($courses as $course) {
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);
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)");
103 // Skip backup of unavailable courses that have remained unmodified in a month
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
);
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) {
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
);
122 $course_status = schedule_backup_launch_backup($course,$starttime);
124 set_field("backup_courses","lastendtime",time(),"courseid",$backup_course->courseid
);
126 if ($course_status) {
127 set_field("backup_courses","laststatus","1","courseid",$backup_course->courseid
);
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
);
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");
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
157 mtrace(" Sending email to admin");
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
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";
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');
187 $message .= " ".get_string('backupfinished')."\n";
190 //Build the message subject
192 $prefix = $site->shortname
.": ";
193 if ($count_error != 0 ||
$count_unfinished != 0) {
194 $prefix .= "[".strtoupper(get_string('error'))."] ";
196 $subject = $prefix.get_string("scheduledbackupstatus");
199 email_to_user($admin,$admin,$subject,$message);
203 //Everything is finished stop backup_sche_running
204 backup_set_config("backup_sche_running","0");
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;
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);
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");
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) {
250 $log->courseid
= $courseid;
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) {
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) {
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);
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) {
305 schedule_backup_log($starttime,$course->id
," checking parameters");
307 //Check the required variable
308 if (empty($course->id
)) {
311 //Get scheduled backup preferences
312 $backup_config = backup_get_config();
314 //Checks backup_config pairs exist
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_blogs
)) {
344 $backup_config->backup_sche_blogs
= 0;
346 if (!isset($backup_config->backup_sche_active
)) {
347 $backup_config->backup_sche_active
= 0;
349 if (!isset($backup_config->backup_sche_weekdays
)) {
350 $backup_config->backup_sche_weekdays
= "0000000";
352 if (!isset($backup_config->backup_sche_hour
)) {
353 $backup_config->backup_sche_hour
= 00;
355 if (!isset($backup_config->backup_sche_minute
)) {
356 $backup_config->backup_sche_minute
= 00;
358 if (!isset($backup_config->backup_sche_destination
)) {
359 $backup_config->backup_sche_destination
= "";
361 if (!isset($backup_config->backup_sche_keep
)) {
362 $backup_config->backup_sche_keep
= 1;
367 //Checks for the required files/functions to backup every mod
368 //And check if there is data about it
370 if ($allmods = get_records("modules") ) {
371 foreach ($allmods as $mod) {
372 $modname = $mod->name
;
373 $modfile = "$CFG->dirroot/mod/$modname/backuplib.php";
374 $modbackup = $modname."_backup_mods";
375 $modcheckbackup = $modname."_check_backup_mods";
376 if (file_exists($modfile)) {
377 include_once($modfile);
378 if (function_exists($modbackup) and function_exists($modcheckbackup)) {
379 $var = "exists_".$modname;
383 // PENNY NOTES: I have moved from here to the closing brace inside
384 // by two sets of ifs()
385 // to avoid the backup failing on a non existant backup.
386 // If the file/function/whatever doesn't exist, we don't want to set this
387 // this module in backup preferences at all.
390 $var = "backup_".$modname;
392 $
$var = $backup_config->backup_sche_modules
;
394 //Now stores all the mods preferences into an array into preferences
395 $preferences->mods
[$modname]->backup
= $
$var;
397 //Check include user info
398 $var = "backup_user_info_".$modname;
400 $
$var = $backup_config->backup_sche_withuserdata
;
402 //Now stores all the mods preferences into an array into preferences
403 $preferences->mods
[$modname]->userinfo
= $
$var;
404 //And the name of the mod
405 $preferences->mods
[$modname]->name
= $modname;
412 if ($coursemods = get_course_mods($course->id
)) {
413 foreach ($coursemods as $mod) {
414 if (array_key_exists($mod->modname
,$preferences->mods
)) { // we are to backup this module
415 if (empty($preferences->mods
[$mod->modname
]->instances
)) {
416 $preferences->mods
[$mod->modname
]->instances
= array(); // avoid warnings
418 $preferences->mods
[$mod->modname
]->instances
[$mod->instance
]->backup
= $preferences->mods
[$mod->modname
]->backup
;
419 $preferences->mods
[$mod->modname
]->instances
[$mod->instance
]->userinfo
= $preferences->mods
[$mod->modname
]->userinfo
;
420 // there isn't really a nice way to do this...
421 $preferences->mods
[$mod->modname
]->instances
[$mod->instance
]->name
= get_field($mod->modname
,'name','id',$mod->instance
);
426 // finally, clean all the $preferences->mods[] not having instances. Nothing to backup about them
427 foreach ($preferences->mods
as $modname => $mod) {
428 if (!isset($mod->instances
)) {
429 unset($preferences->mods
[$modname]);
434 //Convert other parameters
436 $preferences->backup_metacourse
= $backup_config->backup_sche_metacourse
;
437 $preferences->backup_users
= $backup_config->backup_sche_users
;
438 $preferences->backup_logs
= $backup_config->backup_sche_logs
;
439 $preferences->backup_user_files
= $backup_config->backup_sche_userfiles
;
440 $preferences->backup_course_files
= $backup_config->backup_sche_coursefiles
;
441 $preferences->backup_site_files
= $backup_config->backup_sche_sitefiles
;
442 $preferences->backup_messages
= $backup_config->backup_sche_messages
;
443 $preferences->backup_blogs
= $backup_config->backup_sche_blogs
;
444 $preferences->backup_course
= $course->id
;
445 $preferences->backup_destination
= $backup_config->backup_sche_destination
;
446 $preferences->backup_keep
= $backup_config->backup_sche_keep
;
449 //Calculate various backup preferences
451 schedule_backup_log($starttime,$course->id
," calculating backup name");
453 //Calculate the backup file name
454 $backup_name = backup_get_zipfile_name($course);
456 //Calculate the string to match the keep preference
457 $keep_name = backup_get_keep_name($course);
460 $preferences->backup_name
= $backup_name;
461 $preferences->keep_name
= $keep_name;
464 $roles = get_records('role', '', '', 'sortorder');
465 foreach ($roles as $role) {
466 $preferences->backuproleassignments
[$role->id
] = $role;
470 backup_add_static_preferences($preferences);
473 //Calculate the backup unique code to allow simultaneus backups (to define
474 //the temp-directory name and records in backup temp tables
476 $backup_unique_code = time();
477 $preferences->backup_unique_code
= $backup_unique_code;
480 //Calculate necesary info to backup modules
482 schedule_backup_log($starttime,$course->id
," calculating modules data");
483 if ($allmods = get_records("modules") ) {
484 foreach ($allmods as $mod) {
485 $modname = $mod->name
;
486 $modbackup = $modname."_backup_mods";
487 //If exists the lib & function
488 $var = "exists_".$modname;
489 if (isset($
$var) && $
$var) {
491 $var = "backup_".$modname;
494 $var = "backup_user_info_".$modname;
495 //Call the check function to show more info
496 $modcheckbackup = $modname."_check_backup_mods";
497 schedule_backup_log($starttime,$course->id
," $modname");
498 $modcheckbackup($course->id
,$
$var,$backup_unique_code);
505 //Now calculate the users
507 schedule_backup_log($starttime,$course->id
," calculating users");
508 //Decide about include users with messages, based on SITEID
509 if ($preferences->backup_messages
&& $preferences->backup_course
== SITEID
) {
510 $include_message_users = true;
512 $include_message_users = false;
514 //Decide about include users with blogs, based on SITEID
515 if ($preferences->backup_blogs
&& $preferences->backup_course
== SITEID
) {
516 $include_blog_users = true;
518 $include_blog_users = false;
520 user_check_backup($course->id
,$backup_unique_code,$preferences->backup_users
,$include_message_users, $include_blog_users);
523 //Now calculate the logs
525 if ($preferences->backup_logs
) {
526 schedule_backup_log($starttime,$course->id
," calculating logs");
527 log_check_backup($course->id
);
531 //Now calculate the userfiles
533 if ($preferences->backup_user_files
) {
534 schedule_backup_log($starttime,$course->id
," calculating user files");
535 user_files_check_backup($course->id
,$preferences->backup_unique_code
);
539 //Now calculate the coursefiles
541 if ($preferences->backup_course_files
) {
542 schedule_backup_log($starttime,$course->id
," calculating course files");
543 course_files_check_backup($course->id
,$preferences->backup_unique_code
);
547 //Now calculate the sitefiles
549 if ($preferences->backup_site_files
) {
550 schedule_backup_log($starttime,$course->id
," calculating site files");
551 site_files_check_backup($course->id
,$preferences->backup_unique_code
);
555 //If everything is ok, return calculated preferences
557 $status = $preferences;
563 //TODO: Unify this function with backup_execute() to have both backups 100% equivalent. Moodle 2.0
565 //This function implements all the needed code to backup a course
566 //copying it to the desired destination (default if not specified)
567 function schedule_backup_course_execute($preferences,$starttime = 0) {
573 //Some parts of the backup doesn't know about $preferences, so we
574 //put a copy of it inside that CFG (always global) to be able to
575 //use it. Then, when needed I search for preferences inside CFG
576 //Used to avoid some problems in full_tag() when preferences isn't
577 //set globally (i.e. in scheduled backups)
578 $CFG->backup_preferences
= $preferences;
580 //Check for temp and backup and backup_unique_code directory
581 //Create them as needed
582 schedule_backup_log($starttime,$preferences->backup_course
," checking temp structures");
583 $status = check_and_create_backup_dir($preferences->backup_unique_code
);
586 schedule_backup_log($starttime,$preferences->backup_course
," cleaning current dir");
587 $status = clear_backup_dir($preferences->backup_unique_code
);
590 //Create the moodle.xml file
592 schedule_backup_log($starttime,$preferences->backup_course
," creating backup file");
593 //Obtain the xml file (create and open) and print prolog information
594 $backup_file = backup_open_xml($preferences->backup_unique_code
);
595 //Prints general info about backup to file
597 schedule_backup_log($starttime,$preferences->backup_course
," general info");
598 $status = backup_general_info($backup_file,$preferences);
603 //Prints course start (tag and general info)
605 $status = backup_course_start($backup_file,$preferences);
608 //Metacourse information
609 if ($status && $preferences->backup_metacourse
) {
610 schedule_backup_log($starttime,$preferences->backup_course
," metacourse info");
611 $status = backup_course_metacourse($backup_file,$preferences);
616 schedule_backup_log($starttime,$preferences->backup_course
," blocks info");
617 $status = backup_course_blocks($backup_file,$preferences);
622 schedule_backup_log($starttime,$preferences->backup_course
," sections info");
623 $status = backup_course_sections($backup_file,$preferences);
628 schedule_backup_log($starttime,$preferences->backup_course
," user info");
629 $status = backup_user_info($backup_file,$preferences);
632 //If we have selected to backup messages and we are
633 //doing a SITE backup, let's do it
634 if ($status && $preferences->backup_messages
&& $preferences->backup_course
== SITEID
) {
635 schedule_backup_log($starttime,$preferences->backup_course
," messages");
636 $status = backup_messages($backup_file,$preferences);
639 //If we have selected to backup blogs and we are
640 //doing a SITE backup, let's do it
641 if ($status && $preferences->backup_blogs
&& $preferences->backup_course
== SITEID
) {
642 schedule_backup_log($starttime,$preferences->backup_course
," blogs");
643 $status = backup_blogs($backup_file,$preferences);
646 //If we have selected to backup quizzes, backup categories and
647 //questions structure (step 1). See notes on mod/quiz/backuplib.php
648 if ($status and $preferences->mods
['quiz']->backup
) {
649 schedule_backup_log($starttime,$preferences->backup_course
," categories & questions");
650 $status = backup_question_categories($backup_file,$preferences);
653 //Print logs if selected
655 if ($preferences->backup_logs
) {
656 schedule_backup_log($starttime,$preferences->backup_course
," logs");
657 $status = backup_log_info($backup_file,$preferences);
663 schedule_backup_log($starttime,$preferences->backup_course
," scales");
664 $status = backup_scales_info($backup_file,$preferences);
669 schedule_backup_log($starttime,$preferences->backup_course
," groups");
670 $status = backup_groups_info($backup_file,$preferences);
673 //Print groupings info
675 schedule_backup_log($starttime,$preferences->backup_course
," groupings");
676 $status = backup_groupings_info($backup_file,$preferences);
679 //Print groupings_groups info
681 schedule_backup_log($starttime,$preferences->backup_course
," groupings_groups");
682 $status = backup_groupings_groups_info($backup_file,$preferences);
687 schedule_backup_log($starttime,$preferences->backup_course
," events");
688 $status = backup_events_info($backup_file,$preferences);
691 //Print gradebook info
693 schedule_backup_log($starttime,$preferences->backup_course
," gradebook");
694 $status = backup_gradebook_info($backup_file,$preferences);
697 //Module info, this unique function makes all the work!!
698 //db export and module fileis copy
700 $mods_to_backup = false;
701 //Check if we have any mod to backup
702 foreach ($preferences->mods
as $module) {
703 if ($module->backup
) {
704 $mods_to_backup = true;
707 //If we have to backup some module
708 if ($mods_to_backup) {
709 schedule_backup_log($starttime,$preferences->backup_course
," modules");
711 $status = backup_modules_start ($backup_file,$preferences);
712 //Iterate over modules and call backup
713 foreach ($preferences->mods
as $module) {
714 if ($module->backup
and $status) {
715 schedule_backup_log($starttime,$preferences->backup_course
," $module->name");
716 $status = backup_module($backup_file,$preferences,$module->name
);
720 $status = backup_modules_end ($backup_file,$preferences);
724 //Backup course format data, if any.
726 schedule_backup_log($starttime,$preferences->backup_course
," course format data");
727 $status = backup_format_data($backup_file,$preferences);
732 $status = backup_course_end($backup_file,$preferences);
735 //Close the xml file and xml data
737 backup_close_xml($backup_file);
741 //Now, if selected, copy user files
743 if ($preferences->backup_user_files
) {
744 schedule_backup_log($starttime,$preferences->backup_course
," copying user files");
745 $status = backup_copy_user_files ($preferences);
749 //Now, if selected, copy course files
751 if ($preferences->backup_course_files
) {
752 schedule_backup_log($starttime,$preferences->backup_course
," copying course files");
753 $status = backup_copy_course_files ($preferences);
757 //Now, if selected, copy site files
759 if ($preferences->backup_site_files
) {
760 schedule_backup_log($starttime,$preferences->backup_course
," copying site files");
761 $status = backup_copy_site_files ($preferences);
765 //Now, zip all the backup directory contents
767 schedule_backup_log($starttime,$preferences->backup_course
," zipping files");
768 $status = backup_zip ($preferences);
771 //Now, copy the zip file to course directory
773 schedule_backup_log($starttime,$preferences->backup_course
," copying backup");
774 $status = copy_zip_to_course_dir ($preferences);
777 //Now, clean temporary data (db and filesystem)
779 schedule_backup_log($starttime,$preferences->backup_course
," cleaning temp data");
780 $status = clean_temp_data ($preferences);
783 //Unset CFG->backup_preferences only needed in scheduled backups
784 unset ($CFG->backup_preferences
);
789 //This function deletes old backup files when the "keep" limit has been reached
790 //in the destination directory.
791 function schedule_backup_course_delete_old_files($preferences,$starttime=0) {
797 //Calculate the directory to check
799 //if $preferences->backup_destination isn't empty, then check that directory
800 if (!empty($preferences->backup_destination
)) {
801 $dirtocheck = $preferences->backup_destination
;
802 //else calculate standard backup directory location
804 $dirtocheck = $CFG->dataroot
."/".$preferences->backup_course
."/backupdata";
806 schedule_backup_log($starttime,$preferences->backup_course
," checking $dirtocheck");
807 if ($CFG->debug
> 7) {
808 mtrace(" Keeping backup files in $dirtocheck");
811 //Get all the files in $dirtocheck
812 $files = get_directory_list($dirtocheck,"",false);
813 //Get all matching files ($preferences->keep_name) from $files
814 $matchingfiles = array();
815 foreach ($files as $file) {
816 if (substr($file, 0, strlen($preferences->keep_name
)) == $preferences->keep_name
) {
817 $modifieddate = filemtime($dirtocheck."/".$file);
818 $matchingfiles[$modifieddate] = $file;
821 //Sort by key (modified date) to get the oldest first (instead of doing that by name
822 //because it could give us problems in some languages with different format names).
823 ksort($matchingfiles);
825 //Count matching files
826 $countmatching = count($matchingfiles);
827 schedule_backup_log($starttime,$preferences->backup_course
," found $countmatching backup files");
828 mtrace(" found $countmatching backup files");
829 if ($preferences->backup_keep
< $countmatching) {
830 schedule_backup_log($starttime,$preferences->backup_course
," keep limit ($preferences->backup_keep) reached. Deleting old files");
831 mtrace(" keep limit ($preferences->backup_keep) reached. Deleting old files");
832 $filestodelete = $countmatching - $preferences->backup_keep
;
834 foreach ($matchingfiles as $matchfile) {
835 if ($filesdeleted < $filestodelete) {
836 schedule_backup_log($starttime,$preferences->backup_course
," $matchfile deleted");
837 mtrace(" $matchfile deleted");
838 $filetodelete = $dirtocheck."/".$matchfile;
839 unlink($filetodelete);