WordPress Copy a Website

Wiki.TerraBase.info
Jump to navigation Jump to search

If you've got the perfect WordPress website and want to copy it for use with another web site, here's how to do it.

Yes, there are lots of tools, plugins, software, etc. to do it. However, after testing them, there's always stuff that's missed. Plus for large websites with lots of content, plugins, etc., they either take forever or figure out a way to crap out with some stupid error. I've checked up on them after a 'successful' move and found all of the ones tested to be lacking in some way. Sometimes the best way to do it is to roll up the sleeves and get a little dirty, just to make sure it's done the right way.

The key items to change are Domain Names and Paths (physical, virtual, URLs, etc.). And there are two places to change (find / search and replace): Files and the Database

...but First

Make the website as perfect as it can be before copying. IE, update plugins, clean up the database, etc. If using security plugins like WordFence, etc. or any sort of caching plugin like WP-Rocket, etc., disable those plugins before copying the database or files.

There are some contexts to think about when searching for text to change;

  • Content, IE stuff that's written to be read by viewers
  • Physical Directory / Path Names ( /var/www/html/WhatEverWebSiteDirectory )
  • URL Paths ( https://WhatEverDomainName )

There's an order to search for things to modify;

Copy Files and Database

cp -ax WhatEverSourcePath WhatEverDestinationPath

mysqldump -u root -p WhatEverSourceDatabase > WhatEverFileName

mysql -u root -p WhatEVerDestinationDatabase < WhatEverFileName

Modify Domain Name in Database and wp-config.php

In the wp_options Table, using phpMyAdmin, change the siteurl and home option_name to the new Domain Name.

Modify the wp-config.php file to reflect any changes in paths or domain names or user names or passwords, etc.

The site should be functional at this point.

Start on the journey of changing Domain Names, Paths, URLs, etc (with the above advice about context and order) using the Better Search and Replace Plugin: Enter Old and New Domain Names, Select Tables by clicking the Top Item in Select tables, then scrolling to the bottom, holding the Shift Key and clicking on the Bottom Item, Case-insensitive: UNchecked, Replace GUIDs: CHECKED, Run as Dry run: UNchecked

Modify Domain Name in Files (Don't do this until all the database replacements have been done)

The only Directory that should need to be searched is the /wp-content Directory (the wp-admin and wp-includes Directories shouldn't have anything that needs modifying). In the root directory, there is of course the wp-config.php file and possibly a robots.txt file (but unlikely on the robots.txt file)

First run this command to see what needs changing, before actually changing it;

  • grep -Ri WhatEverStringToSearchFor WhatEverPathToSearch
    • -R = Recursive (IE, search sub-directories) (same as -r (lowercase))
    • -i = Ignore Case (IE, make the string that is being searched for Case Insensitive)
    • -l = NOT used in the above example, but noted here as useful, only list the File Names that contain the searched for strings in the results, not the actual text in the file (That's an L (El), not an I (eye) for the switch)
    • no need for an * ( asterisk ) at the end of the path as -R implies *

Review the file and think of it as an opportunity to do some house cleaning

  • Any log files? Those can go.
  • Any absolute paths (https://WhateverDomainName/Path)? Think about changing them to relative paths (/Path) instead.
  • Some plugins will use their /plugins/WhatEverPluginName to store data. Hmmm, why not the database like other plugins? Consider ditching plugins like this and searching for better alternatives.
  • etc.

Utilities (for Windows)

  • Advanced Find and Replace (costs $)
  • Find and Replace (Free, with donation request)
    • Suggested Exclude Mask: *.dll, *.exe, *.png, *.jpg, *.webp, *.bcmap, *.z, *.tmp, *.pdf, *.zip, *.pack, *.idx, *.swf, *.ORIGINAL
    • Suggested Settings: Case Sensitive (turn it ON)

Utilities (for WordPress)

  • String Locator Plugin

Command Line (Cannot find any commands using GREP, SED, FIND, AWK, etc. that actually work properly, so use the Windows Method!)

  • grep -Rl 'WhatEverTextPatternToBeReplaced' WhatEverPathNameOfFiles --exclude=*.{dll,exe,png,jpg,webp,bcmap,z,tmp,pdf,zip,pack,idx,swf,ORIGINAL} | xargs sed -i 's/WhatEverTextPatternToBeReplaced/WhatEverNewTextPattern/g'

That's an L (El), not an I (eye) for the switch: grep -Rl<--

In researching this, there were several mentions of "Rust Tools" which seemed to be newer commands that might be integrated into the standard Linux architecture that do the same thing in a comprehensible syntax instead of the above mess (IE, COMMAND "TextToSearchFor" "TextToReplaceItWith" PATH, done!)

And on frustrating note, in that the above command doesn't account for spaces (if there are any, in file names and directory names). Couldn't find a solution as grep simply will not produce any output, even with --null and / or -Z that xargs -0 will pay attention to. Woof!

Example

Check and see if anything needs to be replaced: grep -Ri "NEW.TerraBase.info" /var/www/html/new.TerraBase.info/* > output.txt

If there is something that needs to be replaced, run this: grep -Rli 'NEW.TerraBase.info' /var/www/html/NEW.TerraBase.info/wp-content --exclude=*.{dll,exe,png,jpg,webp,bcmap,z,tmp,pdf,zip,pack,idx,swf,ORIGINAL} | xargs sed -i 's/new.terrabase.info/www.terrabase.info/g'

..and run the first command to see if anything remains

Speed Note

As for speed, the Command Line is about a hundred times faster than doing it from Windows over a network connection.

Permissions

Set the permissions of all files using the below script;

#!/bin/bash

chown -R apache:apache $1
# To show progress: chown -cR apache:apache $1 OR chown -vR apache:apache $1

find $1 -type d -exec chmod 755 {} \;
find $1 -type f -exec chmod 644 {} \;
# To show progress: find $1 -type d -exec chmod 755 {} \; -print AND find $1 -type f -exec chmod 644 {} \; -print
# The -print may need to go before the ; ( semi-colon )...

If the script file is named CHANGE.sh, then type: ./CHANGE.sh WhatEverPathName

Showing progress isn't recommended as it will lengthen the time considerably.

Watch Out for...

If using WordFence, make sure this file /.user.ini has the proper Domain Name