Commit 52faca26 authored by joe's avatar joe Committed by lechuck

script for redistributing noblogs resources according to backend maps

parent 11755db8
......@@ -10,42 +10,49 @@ require_once('db-config.php');
/*
Parse command line options
*/
$opts = getopt('N','no-database');
if (empty($argv[2])) {
usage();
exit(1);
$opts = getopt('',array('no-database', 'calc-size'));
if (array_key_exists('calc-size', $opts)) {
$opts['no-database'] = true;
$g_added_size = array();
}
$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);
$new_map = noblogs_load_backends(array_pop($argv), $n_hashmap);
$o_hashmap = new Flexihash();
$old_map = noblogs_load_backends(array_pop($argv), $o_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_params = fhash($blog_id, $new_map);
$new_dburi = mysqlurl($new_params);
if ($new_counts[$new_params['host']]) {
$new_counts[$new_params['host']] += 1;
} else {
} 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']) {
if ( array_key_exists('no-database', $opts) ) {
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);
if (array_key_exists('calc-size', $opts)) {
$cmd = escapeshellargs(sprintf("ssh root@%s du -sk /opt/noblogs/www/wp-content/blogs.dir/%d", $old_params['host'], $blog_id));
list($size, $dummy) = explode("\t",exec($cmd, $ret));
$g_added_size[$new_params['host']] += $size;
} else {
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";
}
......@@ -54,9 +61,12 @@ foreach ($blogs as $blog) {
echo "\n\n\nBlog distribution:\n";
print_r($new_counts);
print "\n $moved_count blogs moved\n";
echo "\n $moved_count blogs moved\n";
if (array_key_exists('calc-size', $opts)) {
echo "Variations in disk space for hosts (kB):\n";
print_r($g_added_size);
}
function fhash($dbid, $reversemap) {
global $wpdb;
......@@ -86,10 +96,13 @@ function mysqlurl(&$attrs) {
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";
$sql = "SELECT blog_id, domain, path FROM $wpdb->blogs WHERE public = 1 AND deleted = 0 AND archived = '0' ORDER BY domain, path";
$result = $wpdb->get_results($sql);
return ($result);
}
function usage() {
$str = <<<USAGE
noblogs-new-topology.php [--no-database|--calc-size] <OLD_MAP> <NEW_MAP>
USAGE;
}
\ No newline at end of file
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