Call us Toll-Free:
1-800-218-1525
Email us

 Sponsors

How to Restore a MySQL Backup

Brett Batie, 07-23-2009
This tutorial goes over all the necessary steps to restore a MySQL backup via the shell or command line.

Determine The Basics About Your Database

We need some basic information about the MySQL installation before we get started restoring the backup.

First, lets determine the location of the mysql databases. It is generally located at /var/mysql/data but it is always good to double check. Issue the following command to see where it is:

cat /etc/my.cnf | grep 'innodb_data_home_dir'

This will output something like the following and tells us that the mysql databases are located at /usr/local/var. This location will be used for the remainder of the tutorial so please be sure to replace it with the location that you received from the above command.

innodb_data_home_dir = /usr/local/var/


Remove The Old Database(s)

If MySQL is not corrupted and your backup files are set to drop the table before inserting the new data. You can skip this section. If you are not sure it will not hurt to complete the following steps but we will need to take the database offline for a few minutes.

First we need to shutdown mysql with the following command:

mysqladmin -uroot -pYourPassword shutdown

Now we need to backup the current mysql data directory and create a new one. This can be done with the following 2 commands:

mv /usr/local/var /usr/local/var-old
mkdir /usr/local/var

Next, we will install the basic mysql table by running the mysql_install.sh script. Generally, it is located at /usr/local/mysql/bin/mysql_install_db. If it is not there you can use the following command to find it.

locate mysql_install_db

Note: you may need to update your locate database if the script is not found with the above command. To update your database issue one of the following commands and then repeat the above command.

FreeBSD and Mac OSX
/usr/libexec/./locate.updatedb

For CentOS or RH cores
updatedb

Now that we have found the script we can run it

/usr/local/bin/./mysql_install_db

Restore the Backup

The first step is to take your backup file and uncompress it.

tar -xvzf /usr/local/var/yourBackup.tgz

If mysql.sql was not listed in the backup you can either manually reset the permissions or copy it from the backup you just made.

cp /usr/local/var-old/mysql/user* /usr/local/var/mysql/

Now lets set the ownership of the directory and all files in the directory. Make sure to use the appropriate group and user with the below command.

chown -R mysql:mysql /usr/local/var/

Now create any databases that you are going to restore with a command like the following (repeat the command for every database that you will restore):

echo 'create database YourNewDatabase' | mysql -uroot -pYourPassword

Restore the backup with the below command and repeat the command for every sql file/database that you will restore.

mysql -uroot -pyourPassword YourDatabase < backup.sql

Startup MySQL

mysqld_safe &
Enjoyed this post?

Subscribe Now to receive new posts via Email as soon as they come out.

 Comments
Post your comments












Note: No link spamming! If your message contains link/s, it will NOT be published on the site before manually approved by one of our moderators.



About Us  |  Contact us  |  Privacy Policy  |  Terms & Conditions