Alibaba Cloud mysql data backup and restore data to local mysql (backup + binlog)

Reference: https://help.aliyun.com/knowledge_detail/41817.html?spm=a2c4g.11186623.4.3.49bf4292RNNrSC

Cause: The production environment is not closed due to timing tasks. Timed tasks that should not have been executed are executed. Lead to the loss of important business data!

1. Check the backup situation

Because the incident time was 1:30 on January 5th (timed task execution time).

Check Alibaba Cloud backup, the most recent backup was at 7:43 on January 2 (the backup on January 5 is not available):

So look for the binlog log from 7:43 on January 2 to 1:30 on January 5

Now lock the binlog that needs to be downloaded: mysql-bin.000667-000678, and you also need to download the full backup above, as shown in the figure

2. Start to restore data

Preparation: need to find a linux environment with mysql service and Percona XtraBackup software installed

Steps to install mysql: https://blog.csdn.net/sumengnan/article/details/108736935

Steps to install Percona XtraBackup:

1、yum install https://repo.percona.com/yum/percona-release-latest.noarch.rpm

  • MySQL 5.6 and earlier versions require Percona XtraBackup 2.3 to be installed.
  • MySQL 5.7 version requires Percona XtraBackup 2.4 to be installed

2、yum install percona-xtrabackup-24

If rpm is installed, the dependent package is: yum install cmake gcc gcc-c++ libaio libaio-devel automake autoconf \ bison libtool ncurses-devel libgcrypt-devel libev-devel libcurl-devel \ vim-common

(1) Restore the full backup first

 1. Upload the complete backup to the linux environment

2、解包:cat hins14138181_data_20210102074244_qp.xb | xbstream -x -v -C /home/sumengnan/data2

3、解压:innobackupex --decompress --remove-original /home/sumengnan/data2

4、恢复:innobackupex --defaults-file=/home/sumengnan/data2/backup-my.cnf --apply-log /home/sumengnan/data2

5. Modify the /home/sumengnan/data2/backup-my.cnf file

Add the following parameters:

lower_case_table_names=1
skip-grant-tables

Annotate the following parameters:

 
#innodb_log_checksum_algorithm
#innodb_fast_checksum
#innodb_log_block_size
#innodb_doublewrite_file
#innodb_encrypt_algorithm
#rds_encrypt_data
#redo_log_version
#master_key_id
#server_uuid

6、启动mysql进程:mysqld --defaults-file=/home/sumengnan/data2/backup-my.cnf --user=mysql --datadir=/home/sumengnan/data2

7. Use mysql -uroot (password-free) to connect to the database and check the results, or connect with Navicat:

Question: If Navicat reports an error when connecting to the mysql service : Table'performance_schema.session_variables' doesn't exist

Need to add configuration in my.cnf and restart mysql:

[mysqld]
show_compatibility_56 = ON
performance_schema
 

(2) Restore the binlog backup again

1. First convert binlog into sql file locally (you need to have mysql command locally, or put it in linux environment to execute the same)

Since I use mac, I wrote a script recover_data.sh directly.

Select the binlog of 000667 to set the start time and create recover_data.sql. Other binlogs are additionally written to recover_data.sql. The last binlog of 000678 selects the end time

mysqlbinlog mysql-bin.000667 --start-datetime='2021-01-02 07:43:36' > recover_data.sql
mysqlbinlog mysql-bin.000668 >> recover_data.sql
mysqlbinlog mysql-bin.000669 >> recover_data.sql
mysqlbinlog mysql-bin.000670 >> recover_data.sql
mysqlbinlog mysql-bin.000671 >> recover_data.sql
mysqlbinlog mysql-bin.000672 >> recover_data.sql
mysqlbinlog mysql-bin.000673 >> recover_data.sql
mysqlbinlog mysql-bin.000674 >> recover_data.sql
mysqlbinlog mysql-bin.000675 >> recover_data.sql
mysqlbinlog mysql-bin.000676 >> recover_data.sql
mysqlbinlog mysql-bin.000677 >> recover_data.sql
mysqlbinlog mysql-bin.000678 --stop-datetime='2021-01-05 01:30:00' >> recover_data.sql
gzip recover_data.sql

The result after executing the script

2. Unzip after uploading to the linux environment

  • Execute mysql -uroot to log in to mysql
  • Execute set @@GLOBAL.GTID_MODE = OFF_PERMISSIVE
  • Execute source recover_data.sql to import sql file

3. Verification result

 

complete

 

 

Guess you like

Origin blog.csdn.net/sumengnan/article/details/112241306