0

I've duplicated a site using the following code. All code worked fine until the last row (the site itself goes up on the web but the last row of code fails and I get an error):

echo "Please enter the domain of the site for duplication"
read domain
echo "Please enter the password of the site's DB user"
read -s psw

cp -r /var/www/html/${domain} /var/www/html/test
sed -i "s/${domain}/test"/g /var/www/html/test/wp-config.php
mysql -u root -p <<-MYSQL
    DROP user IF EXISTS 'test'@'localhost'; SELECT user FROM mysql.user;
    DROP database IF EXISTS test; show databases;

    CREATE user 'test'@'localhost' IDENTIFIED BY '${psw}';
    CREATE database test;
    GRANT ALL PRIVILEGES ON test.* TO test@localhost;
MYSQL

cd /var/www/html/test
wp search-replace "${domain}" "test" --allow-root

The last WPCLI command outputs:

Error: The site you have requested is not installed.

The same goes for other WPCLI commands. For some reason, WPCLI doesn't recognize my test site.

Any idea why WPCLI doesn't recognize my site when I'm clearly inside its directory?

Update for motivast:

mysql -u root -p <<-MYSQL
    DROP user IF EXISTS 'test'@'localhost'; SELECT user FROM mysql.user;
    DROP database IF EXISTS test; show databases;

    CREATE user 'test'@'localhost' IDENTIFIED BY '${psw}';
    CREATE database test;
    GRANT ALL PRIVILEGES ON test.* TO test@localhost;
MYSQL

mysqldump -u root -p ${domain} > test.sql
mysql -u root -p test < test.sql

Did I have the logic right? Did I passed the test?

1 Answer 1

3
+100

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

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
11
  • 1
    I need to duplicate all DB data from the original site to test. Do you know a way to do that? If I'll install the core from scratch I won't have that data so I need to somehow complete the script to have that data. Commented Dec 4, 2017 at 9:43
  • 1
    You can use mysqldump command to dump database and mysql to import it. Dump mysqldump -u root -p[root_password] [database_name] > dumpfilename.sql import mysql -u root -p[root_password] [database_name] < dumpfilename.sql
    – kierzniak
    Commented Dec 4, 2017 at 9:45
  • 2
    There is not mysqldump equivalent in SQL language you have to execute this statements as separate commands. If you omit password you would be simply asked for it.
    – kierzniak
    Commented Dec 4, 2017 at 9:54
  • 2
    Your edit seems to be ok. If you want I try can provide you ready script but I can't do it now but for about 10h.
    – kierzniak
    Commented Dec 4, 2017 at 10:33
  • 2
    I updated my answer.
    – kierzniak
    Commented Dec 5, 2017 at 9:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.