Create a Backup of Your WordPress Website Locally

… (the hard way).

There are probably a lot of plugins out there that will walk you through this process, but we’re going to be doing this the hard way. Why would we want to do this the hard way? Well, sometimes doing things the hard way gives you a better understanding of the ins and outs of a system as a whole and how things work. And sometimes trying to do things the easy way breaks down and you’re not sure how to resolve the problem. Not all plugins work out of the box and you have to go through all of the necessary support channels to resolve the issue.

However, if you know what needs to happen to accomplish something from the outset, you can forgo any of the headache that comes with automated processes going wrong. So, let’s take a look at how we might accomplish this.

1. You’ll want to get your local environment set up. If you’re on windows you can use WAMP, and if you’re on a Mac you can use MAMP. Create a directory in your WAMP or MAMP www directory and name it something (e.g. my-local-site). This is where you are going to put all of the WordPress files from your site.

2. Using FTP (like FileZilla), pull down all of the files from your WordPress installation on your live server into the newly created directory.

3. Sign in to phpMyAdmin on your localhost (http://localhost/phpmyadmin/) and create a new database. If you have a WordPress stack, you’ve likely had some exposure to phpMyAdmin at some point or another. You don’t really need to do anything further with it at this point. This is where you’ll be importing the .sql file that you’re going to go get from the database on your live site.

4. Now, sign in to your webhost portal and access phpMyAdmin, however that is done on your host. Find the database that contains all of your WordPress data and click into it. If you are not sure which one it is, ask your webhost or systems administrator. You should see a big listing of WordPress tables, like wp_options, wp_posts, wp_postmeta, and others. Note that the “wp_” might be named something different depending on how you set up your site (or how it was set up for you). But you’re in the right place if the names after the _ look familiar.

3. Click on the “Export” button. This will take you to another page where you’ll save the database. Choose the “Save as File” option at the bottom and go ahead and choose “No compression”. This will save a .sql file for you to your machine.

4. Now go back to your local phpMyAdmin web portal click into the empty database you created earlier. Once you are in there, click the “Import” button. Navigate to the .sql file that you just downloaded, and click OK.

NOTE: Sometimes at this step you can get a common error stating that your database file is too big. If this is the case then you need to update your php.ini file to allow for larger file uploads. In WAMP, this file lives in bin/php/php5.4.3/php.ini. Note that your PHP version number might be different depending on which version of WAMP you’ve installed. Edit the php.ini file in notepad and search for the following at different places in the file…

post_max_size = 2M
...
upload_max_filesize = 2M

What this means is that PHP will only accept file uploads and posts up to 2 MB. If your database is larger than this, it will reject it.

But you can change the limit by simply changing these values…

post_max_size = 8M
...
upload_max_filesize = 8M

5. We now have the files we need and the database we need, but we need to go edit wp-config.php in the root directory of your WordPress installation to link up the two. In your localhost www directory go to the folder where you’ve installed all of your WordPress files and open up the wp-config.php file. You’re going to want to edit the following lines. The values set in these lines are probably configured for your live server and have appropriate usernames and passwords set for security purposes so they are probably filled out with a lot of data/values…

define('DB_NAME', 'wp1239823');
/** MySQL database username */
define('DB_USER', 'ivanuser');
/** MySQL database password */
define('DB_PASSWORD', 'superpassword#1234');
/** MySQL hostname */
define('DB_HOST', 'user.domainname.com');
/** 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', '');

However, because we want to link these values up with our localhost web server, we need to change these values back to something that is more “default.” In the end you’ll probably want your file to looks something like this…

define('DB_NAME', 'wordpress');
/** MySQL database username */
define('DB_USER', 'root');
/** MySQL database password */
define('DB_PASSWORD', '');
/** 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', '');

The important part is your database name. Put the name of the database that you created in step 3 and imported your .sql file into. The database names of your live site and your local site do not have to be the same.

6. We’re almost there, but we just have to fix one final little thing. We need to tell WordPress that your site’s URL is not http://www.yourlivewebsite.com anymore. We need to tell it that your URL is now http://localhost/your-local-site

Really this can be done in one of 2 ways. Probably the easiest way is to add a couple of lines to your wp-config.php file. Look for the following line…

define('WP_DEBUG', false);

and immediately after it, add the following lines…

define('WP_HOME','http://localhost/your-local-site');
define('WP_SITEURL','http://localhost/your-local-site');

where “your-local-site” is the name of the directory that you copied all of the WordPress. What this does is tell WordPress that your site URL is now your local site and not your live site.

Another option is to go into the wp_options table in your database and edit the option fields there with these same values. Really, you should only do this if you are very comfortable with MySQL databases and your understanding of how WordPress options work.

More information on this can be found here on WordPress.org

7. Now if you go to http://localhost/your-local-site you might get a 404. Not to worry. This is likely because your .htaccess file has not been updated. Go to http://localhost/your-local-site/wp-admin. Sign in and go to “Settings” and click on “Permalinks.” All you have to do is click the “Save Changes” button, if all goes well this will update your URL paths for your site on the local box. The permalink option that you have selected on your live site should already be selected for you here but if for some reason it is not choose this option and click “Save Changes.”

Now, if you go to http://localhost/your-local-site you should be able to see your website running seamlessly on your local machine. This is a great place to do editing / testing and messing around with stuff without affecting anything on your live website.

WordPress Themes

Like this post? How about a share?

Stay Updated with the 9bit Studios Newsletter

0 Responses to Create a Backup of Your WordPress Website Locally

Leave a Reply