Following script will make this for your:
#!/bin/bash
#===================================================================================
#
# FILE: wpdup.sh
#
# USAGE: wpdup.sh <dir_old> <dir_new> <dbname_old> <dbname_new> <dbuser_new> <dbpass_new> <domain_old> <domain_new>
#
# DESCRIPTION: Duplicate WP installation with database and replace domain.
#
# OPTIONS: see function ’usage’ below
#===================================================================================
EXPECTED_ARGS=8
E_BADARGS=65
if [ $# -ne $EXPECTED_ARGS ]
then
echo "Usage: $0 <dir_old> <dir_new> <dbname_old> <dbname_new> <dbuser_new> <dbpass_new> <domain_old> <domain_new>"
exit $E_BADARGS
fi
MYSQL=`which mysql`
MYSQLDUMP=`which mysqldump`
WP=`which wp`
CP=`which cp`
TMPDIR='/tmp'
DIR_OLD=$1
DIR_NEW=$2
DBNAME_OLD=$3
DBNAME_NEW=$4
DBUSER_NEW=$5
DBPASS_NEW=$6
DOMAIN_OLD=$7
DOMAIN_NEW=$8
echo -n "Enter root mysql password:"
read -s DBROOT_PASS
echo "Copy directory"
# Copy WP site
$CP -r $DIR_OLD $DIR_NEW
echo "Dump database"
# Dump database
$MYSQLDUMP -u root -p$DBROOT_PASS $DBNAME_OLD > "${TMPDIR}/dbdump.sql"
# Create new database if not exists and add privileges.
Q1="DROP DATABASE IF EXISTS $DBNAME_NEW;"
Q2="DROP USER '$DBUSER_NEW'@'localhost';"
Q3="CREATE DATABASE $DBNAME_NEW COLLATE utf8_general_ci;"
Q4="CREATE USER '$DBUSER_NEW'@'localhost' IDENTIFIED BY '$DBPASS_NEW';"
Q5="GRANT ALL PRIVILEGES ON $DBNAME_NEW.* TO '$DBUSER_NEW'@'localhost' IDENTIFIED BY '$DBPASS_NEW';"
Q6="FLUSH PRIVILEGES;"
SQL="${Q1}${Q2}${Q3}${Q4}${Q5}${Q6}"
echo "Create database"
$MYSQL -e "${SQL}" -u root -p$DBROOT_PASS
echo "Import database"
# Import dumped database
$MYSQL -u root -p$DBROOT_PASS $DBNAME_NEW < "${TMPDIR}/dbdump.sql"
echo "Create wp config"
# Create new wordpress config
cd $DIR_NEW && $WP core config --dbname=${DBNAME_NEW} --dbuser=${DBUSER_NEW} --dbpass=${DBPASS_NEW} --dbhost="localhost" --allow-root --skip-check --force
echo "Replace domain"
# Replace domain in new wp site
cd $DIR_NEW && $WP search-replace "${DOMAIN_OLD}" "${DOMAIN_NEW}" --allow-root
You can use it in the following way:
sudo ./wpdup.sh /var/www/test /var/www/testnew wp_test wp_testnew wp_testnew xxxxxx test.dev testnew.dev