Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
ai
noblogs-wp
Commits
e35e07c9
Commit
e35e07c9
authored
Oct 08, 2011
by
joe
Committed by
agata
Jul 01, 2020
Browse files
tool to move blogs based on topology
parent
a0879bcc
Changes
1
Hide whitespace changes
Inline
Side-by-side
r2db/tools/noblogs-new-topology.php
0 → 100755
View file @
e35e07c9
#!/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\n
Blog 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
);
}
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment