Commit e35e07c9 authored by joe's avatar joe Committed by agata

tool to move blogs based on topology

parent a0879bcc
#!/opt/noblogs/cron/php-noblogs
<?php
// Load wordpress api.
define('WP_CACHE',false);
/** Setup WordPress environment */
require_once('wp-load.php');
require_once('db-config.php');
/*
Parse command line options
*/
$opts = getopt('N','no-database');
if (empty($argv[2])) {
usage();
exit(1);
}
$o_hashmap = new Flexihash();
$old_map = noblogs_load_backends($argv[1], $o_hashmap);
$n_hashmap = new Flexihash();
$new_map = noblogs_load_backends($argv[2], $n_hashmap);
$blogs = get_all_blogs();
foreach ($blogs as $blog) {
$blog_id = $blog->blog_id;
$old_params = fhash($blog_id, $old_map);
$old_dburi = mysqlurl($old_params);
$new_params = fhash($blog_id, $reverse_backend_map);
$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";
if (! $opts['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";
}
printf("rsync -avz --delete root@%s:/opt/noblogs/www/wp-content/blogs.dir/%d root@%s:/opt/noblogs/www/wp-content/blogs.dir/%d\n", $old_params['host'], $blog_id, $new_params['host'], $blog_id);
$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";
function fhash($dbid, $reversemap) {
global $wpdb;
$lookup = $wpdb->hash_map->lookup($dbid);
$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;
// $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";
$result = $wpdb->get_results($sql);
return ($result);
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment