MySQL backup and recovery

Source: Internet
Author: User
Tags mysql backup

Some netizens asked me how to automatically back up the MySQL database under win2003. Since it is an automatic backup, I must write a script. I thought, this is not very difficult, it is very easy to implement, backup can be implemented using scripts, Then how should we implement it automatically? It is also very simple. simply use the "Task Plan" function provided by windows to set a time so that the system can run scripts on a regular basis. Isn't automatic Database Backup enabled? First put the scriptCodePost it:

 
 
  1. @ Echo on
  2.  
  3. Rem ------------------------ backup bugdb whichIsInnoDB -----------------------------
  4. Del F: \ backup \ website \ bugdb _ *. SQL
  5. Cd f: \ USR \ Wamp \ mysql \ bin
  6. SetYear = % Date :~ 0, 4%
  7. SetMonth = % Date :~ 5, 2%
  8. SetDay = % Date :~ 8, 2%
  9. SetFilename = bugdb _ % year % month % day %. SQL
  10. Mysqldump.exe bugdb-uroot-p123456> F: \ backup \ website \ % filename %
  11.  
  12. @ Echo off

Explain the meaning of each sentence in sequence:

    • Row 4: Delete the SQL file with the name "bugdb _" in the specified directory. Because this code was previously written by me and runs once a day on the company's server. Therefore, before each backup, delete the files that have been successfully backed up on the previous day.
    • Row 5: Enter the MySQL bin directory, because there isMysqldump.exe file. This file is used by the MySQL database to back up and restore the MySQL database.
    • Row 6: The Year of the current system date, represented by four digits, for example, 2010.
    • Row 7: takes the month of the current system date, expressed in two digits, for example, 03.
    • Row 8: returns the date of the current system date, expressed in two digits, for example, 12.
    • Row 9: defines the backup file name. The final file name exists in the form of bugdb_20100312. SQL, that is, the file name plus date.
    • Row 10: perform a backup.

Let's explain it again.Syntax format of mysqldump. Format:

 
  
  
  1. Mysqldump.exe"Name of the database to be backed up"-U (connected to the user name)-P (connected to the password)>"Path and file name of the backup file" 

">" Is used to redirect the output.The data backed up by mysqldump.exe is output to a file and saved.

Copy the preceding script to a text file and save it *. bat, such as backup. bat's batch file will be used later. I will store it in D: \ scripts \ backup_bugdb.bat.

Open "Task Scheduler" in "Control Panel" and create a scheduled task:

In "run", click the Browse button to find the saved backup. for bat batch files, set them on the "Plan" tab and "Settings" tab based on your actual needs. After the settings are complete, click "OK" to save the task. Next, the system periodically runs the script at the specified time to automatically back up the database.

In addition, the command for restoring the database is attached:

 
  
  
  1. D: \ HTML \ Wamp \ mysql \ bin \ mysql.exe-uroot-p123456
  2. --Default-Character-Set= Utf8 bugdb <F: \ bugdb_20100312. SQL

Command for backing up MySQL database

Mysqldump-hhostname-uusername-ppassword databasename> backupfile. SQL

Back up the MySQL database in the format of a table with deletion
Backing up a MySQL database is in the format of a table with deletion, so that the backup can overwrite existing databases without the need to manually delete the original database.

Mysqldump -- add-drop-table-uusername-ppassword databasename> backupfile. SQL

Directly compress and back up the MySQL database

Mysqldump-hhostname-uusername-ppassword databasename | gzip> backupfile. SQL .gz

Back up a MySQL database table

Mysqldump-hhostname-uusername-ppassword databasename specific_table1 specific_table2> backupfile. SQL

Back up multiple MySQL databases at the same time

Mysqldump-hhostname-uusername-ppassword-databases databasename1 databasename2 databasename3> multibackupfile. SQL

Back up database structures only

Mysqldump-no-data-databases databasename1 databasename2 databasename3> structurebackupfile. SQL

Back up all databases on the server

Mysqldump-all-databases> allbackupfile. SQL

Command for restoring MySQL database

Mysql-hhostname-uusername-ppassword databasename <backupfile. SQL

Restore a compressed MySQL database

Gunzip <backupfile. SQL .gz | mysql-uusername-ppassword databasename

Transfer database to new server

Mysqldump-uusername-ppassword databasename | mysql-host = *. *-C databasename

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.