Skip to main content
Bounty Ended with 100 reputation awarded by Arcticooling
added 2456 characters in body; added 156 characters in body; edited body
Source Link
kierzniak
  • 2.5k
  • 11
  • 11

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

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
added 204 characters in body; added 8 characters in body
Source Link
kierzniak
  • 2.5k
  • 11
  • 11

Error is from the last command wp search-replace "${domain}" "test" --allow-root. Your WordPress site is not yet installed and you want to execute command which require WordPress to be installed.

Earlier in script you are dropping test database and creating new one, but you have to fill it with WordPress tables. Without this tables WordPress is not installed as the error says. You have to install WordPress by visiting your test page in the browser or execute wp core install with arguments in the command line.

You can also duplicate your database using mysqldump and mysql commands:

Dump: mysqldump -u root -p [database_name] > dumpfilename.sql

Import: mysql -u root -p [database_name] < dumpfilename.sql

Error is from the last command wp search-replace "${domain}" "test" --allow-root. Your WordPress site is not yet installed and you want to execute command which require WordPress to be installed.

Earlier in script you are dropping test database and creating new one, but you have to fill it with WordPress tables. Without this tables WordPress is not installed as the error says. You have to install WordPress by visiting your test page in the browser or execute wp core install with arguments in the command line.

Error is from the last command wp search-replace "${domain}" "test" --allow-root. Your WordPress site is not yet installed and you want to execute command which require WordPress to be installed.

Earlier in script you are dropping test database and creating new one, but you have to fill it with WordPress tables. Without this tables WordPress is not installed as the error says. You have to install WordPress by visiting your test page in the browser or execute wp core install with arguments in the command line.

You can also duplicate your database using mysqldump and mysql commands:

Dump: mysqldump -u root -p [database_name] > dumpfilename.sql

Import: mysql -u root -p [database_name] < dumpfilename.sql

Source Link
kierzniak
  • 2.5k
  • 11
  • 11

Error is from the last command wp search-replace "${domain}" "test" --allow-root. Your WordPress site is not yet installed and you want to execute command which require WordPress to be installed.

Earlier in script you are dropping test database and creating new one, but you have to fill it with WordPress tables. Without this tables WordPress is not installed as the error says. You have to install WordPress by visiting your test page in the browser or execute wp core install with arguments in the command line.