From: stronk7 Date: Mon, 29 Dec 2003 20:24:45 +0000 (+0000) Subject: A new parameter (backup_sche_keep) is included. Oldest backup files X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=b06021549afd4eafff5549dc947483bc7d507118;p=moodle.git A new parameter (backup_sche_keep) is included. Oldest backup files will be deleted automatically. Try it !! --- diff --git a/backup/backup_scheduled.php b/backup/backup_scheduled.php index a4dff78e2a..eaf29f0214 100644 --- a/backup/backup_scheduled.php +++ b/backup/backup_scheduled.php @@ -139,10 +139,18 @@ function schedule_backup_launch_backup($course,$starttime = 0) { schedule_backup_log($starttime,$course->id,"Start backup course $course->fullname"); schedule_backup_log($starttime,$course->id," Phase 1: Checking and counting:"); $preferences = schedule_backup_course_configure($course,$starttime); + if ($preferences) { schedule_backup_log($starttime,$course->id," Phase 2: Executing and copying:"); $status = schedule_backup_course_execute($preferences,$starttime); } + if ($status && $preferences) { + //Only if the backup_sche_keep is set + if ($preferences->backup_keep) { + schedule_backup_log($starttime,$course->id," Phase 3: Deleting old backup files:"); + $status = schedule_backup_course_delete_old_files($preferences,$starttime); + } + } if ($status && $preferences) { echo " End backup OK\n"; schedule_backup_log($starttime,$course->id,"End backup course $course->fullname - OK"); @@ -258,6 +266,9 @@ function schedule_backup_course_configure($course,$starttime = 0) { if (!isset($backup_config->backup_sche_destination)) { $backup_config->backup_sche_destination = ""; } + if (!isset($backup_config->backup_sche_keep)) { + $backup_config->backup_sche_keep = 0; + } } if ($status) { @@ -308,26 +319,52 @@ function schedule_backup_course_configure($course,$starttime = 0) { $preferences->backup_course_files = $backup_config->backup_sche_coursefiles; $preferences->backup_course = $course->id; $preferences->backup_destination = $backup_config->backup_sche_destination; + $preferences->backup_keep = $backup_config->backup_sche_keep; } //Calculate the backup string if ($status) { schedule_backup_log($starttime,$course->id," calculating backup name"); + + //Calculate the backup word //Take off some characters in the filename !! $takeoff = array(" ", ":", "/", "\\", "|"); - $backup_name = str_replace($takeoff,"_",strtolower(get_string("backupfilename"))); + $backup_word = str_replace($takeoff,"_",strtolower(get_string("backupfilename"))); //If non-translated, use "backup" - if (substr($backup_name,0,1) == "[") { - $backup_name = "backup"; + if (substr($backup_word,0,1) == "[") { + $backup_word= "backup"; } - //Calculate the format string - $backup_name_format = str_replace(" ","_",get_string("backupnameformat")); + + //Calculate the date format string + $backup_date_format = str_replace(" ","_",get_string("backupnameformat")); //If non-translated, use "%Y%m%d-%H%M" - if (substr($backup_name_format,0,1) == "[") { - $backup_name_format = "%%Y%%m%%d-%%H%%M"; - } - $backup_name .= str_replace($takeoff,"_","-".strtolower($course->shortname)."-".userdate(time(),$backup_name_format,99,false).".zip"); + if (substr($backup_date_format,0,1) == "[") { + $backup_date_format = "%%Y%%m%%d-%%H%%M"; + } + + //Calculate the shortname + $backup_shortname = clean_filename($course->shortname); + if (empty($backup_shortname) or $backup_shortname == '_' ) { + $backup_shortname = $course->id; + } + + //Calculate the final backup filename + //The backup word + $backup_name = $backup_word."-"; + //The shortname + $backup_name .= strtolower($backup_shortname)."-"; + //The date format + $backup_name .= userdate(time(),$backup_date_format,99,false); + //The extension + $backup_name .= ".zip"; + + //Calculate the string to match the keep preference + $keep_name = $backup_word."-"; + //The shortname + $keep_name .= strtolower($backup_shortname)."-"; + $preferences->backup_name = $backup_name; + $preferences->keep_name = $keep_name; } //Calculate the backup unique code to allow simultaneus backups (to define @@ -555,4 +592,60 @@ function schedule_backup_course_execute($preferences,$starttime = 0) { return $status; } +//This function deletes old backup files when the "keep" limit has been reached +//in the destination directory. +function schedule_backup_course_delete_old_files($preferences,$starttime=0) { + + global $CFG; + + $status = true; + + //Calculate the directory to check + $dirtocheck = ""; + //if $preferences->backup_destination isn't empty, then check that directory + if (!empty($preferences->backup_destination)) { + $dirtocheck = $preferences->backup_destination; + //else calculate standard backup directory location + } else { + $dirtocheck = $CFG->dataroot."/".$preferences->backup_course."/backupdata"; + } + schedule_backup_log($starttime,$preferences->backup_course," checking $dirtocheck"); + echo " Keeping backup files in $dirtocheck\n"; + + //Get all the files in $dirtocheck + $files = get_directory_list($dirtocheck,"",false); + //Get all matching files ($preferences->keep_name) from $files + $matchingfiles = array(); + foreach ($files as $file) { + if (substr($file, 0, strlen($preferences->keep_name)) == $preferences->keep_name) { + $modifieddate = filemtime($dirtocheck."/".$file); + $matchingfiles[$modifieddate] = $file; + } + } + //Sort by key (modified date) to get the oldest first (instead of doing that by name + //because it could give us problems in some languages with different format names). + ksort($matchingfiles); + + //Count matching files + $countmatching = count($matchingfiles); + schedule_backup_log($starttime,$preferences->backup_course," found $countmatching backup files"); + echo " found $countmatching backup files\n"; + if ($preferences->backup_keep < $countmatching) { + schedule_backup_log($starttime,$preferences->backup_course," keep limit ($preferences->backup_keep) reached. Deleting old files"); + echo " keep limit ($preferences->backup_keep) reached. Deleting old files\n"; + $filestodelete = $countmatching - $preferences->backup_keep; + $filesdeleted = 0; + foreach ($matchingfiles as $matchfile) { + if ($filesdeleted < $filestodelete) { + schedule_backup_log($starttime,$preferences->backup_course," $matchfile deleted"); + echo " $matchfile deleted\n"; + $filetodelete = $dirtocheck."/".$matchfile; + unlink($filetodelete); + $filesdeleted++; + } + } + } + return $status; +} + ?> diff --git a/backup/config.html b/backup/config.html index aa2f0fce9b..1b55d4c7b8 100644 --- a/backup/config.html +++ b/backup/config.html @@ -47,6 +47,9 @@ if (!isset($backup_config->backup_sche_destination)) { $backup_config->backup_sche_destination = ""; } + if (!isset($backup_config->backup_sche_keep)) { + $backup_config->backup_sche_keep = 0; + } //print_object($backup_config); //Debug @@ -61,6 +64,22 @@ //Course/alla array for use in course/all menu $courseall_array[0] = $all; $courseall_array[1] = $course; + + //Keep array for use in keep menu + $keep_array[0] = $all; + $keep_array[1] = "1"; + $keep_array[2] = "2"; + $keep_array[5] = "5"; + $keep_array[10] = "10"; + $keep_array[20] = "20"; + $keep_array[30] = "30"; + $keep_array[40] = "40"; + $keep_array[50] = "50"; + $keep_array[100] = "100"; + $keep_array[200] = "200"; + $keep_array[300] = "300"; + $keep_array[400] = "400"; + $keep_array[500] = "500"; ?>
@@ -114,6 +133,18 @@ + +

: + + backup_sche_keep, ""); + print_string("files"); + ?> + + + + + diff --git a/backup/version.php b/backup/version.php index bab2e5c009..25f8f3e678 100644 --- a/backup/version.php +++ b/backup/version.php @@ -5,6 +5,6 @@ // database (backup_version) to determine whether upgrades should // be performed (see db/backup_*.php) -$backup_version = 2003122400; // The current version is a date (YYYYMMDDXX) +$backup_version = 2003122900; // The current version is a date (YYYYMMDDXX) $backup_release = "1.2 development"; // User-friendly version number