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.
- Install BackUpWordPress
- Backup your database and files
- Unzip the file
- Adapt the htaccess file (if necessary)
- Make a new database
- Populate the database
- Install David Coveney’s ‘search replace db’ PHP script
- Update the database
- Adapt php.config
- Cleanup (optional)
- WampServer configuration
- Activate the permalinks
- Are there any missing images?
- Any remaining steps?
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.)
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.
- 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.
- 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:
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
- Navigate to http://localhost/phpmyadmin > Databases > Make new database
- Give your database a name and click Submit.
- 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
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.
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
- 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');
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:
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.
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.
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
Did I forget any steps here? If so, I’d love to hear from you!
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.