Last active
October 31, 2022 20:42
-
-
Save fliphess/d17fc0c742d77a03a6fcd38acb1f6d38 to your computer and use it in GitHub Desktop.
Wordpress migrator script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -e | |
function die() { echo "$1"; exit 1; } | |
function action() { | |
echo "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@" | |
echo "@@@ $(date)" | |
echo "@@@ $1" | |
echo "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@" | |
echo | |
} | |
function end() { | |
echo "@@@ $1" | |
echo | |
} | |
####################################### | |
## Checking environment and input ## | |
####################################### | |
## Parse input arguments | |
for i in "$@"; do | |
case ${i} in | |
-h=*|--host=*) | |
SOURCE_HOST="${i#*=}" | |
shift | |
;; | |
-p=*|--port=*) | |
SOURCE_PORT="${i#*=}" | |
shift | |
;; | |
-u=*|--user=*) | |
SOURCE_USER="${i#*=}" | |
shift | |
;; | |
-s=*|--source=*) | |
SOURCE_PATH="${i#*=}" | |
shift | |
;; | |
-d=*|--dest=*) | |
DEST_PATH="${i#*=}" | |
shift | |
;; | |
*) | |
# unknown option | |
;; | |
esac | |
done | |
## Validate script input | |
if [ "x${SOURCE_PATH}" == "x" ] || \ | |
[ "x${SOURCE_USER}" == "x" ] || \ | |
[ "x${SOURCE_HOST}" == "x" ] || \ | |
[ "x${SOURCE_PORT}" == "x" ] || \ | |
[ "x${DEST_PATH}" == "x" ] ; then | |
die "Usage: $0 -h=HOST -p=PORT -u=USER -s=~/domain.nl/httpdocs -d=domain.nl/" | |
fi | |
## Check for destination directory | |
if [ ! -d "${DEST_PATH}" ] ; then | |
die "Destination directory ${DEST_PATH} Not found!" | |
fi | |
####################################### | |
## Lets do some moving ## | |
####################################### | |
action "Test ssh connection to remote server" | |
if ! (ssh -qq -o BatchMode=yes -l "${SOURCE_USER}" -p "${SOURCE_PORT}" "${SOURCE_HOST}" "exit 0" ); then | |
echo "Failed to make a ssh connection to ${SOURCE_USER}@${SOURCE_HOST}:${SOURCE_PORT}" | |
die "Check if your ssh agent and forwarding are configured correctly" | |
fi | |
end "Testing ssh succeeded" | |
action "Test if local my.cnf is found" | |
if [ ! -f ~/.my.cnf ] ; then | |
die "Local database credentials file ~/.my.cnf not found." | |
fi | |
end "Testing for my.cnf succeeded" | |
action "Parsing local my.cnf" | |
LOCAL_DB_NAME=""; LOCAL_DB_USER=""; LOCAL_DB_HOST=""; LOCAL_DB_PASS="" | |
eval "$( python -c 'import ConfigParser; import os; c = ConfigParser.ConfigParser(); c.read(os.path.join(os.path.expanduser("~"), ".my.cnf")); print "export LOCAL_DB_NAME=\"{}\"; export LOCAL_DB_HOST=\"{}\"; export LOCAL_DB_USER=\"{}\"; export LOCAL_DB_PASS=\"{}\"".format(c.get("client", "database"), c.get("client", "host"), c.get("client", "user"), c.get("client", "password"))' )" | |
end "Parsing .my.cnf succeeded" | |
action "Test if all expected variables are set after parsing config file" | |
if [ "x${LOCAL_DB_NAME}" == "x" ] || \ | |
[ "x${LOCAL_DB_USER}" == "x" ] || \ | |
[ "x${LOCAL_DB_HOST}" == "x" ] || \ | |
[ "x${LOCAL_DB_PASS}" == "x" ] ; then | |
die "Failed to get database, user, password and host from ~/.my.cnf" | |
fi | |
end "Testing for .my.cnf variables succeeded" | |
action "Test if mysql connection to local database server can be made" | |
if ! (mysql "${LOCAL_DB_NAME}" -h "${LOCAL_DB_HOST}" -u "${LOCAL_DB_USER}" -p"${LOCAL_DB_PASS}" -e ';' ); then | |
die "Failed to connect to database" | |
fi | |
end "Mysql connection succeeded" | |
action "Get database dump and import to local database" | |
COMMAND="eval \"\$(php -r \"require_once('wp-config.php'); printf('export DB_USER=%s ; export DB_HOST=%s ; export DB_PASSWORD=%s ; export DB_NAME=%s', DB_USER, DB_HOST, DB_PASSWORD, DB_NAME);\")\" && mysqldump \"\$DB_NAME\" -h \"\$DB_HOST\" -u \"\$DB_USER\" -p\"\$DB_PASSWORD\"" | |
SSH_COMMAND="ssh -p ${SOURCE_PORT} -l ${SOURCE_USER} ${SOURCE_HOST}" | |
REMOTE_COMMAND="cd ${SOURCE_PATH}; cat - | bash" | |
echo "${COMMAND}" | ${SSH_COMMAND} "${REMOTE_COMMAND}" | mysql "${LOCAL_DB_NAME}" -u "${LOCAL_DB_USER}" -h "${LOCAL_DB_HOST}" -p"${LOCAL_DB_PASS}" | |
if [ "${?}" -ne 0 ]; then | |
die "Failed to get database dump from remote wordpress" | |
else | |
action "Database imported! Now getting wordpress files" | |
fi | |
end "Copying database succeeded" | |
action "Getting files from remote server" | |
for i in $( seq 3 ); do | |
rsync -az -e "ssh -l ${SOURCE_USER} -p ${SOURCE_PORT}" "${SOURCE_HOST}":"${SOURCE_PATH}/" "${DEST_PATH}" | |
if [ "${?}" -ne 0 ]; then | |
action "Failed to get (all) files from remote server... Retrying (3 times total)" | |
fi | |
done | |
end "All Files were copied! successfully" | |
action "Rewriting wp-config file" | |
CONFIG_FILE="${DEST_PATH}/wp-config.php" | |
if [ ! -f "${CONFIG_FILE}" ]; then | |
die "Failed to get ${CONFIG_FILE}" | |
fi | |
chmod 0644 "${CONFIG_FILE}" | |
cat <<EOF > "${DEST_PATH}/wp-database.php" | |
<?php | |
defined('ABSPATH') or header('Location: /'); | |
define('DB_HOST', '${LOCAL_DB_HOST}'); | |
define('DB_NAME', '${LOCAL_DB_NAME}'); | |
define('DB_USER', '${LOCAL_DB_USER}'); | |
define('DB_PASSWORD', '${LOCAL_DB_PASS}'); | |
EOF | |
cat "${CONFIG_FILE}" | grep -vE 'DB_NAME|DB_USER|DB_PASSWORD|DB_HOST' | sed -e "s/require_once(ABSPATH . 'wp-settings.php');/require_once(ABSPATH . 'wp-database.php');\nrequire_once(ABSPATH . 'wp-settings.php');/" > ~/.wp-config.tmp | |
cat ~/.wp-config.tmp > "${DEST_PATH}/wp-config.php" && rm ~/.wp-config.tmp | |
end "Done setting up local database configuration" | |
end "Done moving site to byte" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment