1

For our WordPress-website, I wanted to create a test instance - to test WordPress-updates/plugin updates and new developments on the theme and so on ...

so i wrote a script to copy the database and the files:

sourcedir="/srv/www/htdocs/www.example.de"
testdir="/srv/www/htdocs/test.example.de"
workdir="/data/example.de_test-system"
sqldumpfile="$workdir/mysql_dump_example_wp2017.sql"

echo "delete webroot test.example.de"
rm -rf $testdir
echo "www.example.de nach test.example.de kopieren"
cp -R $sourcedir $testdir
echo "cache-verzeichnisse leeren"
rm -rf $testdir/wp-content/cache/minify
rm -rf $testdir/wp-content/cache/db
rm -rf $testdir/wp-content/cache/object
rm -rf $testdir/wp-content/cache/page_enhanced
rm -rf $testdir/wp-content/cache/tmp
echo "alten dump loeschen"
rm -f $sqldumpfile
echo "aktuellen Dump aus DB ziehen"
mysqldump -u root --password=xxx example_wp_2017 > $sqldumpfile
echo "dump bearbeiten - www.example.de gegen test.example.de ersetzen"
sed -i s/www.example.de/test.example.de/g $sqldumpfile
echo "bearbeiteten dump in test-DB einspielen"
mysql -u root --password=xxx example_wp_2017_test < $sqldumpfile
echo "test-config einspielen"
cp /data/example.de_test-system/wp-config.php $testdir
echo "www-nutzer-rechte auf test.example.de setzen"
chown -R wwwrun $testdir
chgrp -R www $testdir

the script will copy the WordPress-folder, empty cache folders, dump the database and find/replace entries of the productive system with test-system and import the dump to the database - finally, copy the test-config to the test folder

this works, but I got some strange errors - like a logo image, where it displays a placeholder image instead

the meta-navigation is missing

and in the backend, some points in the left navigation are missing - like the custom post types

am I missing some things in my script?

2
  • look in the original dump, search www.example.de and you will see that sometimes, bad-builded themes or plugins save the URL in a way that a simple sed cannot modify
    – mmm
    Apr 6 '17 at 10:58
  • Best Plugin to do this is "Duplicator" - just sayin' - all your wheel invention has been done before. Apr 11 '17 at 15:10
1

In general test enviroments better use the same domain name as the live site. Since it doesn't seem like you follow such pattern (and yes, it is harder to follow), you need to make sure you convert all the URLs in the DB properly, and the way you do it will fail with any data which is serialized. Use wp-cli to change URLs instead of re-inventing your own solution.

In addition you will probably will want to change the option or add some code to make sure that google do not try to index the test site.

4
  • thank you for the fast reply - the site is protected by .htaccess and we can only access it from inside our company. URLs are all converted in the DB-Dump with sed - i cant find any occurences of www.example.de in the dump for test. could you tell me more about the serialized data and wp-cli?
    – roeb
    Apr 6 '17 at 7:57
  • a lot of wordpress data is store in serialized php format - phpinternalsbook.com/classes_objects/serialization.html which for strings will include the length of the string as well as the string itself, and in your case the legth is different. It is hard to predict which parts of your code store things in serialized way without access to the code, but generally that is the reason for failures when moving domains. Apr 6 '17 at 8:09
  • wp-cli wp-cli.org is a command line tool which can manipulate all kind of wordpress settings, and one of its options is to do a search and relace which works also on (hopefully all) serialized data wp-cli.org/commands/search-replace. Apr 6 '17 at 8:09
  • thank you! with your help i managed to create a clean clone. custom post types are in test, the logo is the same as in prod and the missing navigations are back. i thought it was a good idea to search/replace the dump with sed - not knowing about the serialized data - now i do it with better search/replace-plugin
    – roeb
    Apr 6 '17 at 9:04
0

I've found the best cloning plugin for my use is WP Clone here: https://wordpress.org/plugins/wp-clone-by-wp-academy/

Quite easy to use, just install on the source and target WP sites. Then export from the source site, and import to the target (log into the target). All your settings (including users) and content including media will be in the target site; you will have to log in using an account that was on the source system.

Great plugin.

3
  • on a production system, you do not want any plugin that is not relevant to the actual functioning of the site, and in any proper IT enviroment it will take more time to get permission to install it than to actually do the backup. In such enviroment you start working with a dump of the DB, making a site in order to install a plugin to make another site is just a double work. You should really invest some time in getting familiar with how to do such things from a command line ... your life will change forever ;) Apr 6 '17 at 17:58
  • The WP Clone plugin can be disabled right after the backup is completed. If you don't need absolutely current content in the backup/testing environment, then one backup and plugin disable on the production system will be sufficient for that need (a functioning, non-production system). Apr 7 '17 at 18:19
  • in the vanilla wordpress setup, disabling a plugin do not always prevent it being used as an attack vector. On a production site you should not have any unused theme or plugin. Apr 8 '17 at 14:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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