最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Automatically clone wordpress site to subdirectory

programmeradmin5浏览0评论

I want to automatically clone mother website to subdirectory, which takes too much steps now and I think there would be easier way. Here's my steps I'm doing now

Make subdirectory site (add site) > install astro theme on subdirectory > delete sample pages and post > import mother site > change few things on cloned site (name etc..)

I think there would be easier way, Would be so nice to make it automatically. If you know any way please let me know! Thanks so much

I want to automatically clone mother website to subdirectory, which takes too much steps now and I think there would be easier way. Here's my steps I'm doing now

Make subdirectory site (add site) > install astro theme on subdirectory > delete sample pages and post > import mother site > change few things on cloned site (name etc..)

I think there would be easier way, Would be so nice to make it automatically. If you know any way please let me know! Thanks so much

Share Improve this question asked Jul 14, 2020 at 4:58 Shiny LeeShiny Lee 1
Add a comment  | 

1 Answer 1

Reset to default 1

I have done this exact thing so that I can test plugin updates. I wrote a shell script (bash) to create the sub directory and copy the files. Then, I leveraged WP-CLI commands to create wp-config file, export/import the database and search and replace the site name in DB. Here is my script with specifics changed to "yoursite"

#!/bin/bash

shopt -s expand_aliases
alias wp=~/bin/wp-cli.phar

wp --version

sourceDir=~/public_html
destDir=~/public_html/test/

### Delete existing files in $destDir
read -p "Deleting all files in $destDir ...Continue? (y/n): " -n 1 -r
echo
if [[ $REPLY = [yY] ]]; then
    #rm -rf "$destDir/".[!.]* "$destDir/"*
    rm -rf "$destDir"
    mkdir "$destDir"
    if [[ -z $(ls -A "$destDir") ]]; then
        echo "Destination cleaned"
    else
        echo "Error: destination not empty"
        exit 2
    fi
else
    echo "Script aborted by user"
    exit 0
fi


### Clone source files
echo "Starting clone of source files"
cp "$sourceDir/"* "$destDir"
cp "$HOME/bin/.htaccess" "$destDir"
cp -R "$sourceDir/wp-"* "$destDir"
echo "Source files transfered"

### Create wp-config.php
cd "$destDir"
rm ./wp-config.php
wp config create \
    --dbname=test_yoursite --dbuser=test_user --dbpass='yoursitePass' --dbprefix=wp_ --extra-php <<EOL
@ini_set('display_errors', 'Off');
@ini_set('log_errors', 'On');
define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);
define('WP_DEBUG_DISPLAY', false);
define('SAVEQUERIES', false);
define('WP_MEMORY_LIMIT', '1024M');
define('WP_POST_REVISIONS', 10);
define('WP_MEMORY_LIMIT', '128M');
define('WP_AUTO_UPDATE_CORE', 'minor');
EOL

### Clone DB and update site name
echo "Starting clone of Database"
cd "$destDir"
wp db export prod.sql --path="$sourceDir"
wp db reset --yes
wp db import prod.sql && rm -f prod.sql

wp option update siteurl http://test.yoursite
wp option update home http://test.yoursite
wp option update blogname 'Yoursite TEST'

echo "Searching and replacing domain name. Please wait this takes several minutes"
wp search-replace 'yoursite' 'test.yoursite' 'wp_posts'
wp rewrite flush

### Disable plugins
wp plugin deactivate w3-total-cache
wp plugin deactivate wordfence

echo "Done"
exit 0

There are also free or paid plugins such as Duplicator which can clone your site in a semi-automated fashion...possibly like you're already using.

There are also web hosting companies that include site cloning features (staging site); such as WP Engine, SiteGround and others.

发布评论

评论列表(0)

  1. 暂无评论