Monday, September 24, 2012

DATABASE BACK-UPS


Backing up is having a copy of your database or file in a database.

Backing up by directly copying files
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.


Using mysqldump and mysqlhotcopy
            mysqldump and mysqlhotcopy are two ways to create back-up in Mysql.

Use the mysqldump script to create a dump file, that is, a file containing the SQL statements necessary to re-create the database.

Command:
Mysqldump –u username –p password database_name > directory:\backup_name.sql

Example:
Mysqldump –u root companydb > d:\backup.sql


Use the mysqlhotcopy script to create a data file. These copies the data files for a particular database directly. Mysqlhotcopy is run only in Unix and NetWare

Command:
shell> mysqlhotcopy db_name [/path/to/new_directory]

The BACKUP and RESTORE commands
The BACKUP statement creates a copy of the definition and data files of a MyISAM table.

Command:
BACKUP TABLE table_name TO 'directory:\\';

Example:
BACKUP TABLE department TO 'd:\\';




The RESTORE statement restores the data.

Command:
RESTORE TABLE tablename FROM '/db_backup_path'

Example:
mysql> RESTORE TABLE sales FROM 'c:\\db_backups';


Using SELECT INTO to back up and LOAD DATA INFILE to restore

Using SELECT INTO statements create a text file that can be used to restore the data with the LOAD DATA command or the mysqlimport utility.

There are three types of SELECT INTO…
- select ….into OUTFILE
Writes a selected row to a file
Syntax:
SELECT … INTO OUTFILE ‘file_name’ from [table_name];
Example:

- select ….into DUMPFILE
writes a single row to a file
Syntax:
SELECT … INTO DUMPFILE ‘file_name’ from [table_name] limit 1;
Example:

- select ….into VAR_LIST
selects column values and into variables
Syntax:
SELECT [column_name] INTO [variable_name] from [table_name];
Example:



Backing up Innodb tables

Two way using mysqldump and copy the file
- To copy file shutdown MySQL server make sure no errors
- Copy all InnoDB data file (ibdata file and .ibd file) to safe place
- Copy all .frm file
- Copy all InnoDB log file
- Copy you’re my.cnf configuration


Replication as a means of backup
Replication, which backs up onto another machine, but also replicates data loss across machines if it's caused by SQL statements.




No comments:

Post a Comment