From: stronk7 Date: Tue, 6 May 2003 22:48:25 +0000 (+0000) Subject: Included support to clean /temp/backup automatically and to write X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=3b8bad6ff01e8bcfee3a43cdc01d92397d801fa3;p=moodle.git Included support to clean /temp/backup automatically and to write the xml file (header and info only) --- diff --git a/backup/backup_execute.html b/backup/backup_execute.html index 7fc8005e4f..ec6bbd4d82 100644 --- a/backup/backup_execute.html +++ b/backup/backup_execute.html @@ -24,11 +24,18 @@ if (!isset($$var)) { $$var = 1; } + //Now stores all the mods preferences into an array into preferences + $preferences->mods[$modname]->backup = $$var; + //Check include user info $var = "backup_user_info_".$modname; if (!isset($$var)) { $$var = 1; } + //Now stores all the mods preferences into an array into preferences + $preferences->mods[$modname]->userinfo = $$var; + //And the name of the mod + $preferences->mods[$modname]->name = $modname; } } @@ -36,30 +43,43 @@ if (!isset($backup_users)) { $backup_users = 1; } + $preferences->backup_users = $backup_users; if (!isset($backup_logs)) { $backup_logs = 1; } + $preferences->backup_logs = $backup_logs; if (!isset($backup_user_files)) { $backup_user_files = 1; } + $preferences->backup_user_files = $backup_user_files; if (!isset($backup_course_files)) { $backup_course_files = 2; } + $preferences->backup_course_files = $backup_course_files; if (!isset($id)) { error ("Course not specified"); } + $preferences->backup_course = $id; if (!isset($backup_name)) { error ("Backup name not specified"); } + $preferences->backup_name = $backup_name; if (!isset($backup_unique_code)) { error ("Backup unique code not specified"); } + $preferences->backup_unique_code = $backup_unique_code; + + //Another Info + $preferences->moodle_version = $version; + $preferences->moodle_release = $release; + $preferences->backup_version = $backup_version; + $preferences->backup_release = $backup_release; if ($count == 0) { notice("No backupable modules are installed!"); @@ -79,6 +99,9 @@ //Start the main tr, where all the backup progress is done echo ""; echo ""; + + //Start the main ul + echo ""; //End the main tr, where all the backup is done + echo ""; //End the main table echo ""; diff --git a/backup/lib.php b/backup/lib.php index 860c990c07..49e6b7c0f2 100644 --- a/backup/lib.php +++ b/backup/lib.php @@ -181,7 +181,7 @@ global $CFG; //Change this if you want !! - $days = 2; + $days = 1; //End change this $seconds = $days * 24 * 60 * 60; $delete_from = time()-$seconds; @@ -192,9 +192,36 @@ $status = execute_sql("DELETE FROM {$CFG->prefix}backup_files WHERE backup_code < '$delete_from'",false); } + //Now, delete old directory (if exists) + if ($status) { + $status = backup_delete_old_dirs($delete_from); + } return($status); } + //Function to delete dirs/files into temp/backup directory + //older than $delete_from + function backup_delete_old_dirs($delete_from) { + + global $CFG; + + $status = true; + $list = get_directory_list($CFG->dataroot."/temp/backup", "", false); + foreach ($list as $file) { + $file_path = $CFG->dataroot."/temp/backup/".$file; + $moddate = filemtime($file_path); + if ($status and $moddate < $delete_from) { + $status = delete_dir_contents($file_path); + //There is nothing, delete the directory itself + if ($status) { + $status = rmdir($file_path); + } + } + } + + return $status; + } + //Function to check if a directory exists //and, optionally, create it function check_dir_exists($dir,$create=false) { @@ -243,11 +270,11 @@ if (filetype($fullfile) == "dir") { delete_dir_contents($fullfile); if (!rmdir($fullfile)) { - $status = false;; + $status = false; } } else { if (!unlink("$fullfile")) { - $status = false;; + $status = false; } } } @@ -271,5 +298,136 @@ return $status; } + + //Function to create, open and write header of the xml file + function backup_open_xml($backup_unique_code) { + + global $CFG; + + $status = true; + + //Open for writing + $file = $CFG->dataroot."/temp/backup/".$backup_unique_code."/moodle.xml"; + $backup_file = fopen($file,"w"); + //Writes the header + $status = fwrite ($backup_file,"\n"); + if ($status) { + $status = fwrite ($backup_file,start_tag("MOODLE_BACKUP",0,true)); + } + if ($status) { + return $backup_file; + } else { + return false; + } + } + + //Close the file + function backup_close_xml($backup_file) { + $status = fwrite ($backup_file,end_tag("MOODLE_BACKUP",0,true)); + return fclose($backup_file); + } + + //Return the xml start tag + function start_tag($tag,$level=0,$endline=false) { + if ($endline) { + $endchar = "\n"; + } else { + $endchar = ""; + } + return str_repeat(" ",$level*2)."<".strtoupper($tag).">".$endchar; + } + + //Return the xml end tag + function end_tag($tag,$level=0,$endline=true) { + if ($endline) { + $endchar = "\n"; + } else { + $endchar = ""; + } + return str_repeat(" ",$level*2)."".$endchar; + } + + //Return the start tag, the contents and the end tag + function full_tag($tag,$level=0,$endline=true,$content,$to_utf=true) { + $st = start_tag($tag,$level,$endline); + $co=""; + if ($to_utf) { + $co = $content; + } else { + $co = $content; + } + $et = end_tag($tag,0,true); + return $st.$co.$et; + } + + //Prints General info about the course + //name, moodle_version (internal and release), backup_version, date, info in file... + function backup_general_info ($bf,$preferences) { + global $CFG; + + fwrite ($bf,start_tag("INFO",1,true)); + + //The name of the backup + fwrite ($bf,full_tag("NAME",2,false,$preferences->backup_name)); + //The moodle_version + fwrite ($bf,full_tag("MOODLE_VERSION",2,false,$preferences->moodle_version)); + fwrite ($bf,full_tag("MOODLE_RELEASE",2,false,$preferences->moodle_release)); + //The backup_version + fwrite ($bf,full_tag("BACKUP_VERSION",2,false,$preferences->backup_version)); + fwrite ($bf,full_tag("BACKUP_RELEASE",2,false,$preferences->backup_release)); + //The date + fwrite ($bf,full_tag("DATE",2,false,$preferences->backup_unique_code)); + //Te includes tag + fwrite ($bf,start_tag("DETAILS",2,true)); + //Now, go to mod element of preferences to print its status + foreach ($preferences->mods as $element) { + //Calculate info + $included = "false"; + $userinfo = "false"; + if ($element->backup) { + $included = "true"; + if ($element->userinfo) { + $userinfo = "true"; + } + } + //Prints the mod start + fwrite ($bf,start_tag("MOD",3,true)); + fwrite ($bf,full_tag("NAME",4,false,$element->name)); + fwrite ($bf,full_tag("INCLUDED",4,false,$included)); + fwrite ($bf,full_tag("USERINFO",4,false,$userinfo)); + + //Print the end + fwrite ($bf,end_tag("MOD",3,true)); + } + //The user in backup + if ($preferences->backup_users == 1) { + fwrite ($bf,full_tag("USERS",3,false,"COURSE")); + } else { + fwrite ($bf,full_tag("USERS",3,false,"ALL")); + } + //The logs in backup + if ($preferences->backup_logs == 1) { + fwrite ($bf,full_tag("LOGS",3,false,"true")); + } else { + fwrite ($bf,full_tag("LOGS",3,false,"false")); + } + //The user files + if ($preferences->backup_user_files == 1) { + fwrite ($bf,full_tag("USERFILES",3,false,"true")); + } else { + fwrite ($bf,full_tag("USERFILES",3,false,"false")); + } + //The course files + if ($preferences->backup_course_files == 1) { + fwrite ($bf,full_tag("COURSEFILES",3,false,"true")); + } else { + fwrite ($bf,full_tag("COURSEFILES",3,false,"false")); + } + + fwrite ($bf,end_tag("DETAILS",2,true)); + + + fwrite ($bf,end_tag("INFO",1,true)); + } ?>