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, here's how to do it. Perfect here is more focused on the backend and administrative stuff, not the content of the site. IE, Plugins, custom themes, and other customizations, etc.

It's a lot like 'moving' a WordPress website, but with some extra stuff.

The key is chaning the Domain Name from the old to the new.

...but First

If using WordFence or any sort of Caching Plugin like WP-Rocket, etc., disable those Plugins before copying the database or files.

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

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

Then use 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)

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!


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