Backing up and restoring MySQL databases



In the past few days I’ve been working on an automated backup system for both my site databases and a few others. I thought I’d take a bit to go through what I’ve done. It’s really simple, but I suspect there are some that either don’t realise it’s this simple, or will think this is rocket science. If you are well versed in command-line MySQL interaction you probably already know this….


For starters, if you’re putting this in a script to run start off with:

#!/bin/bash

next we need to “dump” the database contents…

mysqldump –add-drop-table databasename | bzip2 -c > /home/yoursitedirectory/backup/databasename.bz2;

All of the above should be on just one line in the script. Basically it calls mysqldump (which dumps data from a mysql database), the –add-drop-table adds the command to drop a table so that you’re data will be restored in a pristine form. (It drops the table, then adds it back in the restore process.). Next the databasename should be self explanatory. If you are unsure of the name of the database you could consult the MySQL command line interface, or phpMyAdmin, or you could check the configuration of your scripts as they need to know which database to connect to. Next, we pipe the mysqldump command to bzip2 which compressed the output. That output is saved to a filename at /home/yoursitedirectory/backup/databasename.bz2

All in all it’s fairly simple. I’ve added another few lines which backups up the web directory proper and emails me when the process is finished.

cd /home/yoursitedirectory/;

tar -cpf ./backup/webbackup.tar /var/www/html/ scripts;

gzip -f ./backup/webbackup.tar;

echo “Backup of databases and site done” | mail you@youraddress.com -s “yoursite website db backup”

I’m in a virtual private server environment, so the www directory is actually a symlink to /var/www/html (you’ll need to check the location of this on your own hosted account.), but I essentially change to the home directory for the account, then use tar -cpf to (c)reate the (f)ile webbackup.tar out of the contents of the /var/www/html directory and the scripts directory. These are the main files I would miss if the server melted down. Next the webbackup is compressed and the -f forces an overwrite of a file with the same name. The tar command has c, p and f as options, p should preserve permissions, f tells it to expect to tar to a file, c tells it to create (in other words this is a new tar).

The email command is straightforward.

Of course if you don’t have shell access the above is useless to you, keep using whatever control panel system you have to backup things. My hosting provider backs up everything daily, but it’s nice to have another safety net in case the sky fell and the site needed to move quickly.

Also, if you have shared hosting you may (will) need to adjust some paths. Additionaly, it’s not wise to save your backup files in a web accessible location unless they are to be immediately downloaded and deleted.

The entire backup script looks like this:

#!/bin/bash

mysqldump --add-drop-table databasename | bzip2 -c > /home/yoursitedirectory/backup/databasename.bz2;

cd /home/yoursitedirectory/;

tar -cpf ./backup/webbackup.tar /var/www/html/ scripts;

gzip -f ./backup/webbackup.tar;

echo "Backup of databases and site done" | mail you@youraddress.com -s "yoursite website db backup"

So, can we actually rebuild from all this? Yes. I’ve done a “fire drill” of my own with the database and files, moved to another machine and with a few adjustments (database password and username different on the new server, so config files for the php needs to be modified.) things do work though. Here’s how….

On the new machine you can simply do this…

tar -xzvf webbackup.tar.gz

to expand the contents of the web files. You will likely need to adjust paths (in my case the var/www/html structure was tarred so I had to cd var/www/html and then type mv * ../../../ (or if you like specify an absolute path with mv * /var/www/html/mynewdirectory)). The database is the part that scares everyone though and it’s really fairly simple.

From the command prompt login to the MySQL console:

mysql -u yourusername -p

You will be prompted for a password, then you’re into the MySQL console.
Next type the following

CREATE DATABASE restorefrombackup;

(you can name it the same as it was previously) Make sure to put the ; at the end of the MySQL statement.

Now you can log out of the MySQL console.

unzip your backup

bunzip2 databasename.bz2

This gives you the file databasename (which holds your database.)

Now we put it into MySQL.

mysql -u yourusername -p restorefrombackup < databasename

You will be prompted for the password and then little feedback.

you can connect to Mysql again to check that all is there if you like….

mysql -u yourusername -p

After the password prompt, try typing this….

SHOW TABLES FROM restorefrombackup;

This should show you all the database tables you’ve imported and if you like you can do…

SELECT * FROM tablename;

As I said, config files for your php scripts may need to be modified. (databasename, username, password usually.) Outside of that everything should be in good shape assuming that apache and MySQL are both setup as they should. Hope this is useful, I’ve done this all the way through and it worked for me with no hitches outside of those comments that I’ve already mentioned.

Related Posts

Blog Traffic Exchange Related Posts
  • Busy Evening I've got all traces of the old cms system off of the server now. (Well there might be a README here or there, but all php scripts from the old CMS are now gone.) At this point I can disclose a bit about the sudden move. Well, for starters, any......
  • Having multiple domains direct to one web site Recently I've consolidated a few domain names that I've acquired to direct to this site, and a few others to direct to another site that I maintain. One of the things that I wondered about after I set things up in the sites control panel to receive requests as the......
  • Global White Space Reset (CSS/html) This may not be useful to many people, but I thought it was interesting. If you do web design and use css you'll probably like this... I found this post at leftjustified.net about a neat way to "reset" the padding and margin css information which can help for designing sites......
Blog Traffic Exchange Related Websites
  • Using MySQL In A Hosting Environment MySQL is a heavy-duty database server. Used with various software languages, most notably PHP, and part of the LAMP family (which is comprised of Linux, Apache, MySQL, and PHP), Whether it's a customer relations management list or medical records, any data you have that needs storing, is stored on a......
  • 10 Steps to Start a Blog Is this your first time starting a blog? Here is a ten step checklist for starting your own blog. 1 - Choose a subject - Determine what it is that you want to blog about. You need to have a concept before you can start writing about, so that you......
  • Library - New Web Site Coming Later in Summer As a result of viruses/site hackers, Running Tide (our web host and the Municipality) have decided to close down our website until we go live with the new one sometime at the end of June.  I’ve put together some important links that are not accessible from our site right now......
en.pdf24.org    Send article as PDF   

Similar Posts


See what happened this day in history from either BBC Wikipedia
Search:
Keywords:
Amazon Logo

Comments are closed.


Switch to our mobile site