From f60f4666f9059fb7f98570861be881ca76660a74 Mon Sep 17 00:00:00 2001 From: Eloy Lafuente Date: Mon, 5 Jul 2010 16:37:50 +0000 Subject: [PATCH] MDL-21432 backup - added operation to separate backup & restore --- backup/backup.class.php | 23 ++++++---- backup/controller/backup_controller.class.php | 28 +++++++----- .../dbops/backup_controller_dbops.class.php | 1 + .../helper/backup_general_helper.class.php | 43 +++++++++++++++++++ lib/db/install.xml | 7 +-- lib/db/upgrade.php | 15 +++++++ version.php | 2 +- 7 files changed, 96 insertions(+), 23 deletions(-) diff --git a/backup/backup.class.php b/backup/backup.class.php index 77520724bb73d..9d0113df724e9 100644 --- a/backup/backup.class.php +++ b/backup/backup.class.php @@ -40,7 +40,9 @@ abstract class backup implements checksumable { // Backup format const FORMAT_MOODLE = 'moodle2'; + const FORMAT_MOODLE1 = 'moodle1'; const FORMAT_IMSCC = 'imscc'; + const FORMAT_UNKNOWN = 'unknown'; // Interactive const INTERACTIVE_YES = true; @@ -58,13 +60,14 @@ abstract class backup implements checksumable { // Status of the backup_controller const STATUS_CREATED = 100; - const STATUS_PLANNED = 200; - const STATUS_CONFIGURED = 300; - const STATUS_SETTING_UI = 400; - const STATUS_AWAITING = 500; - const STATUS_EXECUTING = 600; - const STATUS_FINISHED_OK = 700; + const STATUS_REQUIRE_CONV= 200; + const STATUS_PLANNED = 300; + const STATUS_CONFIGURED = 400; + const STATUS_SETTING_UI = 500; + const STATUS_AWAITING = 600; + const STATUS_EXECUTING = 700; const STATUS_FINISHED_ERR= 800; + const STATUS_FINISHED_OK = 900; // Logging levels const LOG_DEBUG = 50; @@ -90,9 +93,13 @@ abstract class backup implements checksumable { const VAR_BACKUPID = -1001; // To reference the backupid being processed const VAR_BASEPATH = -1011; // To reference the dir where the file is generated + // Type of operation + const OPERATION_BACKUP ='backup'; // We are performing one backup + const OPERATION_RESTORE ='restore';// We are performing one restore + // Version (to keep CFG->backup_version (and release) updated automatically) - const VERSION = 2010050500; - const RELEASE = '2.0 Preview 1'; + const VERSION = 2010070500; + const RELEASE = '2.0 Preview 5'; } /* diff --git a/backup/controller/backup_controller.class.php b/backup/controller/backup_controller.class.php index 753c6be659599..e4f61aa6e010d 100644 --- a/backup/controller/backup_controller.class.php +++ b/backup/controller/backup_controller.class.php @@ -51,6 +51,7 @@ class backup_controller extends backup implements loggable { protected $interactive; // yes/no protected $mode; // Purpose of the backup (default settings) protected $userid; // user id executing the backup + protected $operation; // Type of operation (backup/restore) protected $status; // Current status of the controller (created, planned, configured...) @@ -75,6 +76,7 @@ public function __construct($type, $id, $format, $interactive, $mode, $userid){ // Apply some defaults $this->execution = backup::EXECUTION_INMEDIATE; + $this->operation = backup::OPERATION_BACKUP; $this->executiontime = 0; $this->checksum = ''; @@ -179,6 +181,7 @@ public function calculate_checksum() { 'interactive-'. $this->interactive . 'mode-' . $this->mode . 'userid-' . $this->userid . + 'operation-' . $this->operation . 'status-' . $this->status . 'execution-' . $this->execution . 'plan-' . backup_general_helper::array_checksum_recursive(array($this->plan)) . @@ -200,6 +203,10 @@ public function get_type() { return $this->type; } + public function get_operation() { + return $this->operation; + } + public function get_id() { return $this->id; } @@ -259,17 +266,6 @@ public function log($message, $level, $a = null, $depth = null, $display = false backup_helper::log($message, $level, $a, $depth, $display, $this->logger); } - -// Protected API starts here - - protected function calculate_backupid() { - // Current epoch time + type + id + format + interactive + mode + userid - // should be unique enough. Add one random part at the end - $this->backupid = md5(time() . '-' . $this->type . '-' . $this->id . '-' . $this->format . '-' . - $this->interactive . '-' . $this->mode . '-' . $this->userid . '-' . - random_string(20)); - } - public function save_controller() { // Going to save controller to persistent storage, calculate checksum for later checks and save it // TODO: flag the controller as NA. Any operation on it should be forbidden util loaded back @@ -286,6 +282,16 @@ public static function load_controller($backupid) { return $controller; } +// Protected API starts here + + protected function calculate_backupid() { + // Current epoch time + type + id + format + interactive + mode + userid + operation + // should be unique enough. Add one random part at the end + $this->backupid = md5(time() . '-' . $this->type . '-' . $this->id . '-' . $this->format . '-' . + $this->interactive . '-' . $this->mode . '-' . $this->userid . '-' . + $this->operation . '-' . random_string(20)); + } + protected function load_plan() { $this->log('loading controller plan', backup::LOG_DEBUG); $this->plan = new backup_plan($this); diff --git a/backup/util/dbops/backup_controller_dbops.class.php b/backup/util/dbops/backup_controller_dbops.class.php index cc91f45052fc9..19fbda9cac9f2 100644 --- a/backup/util/dbops/backup_controller_dbops.class.php +++ b/backup/util/dbops/backup_controller_dbops.class.php @@ -45,6 +45,7 @@ public static function save_controller($controller, $checksum) { // Get all the columns $rec = new stdclass(); $rec->backupid = $controller->get_backupid(); + $rec->operation = $controller->get_operation(); $rec->type = $controller->get_type(); $rec->itemid = $controller->get_id(); $rec->format = $controller->get_format(); diff --git a/backup/util/helper/backup_general_helper.class.php b/backup/util/helper/backup_general_helper.class.php index 34b16c2998c8b..17f0c3efdfe0a 100644 --- a/backup/util/helper/backup_general_helper.class.php +++ b/backup/util/helper/backup_general_helper.class.php @@ -55,4 +55,47 @@ public static function array_checksum_recursive($arr) { } return $checksum; } + + /** + * Given one temp/backup/xxx dir, detect its format + * + * TODO: Move harcoded detection here to delegated classes under backup/format (moodle1, imscc..) + * conversion code will be there too. + */ + public static function detect_backup_format($tempdir) { + global $CFG; + + // First look for MOODLE (moodle2) format + $filepath = $CFG->dataroot . '/temp/backup/' . $tempdir . '/moodle_backup.xml'; + if (file_exists($filepath)) { // Looks promising, lets load some information + $handle = fopen ($filepath, "r"); + $first_chars = fread($handle,200); + $status = fclose ($handle); + // Check if it has the required strings + if (strpos($first_chars,'') !== false && + strpos($first_chars,'') !== false && + strpos($first_chars,'') !== false) { + return backup::FORMAT_MOODLE; + } + } + + // Then look for MOODLE1 (moodle1) format + $filepath = $CFG->dataroot . '/temp/backup/' . $tempdir . '/moodle.xml'; + if (file_exists($filepath)) { // Looks promising, lets load some information + $handle = fopen ($filepath, "r"); + $first_chars = fread($handle,200); + $status = fclose ($handle); + // Check if it has the required strings + if (strpos($first_chars,'') !== false && + strpos($first_chars,'') !== false && + strpos($first_chars,'') !== false) { + return backup::FORMAT_MOODLE1; + } + } + + // Other formats + + // Arrived here, unknown format + return backup::FORMAT_UNKNOWN; + } } diff --git a/lib/db/install.xml b/lib/db/install.xml index cd337a0bec364..81400211799c9 100644 --- a/lib/db/install.xml +++ b/lib/db/install.xml @@ -1,5 +1,5 @@ - @@ -2617,8 +2617,9 @@ - - + + + diff --git a/lib/db/upgrade.php b/lib/db/upgrade.php index 414cc4bbca6c5..1ed635c0bb6df 100644 --- a/lib/db/upgrade.php +++ b/lib/db/upgrade.php @@ -4829,6 +4829,21 @@ function xmldb_main_upgrade($oldversion) { upgrade_main_savepoint(true, 2010070300); } + if ($oldversion < 2010070500) { + + /// Define field operation to be added to backup_controllers + $table = new xmldb_table('backup_controllers'); + $field = new xmldb_field('operation', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, 'backup', 'backupid'); + + /// Conditionally launch add field operation + if (!$dbman->field_exists($table, $field)) { + $dbman->add_field($table, $field); + } + + /// Main savepoint reached + upgrade_main_savepoint(true, 2010070500); + } + return true; } diff --git a/version.php b/version.php index 1d67bd78e2524..9c76dbc5370fd 100644 --- a/version.php +++ b/version.php @@ -6,7 +6,7 @@ // This is compared against the values stored in the database to determine // whether upgrades should be performed (see lib/db/*.php) - $version = 2010070300; // YYYYMMDD = date of the last version bump + $version = 2010070500; // YYYYMMDD = date of the last version bump // XX = daily increments $release = '2.0 Preview 4 (Build: 20100705)'; // Human-friendly version name