How to Backup Your Hosted Site
Backups are important because they protect you from loosing all your hard work when something goes wrong. Regardless of the kind of problem, a good backup will let you get back parts of your web site (for example, if you accidentally delete an important post or your theme files) and go as far as to set up your entire site at a new hosting service (for example, if your hosting service goes out of business).
This page is concerned with CMS software that (1) resides on web server1 as a bunch of files within a single subdirectory or folder2 and (2) uses MySQL for its database. 3 If you use a hosted blogging service where you simply sign in to your account and create pages, then how to backup your site should help you find better suited instructions.
Looking Ahead to the Final Result
When you finish this process, you will have a single zip file that contains all of the files required to run your site, plus everything stored in your database. The goal here is to have a self-contained file that contains everything you need to completely restore your site. Other techniques may save a bit of time and disk space, but I think it is better to have everything you need when disaster strikes and you are scrambling to get your site running again.
Getting Ready
Before you begin, you will need:
- Access to a shell on your web server. This is usually called "ssh access". You usually have to do something to get this access; most hosting services don't allow it by default.4
- Secure shell (ssh) client software. If you're using a mac or Linux, then ssh is built-in. If you are using Windows, then I recommend putty. For the rest of this article, I will assume you are using putty.
- The top directory of the web server files. This is the directory that contains all the files your site uses.
- The connection details for your database. I assume you are using MySQL; if you are using some other database, you will need to figure out what command to use in step 1.
Making a Backup, Step-by-Step
Once you have that information, the process is:
Step 1: Open a shell window.
To do this, start putty, enter your hostname5, and click the Open button. Putty will switch to the classic terminal window.
The terminal window will ask for your username. Type it and then press enter. Next, it will ask for your password. Although it will not show what you type (to keep your password secret); just be sure to press enter.
When you've successfully logged in, you will see a command prompt. You will use the command line for the next few steps.
Step 2: Create a database dump file.
This step copies everything out of your database into a single text file that contains all the SQL commands required to build your database from scratch.
To do this, you will need a few details about how to connect to your database. The best place to get that information is directly from your software configuration file.6
- DBHOST Is the name of the database server. Often, it is "localhost".
- DBUSERNAME Is the database user, not the username you used when you logged into the server with putty. Those names could be the same, but they don't have to be.
- DBPASSWORD Is the database password. Again, this does not have to be the same password as when you logged in using putty.
- DBNAME Is the database (or schema) name.
Here is the command you need to type. It all goes on one line, even though it is too long to fit on one line here.
mysqldump --add-drop-table --order-by-primary --skip-extended-insert --host=DBHOST --user=DBUSERNAME --password=DBPASSWORD DBNAME > dump.sql
Step 3: Create the zip file.
The zip file contains the database dump and all the files that the site uses. I have called it ZIPFILE but you should give it a sensible name that includes the date, such as "myblog-2010-01-02.zip".
You also need to replace TOPDIR the correct path to top-level directory.
To create the zip file, run this command with the proper zip file name and top-level directory:
zip -qry9 ZIPFILE dump.sql TOPDIR
Step 4: Copy the backup somewhere safe.
Be sure to copy the ZIPFILE to another computer for safe-keeping. That way, if you cannot access the server, you can still use it.
That's it. Just be sure to repeat this process often enough that you will always have a fairly current backup copy.
----------- It doesn't matter if the web server is a web hosting service or your own server, as longs you have the right kind of access. (⇑)
- It is hard to imagine a CMS that doesn't reside on the server's filesystem, but it is possible. If you're running a system like that, you'll need to adapt these instructions. (⇑)
- If you are using a database other than MySQL, you should be able to adapt the one step that uses "mysqldump" to use whatever your database uses. If your software doesn't use a database, then you can simply skip those steps. (⇑)
- Although you have to request it, ssh access should not cost extra beyond the normal cost of your hosting. If it does cost extra, you should probably find a new web host provider. (⇑)
- If you don't know your hostname, you will need to get them from your hosting service. They also need to give you a username and password. (⇑)
- Don't guess about this, because you need to make sure you get the data from the same database that the site uses. Otherwise, your backup will be worthless. (⇑)