Duplicate a site for testing

So I’ve been asked how and what I use to duplicate WordPress sites for testing purposes. I do this fairly regularly on my own site and sites I host for clients and friends,, I recently found a new trick using SSH to make the mysql backups too which I’ll go through. I assume you have a fairly straight forward install of WordPress in this post since if you have something customized you probably know the importance in backups and have your own system in place. I work mainly on WordPress sites but most of these commands can be used for backing up other websites,, there is only one last extra step when setting up your WordPress site locally that is important.

Some of you may ask ‘Why is duplicating a WordPress site necessary?’

  • Well I do it to test upgrades of wp-e-commerce, to make sure things don’t break after an upgrade and serves as a file backup if I ever need one in the future.
  • They are also useful for backups
  • It also comes in handy when developing new functionality specific to a site

A few things to note, if you are unfamiliar with SSH and terminal then I’d suggest you learning a little about that.. SSH is a more secure form if FTP, and it allows you to perform some functions that are either impossible or slow to do via FTP. Just Google ‘basic SSH commands’ and you should get some good info… I also just read a good ‘ 7 usefull terminal commands ‘ article on nettuts recently which I would highly recommend bookmarking.
I also assume you are running MAMP on your mac or XAMP on your PC and are familiar with using it.. If you are not and you are wanting to learn about being a web developer than you should Google them and download and install them.

Obtaining the files

1. Login to your server via SSH:

ssh Username@mywebsite.com

you will then be prompted to enter in your password.

2. Navigate to just outside your websites folder then use the zip command to zip up your files ( this might take awhile depending on the size of your site)

zip -r mybackup.zip mysite/

3. Move this file to a place accessible via a webbrowser (downloading it via SSH or FTP will take longer- I put it in the site the make sure to delete it once I’ve downloaded it)

mv mybackup.zip mysite/mybackup.zip

4. In a web browser navigate to http://mysite.com/mybackup.zip and download it.. Once it’s downloaded i usually move it into a folder outside of site so other people can’t download it instead of deleting it. Ie mv mysite/backup/mybackup.zip mybackup.zip

5. Now that we have the files it’s time to export the database, there’s a few ways of doing this.. I used to just use phpMyAdmin but there’s an even easier way if your already logged into SSH. All you need is the database host (usually localhost but can sometimes be different), username, password and database name.
In SSH type:

mysqldump --host=mysql.mysite.com --user=username --password=password --all-databases > mysite_db.sql

Once that’s done you should have a new sql file called mysite_db.sql repeat step 3 and 4 to download the file to your local computer.

Using the files

Cool now you should have two files on your computer that’s all we need to install a working version of your WordPress site locally.
Steps for installing are rather straight forward,

1. In your localhost environment unzip the mybackup.zip file so you have localhost/mybackup

2. In your localhost/mybackup/wp-config.php change your WP_USER, WP_PASS,WP_DB, and WP_HOST values to suit your localhost environment.

3. So now we have the files ready it’s time to make the database.. Login to your localhost phpMyAdmin and create a new database mysitedb then in the upper right corner select import and navigate to the files we downloaded (mysite_db.sql) and hit import

4. WordPress stores two options called ‘siteurl’ and ‘home’ which need to be changed otherwise if you try to login or view your site you will keep being sent to your real site online! This part confused the hell out of me the first time I tried it but was so aparent afterwards~ In your wp_options table search for the value siteurl and home then change them to the new location ie http://localhost/mybackup

Once this is done you should be ready to test your upgrades, work on modifications etc… Note sometimes you might get permissons errors on your wp-content folders, I usually resolve this issue by checking the sections in wp-admin>wordpress>miscellaneous section. You might also get the dreaded 500 Internal Server Error, in which case I usually delete the .htaccess rules or modify them slightly…

Well I hope you find this useful. I’m sure I’ll be reading this article again the next time I have to do a backup :D Good luck and happy coding!

Best
Jeff

One thought on “Duplicate a site for testing

  1. Thanks for the info Jeff. I think this level is the next step for me as a developer. Doing this sort of testing should be really helpful. I’ve used SSH and XAMP tools very infrequently, this ought to stretch my tool box in a good way.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>