Backup WordPress to localhost

Backup WordPress to localhost

It’s always nice to have a backup plan for your WordPress website. Possible disaster will arise after updating WordPress to a new release, installing a plugin or theme, changing WordPress’s default files and so on.

 

 

 

 

 

1. What do we want to achieve?

For times like these, it’s a good habit to first try it out on a local backup, one that’s running on your own machine. If disaster will happen, it’s only affecting the local website while your live website is still up and running.

So, this is the thing we’d like to achieve:

  • We have a live website like for instance https://cool-tricks.net.
  • This website will be restored on our local machine.
    (We’re using WampServer to do this, although the same procedure should work for XAMPP too.)

 

2. Install BackUpWordPress

The easiest way to backup both your database and files, is to use a plugin called BackUpWordPress. (As of this writing, the version is 1.6.9).

  • Navigate to Plugins > Add New.
  • In the Search box, type backupwordpress.
  • Click Install Now.
  • Click Activate Plugin.

 

3. Backup your database and files

  • Navigate to Tools > Backups > Run Now. (Just stick with the default settings.) After about a minute (for a website around 20 MB) a new record will appear on your page.
  • Click Download and save the zip file to your desktop.

 

4. Unzip the file

  • Go to the root web folder of your local web server. In case of WampServer this’ll be something like C:\wamp\www.
  • Make a new directory in this folder like: C:\wamp\www\yourwordpressfolder.
  • Unzip the file you downloaded to this folder.
  • You should now have the following directory structure:
    • C:\wamp\www\yourwordpressfolder\wp-admin
    • C:\wamp\www\yourwordpressfolder\wp-content

 

5. Adapt the htaccess file (if necessary)

Open the file C:\wamp\www\yourwordpressfolder\.htaccess. Anything there that still points to the url of your live website? If so, change it so it refers to your localhost url. (In my case, there was nothing to change here).

 

Did you use this .htaccess file on your live webserver, to prevent public access?

If this is the case, look for these lines:

AuthUserFile "path/to/password/file/.htpasswd"
AuthType Basic
AuthName "Private area"
require valid-user

 

Comment them out like this and save the file.

#AuthUserFile "path/to/password/file/.htpasswd"
#AuthType Basic
#AuthName "Private area"
#require valid-user

 

6. Make a new database

 

7. Populate the database

  • On the left hand side of phpMyAdmin, click on the name of the database you just created.
  • Click Import > Choose file.
  • In the popup that appears navigate to the file C:\wamp\www\yourwordpressfolder\database_yourwordpressfolder.sql.
  • Click Open.
  • Click Start to populate your new database.

 

No data was received to import

Do you receive the above error message in your phpMyAdmin panel? If so, it’s possible that your database sql file is above the default php settings of 2 MB. In order to get this to work, we first have to change this setting in WampServer.

  • From your WampServer icon in the Windows system tray, navigate to PHP > php.ini.
  • Search for:
upload_max_filesize = 2M
  • Change this to something bigger. At least, it should be a higher value than the size of you sql file.
upload_max_filesize = 8M
  • Restart WampServer

 

8. Install David Coveney’s ‘search replace db’ PHP script

We need to change some values in our database. Since WordPress uses something called serialized PHP strings, we have to be very careful.

WordPress uses the PHP function serialize, to store certain values to the database. By doing this, the database will become non-portable. This is a big issue for us, since we need to make some changes in our database. If we manually change some values, there might be a chance that we’re corrupting our data. That’s not exactly something we want to happen…

 

To overcome the data-integrity damage that may be caused by the PHP serialization, we need a safe way to edit our database. Luckily, we can use David Coveney’s search replace db PHP script.

    • Download search replace db v 2.1.0. (You can always check David’s website to see if there’s a newer version of the script available.)
    • Put the unzipped file searchreplacedb2.php in the folder C:\wamp\www\yourwordpressfolder.

 

 

9. Update the database

Be aware that your localhost database is a one-to-one copy of your original online database. As a result, the localhost database is still referencing the original instance of your online website. We now have to update the localhost database to make sure everything is pointing to the new location. Therefore, we’ll use the ‘search replace db’ PHP script from David Coveney that we installed in step 8.

 

