错误来自上一个命令wp search-replace "${domain}" "test" --allow-root
. 您的WordPress站点尚未安装,您希望执行需要安装WordPress的命令。
在脚本的前面,您正在删除test
并创建一个新的数据库,但您必须用WordPress表填充它。如果没有此表,WordPress不会按照错误说明安装。您必须通过访问浏览器中的测试页或执行wp core install
命令行中有参数。
您还可以使用复制数据库mysqldump
和mysql
命令:
转储:mysqldump -u root -p [database_name] > dumpfilename.sql
导入:mysql -u root -p [database_name] < dumpfilename.sql
以下脚本将为您的:
#!/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
您可以通过以下方式使用它:
sudo ./wpdup.sh /var/www/test /var/www/testnew wp_test wp_testnew wp_testnew xxxxxx test.dev testnew.dev