Backing up your WordPress install: the Basics

Backing up. You know you should be doing it. In fact, it’s on your to do list. The problem is, it’s been there for 3 months. In some small way, the fact that it’s on your to do list makes you feel better about the fact that you’re totally vulnerable. It’s on the list, right? It’s bound to happen soon.

Do I really have to know this?

Have to? No. Should? Probably. It’s true that you can use any number of plugins or other easy-setup tools to back up your site. However – knowing the basics will help you choose the right tool, and ensure you configure it correctly. The more you know, the less likely you are to make mistakes.

Why don’t you have backups?

The fact is, for many blog owners, it happens never. Why? There are a number of reasons:

  1. I don’t know what I need to save

    In theory, everybody understands what a backup is. You need your content and files in case of catastrophe, so you store a copy somewhere else. Simple. However – once you get into the nitty gritty of it – where are my posts stored? do I need to save my theme? All the files on my server? – it becomes apparent that there’s more to it than copying a few files.

  2. I don’t know where to save it

    If you store your backups on your server, and your server lights on fire, what then? More realistically – someone with less-than-stellar intentions gets access to your hosting account, and erases all it’s content. Your backups do you no good if you lose them when you lose your content. So – where should you store them?

  3. I need to restore my site, and I don’t know how to

    Your site has been hacked, and the hacker replaced the entire server with a single page, with a link to his favorite pharma site. Good news – you backed up! All that’s left is to reload all your old content. The problem is, you’ve got no idea what that entails. Do I need to copy all the files from my backup over? How do I get my posts back?

So – after giving it some thought, and realizing it’s a bigger job than you hoped, you put it off. Backups aren’t exactly paying the bills, right? You’ll get to it when you’ve got some free time, and a hankering for sifting through database usage documentation.

We’re going to get you pointed in the right direction. By the end of this article, you’ll have the knowledge you need to move forward, and choose the right backup plan for your situation.

What do I need to back up?

