diff --git a/bin/generate-noblogsmv-map b/bin/generate-noblogsmv-map
index 771bb33eff34187be9a14dab82c8589c2eec65f7..a462dfe8de07b7e46d325bf08e0b0d3d6d109957 100755
--- a/bin/generate-noblogsmv-map
+++ b/bin/generate-noblogsmv-map
@@ -134,7 +134,11 @@ function backend_to_http_endpoint($backend) {
 // Print the blog -> backend map.
 function generate_map() {
   global $wpdb;
-  $wpdb_hash = &$wpdb->hash_map;
+  global $r2db_hash_map;
+  $wpdb_hash = $r2db_hash_map;
+  if (!$wpdb_hash) {
+    $wpdb_hash = &$wpdb->hash_map;
+  }
 
   $blogs = get_blogs();
 
diff --git a/lib/blogs.php b/lib/blogs.php
index 257827b41e8877af9bf3d0da3c1842efc7b073f4..69787759c1bd51ee64e8557338e1a5422f46e5de 100644
--- a/lib/blogs.php
+++ b/lib/blogs.php
@@ -28,6 +28,13 @@ function noblogs_get_blog($blogname) {
 // Return the database connection information associated with a blog.
 function noblogs_get_backend_for_blog($blog_id) {
   global $wpdb;
+  global $r2db_hash_map;
+
+  // Compatibility shim between old/new r2db.
+  $hash_map = $r2db_hash_map;
+  if (!$hash_map) {
+    $hash_map = $wpdb->hash_map;
+  }
 
   // Create a temporary hash object just to load the backends
   // (we cannot use the map in $wpdb because it doesn't have the
@@ -37,7 +44,7 @@ function noblogs_get_backend_for_blog($blog_id) {
 
   // Lookup the blog ID using the $wpdb hash just to be safe (though
   // we should get an identical result using $hashmap).
-  $lookup = $wpdb->hash_map->lookup($blog_id);
+  $lookup = $hash_map->lookup($blog_id);
   $backend = $reversemap[$lookup];
 
   // Be nice and split the database host into 'host' and 'port' elements.
@@ -57,14 +64,20 @@ function noblogs_get_backend_for_blog($blog_id) {
 // Return the full backend -> blogs map.
 function noblogs_get_backend_map() {
   global $wpdb;
-  $wpdb_hash = &$wpdb->hash_map;
+  global $r2db_hash_map;
+
+  // Compatibility shim between old/new r2db.
+  $hash_map = $r2db_hash_map;
+  if (!$hash_map) {
+    $hash_map = $wpdb->hash_map;
+  }
 
   $blogs = noblogs_get_blogs();
   $backend_map = array();
 
   foreach ($blogs as $blog) {
     $blog_id = $blog->blog_id;
-    $backend_id = $wpdb_hash->lookup($blog_id);
+    $backend_id = $hash_map->lookup($blog_id);
     if (!array_key_exists($backend_id, $backend_map)) {
       $backend_map[$backend_id] = array();
     }