Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 26

DATABASE

BACKUP
OVERVIEW
 Backing up is one of the most important tasks for your database and one of
the most neglected. Unpredictable events can be disastrous simply because of
their unpredictability.
 Many have learned that the hard way by putting off a backup for some future
date, which never seemed to come around, and then paying the price with a
complete loss of data.
 The more important the data and the more frequent the changes, the more
frequent the backup needs to be.
 For a news database, where changes are happening continually, a daily
backup is prudent, with logging enabled to allow recovery of the day's work.
 For a small website where the data is changed weekly, a weekly backup
makes more sense. However large or small your database, there's no avoiding
it. A backup is a critical component of any data storage system
BACKUP COMMAND
The BACKUP statement creates a copy of the definition
and data files of a MyISAM table
Syntax:
BACKUP TABLE <tablename> TO '<directory>';
Example:
RESTORE COMMAND
The RESTORE statement restores the data.
Syntax:
RESTORE TABLE <tablename> FROM ‘<directory>’;
Example:
BACKING UP BY DIRECTLY COPYING FILES
MyISAM tables are stored as files:
.FRM for the definition
.MYD for the data
.MYI for the indexes
inside a directory named after the database, so an easy way to
back up the data is to copy the files. Unlike BACKUP, directly
copying the files does not automatically lock the tables, so you
need to lock the tables yourself to get a consistent picture.
Alternatively, you can do a direct copy while the server is down.
Once the tables have been locked, you should flush the tables to
make sure any unwritten indexes are written to disk.
DIRECT COPYING CONT..
For Window1:
LOCKING TABLES
Syntax:
LOCK TABLES <tablename> READ;
Example:
DIRECT COPYING CONT..
For Window1:
FLUSHING TABLES
Syntax:
FLUSH TABLES <tablename>;
Example:
DIRECT COPYING CONT..
For Window2:
Syntax:
COPY <tablename>.* <directories>
Example:
UNLOCK THE TABLES
Windows1:
Syntax:
UNLOCK TABLES;
Example:
BACKING UP WITH MYSQLDUMP
 Which creates a text file containing the SQL statements needed
to regenerate the tables.
 The previous two methods directly copy the files—and only work
with MyISAM tables. (InnoDB tables are not stored as files, so
they cannot make use of the earlier methods.)
 Another method, mysqldump, makes a dump of the SQL
statements needed to create the tables being backed up.
MYSQLDUMP FOR TABLE
Syntax:
mysqldump –u root -p <databasename><tablename> >
<directory>\<filename>.sql
Example:
RESTORING A DATABASE BACKED UP WITH
MYSQLDUMP
Syntax:
mysql –u root -p <dbname> < <directory>\<filename>.sql
Example:
BACKING UP WITH SELECT INTO
 Another way to perform a backup is to use SELECT
INTO. This is similar to mysqldump in that it creates a
file that is used to re-create the backed-up table. It is
also the opposite of the LOAD DATA INTO statement.
The resulting file can only be created on the MySQL
server, not any other host.
BACKING UP WITH SELECT INTO
 To create a backup of the customer table
Syntax:
SELECT * FROM <tablename> INTO OUTFILE
‘<directory>\\<filename>.dat';
Example:
SELECT INTO CONT..
Syntax:
SELECT * FROM <tablename> INTO OUTFILE
‘<directory>\\<nameoffile>.dat' FIELDS TERMINATED BY
‘<any characters>';
Example:
SELECT INTO CONT..
Syntax:
SELECT * FROM <tablename> INTO OUTFILE
‘<directory>\\<nameoffile>.txt’;
Example:
SELECT INTO CONT..
Syntax:
SELECT * FROM <tablename> INTO OUTFILE
‘<directory>\\<nameoffile>.csv’;
Example:
SELECT INTO CONT..
Syntax:
SELECT * FROM <tablename> INTO OUTFILE
‘<directory>\\<nameoffile>.dat' FIELDS TERMINATED BY ‘|’
LINES TERMINATED BY ‘\n‘;
Example:
SELECT INTO CONT..
Syntax:
SELECT * FROM <tablename> INTO OUTFILE
‘<directory>\\<nameoffile >.dat' FIELDS TERMINATED BY
‘|’ ENCLOSED BY ‘’’’ LINES TERMINATED BY ‘\n‘;
Example:
OUTPUT FILE
RESTORING A TABLE WITH LOAD DATA
 To restore a table created with SELECT INTO, use the LOAD DATA
statement. You can also use LOAD DATA to add data that has been
created in some other way, perhaps an application or a spreadsheet. It is
the fastest way to add data, especially large quantities of data

 Let's remove the data from the customer table and restore it using LOAD
DATA:

TRUNCATE – is to remove the content inside the table.

Syntax:
TRUNCATE <tablename>;
EXAMPLE:

• Using LOAD DATA


Syntax:
LOAD DATA INFILE ‘<directory>\\<filename >.dat'
INTO TABLE <tablename>;
EXAMPLE:
LOAD DATA INFILE FOR .CSV
Syntax:
LOAD DATA INFILE ‘<directory\\filename>.csv‘ INTO
TABLE <tablename>;
LOAD DATA INFILE FOR .TXT
Syntax:
LOAD DATA INFILE ‘<directory>\\<filename>.txt’ INTO TABLE
<tablename>;
END

You might also like