Step 1 of 2. Update the Uniform Resource Locator (url)

  • Navigate in your browser to the location http://localhost/yourwordpressfolder/searchreplacedb2.php.
  • Click Submit to populate the form with the old connection values to the database.
  • Adapt this form so we can make a connection to our local MySQL database server.
    • Server Name: localhost
    • Database Name: name_of_your_database (see step 5)
    • Username: root
    • Password: password_to_MySQL
  • Click Submit DB details
  • Click Continue
  • Click OK
  • Search for: http://yourlivewebsite.com (Be aware not to use a / on the end of your url!)
  • Replace with: http://localhost/yourwordpressfolder
  • Click Submit Search string
  • Click OK

 

Step 2 of 2. Update the physical path of the server

  • Don’t close the script yet, but click the Go Back button of your browser
  • Search for (case sensitive string): /var/www/yourwordpressfolder (Check whether you’re using the correct root path of your online webfolder. Also, be aware not to use a / on the end of your url!)
  • Replace with: C:/wamp/www/yourwordpressfolder (Notice that we used Wampserver for our localhost installation.)
  • Click Submit Search string
  • Click OK

 

10. Adapt php.config

  • Open the file C:\wamp\www\yourwordpressfolder\wp-config.php.
  • Look for the following lines and change the bold values with your own:
/** The name of the database for WordPress */
define('DB_NAME', 'database_name_here');
/** MySQL database username */
define('DB_USER', 'username_here');
/** MySQL database password */
define('DB_PASSWORD', 'password_here');
/** MySQL hostname */
define('DB_HOST', 'localhost');
/** Database Charset to use in creating database tables. */
define('DB_CHARSET', 'utf8');
/** The Database Collate type. Don't change this if in doubt. */
define('DB_COLLATE', '');

...

define('WP_TEMP_DIR', '/path/to/webfolder/wp-content/uploads');

 

11. Cleanup (optional)

This step is not necessary since we are restoring a live website to our localhost. However, in a live environment, you should definitely delete both the database sql file we imported in step 6, and the script file we used in step 7. They will become a major security threat if you leave them on your server! Therefore, delete the following files:

  • C:\wamp\www\yourwordpressfolder\searchreplacedb2.php
  • C:\wamp\www\yourwordpressfolder\database_yourwordpressfolder.sql

 

12. WampServer configuration

After doing the above steps, you should be able to navigate to the homepage of your local website via http://localhost/yourwordpressfolder. However, as soon as you navigate to something like http://localhost/yourwordpressfolder/about/ you may encounter the following error:

The requested URL /yourwordpressfolder/about/ was not found on this server.

 

If this is the case, you probably need to activate the rewrite_module of your Apache webserver. By doing this, you’ll able to follow the permalinks that your WordPress website is generating.

  • Starting from your WampServer icon in the tray bar, navigate to Apache > Apache modules > rewrite_module.
  • Restart WampServer.

 

13. Activate the permalinks

For some reason, whenever I tried to navigate to a page like http://localhost/yourwordpressfolder/about my default WampServer homepage showed up instead of the page I requested. There’s a simple trick to solve this problem.

  • Navigate to http://localhost/yourwordpressfolder/wp-admin.
  • Go to Settings > Permalinks.
  • Don’t do anything, just click Save Changes.

 

14. Are there any missing images?

In case there are some images not showing on your localhost website, check and see whether special characters are used in the filename. A good tip for dealing with these problems is to always use lower case for every file you put on a server. As an example.

  • Some servers change belgië.png to belgi├½.png
  • Simply change the filename to overcome these issues

 

15. Any remaining steps?

Did I forget any steps here? If so, I’d love to hear from you!

 

 

16. Conclusion

It has become a long list, but it should work out of the box! By now, you should have a working backup of your live website, on your localhost environment.

 

tip: You can also use the above procedure to migrate a WordPress website from one server to another server, even if you’re going to use a different domain name. Be careful though, always make a backup first! Here you can find a complete step-by-step guide that explains you how to Restore WordPress from localhost to an online web server.

It's only fair to share...Share on Facebook
Facebook
0Tweet about this on Twitter
Twitter
Share on LinkedIn
Linkedin

