Back Up A Large MySQL Database

Anyone that’s dealt with a Web application using a MySQL backend has probably dealt with incomplete backups. Those of you that use phpBB have almost certainly encountered this issue. Unfortunately, within phpBB and quite a few other PHP/MySQL-based applications, the backup scripts fail miserably with any reasonably large amount of data. Most of them fail because of either PHP time outs or file size limits. This article will show you two different ways to successfully back up that data. At least one of these two methods should be available in almost every remote hosting package that uses MySQL and PHP.

phpMyAdmin

phpMyAdmin Interface

Your first option is to use phpMyAdmin, which is usually the tool Web hosts provide to manage MySQL databases. This process can be extremely slow and tedious, but it is almost universally available.

Once you login to phpMyAdmin, make sure that the database you want to back up is identified in the left navigation menu (in the screen shot, the database is called “blog_wp_090109”). If you see a list of your databases, instead, click on the name of the database you want to back up.

Next, click the “Export” tab. By default, phpMyAdmin generally has all of the tables in the database selected in the select list. In order to avoid a timeout, you want to click on a single table. If you know that you have a few smaller tables, you can select all of those at once. Then, click the “Go” button to export the SQL information.

If you come to a table that causes a timeout, click on the table name in the left navigation menu and then click the “Export” tab. There, you will have the option to dump a specific number of rows at a time. If you click the “Browse” button, you will see a yellow box near the top of the screen that tells you how many total rows are included in the table (it will say something like “Showing rows 0-29 (983 total, Query took 0.0452 seconds)”). Now, go back to the “Export” screen. In the area where it says “Dump _____ row(s) starting at record #___”, enter a round number in the first text box. For instance, if there are 1,000 rows in your table, start by trying to dump half of them at a time. In that case, you would enter “500” in the first text box and “0” in the second. If that works successfully, go back to that screen and enter “500” in the first text box again, then “500” in the second.

When you export using phpMyAdmin, it will default to simply printing the information in a textarea for you to copy and paste. That won’t do you much good if you are actually trying to create a back up. Instead, you want to tell phpMyAdmin to “Save as File.” That will prompt you to save an SQL file when it is done dumping the information. Save each of these files in a safe place, as you may need to use them to restore your database in the future. When you need to import that information back into a database, click the “Import” tab and follow the instructions there.

If you are just exporting the information so that you can import it into a new database, it’s generally acceptable just to have phpMyAdmin show the information in the textarea. You can then copy that information and paste it into the “SQL” tab of the phpMyAdmin interface for your new database.

MySQLDump

To use MySQLDump, you will need SSH access to your server. Quite a few hosts offer this nowadays. If you are on Windows, you can use PuTTY to SSH into your server. If you are using Linux (or Mac, I believe), you can simply open a terminal and type “ssh username@hostname” to SSH into your server. If you don’t know whether or not your host offers SSH, give it a try. Use the same login information you use to FTP into your server, but make sure that you are logging into port 22 instead of port 21. If it works, then you have SSH.

When you SSH into your server the first time, you will be asked to accept the security certificate. Go ahead and do so.

Next, you want to see if mysqldump is installed on your server. You can do so by simply typing “mysqldump” at the command line. If you then see information about using mysqldump, it is installed and usable.

The next step is to actually dump the information. To do so, type the following command:

mysqldump -usqluser -p --opt databasename >/home/username/databasename.backup.sql

Replace “sqluser” (making sure to leave the -u in front of it) with the username you use to access the database. Replace “databasename” with the actual name of your database. Finally, replace “/home/username/databasename.backup.sql” with the full path to the location on the server in which you want to create the new back up file.

When you hit enter, you will be prompted for your password. Type in MySQL password used to access the database and press Enter again. Then, depending on how large your database is, it can take anywhere from a few seconds to a few minutes (or even a few hours, if you have an extremely large database – one of my databases dumps to a file approximately one gigabyte, and it takes about fifteen minutes to complete) to finish. However, once it returns to the command line again, you will have a complete backup of your database stored in a plain text file on your server. To download it, simply login to your FTP account, locate the file and copy it to your computer.

If you need to restore your database, you can do so using the “Import” tab within phpMyAdmin or you can use the MySQL commandline. To use the MySQL commandline, simply SSH to your server again and type the following command:

mysql -usqluser -p databasename </home/username/databasename.backup.sql

Again, replace “sqluser” with the username you use to access your MySQL database, replace “databasename” with the name of your database and replace “/home/username/databasename.backup.sql” with the full path to the back up file on your server. That will then insert all of the information back into your database. It will completely overwrite the old database, so be aware of that when restoring. It will not attempt to merge the information.

Good luck. I hope that helps some people.

3 Responses

  • Allen

    i’ve been using navicat and will be posting a review soon – it’s just damn awesome for managing multiple databases and does backups so nicely – keeps a catalog of them and everything… while it’s not cheap (i think it’s $150) it’s so much easier to use than phpMyAdmin.

  • till

    If you can use Navicat, you can also use mysqldump or MySQL Administrator.

    It’s simple — get the community edition of MySQL server which includes mysqldump. And then, all you need to is start ‘cmd.exe’ (on Windows ;-)) and add the “-h” switch to the mysqldump command. “-h” allows you to supply the hostname/IP of the database server.

    E.g.
    mysqldump -u curtis -pSUPERSECRET -h allens.server.com –all-databases > dump.sql

    MySQL Administrator is also an alright tool. Especially because it has a backup and restore feature which includes a scheduler. And just like the MySQL server, it’s free.

  • Alejandro Arauz

    If you are looking for a tool to replace the phpMyAdmin interface you can also try MySQLBackupFTP(http://mysqlbackupftp.com). Though it isn’t free, it has a free version that allows you to schedule two databases. I use the free version at work and it is pretty useful, it has some good features like ftp the compressed backups to a remote server and mail notification.

Post Your Comment

Your email address will not be published.