MySQL PITR
MySQL PITR
The point-in-time recovery allows you to restore a MySQL database to a specific time in the
past. The point-in-time recovery relies on two key components:
Full back up: This serves as the foundation for recovery, providing the starting state of the
database.
Binary logs: These binary log files record all changes made to the database, allowing you to
replay those changes to a desired point.
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-bin option to place the files in a different location. To see a listing of
all binary log files, use this statement:
To determine the name of the current binary log file, issue the following statement:
The mysqlbinlog utility converts the events in the binary log files from binary format to text so
that they can be viewed or applied. mysqlbinlog has options for selecting sections of the binary
log based on event times or position of events within the log.
mysqlbinlog binlog_files
USE mydb;
CREATE TABLE IF NOT EXISTS contacts (id INT AUTO_INCREMENT PRIMARY KEY,first_name
VARCHAR(255) NOT NULL,last_name VARCHAR(255) NOT NULL,email VARCHAR(255) UNIQUE
NOT NULL);
Take a full backup of the mydb database and store the dump file in the backup directory:
Since we delete all rows from the contacts table unintentionally, we want to recover the
contacts’ table.
Step1) Restore the last full backup created before the point-in-time recovery:
From the output of mysqlbinlog, the data is available till “2024-12-20 9:10:38”
Step1) Take a full backup of the mydb database and store the dump file in the backup
directory:
Note:
Only use the --start-datetime and --stop-datetime options to help you find the actual event
positions of interest. Using the two options to specify the range of binary log segment to apply is
not recommended: there is a higher risk of missing binary log events when using the options.
Use --start-position and --stop-position instead.