--- backup_migrate.module	2007-12-03 18:46:24.812500000 +0100
+++ backup_migrate.module	2007-12-03 23:16:35.437500000 +0100
@@ -578,8 +578,8 @@ function _backup_migrate_get_table_struc
   $out = "";
   $result = db_query("SHOW CREATE TABLE `". $table['Name'] ."`");
   if ( $create = db_fetch_array($result) ) {    
-    $out .= "DROP TABLE IF EXISTS `". $table['Name'] ."`;\n";
-    $out .= strtr( $create['Create Table'], "\n", " " );
+    $out .= "DROP TABLE IF EXISTS `". $table['Unprefixed'] ."`;\n";
+    $out .= strtr(strtr( $create['Create Table'], "\n", " " ), array("CREATE TABLE `{$table['Name']}`" => "CREATE TABLE `{$table['Unprefixed']}`"));
     if ( $table['Auto_increment'] ) {
       $out .= " AUTO_INCREMENT=". $table['Auto_increment'];
     }
@@ -596,12 +596,15 @@ function _backup_migrate_dump_table_data
   while ( $row = db_fetch_array($data) ) {
     $items = array();
     foreach ( $row as $key => $value ) {
-      $items[] = is_null($value) ? "null" : "'". db_escape_string($value) ."'";
+      if ($table['Unprefixed']=="{__PREFIXsequences__}" && $key=="name")
+       $items[] = is_null($value) ? "null" : "'". db_escape_string(_backup_migrate_unprefix_tablename($value,"_.*")) ."'";
+      else 
+        $items[] = is_null($value) ? "null" : "'". db_escape_string($value) ."'";
     }
     if ( $items ) {
       fwrite( 
         $dst, 
-        "INSERT INTO `". $table['Name'] ."` VALUES (". implode( ",", $items ) .");\n"
+        "INSERT INTO `". $table['Unprefixed'] ."` VALUES (". implode( ",", $items ) .");\n"
        );
     }
   }
@@ -649,7 +652,8 @@ function _backup_migrate_get_tables( ) {
   // get auto_increment values and names of all tables
   $tables = db_query("show table status");
   while ( $table = db_fetch_array($tables) ) {
-    $out[] = $table;
+    $table['Unprefixed'] = _backup_migrate_unprefix_tablename($table['Name']);
+    if ($table['Unprefixed']!=FALSE) $out[] = $table;
   }
   return $out;
 }
@@ -662,7 +666,8 @@ function _backup_migrate_get_table_names
   // get auto_increment values and names of all tables
   $tables = db_query("show table status");
   while ( $table = db_fetch_array($tables) ) {
-    $out[$table['Name']] = $table['Name'];
+    if (_backup_migrate_unprefix_tablename($table['Name'])!=FALSE)
+      $out[$table['Name']] = $table['Name'];
   }
   return $out;
 }
@@ -673,6 +678,8 @@ function _backup_migrate_get_table_names
  * Restore from a previously backed up files. Accepts any file created by the backup function.
  */ 
 function _backup_migrate_restore_file( $filepath, $filename="", $delete=false ) {
+  $start=time();
+  set_time_limit(0);
   if ( !$filename ) {
     $filename = $filepath;
   }
@@ -775,7 +782,7 @@ function _backup_migrate_restore_file( $
       $line = trim( $line );
       if ( $line ) {
         // use the helper instead of the api function to avoid substitution of '{' etc
-        _db_query( $line );
+        _db_query( _backup_migrate_prefix ( $line ));
         $num++;
       }
     }    
@@ -788,6 +795,7 @@ function _backup_migrate_restore_file( $
     }
     
     $message = t( "Restore complete. %num SQL commands executed. ", array("%num"=>$num) );
+    $message .= t( "The time needed was: %time. ", array("%time"=>format_interval(time()-$start)) );
     $message .= $file_is_temp ? "" : "(". l( t( "Restore Again..." ), "admin/content/backup_migrate/restorefile/".$filepath ) .")";
     drupal_set_message( $message );
     $goto = "admin/content/backup_migrate/restore";
@@ -1117,7 +1125,60 @@ function _backup_migrate_default_structu
     'devel_queries',
     'devel_times',
   );
+  foreach ($core as $k => $t) $core[$k]=db_prefix_tables("{{$t}}");
   $alltables = array_merge($core, module_invoke_all('devel_caches'));
   return $alltables;
 }
 
+
+
+function _backup_migrate_unprefix_tablename ($table,$suffix="") {
+  global $db_prefix;
+  if (is_array($db_prefix)) {
+    if (array_key_exists('default', $db_prefix)) {
+      $tmp = $db_prefix;
+      unset($tmp['default']);
+      $f=FALSE;
+      foreach ($tmp as $key => $val) {
+        if (preg_match("/^$val($key)($suffix)$/",$table,$x)!=0) {
+          $f=TRUE;
+          $table="{__PREFIX{$key}__}{$x[2]}";
+          break;
+        }
+      }
+      if (!$f) {
+        if (preg_match("/^{$db_prefix['default']}([a-zA-Z0-9_]*)($suffix)$/",$table,$x)!=0) {
+          $table="{__PREFIX{$x[1]}__}{$x[2]}";
+        } else $table=FALSE;
+      }
+    }
+    else {
+      $f=FALSE;
+      foreach ($db_prefix as $key => $val) {
+        if (preg_match("/^$val($key)($suffix)$/",$table,$x)!=0) {
+          $f=TRUE;
+          $table="{__PREFIX{$key}__}{$x[2]}";
+          break;
+        }
+      }
+      if (!$f) {
+        if (preg_match("/^(.*)($suffix)$/",$table,$x)!=0)
+          $table="{__PREFIX{$table}__}{$x[2]}";
+      }
+    }
+  }
+  else {
+    if (preg_match("/^{$db_prefix}([a-zA-Z0-9_]*)($suffix)$/",$table,$x)!=0) {
+       $table="{__PREFIX{$x[1]}__}{$x[2]}";
+    } else $table=FALSE;
+  }
+  return $table;
+}
+
+function _backup_migrate_prefix ($sql) {
+  global $db_prefix;
+  while (preg_match("/{__PREFIX([a-zA-Z0-9_]*)__}/",$sql,$x)!=0) {
+    $sql=strtr($sql, array($x[0] => db_prefix_tables("{{$x[1]}}")));
+  }
+  return $sql;
+}
