From 10dcde409b5051fad333aaef5bc191a0ff56e170 Mon Sep 17 00:00:00 2001 From: moodler Date: Fri, 1 Aug 2003 13:59:17 +0000 Subject: [PATCH] Further integration of backup into main Moodle. The database checks/upgrades are now done on the main admin page like the other modules. The database files are now standard format .sql and .php files PostgreSQL support was added - BUT HAS NOT BEEN TESTED YET!! --- admin/index.php | 6 ++ backup/backup.php | 4 +- backup/db/{backup_mysql.php => mysql.php} | 0 backup/db/mysql.sql | 27 +++++++ backup/db/postgres7.php | 34 +++++++++ backup/db/postgres7.sql | 34 +++++++++ backup/lib.php | 92 +++++++++++++---------- backup/restore.php | 10 +-- 8 files changed, 155 insertions(+), 52 deletions(-) rename backup/db/{backup_mysql.php => mysql.php} (100%) create mode 100644 backup/db/mysql.sql create mode 100644 backup/db/postgres7.php create mode 100644 backup/db/postgres7.sql diff --git a/admin/index.php b/admin/index.php index 837ecf29af8..71ef12c3162 100644 --- a/admin/index.php +++ b/admin/index.php @@ -209,6 +209,12 @@ } +/// Upgrade backup/restore system if necessary + + require_once("$CFG->dirroot/backup/lib.php"); + upgrade_backup_db("$CFG->wwwroot/$CFG->admin/index.php"); // Return here afterwards + + /// Find and check all modules and load them up or upgrade them if necessary if (!$mods = get_list_of_plugins("mod") ) { diff --git a/backup/backup.php b/backup/backup.php index 46879f32d4f..eb516896175 100644 --- a/backup/backup.php +++ b/backup/backup.php @@ -14,8 +14,6 @@ //Units used require_once ("$moodle_home/config.php"); require_once ("$moodle_home/version.php"); - require_once ("version.php"); - require_once ("db/backup_$CFG->dbtype.php"); require_once ("lib.php"); require_once ("backuplib.php"); @@ -39,7 +37,7 @@ backup_required_functions(); //Check backup_version - upgrade_backup_db($backup_version,$backup_release,"backup.php"); + upgrade_backup_db("backup.php"); //Get strings $strcoursebackup = get_string("coursebackup"); diff --git a/backup/db/backup_mysql.php b/backup/db/mysql.php similarity index 100% rename from backup/db/backup_mysql.php rename to backup/db/mysql.php diff --git a/backup/db/mysql.sql b/backup/db/mysql.sql new file mode 100644 index 00000000000..696f2ac5768 --- /dev/null +++ b/backup/db/mysql.sql @@ -0,0 +1,27 @@ +# +# Table structure for table `prefix_backup_files` +# + +CREATE TABLE `prefix_backup_files` ( + `backup_code` int(10) unsigned NOT NULL default '0', + `file_type` varchar(10) NOT NULL default '', + `path` varchar(255) NOT NULL default '', + `old_id` int(10) unsigned default NULL, + `new_id` int(10) unsigned default NULL, + PRIMARY KEY (`backup_code`,`file_type`,`path`) +) TYPE=MyISAM COMMENT='To store and recode ids to user and course files.'; +# -------------------------------------------------------- + +# +# Table structure for table `prefix_backup_ids` +# + +CREATE TABLE `prefix_backup_ids` ( + `backup_code` int(12) unsigned NOT NULL default '0', + `table_name` varchar(30) NOT NULL default '', + `old_id` int(10) unsigned NOT NULL default '0', + `new_id` int(10) unsigned default NULL, + `info` mediumtext, + PRIMARY KEY (`backup_code`,`table_name`,`old_id`) +) TYPE=MyISAM COMMENT='To store and convert ids in backup/restore'; + diff --git a/backup/db/postgres7.php b/backup/db/postgres7.php new file mode 100644 index 00000000000..3bd5f28eb17 --- /dev/null +++ b/backup/db/postgres7.php @@ -0,0 +1,34 @@ + diff --git a/backup/db/postgres7.sql b/backup/db/postgres7.sql new file mode 100644 index 00000000000..8a951ee1f19 --- /dev/null +++ b/backup/db/postgres7.sql @@ -0,0 +1,34 @@ +# THIS FILE IS UNTESTED!!! +# PLEASE HELP TEST/FIX IT AND CONTACT MARTIN OR ELOY! + +# +# Table structure for table prefix_backup_files +# + +CREATE TABLE prefix_backup_files ( + prefix_backup_codetypepath_idx PRIMARY KEY, + backup_code integer NOT NULL default '0', + file_type varchar(10) NOT NULL default '', + path varchar(255) NOT NULL default '', + old_id integer default NULL, + new_id integer default NULL, +) TYPE=MyISAM COMMENT='To store and recode ids to user and course files.'; +# -------------------------------------------------------- + +CREATE INDEX prefix_backup_codetypepath_idx ON prefix_backup_files (backup_code,file_type,path) + +# +# Table structure for table prefix_backup_ids +# + +CREATE TABLE prefix_backup_ids ( + prefix_backup_codenameid_idx PRIMARY KEY, + backup_code int(12) unsigned NOT NULL default '0', + table_name varchar(30) NOT NULL default '', + old_id int(10) unsigned NOT NULL default '0', + new_id int(10) unsigned default NULL, + info mediumtext, +) TYPE=MyISAM COMMENT='To store and convert ids in backup/restore'; + +CREATE INDEX prefix_backup_codenameid_idx ON prefix_backup_ids (backup_code,table_name,old_id) + diff --git a/backup/lib.php b/backup/lib.php index 84611420eed..d0491e3a5bd 100644 --- a/backup/lib.php +++ b/backup/lib.php @@ -287,53 +287,63 @@ // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ - //This function upgrades, if necesary, the backup-restore tables - //It's called from backup.php and restore.php - function upgrade_backup_db($updgradeto,$backup_release,$continueto) { - - global $CFG,$db; - //Check backup_version - if ($CFG->backup_version) { - if ($updgradeto > $CFG->backup_version) { // upgrade - $a->oldversion = $CFG->backup_version; - $a->newversion = $updgradeto; - $strdatabasechecking = get_string("databasechecking", "", $a); - $strdatabasesuccess = get_string("databasesuccess"); - print_header($strdatabasechecking, $strdatabasechecking, $strdatabasechecking); - print_heading($strdatabasechecking); - $db->debug=true; - if (backup_upgrade($a->oldversion)) { - $db->debug=false; - if (set_config("backup_version", $a->newversion)) { - notify($strdatabasesuccess, "green"); - notify("You are running Backup/Recovery version ".$backup_release,"black"); - print_continue($continueto); - die; - } else { - notify("Upgrade failed! (Could not update version in config table)"); - die; - } - } else { - $db->debug=false; - notify("Upgrade failed! See version.php"); - die; - } - } else if ($updgradeto < $CFG->backup_version) { - notify("WARNING!!! The code you are using is OLDER than the version that made these databases!"); - } - //Not exists. Starting installation - } else { + function upgrade_backup_db($continueto) { + /// This function upgrades the backup tables, if necessary + /// It's called from admin/index.php, also backup.php and restore.php + + global $CFG, $db; + + require_once ("$CFG->dirroot/backup/version.php"); // Get code versions + + if (empty($CFG->backup_version)) { // Backup has never been installed. $strdatabaseupgrades = get_string("databaseupgrades"); print_header($strdatabaseupgrades, $strdatabaseupgrades, $strdatabaseupgrades); - - if (set_config("backup_version", "2003010100")) { - print_heading("You are currently going to install the needed structures to Backup/Recover"); - print_continue($continueto); - die; + + $db->debug=true; + if (modify_database("$CFG->dirroot/backup/db/$CFG->dbtype.sql")) { + $db->debug = false; + if (set_config("backup_version", $backup_version)) { + notify(get_string("databasesuccess"), "green"); + notify(get_string("databaseupgradebackups", "", $backup_release)); + print_continue($continueto); + exit; + } else { + error("Upgrade of backup system failed! (Could not update version in config table)"); + } + } else { + error("Backup tables could NOT be set up successfully!"); } } + + + if ($backup_version > $CFG->backup_version) { // Upgrade tables + $strdatabaseupgrades = get_string("databaseupgrades"); + print_header($strdatabaseupgrades, $strdatabaseupgrades, $strdatabaseupgrades); + + require_once ("$CFG->dirroot/backup/db/$CFG->dbtype.php"); + + $db->debug=true; + if (backup_upgrade($CFG->backup_version)) { + $db->debug=false; + if (set_config("backup_version", $backup_version)) { + notify(get_string("databasesuccess"), "green"); + notify(get_string("databaseupgradebackups", "", $backup_release)); + print_continue($continueto); + exit; + } else { + error("Upgrade of backup system failed! (Could not update version in config table)"); + } + } else { + $db->debug=false; + error("Upgrade failed! See backup/version.php"); + } + + } else if ($backup_version < $CFG->backup_version) { + notify("WARNING!!! The code you are using is OLDER than the version that made these databases!"); + } } + //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 diff --git a/backup/restore.php b/backup/restore.php index f739fa8f98a..9cc6b61bcc0 100644 --- a/backup/restore.php +++ b/backup/restore.php @@ -3,21 +3,14 @@ //Define some globals for all the script - //MUST CHANGE WITH FINAL BACKUP LOCATION !! WITHOUT TRAILING SLASH !! - //ALL RELATIVE FROM THE LOCATION OF THE restore.php SCRIPT !!! - $moodle_home = ".."; $mods_home = "../mod"; - //END MUST CHANGE - //Units used require_once ("$moodle_home/config.php"); require_once ("$moodle_home/version.php"); require_once ("$moodle_home/lib/xmlize.php"); require_once ("$moodle_home/course/lib.php"); - require_once ("version.php"); - require_once ("db/backup_$CFG->dbtype.php"); require_once ("lib.php"); require_once ("restorelib.php"); @@ -46,7 +39,8 @@ } else { $linkto = "restore.php"; } - upgrade_backup_db($backup_version,$backup_release,$linkto); + + upgrade_backup_db($linkto); //Get strings $strcourserestore = get_string("courserestore");