]> git.mjollnir.org Git - moodle.git/commitdiff
A new parameter (backup_sche_keep) is included. Oldest backup files
authorstronk7 <stronk7>
Mon, 29 Dec 2003 20:24:45 +0000 (20:24 +0000)
committerstronk7 <stronk7>
Mon, 29 Dec 2003 20:24:45 +0000 (20:24 +0000)
will be deleted automatically. Try it !!

backup/backup_scheduled.php
backup/config.html
backup/version.php

index a4dff78e2af7667a29e019358d206faec1880705..eaf29f021425828a0882ff4360da2ae223de5b37 100644 (file)
@@ -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;
+}
+
 ?>
index aa2f0fce9bef39dfba13f943196e1bb62f3262a2..1b55d4c7b8649cd1fc8f0c84025ef52adf36aee5 100644 (file)
@@ -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
 
     //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";
 ?>
 <form method="post" action="backup.php" name="form">
 
     <?php print_string("backupcoursefileshelp") ?>
     </td>
 </tr>
+<tr valign=top>
+    <td align=right><p><?php print_string("keep") ?>:</td>
+    <td>
+    <?php
+        choose_from_menu($keep_array, "backup_sche_keep", $backup_config->backup_sche_keep, "");
+        print_string("files");
+     ?>
+    </td>
+    <td>
+    <?php print_string("backupkeephelp") ?>
+    </td>
+</tr>
 <tr valign=top>
     <td colspan = 3 align=center><strong><?php print_string("schedule") ?></strong></td>
 </tr>
index bab2e5c009efe1ee7191bb3984a9b79dfb7b2ad2..25f8f3e678a508d1a0b08327b32cd62681060f37 100644 (file)
@@ -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