db-migrate-to-new-schema.php 2.78 KB
Newer Older
1 2 3 4 5 6 7
#!/opt/noblogs/cron/php-noblogs
<?php

// Load wordpress api.
define('WP_CACHE',false);
/** Setup WordPress environment */
require_once('wp-load.php');
8
require_once('db-config.php');
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26


function old_hash($dbid) {
  if (($dbid % 2) == 0) {
    return array('host' => '172.16.1.3',
		 'port' => '3307',
		 'user' => 'noblogs',
		 'password' => 'n0bl0gst3st',
		 'db' => 'noblogs_2');
  } else {
    return array('host' => '172.16.1.8',
		 'port' => '3307',
		 'user' => 'noblogsusr',
		 'password' => 'n0bl0gsdb4xpw!',
		 'db' => 'noblogs');
  }
}

27 28 29 30
function new_hash($dbid, $reversemap) {
  global $wpdb;
  $lookup = $wpdb->hash_map->lookup($dbid);
  $backend = $reversemap[$lookup];
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
  $result = array();
  if (preg_match('/^(.*):([0-9]*)$/', $backend['host'], $matches)) {
    $result['host'] = $matches[1];
    $result['port'] = $matches[2];
  }
  $result['user'] = $backend['user'];
  $result['password'] = $backend['password'];
  $result['db'] = $backend['name'];
  return $result;
}

function mysqlopts(&$attrs) {
  return ("-h" . $attrs['host'] . " -P" . $attrs['port'] . " -u" . $attrs['user']
	  . " '-p" . $attrs['password'] . "'");
}

function mysqlurl(&$attrs) {
  return ("mysql://" . $attrs['user'] . "@" . $attrs['host']
	  . ":" . $attrs['port'] . "/" . $attrs['db']);
}

function get_all_blogs() 
{
  global $wpdb;

56 57
//  $sql = "SELECT blog_id, domain, path FROM $wpdb->blogs WHERE public = 1 AND deleted = 0 AND archived = '0' ORDER BY domain, path";
  $sql = "SELECT blog_id, domain, path FROM $wpdb->blogs WHERE public = 0 ORDER BY domain, path";
58 59 60 61
  $result = $wpdb->get_results($sql);
  return ($result);
}

62 63 64
$hashmap = new Flexihash();
$reverse_backend_map = noblogs_load_backends(NOBLOGS_BACKEND_CONFIG, $hashmap);

65 66 67 68 69 70 71 72
$new_counts = array();
$moved_count = 0;
$blogs = get_all_blogs();
foreach ($blogs as $blog) {
  $blog_id = $blog->blog_id;

  $old_params = old_hash($blog_id);
  $old_dburi = mysqlurl($old_params);
73
  $new_params = new_hash($blog_id, $reverse_backend_map);
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
  $new_dburi = mysqlurl($new_params);

  if ($new_counts[$new_params['host']]) {
    $new_counts[$new_params['host']] += 1;
  } else { 
    $new_counts[$new_params['host']] = 1;
  }

  if ($old_dburi != $new_dburi) {
    echo "echo moving blog $blog_id from " . $old_params['host'] . " to " . $new_params['host'] . "\n";
    echo "tables=\$(mysql " . mysqlopts($old_params) . " " . $old_params['db'] . " -NBe \"show tables like 'wp\\_" . $blog_id . "\\_%'\")\n";
    echo "mysqldump --opt " . mysqlopts($old_params) . " " . $old_params['db'] . " \${tables} \\\n";
    echo "  | mysql " . mysqlopts($new_params) . " " . $new_params['db'] . "\n";
    $moved_count += 1;
  } else {
    echo "echo blog $blog_id stays on " . $old_params['host'] . "\n";
  }

}

echo "\n\n\nBlog distribution:\n";
print_r($new_counts);
print "\n $moved_count blogs moved\n";