Restore SQL backup

From DreamHost
Revision as of 19:41, 7 November 2011 by Myqlarson (Talk | contribs)

Jump to: navigation, search

Howto: Restore a mysql backup

If you are moving to a new host, or simply want to restore a backup of your MySQL database. These are the steps you should follow. We're assuming you already have a backup.sql file.

  • Upload backup.sql to your server via FTP. This doesn't have to be in a web accesible place. Do remember where you put it.
  • If you haven't created a database yet, do that first in your DreamHost panel. If you're restoring your backup to an existing database, it might be recommended to empty it first.
  • Connect to your server using SSH.
  • Now, go to the Panel, click Goodies, then MySQL. You will see a mysql command line for your database. Run that command with < /path/to/backup.sql appended, for instance:
mysql -u dbuser -p -h dbhost.yoursite.com dbname < /path/to/backup.sql


Alternate Instructions

(If you find the above a little hard to follow..try these instructions instead)

If you have just moved to DreamHost and wish to restore a database from your previous hosting arrangement, then the following instructions will allow you to do this with databases of any size. If your database is smaller than 10mb, then you can use the phpMyAdmin Import function to complete this task. Its a lot easier!

Preparation:

  • Make sure you have a Dreamhost account! ;) (In this example, oursite.com)
  • Create the destination database(Where we're going to put the data) by going to the DreamHost panel and going to 'Goodies' and 'manage MySQL'.(In this example, database1)
  • Make sure you have a shell enabled user(In this case, user1). If you havn't done so already, you can give any of your users shell access by loging into your DreamHost panel and going to 'Users' and then 'Manage Users'

(In this example, i'm assuming our shell user, user1, also has full access to the database we want to edit)

  • Make sure you have an SSH client. MacOS and Linux users probably already have one in the terminal. Windows users can use a program like Putty.

Steps:

  • 1:Upload the database your wish to restore(In this example restore1.sql) to your DreamHost account(In this example, ftp://oursite.com/oursite.com) via FTP and make a note of the path(In this example /oursite.com/restore1.sql)
  • 2:Use your SSH client to connect to your site(In this example, oursite.com).(Use your shell enabled user, in this example user1, to log in)
  • 3: Now you can use the following command to restore your database:
mysql -u dbuser -p -h dbhost.yoursite.com dbname < /path/to/backup.sql

In our example, the command would be:

mysql -u user1 -p -h mysql.oursite.com database1 < oursite.com/restore1.sql

It's very good habit to restoring database in screen session. To create new screen session type

screen

then enter command to run the whole process. After that (while command is already running) press Crtl+A and D after that (first: Ctrl+A , second: only D). You'll get

[detached]

message. Now you can logout completely and script will be still running.

Gotchas

Note that if you restore an SQL backup to a database under another user and have any views in your database, you'll get an error similar to this:

ERROR 1227 (42000) at line 694: Access denied; you need the SUPER privilege for this operation

You'll need to change the DEFINER mentioned in the .sql file at the line specified in the error. Details of how to do this can be found here.

See also

  • Backup MySQL tells how to create a backup of your MySQL database, which you can restore using these instructions.
  • Migrate MySQL gives alternate instructions for the restore step