With a site running on wordpress, you’ve got 2 important pieces that need to be backed up.

  • Database

    Most importantly, you’ve got a database. WordPress stores all of your posts, comments, options, logins – everything in a MySQL database. This database is absolutely the heart of your site, and the most important thing to backup.

  • Files

    Also important (but far less so) are your files. In a typical WordPress install, you start with what you downloaded from wordpress.org (or installed via Fantastico, or similar). These automatically installed files aren’t terribly important – if you lose them, you can just download a fresh copy from wordpress.org, or install again from fantastico. However, once you set up your blog and begin using it, you create a number of files that you DO want to back up.

    • wp-config.php
      This file is stored in the root of your wordpress install. It’s not the most important file on the list – if you lose it, you can come up with another one pretty easily – but it’s not a bad idea to back it up. This file contains the configuration settings for your blog – the database username and password to use, and a few other similar pieces of information.
    • wp-content

      This folder is found in the root of your WordPress install as well, and it’s important. The wp-content folder holds all of your plugins (in wp-content/plugins), your themes (in wp-content/themes), and most importantly, your uploads (in wp-content/uploads). Your plugin files are important, but could be downloaded and reinstalled in most cases.

      Themes are a little more important – if you’ve spent any time making changes to the look and feel of your site, those changes are in this folder – you don’t want to lose them.

      Lastly, your uploads folder – if you’ve uploaded any content to your blog (images, most commonly, but in many cases users upload other types of files as well), they’ll be stored in this folder. If you lose it, and restore your blog, you’ll be looking at broken images all over. Going back through your posts, finding broken images, and replacing them with good ones pure drudgery. You don’t want to do it.

    How do I back these pieces up?

    • Database

      Your database is the tricky piece. Unlike files, which are easy to just copy or download, you actually need to export your database files. There are a number of ways to do this, but we’ll focus on the easiest and most accessible – using phpMyAdmin.

      What is phpMyAdmin?

      Command LinephpMyAdmin is a useful tool that comes standard with nearly all shared (as opposed to dedicated – if you’re not sure if you’re on a shared hosting plan, you probably are) hosting packages. While access to your database is normally restricted to the command line (think text only input – like the old ms-dos prompt) phpMyAdmin gives you a fairly simple interface to issue commands to your database.

      Exporting your WordPress database using phpMyAdmin:

      1. Open phpMyAdmin
        First, you need to get to phpMyAdmin. I can’t give specific instructions here, because it varies from host to host – but it’s usually a safe bet to get to find it on your hosting control panel (it may be listed under “databases”, “MySQL”, or something similar.

        Once you’ve opened phpMyAdmin, you should see a screen similar to this:
        phpMyAdmin Home

      2. Choose your database
        Next, you need to choose the database you’d like to back up. You MySQL installation can be partitioned into separate databases, and your WordPress install lives in one of these databases. If it’s not immediately apparent which database you need to open, you can check in the wp-config.php file in the root of your WordPress install. Look for the line that looks like this:

        define('DB_NAME', 'yourdatabasenamehere');

        You should see a link on the left side of the screen that matches the name of your database. Click here to open it up.

      3. Export the database
        You should now be looking at a page that looks vaguely like this:
        Database Detail Page

        You’re now looking at the individual tables of the database you chose. At this point, we want ask phpMyAdmin to actually export these files. Fortunately, they make it easy for us, with an “export” tab right at the top of the page. Click here to configure your export.

        Next, you’ll be presented with the export screen. Here you need to choose which tables you want to export, how you want them to export, and how you want the file to be saved. Making your screen look like this is a pretty safe bet:
        phpMyAdmin Export
        A couple of important points:

        • In most cases, you just want to export all your tables.
        • Make sure “SQL” is selected in the export box. We need the file in this format for easy restoration
        • Make sure the checkbox next to “Data” is selected. Without it, you’ll only get the database structure, not any of the data (your posts, options, etc).
        • Make sure “Save as File” is checked. This packages the export up into a download, instead of just displaying the contents to your screen
        • You probably want to choose “gzipped” for “the “Compression” option. Doing so will zip up your export before it gives it to you as a download, making the download smaller and quicker (“zipped” will do the same, but phpMyAdmin’s importer seems to play nicer with the gzip setting – and that’s probably the only way you’ll ever open this file).
      4. Run the export
        Once you’ve got that set, hit “go”. Depending on the size of your database, this could take a while. Be patient, and wait until the file it gives you has downloaded.
      5. Just like that, your database is backed up. You’ve got all of your posts, options, tags, categories, and comments wrapped up into one neat little file, which you can keep in a safe place. Keep in mind – this export is a snapshot of how the database looked when you exported – so any posts made after the export will require a new one to be backed up. We’ll talk about that more later.

    • Files

      FTP?

      Don’t have any experience working with files via FTP? We’ll have an article up soon to help. In the meantime, give this tutorial a try.

      Your files are a little easier to back up, if you’ve got any experience working on your site via FTP. Download the files we mentioned before:

      • wp-config.php (file)
      • wp-content (directory)

      to your computer, and you’ll be set.

    Where to save these?

    You’ve made the first step – making a current backup of your site, and saving it to your computer. You now have at least SOME defense against total data loss, which is great. As mentioned before, however – your home computer isn’t a great place to leave these files. What if your home computer crashes, and takes your backups with it? What if your wife uses the computer, and deletes the files accidentally? What if you’re out of town when your site goes down, and you don’t have access to your home computer?

    Amazon S3Your best bet is to figure out how to get these backup files online, preferably with a dedicated backup service. Doing so gives you peace of mind as to their security, and in most cases, it allows you to access your files from somewhere other than home. In it’s simplest form, this means uploading to another hosting account that you have access to – but if you want to go all the way, try using a service like Amazon S3 (seem too complicated? Check out this Firefox Amazon S3 plugin) or the super-simple dropbox.

    How to restore your site

    Your site got wiped out. Hamsters in the server room. While your friends are all lamenting the loss of their data, you’re sitting pretty – you’ve got a fresh backup just waiting to go online. So what do you need to do?

    1. Get your backup onto your computer
      If you’ve been storing the backups on your computer, you’re already here – if you’ve been using a service like Amazon S3, you’ll need to download the files to your computer, so you can work with them.
    2. Reinstall WordPress
      Either through fantastico, or standard FTP, you need to get the WordPress core reinstalled on your server. Make sure to put it in the same location it was before (i.e. if it used to be in /blog, make sure to install it there this time), to prevent problems. You don’t need to run through the WordPress install process though – all we want is to get the files on the server.
    3. Restore your backed up files
      The files you saved (wp-config.php and the wp-content directory) will be the first things you’ll put back. Upload these both to the directory that you installed wordpress in. wp-config.php should not need to overwrite anything, as WordPress doesn’t put this file in until you run through the installer. wp-content, however, will be overwriting a folder that came with your fresh install. That is fine – if your FTP program warns you about it, tell it to go ahead and overwrite.

      At this point, you’ve got your file structure back in working order – but you dont have any data for the site to work with. Trying to pull up the site now, loads up the wordpress installer, which is not what you’re after.

    4. Restore the database
      Now we import your data from the backup. Lucky for us, phpMyAdmin has a handy feature to handle this as well, and we get to it the same way we got to the export – with a couple of exceptions. First, there is no database created for the WordPress install yet (that happens when you run the installer). We exported the entire database before (instead of just a few tables), so when you open up phpMyAdmin this time, don’t choose a database first – just go from the home page to the “import” tab.
      Choose import file

      Click “Go”, and phpMyAdmin will run the import, restoring your database.

      The wp-config.php file still has the connection settings for the database, and for the table names used in your old install. If you’ve already uploaded this, you should be set to go- load up your blog, and it will be back to normal. You’re back in business!

    Backup: Accomplished

    You now know how to back up your blog by hand. As you might have guessed, doing this by hand as often as you should to keep a current backup in case of disaster is a lot of work – fortunately, there are a lot of great tools out there that can help you schedule and automate the process. Now that you know what is going on, you know what to look for in a backup tool, and how to configure it. We’ll put together a post comparing WordPress backup plugins soon to help you along. In the meantime, get those sites backed up, and if you have any questions, we can get them worked out in the comments section.

    Good luck!