This section summarizes some general methods for making backups.
The mysqldump program and the mysqlhotcopy script can make backups. mysqldump is more general because it can back up all kinds of tables. mysqlhotcopy works only with some storage engines. (See Section 7.4, “Using mysqldump for Backups”, and Section 4.6.9, “mysqlhotcopy — A Database Backup Program”.)
Making Backups by Copying Table Files
For storage engines that represent each table using its own files,
tables can be backed up by copying those files. For example,
MyISAM tables are stored as files, so it is
easy to do a backup by copying files (
To get a consistent backup, stop the server or lock and flush the
tbl_listREAD; FLUSH TABLES
You need only a read lock; this enables other clients to continue
to query the tables while you are making a copy of the files in
the database directory. The
statement is needed to ensure that the all active index pages are
written to disk before you start the backup. See
Section 13.3.5, “LOCK TABLES and UNLOCK TABLES Syntax”, and Section 18.104.22.168, “FLUSH Syntax”.
You can also create a binary backup simply by copying all table
files, as long as the server isn't updating anything. The
mysqlhotcopy script uses this method. (But note
that table file copying methods do not work if your database
mysqlhotcopy does not work for
InnoDB tables because
does not necessarily store table contents in database directories.
Also, even if the server is not actively updating data,
InnoDB may still have modified data cached in
memory and not flushed to disk.)
Making Delimited-Text File Backups
To create a text file containing a table's data, you can use
SELECT * INTO OUTFILE
'. The file is created
on the MySQL server host, not the client host. For this statement,
the output file cannot already exist because permitting files to
be overwritten constitutes a security risk. See
Section 13.2.8, “SELECT Syntax”. This method works for any kind of data
file, but saves only table data, not the table structure.
Another way to create text data files (along with files containing
CREATE TABLE statements for the
backed up tables) is to use mysqldump with the
--tab option. See
Section 7.4.3, “Dumping Data in Delimited-Text Format with mysqldump”.
Making Incremental Backups by Enabling the Binary Log
MySQL supports incremental backups: You must start the server with
--log-bin option to enable
binary logging; see Section 5.2.3, “The Binary Log”. The binary log
files provide you with the information you need to replicate
changes to the database that are made subsequent to the point at
which you performed a backup. At the moment you want to make an
incremental backup (containing all changes that happened since the
last full or incremental backup), you should rotate the binary log
LOGS. This done, you need to copy to the backup location
all binary logs which range from the one of the moment of the last
full or incremental backup to the last but one. These binary logs
are the incremental backup; at restore time, you apply them as
explained in Section 7.5, “Point-in-Time (Incremental) Recovery Using the Binary Log”. The next
time you do a full backup, you should also rotate the binary log
mysqldump --flush-logs, or
mysqlhotcopy --flushlog. See
Section 4.5.4, “mysqldump — A Database Backup Program”, and Section 4.6.9, “mysqlhotcopy — A Database Backup Program”.
Making Backups Using Replication Slaves
If you have performance problems with your master server while making backups, one strategy that can help is to set up replication and perform backups on the slave rather than on the master. See Section 16.3.1, “Using Replication for Backups”.
If you are backing up a slave replication server, you should back
relay-log.info files when you back up the
slave's databases, regardless of the backup method you choose.
These information files are always needed to resume replication
after you restore the slave's data. If your slave is replicating
INFILE statements, you should also back up any
SQL_LOAD-* files that exist in the directory
that the slave uses for this purpose. The slave needs these files
to resume replication of any interrupted
INFILE operations. The location of this directory is the
value of the
option. If the server was not started with that option, the
directory location is the value of the
tmpdir system variable.
Recovering Corrupt Tables
If you have to restore
MyISAM tables that have
become corrupt, try to recover them using
REPAIR TABLE or myisamchk
-r first. That should work in 99.9% of all cases. If
myisamchk fails, see
Section 7.6, “MyISAM Table Maintenance and Crash Recovery”.
Making Backups Using a File System Snapshot
If you are using a Veritas file system, you can make a backup like this:
Similar snapshot capabilities may be available in other file systems, such as LVM or ZFS.