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.com
wp option update home http://test.yoursite.com
wp option update blogname 'Yoursite TEST'
echo "Searching and replacing domain name. Please wait this takes several minutes"
wp search-replace 'yoursite.com' 'test.yoursite.com' '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.