Point-in-time recovery refers to recovery of data changes made since a given point in time. Typically, this type of recovery is performed after restoring a full backup that brings the server to its state as of the time the backup was made. (The full backup can be made in several ways, such as those listed in Section 7.2, “Database Backup Methods”.) Point-in-time recovery then brings the server up to date incrementally from the time of the full backup to a more recent time.
Many of the examples here use the mysql
client to process binary log output produced by
mysqlbinlog. If your binary log contains
\0 (null) characters, that output cannot be
parsed by mysql unless you invoke it with the
Point-in-time recovery is based on these principles:
The source of information for point-in-time recovery is the set of incremental backups represented by the binary log files generated subsequent to the full backup operation. Therefore, the server must be started with the
--log-binoption to enable binary logging (see Section 5.4.4, “The Binary Log”).
To restore data from the binary log, you must know the name and location of the current binary log files. By default, the server creates binary log files in the data directory, but a path name can be specified with the
--log-binoption to place the files in a different location. Section 5.4.4, “The Binary Log”.
To see a listing of all binary log files, use this statement:
SHOW BINARY LOGS;
To determine the name of the current binary log file, issue the following statement:
SHOW MASTER STATUS;
The mysqlbinlog utility converts the events in the binary log files from binary format to text so that they can be executed or viewed. mysqlbinlog has options for selecting sections of the binary log based on event times or position of events within the log. See Section 4.6.7, “mysqlbinlog — Utility for Processing Binary Log Files”.
Executing events from the binary log causes the data modifications they represent to be redone. This enables recovery of data changes for a given span of time. To execute events from the binary log, process mysqlbinlog output using the mysql client:
binlog_files| mysql -u root -p
Viewing log contents can be useful when you need to determine event times or positions to select partial log contents prior to executing events. To view events from the log, send mysqlbinlog output into a paging program:
Alternatively, save the output in a file and view the file in a text editor:
Saving the output in a file is useful as a preliminary to executing the log contents with certain events removed, such as an accidental
DROP DATABASE. You can delete from the file any statements not to be executed before executing its contents. After editing the file, execute the contents as follows:
mysql -u root -p < tmpfile
If you have more than one binary log to execute on the MySQL server, the safe method is to process them all using a single connection to the server. Here is an example that demonstrates what may be unsafe:
mysqlbinlog binlog.000001 | mysql -u root -p # DANGER!!shell>
mysqlbinlog binlog.000002 | mysql -u root -p # DANGER!!
Processing binary logs this way using different connections to the
server causes problems if the first log file contains a
TABLE statement and the second log contains a statement
that uses the temporary table. When the first
mysql process terminates, the server drops the
temporary table. When the second mysql process
attempts to use the table, the server reports “unknown
To avoid problems like this, use a single connection to execute the contents of all binary logs that you want to process. Here is one way to do so:
mysqlbinlog binlog.000001 binlog.000002 | mysql -u root -p
Another approach is to write all the logs to a single file and then process the file:
mysqlbinlog binlog.000001 > /tmp/statements.sqlshell>
mysqlbinlog binlog.000002 >> /tmp/statements.sqlshell>
mysql -u root -p -e "source /tmp/statements.sql"