Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/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-new.php');
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');
}
}
function new_hash($dbid) {
global $wpdb_hash;
global $wpdb_reverse_backend_map;
$lookup = $wpdb_hash->lookup($dbid);
$backend = $wpdb_reverse_backend_map[$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";
$result = $wpdb->get_results($sql);
return ($result);
}
$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);
$new_params = new_hash($blog_id);
$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";