Skip to content

Instantly share code, notes, and snippets.

@magnetikonline magnetikonline/README.md
Last active Aug 29, 2015

Embed
What would you like to do?
Update WordPress database dump URL paths.

Update WordPress URL paths

PHP script that takes a mysqldump file and brute force replaces URLs into an output dump file.

Ensure to dump the source SQL database using the --skip-extended-insert switch to ensure INSERT INTO() statements don't end up on singular and very long lines, which won't help the script crunch all the replacements.

E.g.

$ mysqldump \
	-hlocalhost -uUSERNAME -p \
	--single-transaction --skip-extended-insert \
	DATABASE > dumpfile.sql
<?php
class UpdateWPDumpURLPath {
// note: MUST use --skip-extended-insert to ensure INSERT INTO's are not on a single line
// $ mysqldump -hlocalhost -uUSERNAME -p --single-transaction --skip-extended-insert DATABASE > dumpfile.sql
const SQL_FILE_IN = './dumpfile.sql';
const SQL_FILE_OUT = './dumpfileprocessed.sql';
public function __construct() {
$fhIn = fopen(self::SQL_FILE_IN,'r');
$fhOut = fopen(self::SQL_FILE_OUT,'w');
while (($fileInLine = fgets($fhIn)) !== false) {
fwrite(
$fhOut,
$this->updateURLs($fileInLine)
);
}
fclose($fhOut);
fclose($fhIn);
}
private function updateURLs($text) {
$text = str_replace('://site.com','://siteupdated.com',$text);
// might need more str_replace() steps here...
return $text;
}
}
new UpdateWPDumpURLPath();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.