Quantcast
Channel: SourceHints » mysql database
Viewing all articles
Browse latest Browse all 5

How to backup and Restore MySQL Data

$
0
0

Post to Twitter

Did you know how to backup or restore MySQL Data?

  • Requirements: MySQL Database Engine
  • Programming Level: Moderate
  • Language: MySQL

Scenario:

You want to backup your data at run-time without shutting down the mysql server (Binary Backup), in simply term you want to perform an online backup while other client is connected to server performing transaction. No matter how frequent you perform transactions still you will be able to backup your data.

Advantage:

The Advantage of this kind of backup is that, it can perform an archive of backup with specified date and time and other than that, you need not to shutdown the server that could disconnect all users connected to it, instead while they are performing transaction you can perform a backup. This kind of backup is also an advantage in such a way it only backup good data, meaning a corrupted data that has a discrepancy with the index will not be included on output result.

The SQL Command:

The Key on this type of backup is the “SELECT INTO OUTFILE”, is use to create a text file to backup data and uses “LOAD DATA INFILE” to restore data back to database. These two types of statement are related to each other in an opposite way. The Select Into to backup and Load Data is to restore data.

The Code:

‘Creates a file with Insert Statement into output file:

SELECT * FROM tblcargodescription INTO OUTFILE 'c:/Script/cargo.sql';
The above SQL command backup the tblcargodescription  table to an output file cargo.sql on C drive with in the folder Script. In this case it backup all the data, you can select some data only by including a where Statement on select statement. Please take note that use Slash (/) instead of Back Slash (\) as we normally use in assigning drive location.So if the that table contains one million records so all records will be inserted on to that file, It is recommended you use the ‘sql’ extension on this type of backup. Although you can use a txt file if you like or any other extension for that matter.
'Loads data from Output file into table and replace data that is duplicated:
LOAD DATA INFILE 'c:/Script/cargo.sql' REPLACE INTO TABLE tblcargodescription;
The above statement is the opposite way of backup,  or it is a restore statement performing a restoration of data, make sure that the table structure where this backup file has been generated resembles the structure of table that is about to restore data on it. This Process generates index upon insert of data and it is the faster way to restore data than any other type of restoration.

Output:

The above queries has the ouput upon executing it;

(953 row(s) affected)
Execution Time : 00:00:00:016
Transfer Time : 00:00:00:000
Total Time : 00:00:00:016

and with the Load Data Statement:

(953 row(s) affected)
Execution Time : 00:00:00:140
Transfer Time  : 00:00:00:000
Total Time : 00:00:00:140

Post to Twitter

Keywords: , , , , , , , , , , ,

Other reading this article are also reading these:


Viewing all articles
Browse latest Browse all 5

Trending Articles