|
<?php |
|
if (count($argv) < 7){?> |
|
|
|
WP-MIGRATE |
|
Migrates a standalone WP database dump into a new DB. |
|
|
|
Author: @mrazzari |
|
Author URL: http://ConVistaAlMar.com.ar |
|
You found this at: https://gist.github.com/4088361 |
|
|
|
USAGE: |
|
php wp-migrate.php <dump_name.sql> <main_blog_hostname> <blog_url_old> <blog_url_new> <table_prefix_old> <blog_dir> |
|
|
|
dump_name: mysqldump file. Note that this script will modify it. |
|
main_blog_hostname: Host name of the main WP install. |
|
blog_url_old: the site_url found in the db dump. If you enter a www-prefixed URL, both www and non-www URLs are replaced. |
|
blog_url_new: the url of your new site. You may repeat the old URL here, if it's not changing. |
|
table_prefix_old: the table prefix found in the DB dump. Usually wp_. If you weren't using one... this won't work! |
|
blog_dir: absolute or relative path to the WP install's root dir (where wp-config is). |
|
|
|
EXAMPLE: |
|
php wp-migrate.php old-site.sql new-site.com "http://www.old-site.com" "http://new-site.com" wp_ /var/www/example |
|
|
|
USAGE NOTES: |
|
* No trailing slashes in paths! |
|
* Your new DB should be setup, and wp-config connecting to it successfully. |
|
* This script will alter your .sql dump file. Make sure to have a backup copy of it, in case you mess up with the params and need to run it again. |
|
* It's not meant to address all possible situations and edge cases. "It works for me". |
|
* Bottom line: please read the code and review it carefully before executing it. And backup your data! |
|
|
|
|
|
<?php |
|
return 0; |
|
} |
|
# Yeah argv ftw! |
|
$dump = $argv[1]; |
|
$host = $argv[2]; |
|
$blog_url_old = $argv[3]; |
|
$blog_url_new = $argv[4]; |
|
$old_prefix = $argv[5]; |
|
$blog_dir = $argv[6]; |
|
|
|
# Set some context |
|
set_time_limit(0); |
|
ini_set( "memory_limit", "128M" ); |
|
$dump_line_length = 1 * 1024 * 1024; // 1MB, per MySQL's max_allowed_packet setting. |
|
|
|
# Load WordPress |
|
$_SERVER['HTTP_HOST'] = $host; |
|
define('SHORTINIT', true); |
|
require_once( $blog_dir . '/wp-load.php'); |
|
|
|
echo "Replacing old table prefix with new one in the MySQL dump.\n"; |
|
$new_prefix = $table_prefix; |
|
$cmd = sprintf('sed -i "s/\`%s/\`%s/gI" %s', $old_prefix, $new_prefix, $dump); |
|
echo $cmd, "\n"; |
|
$out = shell_exec($cmd); |
|
echo $out, "\n\n"; |
|
|
|
|
|
echo "Replacing old blog URL with the new blog URL.\n"; |
|
$blog_url_old_www_reg = str_replace("www.", "(www\.)?", $blog_url_old); |
|
$cmd = sprintf('sed -i -r "s#%s#%s#gI" %s', $blog_url_old_www_reg, $blog_url_new, $dump); |
|
echo $cmd, "\n"; |
|
$out = shell_exec($cmd); |
|
echo $out, "\n\n"; |
|
|
|
|
|
echo "Taking care of string lengths within serialized data.\n"; |
|
$newDump = "${dump}_tmp_replaced"; |
|
$reg = '#s:(\d+):\\\\"(.*?)\\\\";#sm'; |
|
echo "Searching $reg in $newDump and updating strlens.\n"; |
|
$handle = @fopen($dump, "r"); |
|
while (($newLine = fgets($handle, $dump_line_length)) !== false) { |
|
$newLine = preg_replace_callback($reg, function($m){ |
|
return 's:' . strlen(stripslashes($m[2])) . ':\\"' . $m[2] . '\\";'; |
|
}, $newLine); |
|
file_put_contents($newDump, $newLine, FILE_APPEND); |
|
} |
|
fclose($handle); |
|
shell_exec("mv $newDump $dump"); |
|
echo "\n\n"; |
|
|
|
|
|
echo "Importing MySQL dump.\n"; |
|
$cmd = sprintf("mysql -u %s -h %s --password=%s %s < %s", DB_USER, DB_HOST, DB_PASSWORD, DB_NAME, $dump ); |
|
echo $cmd, "\n"; |
|
$out = shell_exec($cmd); |
|
echo $out, "\n\n"; |
|
|
|
echo "\nDone. Go check your blog! :)\n"; |