From 7d399db3459ce77c2f4b756aa4bdbb364b0c07a2 Mon Sep 17 00:00:00 2001 From: joe <joe@incal.net> Date: Sat, 8 Oct 2011 23:22:19 +0200 Subject: [PATCH] tool to move blogs based on topology --- tools/noblogs-new-topology.php | 95 ++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100755 tools/noblogs-new-topology.php diff --git a/tools/noblogs-new-topology.php b/tools/noblogs-new-topology.php new file mode 100755 index 0000000..f153ad0 --- /dev/null +++ b/tools/noblogs-new-topology.php @@ -0,0 +1,95 @@ +#!/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); +} + -- GitLab