diff --git a/backup/restorelib.php b/backup/restorelib.php
index 937946f..0385d77 100644
--- a/backup/restorelib.php
+++ b/backup/restorelib.php
@@ -711,7 +711,7 @@
     //This function creates all the block stuff when restoring courses
     //It calls selectively to  restore_create_block_instances() for 1.5 
     //and above backups. Upwards compatible with old blocks.
-    function restore_create_blocks($restore, $backup_block_format, $blockinfo, $xml_file) {
+    function restore_create_blocks(&$restore, $backup_block_format, $blockinfo, $xml_file) {
 
         $status = true;
 
@@ -765,7 +765,7 @@
 
     //This function creates all the block_instances from xml when restoring in a
     //new course
-    function restore_create_block_instances($restore,$xml_file) {
+    function restore_create_block_instances(&$restore,$xml_file) {
 
         $status = true;
         //Check it exists
@@ -824,6 +824,7 @@
         }
 
         $blocks = get_records_select('block', '', '', 'name, id, multiple');
+        $allnewblocks = array();
 
         // For each type of page we have restored
         foreach($pageinstances as $thistypeinstances) {
@@ -872,18 +873,36 @@
                     
                     //Get an object for the block and tell it it's been restored so it can update dates
                     //etc. if necessary
-                    $blockobj=block_instance($instance->name,$instance);
-                    $blockobj->after_restore($restore);
     
                     //Now we can increment the weight counter
                     ++$maxweights[$instance->position];
     
                     //Keep track of block types we have already added
                     $addedblocks[$instance->name] = true;
+                    $allnewblocks[] = $instance;
 
                 }
             }
         }
+        $restore->block_instances = $allnewblocks;
+
+        return $status;
+    }
+
+    function restore_run_block_after_restore(&$restore) {
+
+        $status = true;
+
+        if(empty($restore->block_instances)) {
+            return $status;
+        }
+
+        //Get an object for the block and tell it it's been restored so it can update dates
+        //etc. if necessary
+        foreach($restore->block_instances as $instance) {
+            $blockobj=block_instance($instance->name,$instance);
+            $blockobj->after_restore($restore);
+        }
 
         return $status;
     }
@@ -6345,6 +6364,10 @@
             }
         }
 
+        if ($status) {
+            restore_run_block_after_restore($restore, $xml_file);
+        }
+
         //Now, if all is OK, adjust activity events
         if ($status) {
             if (!defined('RESTORE_SILENTLY')) {
diff --git a/blocks/glossary_random/block_glossary_random.php b/blocks/glossary_random/block_glossary_random.php
index 315e830..1f5cbd2 100644
--- a/blocks/glossary_random/block_glossary_random.php
+++ b/blocks/glossary_random/block_glossary_random.php
@@ -208,5 +208,38 @@ class block_glossary_random extends block_base {
         return false;
     }
 
+    function after_restore($restore) {
+        global $CFG;
+
+        # Retrieve the id of the old course module.
+        $sql = "
+            SELECT
+                cm.*
+            FROM    {$CFG->prefix}course_modules cm
+            JOIN    {$CFG->prefix}modules m ON cm.module=m.id
+            WHERE   cm.instance     = {$this->config->glossary}
+                    AND cm.course   = {$this->course->id}
+                    AND m.name      = 'glossary'
+        ";
+        $cm = get_record_sql($sql);
+        if (empty($cm) || empty($cm->id)) {
+            error_log("glossary_random block : after_restore failed to find course module id");
+            return;
+        }
+
+        # Look for the new id in backup_ids table.
+        $newid = backup_getid($restore->backup_unique_code, 'course_modules', $cm->id);
+        if (empty($newid)) {
+            error_log("glossary_random->after_restore() failed to retrieve new cmid from backup_ids (old id={$cm->id}");
+            return;
+        }
+
+        # Retrieve the instance id.
+        $newid = get_field('course_modules', 'instance', 'id', $newid->new_id);
+
+        # Update this block
+        $this->config->glossary = $newid;
+        $this->instance_config_commit();
+    }
 }
 ?>
