noblogs-new-topology.php 3.66 KB
Newer Older
joe's avatar
joe committed
1 2 3 4 5
#!/opt/noblogs/cron/php-noblogs
<?php
/*
   Parse command line options
 */
6
$opts = getopt('',array('no-database', 'calc-size', 'db-only'));
7 8 9
if (array_key_exists('calc-size', $opts)) {
    $opts['no-database'] = true;
    $g_added_size = array();
joe's avatar
joe committed
10
}
11 12 13 14 15 16 17 18 19 20 21 22
$new_topology = array_pop($argv);
$old_topology = array_pop($argv);
if (!($new_topology && $old_topology)) {
    usage();
    exit(-1);
}

// Get all blogs
define('WP_CACHE',false);
require_once('wp-load.php');
require_once('db-config.php');
$blogs = get_all_blogs();
joe's avatar
joe committed
23 24

$n_hashmap = new Flexihash();
25
$new_map = noblogs_load_backends($new_topology, $n_hashmap);
26 27

$o_hashmap = new Flexihash();
28 29 30
$old_map = noblogs_load_backends($old_topology, $o_hashmap);


joe's avatar
joe committed
31 32 33

foreach ($blogs as $blog) {
    $blog_id = $blog->blog_id;
34

35
    $old_params = fhash($blog_id, $old_map, $o_hashmap);
joe's avatar
joe committed
36
    $old_dburi = mysqlurl($old_params);
37
    $new_params = fhash($blog_id, $new_map, $n_hashmap);
joe's avatar
joe committed
38 39 40 41
    $new_dburi = mysqlurl($new_params);

    if ($new_counts[$new_params['host']]) {
        $new_counts[$new_params['host']] += 1;
42
    } else {
joe's avatar
joe committed
43 44 45 46 47
        $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";
48
        if ( !array_key_exists('no-database', $opts) ) {
joe's avatar
joe committed
49 50 51 52
            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";
        }
53
	if (!array_key_exists('db-only',$opts)) {
54
        if (array_key_exists('calc-size', $opts)) {
55 56 57
#            $cmd = sprintf("ssh root@%s du -sk /opt/noblogs/www/wp-content/blogs.dir/%d", $old_params['host'], $blog_id);
	    $cmd = sprintf("du -sk /opt/noblogs/www/wp-content/blogs.dir/%d", $blog_id);
	    list($size, $dummy) = explode("\t",exec($cmd, $ret));
58 59
            $g_added_size[$new_params['host']] += $size;
        } else {
60
            printf("ssh root@%s rsync -avz --delete /opt/noblogs/www/wp-content/blogs.dir/%d root@%s:/opt/noblogs/www/wp-content/blogs.dir/\n", $old_params['host'], $blog_id, $new_params['host']);
61
        }
62
	}
joe's avatar
joe committed
63 64 65 66 67 68 69 70 71
        $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);
72
echo "\n $moved_count blogs moved\n";
joe's avatar
joe committed
73

74 75 76 77
if (array_key_exists('calc-size', $opts)) {
    echo "Variations in disk space for hosts (kB):\n";
    print_r($g_added_size);
}
joe's avatar
joe committed
78

79 80
function fhash($dbid, $reversemap, $hashptr) {
  $lookup = $hashptr->lookup($dbid);
joe's avatar
joe committed
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
  $backend = $reversemap[$lookup];
  $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;
106
  $sql = "SELECT blog_id, domain, path FROM $wpdb->blogs WHERE deleted = 0 AND archived = '0' ORDER BY domain, path";
joe's avatar
joe committed
107 108 109 110
  $result = $wpdb->get_results($sql);
  return ($result);
}

111 112 113 114
function usage() {
    $str = <<<USAGE
noblogs-new-topology.php [--no-database|--calc-size] <OLD_MAP> <NEW_MAP> 
USAGE;
115 116
    echo $str;
}