Xtrabackup tool for MySQL backup and recovery (2)

Xtrabackup physical backup tool, backup MySQL5.6.36

(1) Introduction to Xtrabackup tool
Percona's backup tool has relatively high performance. Physical backup tool.
Features:
Physical backup tools have much better performance than logical backup based on the same level of data volume.
Especially when the amount of data is relatively large, it is more obvious.
Backup method:
1. Copy data files (Myisam storage engine can directly copy cp, but innodb cannot)
2. Copy data page
backup principle (innodb):
1. For innodb tables, hot backup can be realized
(1) in the data still At the time of modification operation, directly back up the data pages in the data file.
At this time, the data backed up is inconsistent with the current mysql.
(2) Keep redo and undo in the backup process together.
(3) When restoring, as long as the data page LSN that is backed up can match the redo LSN,
the data that will be restored in the future will be consistent. Redo application and undo application.
2. For myisam table, realize automatic lock table copy file.

(2) Xtrabackup software installation:
1. Installation
(1) Dependent package solution
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
yum- y install perl perl-devel libaio libaio-devel perl-Time-HiRes perl-DBD-MySQL

wget http://rpmfind.net/linux/dag/redhat/el6/en/x86_64/dag/RPMS/libev-4.15-1.el6.rf.x86_64.rpm
rpm -ivh libev-4.15-1.el6.rf.x86_64.rpm

(2)Download Xtrabackup software and install
wget https://www.percona.com/downloads/XtraBackup/Percona-XtraBackup-2.4.4/binary/redhat/6/x86_64/percona-xtrabackup-24-2.4.4-1 .el6.x86_64.rpm
yum -y install percona-xtrabackup-24-2.4.4-1.el6.x86_64.rpm

2. Detailed explanation of command parameters and backup examples
Create a backup path
mkdir /server/backup -p

Explanation of innobackupex parameters:
--user=
--password=
--socket= //If specified in the configuration file /etc/my.cnf, there is no need to specify
--no-timestamp

Full backup example:
innobackupex --user=root --password=oldboy123 /server/backup/ //When backing up, you need to specify the backup path. This will use the timestamp as the directory and back up
innobackupex --no-timestamp /server/ backup/full //Store data in the full directory

(3) Full backup and recovery combat:
1. Data backup
innobackupex --user=root --password=oldboy123 --no-timestamp /server/backup/full

2. Preparation before restoring data (merge xtabackup_log_file and backed up physical file)
innobackupex --apply-log --use-memory=32M /server/backup/full/

3. Simulate fault
shutdown:
pkill mysqld
destroys data:
cd /application/mysql/data
\rm -rf *