12 Comments

  1. Theo · September 27, 2012 Reply

    Hi,
    I’m am trying to achieve the opposite and find it’s like a “mission impossible” I just spent an entire week creating my first site in WP on a local machine (using Xampp) and about nearly into a week of just trying to “clone” or upload the site to a live server. I’ve read tons of articles, but due to mainly lack of knowledge – I ‘m finding it a difficult process.
    Which is how I came about your article..
    Any proven hints and tips from a seasoned pro like yourself?
    Regards,
    Theo
    South Africa
    ps…am going to try Xcloner now…//sigh – hope for the best!

  2. Imre · September 28, 2012 Reply

    Hi Theo

    I can imagine you’re having a hard time getting your locally developed WordPress website online. Especially if these are your first attempts getting to work with WordPress.

    Just know that a lot of people are in your situation. That means developing a website on their own computer, and then struggling like hell to get it online. There are indeed tons of articles out there, but most of them don’t really go that much into detail I guess.

    Anyway, I recently posted a step by step guide on all the steps involved into getting a localhost website online. You can have a look there to discover if there might be something you didn’t pay attention to. I hope it’ll be of any use to you.

  3. wecode · May 18, 2013 Reply

    Hi
    thanks a lot for sharing this valuable information. I was struggling for weeks unable to restore the live DB to my local machine. Even though I’, working in windows 7 environment it saved me.
    Thanks again

  4. Sudeep · October 4, 2013 Reply

    thank yo so much for providing this very useful information.

  5. Pankaj · January 26, 2014 Reply

    The information provided by you is a real and a rare gem

  6. Cok Tri · February 13, 2015 Reply

    Your tips number 12 solve my problem. I never think about wampserver configuration before. Thanks a lot man!

  7. Glen Pavelich · February 26, 2015 Reply

    I have been working to accomplish this task for most of the day and just came across your tutorial. Thanks for posting it.
    However, when I get to step 9 and navigate to the searchreplacedb2.php file inside my local folder I get
    Forbidden – You don’t have permission to access /mysite/searchreplacedb2.php on this server.

    (mysite) replaces my actual folder name just for this post.

    Can you tell me what I need to do in this case?

    Thanks

    • Glen Pavelich · February 26, 2015 Reply

      Also – there seems to be a missing step, which is when I set up the new database how do I associate a user and password with it. Do I go to ‘privileges’ and create a ‘new user’ with all privileges?

      In step 9 you have

      Server Name: localhost
      Database Name: name_of_your_database (see step 5)
      Username: root
      Password: password_to_MySQL

      but I’m assuming that password just a default for the search and replace tool?

      But what do we put in the config.php?

      Thanks

      • Imre · March 2, 2015 Reply

        Hi Glen

        For your first issue, it’s possible that there’s an .htaccess file in the same folder as your searchreplacedb2.php file blocking access to certain files. Check if there’s an .htaccess file and rule out the lines that are causing the issue. It also might be the configuration of your wamp or mamp installation…

        This tutorial is written for restoring a backup to your own computer for testing purposes. I’ve used WampServer for my local test environment. Before restoring your website backup, you should first create a new database on your local system. Most of the time phpMyAdmin is used for this… It’s also part of WampServer. That’s also the place were you determine the name of your new database. By default, phpMyAdmin is set up without a password for the root user. You can manage this by logging in to phpMyAdmin and going to the Users tab. There you can edit your current user(s) and add new ones. In short, this is were you manage the MySQL login credentials to get access to a particular database. For this tutorial, I used the default root user and added a password to it.

        Finally, in step 10 you can already see in the comments of the screenshot that you need to enter the MySQL database password. That’s the one I talked about in my previous paragraph. So to conclude, you should enter the login credentials that you generated through phpMyAdmin.

        Good luck!

  8. Kris · August 14, 2015 Reply

    Hi Imre! Thank you so much for this – it is really helpful! I was going along fine, but at step 13 I had a problem. My browser also sends me to the WAMPServer configuration page if I try to go to any pages from the main page. If it type in http://localhost/mywordpressfolder/wp-admin, though, I get a 404 page.

    I also noticed that if I click on the my_folder link under “Your Projects” on the config page, it tries to send me straight to http://my_folder/, which doesn’t work.

    Any ideas?

    Thanks!

  9. Ismail Eltahawy · December 25, 2015 Reply

    that was very helpful, thanks alot

  10. Rahul Varadkar · November 12, 2016 Reply

    I am using XAMPP Server and WordPress on my localhost.
    WordPress is installed separately for every Site Folder on my local machine.
    I wanted to make a copy of existing Site-A to Site-B and similarly existing DB-A to DB-B on same machine.
    I tried all steps in this post. Every thing worked correctly.
    Except when I run VISIT SITE from word-press all Menus are displayed as LINKS. I am using default Theme provided by WordPress.
    Did I miss anything? How to fix this?

Leave a Reply