Skip to content

Instantly share code, notes, and snippets.

@mrazzari
Last active October 29, 2022 00:06
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrazzari/4088361 to your computer and use it in GitHub Desktop.
Save mrazzari/4088361 to your computer and use it in GitHub Desktop.
WP-Migrate: standalone WP database dump into a new DB.

WP-MIGRATE

Migrates a standalone WP database dump into a new DB.

This script will take a mysqldump sql file, and:

  1. Replace the old blog's URL with the new one
  2. Replace an old WP table_prefix with a new one
  3. Take care of string lengths within serialized data

It will finally import the MySQL dump into the new DB, using wp-config's settings.

  • This is really a series of simple steps and best guesses, so you may want to edit it to suit your needs.
  • 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!

Run php wp-migrate.php for usage instructions.

<?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";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment