Permalink
Browse files

Changes in:

   - Saving temp data to fs, instead of db
     (mysql limit bug OUT)
   - Modified clean directories (moodle standard delete dir
     function seems to work wrong with large dirs
   - Send info to browser continuosly to avoid timeouts.
   - Increase php execution time limit
   - More checks.
   - Restore into existing course now works fine
   - Create course_dir if it doesn't exist in the final step
     of backup.
  • Loading branch information...
1 parent 28a1cb4 commit af9cd9552e0c2b6f2d768225a9cef7243ba6e874 stronk7 committed Jun 20, 2003
Showing with 191 additions and 68 deletions.
  1. +25 −6 backup/backuplib.php
  2. +124 −25 backup/lib.php
  3. +42 −37 backup/restorelib.php
View
@@ -866,13 +866,32 @@ function copy_zip_to_course_dir ($preferences) {
global $CFG;
- //Backup zip file name
+ //Define zip location (from)
$from_zip_file = $CFG->dataroot."/temp/backup/".$preferences->backup_unique_code."/".$preferences->backup_name;
- $to_zip_file = $CFG->dataroot."/".$preferences->backup_course."/".get_string("backupdir")."/".$preferences->backup_name;
-
- //Check to directory
- $status = check_dir_exists(dirname($to_zip_file),true);
-
+
+ //Define zip destination (course dir)
+ $to_zip_file = $CFG->dataroot."/".$preferences->backup_course;
+
+ echo "<p>From: ".$from_zip_file."<br>"; //Debug
+
+ echo "<p>Checking: ".$to_zip_file."<br>"; //Debug
+
+ //Checks course dir exists
+ $status = check_dir_exists($to_zip_file,true);
+
+ //Define zip destination (backup dir)
+ $to_zip_file = $to_zip_file."/".get_string("backupdir");
+
+ echo "<p>Checking: ".$to_zip_file."<br>"; //Debug
+
+ //Checks backup dir exists
+ $status = check_dir_exists($to_zip_file,true);
+
+ //Define zip destination (zip file)
+ $to_zip_file = $to_zip_file."/".$preferences->backup_name;
+
+ echo "<p>To: ".$to_zip_file."<br>"; //Debug
+
//Copy zip file
if ($status) {
$status = backup_copy_file ($from_zip_file,$to_zip_file);
View
@@ -38,7 +38,7 @@ function backup_delete_old_dirs($delete_from) {
foreach ($list as $file) {
$file_path = $CFG->dataroot."/temp/backup/".$file;
$moddate = filemtime($file_path);
- if ($status and $moddate < $delete_from) {
+ if ($status && $moddate < $delete_from) {
//If directory, recurse
if (is_dir($file_path)) {
$status = delete_dir_contents($file_path);
@@ -92,36 +92,62 @@ function check_and_create_backup_dir($backup_unique_code) {
}
//Function to delete all the directory contents recursively
- //Copied from admin/delete.php
- function delete_dir_contents ($rootdir) {
+ //Copied from the web !!
+ function delete_dir_contents ($dir) {
- $dir = opendir($rootdir);
+ $slash = "/";
- $status = true;
+ // Create arrays to store files and directories
+ $dir_files = array();
+ $dir_subdirs = array();
- while ($file = readdir($dir)) {
- if ($file != "." and $file != "..") {
- $fullfile = "$rootdir/$file";
- if (filetype($fullfile) == "dir") {
- delete_dir_contents($fullfile);
- if (!rmdir($fullfile)) {
- $status = false;
- }
- } else {
- if (!unlink("$fullfile")) {
- $status = false;
- }
+ // Make sure we can delete it
+ chmod($dir, 0777);
+
+ if ((($handle = opendir($dir))) == FALSE) {
+ // The directory could not be opened
+ return false;
+ }
+
+ // Loop through all directory entries, and construct two temporary arrays containing files and sub directories
+ while($entry = readdir($handle)) {
+ if (is_dir($dir. $slash .$entry) && $entry != ".." && $entry != ".") {
+ $dir_subdirs[] = $dir. $slash .$entry;
+ }
+ else if ($entry != ".." && $entry != ".") {
+ $dir_files[] = $dir. $slash .$entry;
+ }
+ }
+
+ // Delete all files in the curent directory return false and halt if a file cannot be removed
+ for($i=0; $i<count($dir_files); $i++) {
+ chmod($dir_files[$i], 0777);
+ if (((unlink($dir_files[$i]))) == FALSE) {
+ return false;
+ }
+ }
+
+ // Empty sub directories and then remove the directory
+ for($i=0; $i<count($dir_subdirs); $i++) {
+ chmod($dir_subdirs[$i], 0777);
+ if (delete_dir_contents($dir_subdirs[$i]) == FALSE) {
+ return false;
+ }
+ else {
+ if (rmdir($dir_subdirs[$i]) == FALSE) {
+ return false;
}
}
}
- closedir($dir);
-
- return $status;
+ // Close directory
+ closedir($handle);
+
+ // Success, every thing is gone return true
+ return true;
}
//Function to clear (empty) the contents of the backup_dir
- //Copied from admin/delete.php
function clear_backup_dir($backup_unique_code) {
global $CFG;
@@ -305,23 +331,50 @@ function upgrade_backup_db($updgradeto,$backup_release,$continueto) {
}
//This function is used to insert records in the backup_ids table
+ //If the info field is greater than max_db_storage, then its info
+ //is saved to filesystem
function backup_putid ($backup_unique_code, $table, $old_id, $new_id, $info="") {
global $CFG;
+
+ $max_db_storage = 128; //Max bytes to save to db, else save to file
+ //MUST BE LOWER THAN 256 (VARCHAR LIMIT)
$status = true;
//First delete to avoid PK duplicates
$status = backup_delid($backup_unique_code, $table, $old_id);
- $status = execute_sql("INSERT INTO {$CFG->prefix}backup_ids
- (backup_code, table_name, old_id, new_id, info)
- VALUES
- ($backup_unique_code, '$table', '$old_id', '$new_id', '$info')",false);
+ //Now, serialize info
+ $info_ser = serialize($info);
+
+ //Now, if the size of $info_ser > $max_db_storage, save it to filesystem and
+ //insert a "infile" in the info field
+
+ if (strlen($info_ser) > $max_db_storage) {
+ //Calculate filename (in current_backup_dir, $backup_unique_code_$table_$old_id.info)
+ $filename = $CFG->dataroot."/temp/backup/".$backup_unique_code."/".$backup_unique_code."_".$table."_".$old_id.".info";
+ //Save data to file
+ $status = backup_data2file($filename,$info_ser);
+ //Set info_to save
+ $info_to_save = "infile";
+ } else {
+ //Saving to db, addslashes
+ $info_to_save = addslashes($info_ser);
+ }
+
+ //Now, insert the record
+ if ($status) {
+ $status = execute_sql("INSERT INTO {$CFG->prefix}backup_ids
+ (backup_code, table_name, old_id, new_id, info)
+ VALUES
+ ($backup_unique_code, '$table', '$old_id', '$new_id', '$info_to_save')",false);
+ }
return $status;
}
//This function is used to delete recods from the backup_ids table
+ //If the info field is "infile" then the file is deleted too
function backup_delid ($backup_unique_code, $table, $old_id) {
global $CFG;
@@ -336,16 +389,37 @@ function backup_delid ($backup_unique_code, $table, $old_id) {
}
//This function is used to get a record from the backup_ids table
+ //If the info field is "infile" then its info
+ //is read from filesystem
function backup_getid ($backup_unique_code, $table, $old_id) {
global $CFG;
$status = true;
+ $status2 = true;
$status = get_record ("backup_ids","backup_code",$backup_unique_code,
"table_name",$table,
"old_id", $old_id);
+ //If info field = "infile", get file contents
+ if ($status->info == "infile") {
+ $filename = $CFG->dataroot."/temp/backup/".$backup_unique_code."/".$backup_unique_code."_".$table."_".$old_id.".info";
+ //Read data from file
+ $status2 = backup_file2data($filename,$info);
+ if ($status2) {
+ //unserialize data
+ $status->info = unserialize($info);
+ } else {
+ $status = false;
+ }
+ } else {
+ ////First strip slashes
+ $temp = stripslashes($status->info);
+ //Now unserialize
+ $status->info = unserialize($temp);
+ }
+
return $status;
}
@@ -378,5 +452,30 @@ function backup_flush($n=0,$time=false) {
flush();
}
+ //This function creates the filename and write data to it
+ //returning status as result
+ function backup_data2file ($file,&$data) {
+
+ $status = true;
+ $status2 = true;
+
+ $f = fopen($file,"w");
+ $status = fwrite($f,$data);
+ $status2 = fclose($f);
+
+ return ($status && $status2);
+ }
+ //This function read the filename and read data from it
+ function backup_file2data ($file,&$data) {
+
+ $status = true;
+ $status2 = true;
+
+ $f = fopen($file,"r");
+ $data = fread ($f,filesize($file));
+ $status2 = fclose($f);
+
+ return ($status && $status2);
+ }
?>
Oops, something went wrong.

0 comments on commit af9cd95

Please sign in to comment.