In this blog, I’ll look at how to do MySQL point in time recovery (PITR) correctly.
Sometimes we need to restore from a backup, and then replay the transactions that happened after the backup was taken. This is a common procedure in most disaster recovery plans, when for example you accidentally drop a table/database or run an update/delete without the “where” clause and lose data.
The usual way is to get a copy of your binlogs and use mysqlbinlog to replay those transactions. But this approach has many pitfalls that can make the whole PITR process a nightmare. Some examples:
|
1 |
shell> mysqlbinlog binlog.000001 | mysql -u root -p # Creates tmp table X<br>shell> mysqlbinlog binlog.000002 | mysql -u root -p # Uses tmp table X |
Lost connection to MySQL server during query error, and so on.So how can we overcome those limitations and have a reliable way to do Point In Time Recovery?
We can restore the backup on the desired server, build a second server with just the minimal MySQL required data and move the all binary logs to this “fake” server datadir. Then we need to configure the server where we want the PITR to happen as a slave of the fake server. From this point forward, it’s going to be pure MySQL replication happening.
To illustrate it, I create a Docker container on the machine. I have Percona Server for MySQL running on the box listening on 3306, and have already restored the backup on it. There is a tarball there with all binlogs required. The saved positions for PITR are as follows:
|
1 |
[root@localhost ~]# cat /var/lib/mysql/xtrabackup_binlog_info<br>master-bin.000007 1518932 |
I create a folder to store the Docker MySQL datadir:
|
1 |
mkdir /tmp/pitr<br>chown -R 1001 /tmp/pitr<br> |
I start the Docker container. As we can see from xtrabackup_binlog_info, my binlogs are named master-bin and I’ll be setting the same server-id as original master:
|
1 |
docker run --name ps_pitr -v /tmp/pitr:/var/lib/mysql <br>-p 3307:3306 -e MYSQL_ROOT_PASSWORD=secret <br>-d percona/percona-server:5.7.18 <br>--log_bin=master-bin --server-id=10<br> |
In case you want to make usage of GTID, append --gtid-mode=ON --enforce_gtid_consistency=ON to the end of the Docker command.
The command above starts a MySQL instance, invokes mysqld –initialize, sets the root password to secret and it’s port 3306 is mapped back to my local 3307 port. Now I’ll stop it, remove the binlogs that it created, uncompress and move all required binlogs to its datadir mapped folder and start it again:
|
1 |
docker stop ps_pitr<br>rm /tmp/pitr/master-bin.*<br>tar -zxf binlogs.tgz -C /tmp/pitr<br>chown -R 1001 /tmp/pitr/master-bin.*<br>docker start ps_pitr<br> |
If it all worked correctly, at this point we can see the full list of binary logs on the Docker container by connecting on port 3307:
|
1 |
mysql -u root -psecret -P 3307 --protocol=TCP -e "SHOW BINARY LOGS"<br>mysql: [Warning] Using a password on the command line interface can be insecure.<br>+-------------------+-----------+<br>| Log_name | File_size |<br>+-------------------+-----------+<br>| master-bin.000005 | 26216208 |<br>| master-bin.000006 | 26214614 |<br>| master-bin.000007 | 26214902 |<br>. . .<br>| master-bin.000074 | 154 |<br>+-------------------+-----------+<br> |
Now, all we need to do is connect to our server, which has the backup restored, and configure it as a slave from 3307:
|
1 |
mysql -u root -p<br>Enter password: <br>Welcome to the MySQL monitor. Commands end with ; or g.<br>Your MySQL connection id is 6<br>Server version: 5.7.18-16 Percona Server (GPL), Release 16, Revision d7301f8<br><br>Copyright (c) 2009-2017 Percona LLC and/or its affiliates<br>Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.<br><br>Oracle is a registered trademark of Oracle Corporation and/or its<br>affiliates. Other names may be trademarks of their respective<br>owners.<br><br>Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.<br><br>mysql> CHANGE MASTER TO MASTER_HOST='127.0.0.1', MASTER_PORT=3307, MASTER_USER='root', MASTER_PASSWORD='secret', MASTER_LOG_FILE='master-bin.000007', MASTER_LOG_POS=1518932;<br>Query OK, 0 rows affected, 2 warnings (0.01 sec)<br><br>mysql> START SLAVE;<br>Query OK, 0 rows affected (0.01 sec)<br><br>mysql> SHOW SLAVE STATUSG<br>*************************** 1. row ***************************<br> Slave_IO_State: Waiting for master to send event<br> Master_Host: 127.0.0.1<br> Master_User: root<br> Master_Port: 3307<br> Connect_Retry: 60<br> Master_Log_File: master-bin.000008<br> Read_Master_Log_Pos: 449696<br> Relay_Log_File: localhost-relay-bin.000002<br> Relay_Log_Pos: 28957<br> Relay_Master_Log_File: master-bin.000007<br> Slave_IO_Running: Yes<br> Slave_SQL_Running: Yes<br> Replicate_Do_DB: <br> Replicate_Ignore_DB: <br> Replicate_Do_Table: <br> Replicate_Ignore_Table: <br> Replicate_Wild_Do_Table: <br> Replicate_Wild_Ignore_Table: <br> Last_Errno: 0<br> Last_Error: <br> Skip_Counter: 0<br> Exec_Master_Log_Pos: 15217950<br> Relay_Log_Space: 11476311<br> Until_Condition: None<br> Until_Log_File: <br> Until_Log_Pos: 0<br> Master_SSL_Allowed: No<br> Master_SSL_CA_File: <br> Master_SSL_CA_Path: <br> Master_SSL_Cert: <br> Master_SSL_Cipher: <br> Master_SSL_Key: <br> Seconds_Behind_Master: 4382<br>Master_SSL_Verify_Server_Cert: No<br> Last_IO_Errno: 0<br> Last_IO_Error: <br> Last_SQL_Errno: 0<br> Last_SQL_Error: <br> Replicate_Ignore_Server_Ids: <br> Master_Server_Id: 10<br> Master_UUID: 80b9fe26-a945-11e7-aa1d-0242ac110002<br> Master_Info_File: /var/lib/mysql/master.info<br> SQL_Delay: 0<br> SQL_Remaining_Delay: NULL<br> Slave_SQL_Running_State: Opening tables<br> Master_Retry_Count: 86400<br> Master_Bind: <br> Last_IO_Error_Timestamp: <br> Last_SQL_Error_Timestamp: <br> Master_SSL_Crl: <br> Master_SSL_Crlpath: <br> Retrieved_Gtid_Set: <br> Executed_Gtid_Set: <br> Auto_Position: 0<br> Replicate_Rewrite_DB: <br> Channel_Name: <br> Master_TLS_Version: <br>1 row in set (0.17 sec)<br><br>. . .<br><br>mysql> SHOW SLAVE STATUSG<br>*************************** 1. row ***************************<br> Slave_IO_State: Waiting for master to send event<br> Master_Host: 127.0.0.1<br> Master_User: root<br> Master_Port: 3307<br> Connect_Retry: 60<br> Master_Log_File: master-bin.000074<br> Read_Master_Log_Pos: 154<br> Relay_Log_File: localhost-relay-bin.000133<br> Relay_Log_Pos: 381<br> Relay_Master_Log_File: master-bin.000074<br> Slave_IO_Running: Yes<br> Slave_SQL_Running: Yes<br> Replicate_Do_DB: <br> Replicate_Ignore_DB: <br> Replicate_Do_Table: <br> Replicate_Ignore_Table: <br> Replicate_Wild_Do_Table: <br> Replicate_Wild_Ignore_Table: <br> Last_Errno: 0<br> Last_Error: <br> Skip_Counter: 0<br> Exec_Master_Log_Pos: 154<br> Relay_Log_Space: 819<br> Until_Condition: None<br> Until_Log_File: <br> Until_Log_Pos: 0<br> Master_SSL_Allowed: No<br> Master_SSL_CA_File: <br> Master_SSL_CA_Path: <br> Master_SSL_Cert: <br> Master_SSL_Cipher: <br> Master_SSL_Key: <br> Seconds_Behind_Master: 0<br>Master_SSL_Verify_Server_Cert: No<br> Last_IO_Errno: 0<br> Last_IO_Error: <br> Last_SQL_Errno: 0<br> Last_SQL_Error: <br> Replicate_Ignore_Server_Ids: <br> Master_Server_Id: 10<br> Master_UUID: 80b9fe26-a945-11e7-aa1d-0242ac110002<br> Master_Info_File: /var/lib/mysql/master.info<br> SQL_Delay: 0<br> SQL_Remaining_Delay: NULL<br> Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates<br> Master_Retry_Count: 86400<br> Master_Bind: <br> Last_IO_Error_Timestamp: <br> Last_SQL_Error_Timestamp: <br> Master_SSL_Crl: <br> Master_SSL_Crlpath: <br> Retrieved_Gtid_Set: <br> Executed_Gtid_Set: <br> Auto_Position: 0<br> Replicate_Rewrite_DB: <br> Channel_Name: <br> Master_TLS_Version: <br>1 row in set (0.01 sec)<br> |
If you want to apply logs up to a particular time you can make use of mysqlbinlog to verify what the last position / GTID it should apply, and use START SLAVE UNTIL MASTER_LOG_FILE = 'log_name', MASTER_LOG_POS = log_pos or START SLAVE SQL_THREAD UNTIL SQL_AFTER_GTIDS = 3E11FA47-71CA-11E1-9E33-C80AA9429562:11-56.
Special thanks to Marcos Albe, who originally showed me this MySQL point in time recovery approach.
Resources
RELATED POSTS