Backup and Restore Databases

On May 4, 2005, in MySQL, by Brad

The following steps can be executed as a non privileged user on the server, however, the username and password you use with the commands needs to be a database user that has r/w privileges within MySQL (think “sa” account for Microsoft SQL Server).

1. Export the database to a SQL file.

[user@host]$ mysqldump --opt --user=dbuser \
--password database > dumpfile.sql

2. Edit the dump file and put these lines at the beginning:

SET AUTOCOMMIT = 0;
SET FOREIGN_KEY_CHECKS=0;

3. Put these lines at the end:

SET FOREIGN_KEY_CHECKS = 1;
COMMIT;
SET AUTOCOMMIT = 1;

4. Import the database SQL file into MySQL. This will overwrite the contents of the current database.

[user@host]$ mysql --user=dbuser \
--password database < dumpfile.sql

Click here for a good shell script that automates the backup of MySQL databases. Version 2.5 is available here for download.

 

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>