Adds script to pull down site's database and do the search and replace

master
Jonathan Hodgson 5 years ago
parent c3d90c22b7
commit 9ed5c5c0ec
  1. 49
      bin/wordpress/get-site-database

@ -0,0 +1,49 @@
#!/usr/bin/env bash
# A bash script to replace a local database with one on a server
# It relies on wp being installed on both the server and the local machine
# - Note, if using xampp, mamp .. you should make sure that mysql and php are
# both in your $PATH
# On your local machine, it assumes that WP is in a folder called public_html
# SSH entry from .ssh/config
sshKey="$1"
# The folder so search for on the remote server
# the home folder is grepped for this so public should get public/ and public_html/
sshFolder="${2:-public}"
# If the ssh entry isn't given, exit with a warning
if [ -z "$sshKey" ]; then
echo "Add an SSH entry"
exit 1
fi
# Finds the local public_html folder
public_html="${PWD%/public_html*}/public_html"
# Gets the folder that we should Copy from
sshFolder="$(ssh "$sshKey" ls | grep "$sshFolder")"
# gets the fell path of the folder for the path replace later on
sshFolderPath="$(ssh "$sshKey" "cd $sshFolder; pwd")"
# Gets an array of domains on the remote server
remoteDomains=($(ssh "$sshKey" "cd $sshFolder; wp site list 2> /dev/null" | cut -f 2 | tail -n +2 | sed -E "s/https?:\/\///g" | sed -E "s/\/$//g"))
# Gets an array of domains on the local server
localDomains=($(wp site list 2> /dev/null | cut -f 2 | tail -n +2 | sed -E "s/https?:\/\///g" | sed -E "s/\/$//g"))
# Dumps the database into our public_html folder
ssh "$sshKey" "cd $sshFolder; wp db export -" > "$public_html"/db.dump
# Imports the new database
wp db import "$public_html"/db.dump
# Loops through the domains and does a search and replace
for (( i = 0; i < ${#remoteDomains[@]}; i++ )); do
echo "${remoteDomains[$i]} -> ${localDomains[$i]}"
wp search-replace --all-tables --url="${remoteDomains[$i]}" "${remoteDomains[$i]}" "${localDomains[$i]}" 2> /dev/null
done
# Searches and replaces the paths
echo "$sshFolderPath -> $public_html"
wp search-replace --all-tables --url="${localDomains[0]}" "$sshFolderPath" "$public_html" 2> /dev/null
# Removes the db.dump that we created
rm "$public_html"/db.dump
Loading…
Cancel
Save