4. Recovery
(cp -a /server/backup/full/* /application/mysql/data) //This method is generally not recommended, the second method is the best
or
innobackupex --copy-back /server/backup /full/ ###Note: When restoring, make sure that the data path is empty and the database is stopped

chown -R mysql.mysql /application/mysql/data //Recursive authorization is too important, otherwise the database cannot be started, remember! ! ! Have stepped on the pit

Start:
/etc/init.d/mysqld start

(4) xtrabackup realizes incremental backup and fault recovery:

Fully prepared on Sunday, incremental from Monday to Saturday

1. Full backup on Sunday:
mkdir /backup/full
innobackupex --user=root --password=123 --no-timestamp /backup/full/
2. Simulate data changes (data changes on Monday)
mysql> insert into t1 values(7, 'zhao3');
.
.
3. The first increment (increment on Monday night):
innobackupex --user=root --password=123 --incremental --no-timestamp --incremental-basedir=/backup/full/ /backup/inc1
4. Simulation data changes (data changes Tuesday)
INSERT INTO T1 values (. 8, 'tangyong');
.
.
5. The second increment (increment on Tuesday night):
innobackupex --user=root --password=123 --incremental --no-timestamp --incremental-basedir=/backup/inc1 /backup/inc2
6. Simulation Data corruption
n many operations. . . . .
At 10:00 on Wednesday morning, delete the t1 table
drop table t1;

7、数据恢复前准备:
innobackupex --apply-log --redo-only /backup/full
innobackupex --apply-log --redo-only --incremental-dir=/backup/inc1 /backup/full
innobackupex --apply-log --incremental-dir=/backup/inc2 /backup/full
innobackupex --apply-log /backup/full

8. Data recovery: (remember, you need to close the database before recovery)
Error: datadir must be specified. (In this case, you need to specify the data file directory in the /etc/my.cnf configuration file)
innobackupex --copy-back /backup /full/

Start the database:
chown -R mysql.mysql /applicaion/mysql/data/ Note: You must change the main and group you belong to, otherwise you cannot
start /etc/init.d/mysql start


Homework: Production Recovery Case:
Background:
1. xtrabackup backup strategy every Sunday, full backup
2. xtrabackup Monday to Saturday, inc1-inic6
3. Total data volume 200G
4. Wednesday at 10 am mistakenly deleted table t1, data volume Around 1G
5. After the inc2 backup was completed on Tuesday night, many operations were done until 10 AM on Wednesday.

How to restore the database to the state before the t1 table was accidentally deleted?

Ideas:
1. Stop the business and hang up the maintenance page.
2. Find the backup library.
3. Merge full+inc1+inc2.
4. After intercepting the backup of inc2 on Tuesday night to 10 am on Wednesday, delete the binlog log before the t1 table.
5. Combine the merged The binlog intercepted by full+ is restored to the standby database
6. Verify data availability and integrity
7. Use the standby database instead of the production database or export and import the t1 table back to the production database
8. Business recovery

Job Practice Verification-Method 1: (Commonly used method)
Innobackupex incremental backup practice
1. Full backup of data on weekends and evenings
innobackupex --user=root --password=oldboy123 --no-timestamp /backup/full
2. Data on Monday Perform input and other operations
insert into t1 values(9,'tang1');
insert into t1 values(10,'tang2');
3. Make incremental backups of data on Monday night
innobackupex --user=root --password=oldboy123- -incremental --no-timestamp --incremental-basedir=/backup/full/ /backup/inc1
4. Enter data on Tuesday
insert into t1 values(11,'tao1');
insert into t1 values(12,' TaO2 ');
. 5, Tuesday night incremental backup data
innobackupex --user --password = = the root-oldboy123 --incremental --no---incremental the basedir = timestamp / backup / INC1 / backup / INC2
. 6, Wednesday data input operations
insert into t1 values (13, ' shanchu1');
insert into t1 values(14,'shanchu2');
7. Delete data before backup on Wednesday
drop table t1;
8. Prepare
innobackupex before data recovery --apply-log --redo-only /backup/full
innobackupex - apply-log --redo-only --incremental-dir=/backup/inc1 /backup/full
innobackupex --apply-log --incremental-dir=/backup/inc2 /backup/full
innobackupex --apply-log /backup / Full
9, data recovery
innobackupex --copy-back / backup / full /
start the database:
chown -R mysql.mysql / applicaion / MySQL / the data / Note: change the owner and group-owned, must be done, otherwise it will not start
/ etc/init.d/mysql start
10. Binary log interception
[root@db02 full]# more xtrabackup_binlog_pos_innodb //Check the last backup flag
my-bin.000003 906

show binary logs and show binlog events in'my-bin.000003'; //View the flag before deleting the table
| my-bin.000003 | 1275 | Xid | 1 | 1306 | COMMIT / xid=68 /

mysqlbinlog --start-position=906 --stop-position=1306 /data/binlog/my-bin.000003 >/backup/binlog.sql

11. Restore the binlog log file.//At this point, the data backed up by xtrabackup and binlog binary backup have all been restored.
Temporarily close the binary log
set sql_log_bin=0;
restore operation
source /backup/binlog.sql;

Work Practice Verification-Method 2:
//Note: This kind of table structure can only be developed, and the table structure must be known. This method does not need to restore all the data, just recover the lost 1G data.
drop table t1; //Simulate t1 table being deleted by mistake
create table t1 (id int,name varchar(20)); //Note: This kind of table structure can only be developed, and the table structure must be known.
alter table t1 discard tablespace;

cd /application/mysql/data/oldboy
cp /backup/full/oldboy/t1.ibd ./ //Copy the t1.ibd file in the inc2 state

chown -R mysql.mysql * //Modify the file belonging to mysql
alter table t1 import tablespace;

Guess you like

Origin blog.51cto.com/tangyong/2547122