XtraBackup backup and restore instance

xtrabackup full backup and recovery

1. Full backup

Create a user xbK for backup and restore and give permissions

mysql> create user xbk@'localhost' identified by '123456';
Query OK, 0 rows affected (0.00 sec)

mysql> grant reload,process,lock tables,replication client on *.* to xbk@localhost;
Query OK, 0 rows affected (0.00 sec)

Create backup directory

[root@qht26 u01]# mkdir backup
[root@qht26 u01]# chown -R mysql.mysql /u01/backup

Perform a full database backup of the database

[root@qht26 u01]# innobackupex --defaults-file=/etc/my.cnf --user=xbk --password=123456 --stocket=/u01/mysql/mysql.sock /u01/backup

You can see the entire backup process: connect to the database, start copying the redo log, copy the innodb table file, lock the table, copy the non-innodb table file, stop copying the redo log, and unlock it .

Check out the generated file:

[root@qht26 backup]# cd 2018-04-24_11-54-46/
[root@qht26 2018-04-24_11-54-46]# ls -lth
total 13M
-rw-r----- 1 root root  424 Apr 24 11:55 backup-my.cnf
-rw-r----- 1 root root  350 Apr 24 11:55 ib_buffer_pool
drwxr-x--- 2 root root 4.0K Apr 24 11:55 mysql
-rw-r----- 1 root root  113 Apr 24 11:55 xtrabackup_checkpoints
-rw-r----- 1 root root  481 Apr 24 11:55 xtrabackup_info
-rw-r----- 1 root root 2.5K Apr 24 11:55 xtrabackup_logfile
drwxr-x--- 2 root root  12K Apr 24 11:54 sys
drwxr-x--- 2 root root 4.0K Apr 24 11:54 l5m
drwxr-x--- 2 root root 4.0K Apr 24 11:54 performance_schema
-rw-r----- 1 root root  12M Apr 24 11:54 ibdata1

Several of the directory files mysql, sys, l5m, and performance_schema are the files stored in their respective databases.

backup-my.cnf, the configuration option information used by the backup command;

[root@qht26 2018-04-24_11-54-46]# cat backup-my.cnf
# This MySQL options file was generated by innobackupex.

# The MySQL server
[mysqld]
innodb_checksum_algorithm=crc32
innodb_log_checksum_algorithm=strict_crc32
innodb_data_file_path=ibdata1:12M:autoextend
innodb_log_files_in_group=2
innodb_log_file_size=50331648
innodb_fast_checksum=false
innodb_page_size=16384
innodb_log_block_size=512
innodb_undo_directory=./
innodb_undo_tablespaces=0
server_id=0

redo_log_version=1

ib_buffer_pool, the hot data in the buffer pool, when innodb_buffer_pool_dump_at_shutdown=1 is set, when MySQL is closed, the hot data in the memory will be saved in the ib_buffer_pool file on the disk, located in the data directory.

bdata1, the backed up shared tablespace file;

xtrabackup_info, records the basic information of backup, uuid, backup command, backup time, binlog, LSN, and other encryption and compression information.

[root@qht26 2018-04-24_11-54-46]# cat xtrabackup_info
uuid = 442078de-4773-11e8-be9d-001a646dd15e
name =
tool_name = innobackupex
tool_command = --defaults-file=/etc/my.cnf --user=xbk --password=... --stocket=/u01/mysql/mysql.sock /u01/backup
tool_version = 2.4.10
ibbackup_version = 2.4.10
server_version = 5.7.21
start_time = 2018-04-24 11:54:46
end_time = 2018-04-24 11:55:02
lock_time = 0
binlog_pos =
innodb_from_lsn = 0
innodb_to_lsn = 2556615
partial = N
incremental = N
format = file
compact = N
compressed = N
encrypted = N

xtrabackup_checkpoints, backup type (such as full or incremental), backup status (such as whether it has been prepared) and LSN (log sequence number) range information;

[root@qht26 2018-04-24_11-54-46]# cat xtrabackup_checkpoints
backup_type = full-backuped
from_lsn = 0
to_lsn = 2556615
last_lsn = 2556624
compact = 0
recover_binlog_info = 0

trabackup_logfile, this file is the backup redo log file.

Since binlog is not enabled, xtrabackup_binlog_info cannot be seen.

2. Full recovery

Close the database and delete all data files

[root@qht26 ~]# service mysql stop
Shutting down MySQL..                                      [  OK  ]
[root@qht26 u01]# cd /u01
[root@qht26 u01]# mv mysql/ mysql_bak
[root@qht26 u01]# mkdir mysql
[root@qht26 u01]# chown mysql.mysql mysql

Prepare (prepare) a full backup: --apply-log /u01/backup/2018-04-24_11-54-46 is the backup directory, after execution, backup_type = full-prepared in the xtrabackup_checkpoints file)

--apply-log //Apply the xtrabackup_logfile transaction log file in BACKUP-DIR. Under normal circumstances, after the backup is completed, the data cannot be used for recovery operations, because the backed up data may contain uncommitted transactions or transactions that have been committed but have not yet been synchronized to the data file. Therefore, the data files are still in an inconsistent state at this point. The main function of "prepare" is to keep the data file in a consistent state by rolling back uncommitted transactions and synchronizing committed transactions to the data file.

[root@qht26 u01]# innobackupex --apply-log /u01/backup/2018-04-24_11-54-46/
。。。
InnoDB: Setting file './ibtmp1' size to 12 MB. Physically writing the file full; Please wait ...
InnoDB: File './ibtmp1' size is now 12 MB.
InnoDB: 96 redo rollback segment(s) found. 1 redo rollback segment(s) are active.
InnoDB: 32 non-redo rollback segment(s) are active.
InnoDB: Waiting for purge to start
InnoDB: 5.7.19 started; log sequence number 2556949
xtrabackup: starting shutdown with innodb_fast_shutdown = 1
InnoDB: FTS optimize thread exiting.
InnoDB: Starting shutdown...
InnoDB: Shutdown completed; log sequence number 2556968
180424 13:15:12 completed OK!
[root@qht26 2018-04-24_11-54-46]# cat xtrabackup_checkpoints
backup_type = full-prepared
from_lsn = 0
to_lsn = 2556615
last_lsn = 2556624
compact = 0
recover_binlog_info = 0

To perform a restore operation:

--copy-back //Copy all files previously backed up to their original paths. But there cannot be any files or directories under the original path unless the --force-non-empty-directories option is specified.

[root@qht26 ~]# innobackupex --defaults-file=/etc/my.cnf --copy-back --rsync /u01/backup/2018-04-24_11-54-46/
。。。
180424 13:20:37 [01] Copying ./mysql/tables_priv.MYI to /u01/mysql/mysql/tables_priv.MYI
180424 13:20:37 [01]        ...done
180424 13:20:37 [01] Copying ./mysql/ndb_binlog_index.MYD to /u01/mysql/mysql/ndb_binlog_index.MYD
180424 13:20:37 [01]        ...done
180424 13:20:37 [01] Copying ./mysql/user.frm to /u01/mysql/mysql/user.frm
180424 13:20:37 [01]        ...done
180424 13:20:37 [01] Copying ./mysql/event.MYI to /u01/mysql/mysql/event.MYI
180424 13:20:37 [01]        ...done
180424 13:20:37 [01] Copying ./mysql/time_zone_transition_type.ibd to /u01/mysql/mysql/time_zone_transition_type.ibd
180424 13:20:37 [01]        ...done
180424 13:20:37 [01] Copying ./mysql/gtid_executed.frm to /u01/mysql/mysql/gtid_executed.frm
180424 13:20:37 [01]        ...done
180424 13:20:37 completed OK!

The data files are all restored:

 
 
[root@qht26 mysql]# ls -lth
total 121M
drwxr-x--- 2 root root 4.0K Apr 24 13:20 mysql
drwxr-x--- 2 root root  12K Apr 24 13:20 sys
drwxr-x--- 2 root root 4.0K Apr 24 13:20 l5m
-rw-r----- 1 root root  481 Apr 24 13:20 xtrabackup_info
-rw-r----- 1 root root  350 Apr 24 13:20 ib_buffer_pool
-rw-r----- 1 root root  12M Apr 24 13:20 ibtmp1
drwxr-x--- 2 root root 4.0K Apr 24 13:20 performance_schema
-rw-r----- 1 root root  12M Apr 24 13:20 ibdata1
-rw-r----- 1 root root  48M Apr 24 13:20 ib_logfile0
-rw-r----- 1 root root  48M Apr 24 13:20 ib_logfile1

Start the database after modifying the permissions:

[root@qht26 mysql]# chown -R mysql.mysql /u01/mysql
[root@qht26 mysql]# service mysql start
Starting MySQL..                                           [  OK  ]

Data has been fully recovered

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| l5m |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql> use l5m
Database changed
mysql> show tables;
+---------------+
| Tables_in_l5m |
+---------------+
| tab1          |
+---------------+
1 row in set (0.00 sec)

xtrabackup incremental backup recovery

We use the previous full backup as the benchmark and make incremental backups based on it:

Make some changes to the database first:

mysql> use l5m
Database changed

mysql> create table tab2 (id int);
Query OK, 0 rows affected (0.25 sec)

mysql> insert into tab2 values(100);
Query OK, 1 row affected (0.06 sec)

mysql> insert into tab1 values(2);
Query OK, 1 row affected (0.05 sec)

mysql> commit;
Query OK, 0 rows affected (0.00 sec)
 
 
mysql> select * from tab1;
+------+
| id   |
+------+
|    1 |
|    2 |
+------+
2 rows in set (0.03 sec)

mysql> select * from tab2;
+------+
| id   |
+------+
|  100 |
+------+
1 row in set (0.02 sec)

Incremental backup 1, based on the full backup above

[root@qht26 mysql]# innobackupex --defaults-file=/etc/my.cnf --user=xbk --password=123456 --socket=/u01/mysql/mysql.sock --incremental /u01/backup/inc --incremental-basedir=/u01/backup/2018-04-24_11-54-46/ --parallel=2

The backed up files are as follows:

[root@qht26 2018-04-24_14-36-17]# pwd
/u01/backup/inc/2018-04-24_14-36-17
[root@qht26 2018-04-24_14-36-17]# ls -lth
total 532K
-rw-r----- 1 root root  424 Apr 24 14:36 backup-my.cnf
-rw-r----- 1 root root  350 Apr 24 14:36 ib_buffer_pool
drwxr-x--- 2 root root 4.0K Apr 24 14:36 mysql
-rw-r----- 1 root root  117 Apr 24 14:36 xtrabackup_checkpoints
-rw-r----- 1 root root  572 Apr 24 14:36 xtrabackup_info
-rw-r----- 1 root root 2.5K Apr 24 14:36 xtrabackup_logfile
drwxr-x--- 2 root root  12K Apr 24 14:36 sys
drwxr-x--- 2 root root 4.0K Apr 24 14:36 l5m
drwxr-x--- 2 root root 4.0K Apr 24 14:36 performance_schema
-rw-r----- 1 root root 480K Apr 24 14:36 ibdata1.delta
-rw-r----- 1 root root   44 Apr 24 14:36 ibdata1.meta

Insert some data into the table again:

mysql> insert into tab1 values(3);
Query OK, 1 row affected (0.06 sec)

mysql> insert into tab1 values(4);
Query OK, 1 row affected (0.01 sec)

mysql> insert into tab2 values(200);
Query OK, 1 row affected (0.06 sec)

mysql> commit;
Query OK, 0 rows affected (0.00 sec)

mysql> select * from tab1;
+------+
| id   |
+------+
|    1 |
|    2 |
|    3 |
|    4 |
+------+
4 rows in set (0.00 sec)

mysql> select * from tab2;
+------+
| id   |
+------+
|  100 |
|  200 |
+------+
2 rows in set (0.00 sec)

Do an incremental backup based on the last incremental backup again (backup based on /u01/backup/inc/2018-04-24_14-36-17/)

[root@qht26 ~]# innobackupex --defaults-file=/etc/my.cnf --user=xbk --password=123456 --socket=/u01/mysql/mysql.sock --incremental /u01/backup/inc --incremental-basedir=/u01/backup/inc/2018-04-24_14-36-17/ --parallel=2
[root@qht26 inc]# ls -lth
total 8.0K
drwxr-x--- 6 root root 4.0K Apr 24 14:42 2018-04-24_14-41-43
drwxr-x--- 6 root root 4.0K Apr 24 14:36 2018-04-24_14-36-17
 
 
[root@qht26 inc]# pwd
/u01/backup/inc

Restoration of incremental backups:

There are 3 steps to restore incremental backups

  1. Restoring a full backup
  2. Restore the incremental backup to the full backup (add the --redo-only parameter to the incremental backup that starts to restore, and remove the --redo-only parameter to the last incremental backup)
  3. Restore the entire full backup and roll back uncommitted data

#To prepare a full backup, you need to add redo-only
[root@qht26 backup]# innobackupex --apply-log --redo-only /u01/backup/2018-04-24_11-54-46/
#Apply increment 1 to full backup
Need to add redo-only
[root@qht26 backup]# innobackupex --apply-log --redo-only /u01/backup/2018-04-24_11-54-46/ --incremental-dir=/u01/backup/inc/2018-04-24_14-36-17/
 
 
#Apply increment 2 to full backup
[root@qht26 backup]# innobackupex --apply-log --redo-only /u01/backup/2018-04-24_11-54-46/ --incremental-dir=/u01/backup/inc/2018-04-24_14-41-43/
##把所有合在一起的完全备份整体进行一次apply操作,回滚未提交的数据##
[root@qht26 2018-04-24_11-54-46]# innobackupex --apply-log /u01/backup/2018-04-24_11-54-46
xtrabackup: Log applied to lsn 2556977
xtrabackup: The intended lsn is 2564716

模拟数据损坏:

mysql> use l5m
Database changed
mysql> drop table tab2;
Query OK, 0 rows affected (0.15 sec)

mysql> drop table tab1;
Query OK, 0 rows affected (0.04 sec)

[root@qht26 ~]# service mysql stop
[root@qht26 ~]# mv /u01/mysql /u01/mysql_bk2
[root@qht26 ~]# mkdir /u01/mysql
[root@qht26 ~]# chown -R mysql.mysql /u01/mysql
[root@qht26 ~]# innobackupex --defaults-file=/etc/my.cnf --copy-back --rsync /u01/backup/2018-04-24_11-54-46/
[root@qht26 ~]# chown -R mysql.mysql /u01/mysql
[root@qht26 ~]# service mysql start
Starting MySQL..                                           [  OK  ]

#数据已全部恢复

mysql> use l5m
Database changed
mysql> show tables;
+---------------+
| Tables_in_l5m |
+---------------+
| tab1          |
| tab2          |
+---------------+
2 rows in set (0.00 sec)

mysql> select * from tab1;
+------+
| id   |
+------+
|    1 |
|    2 |
|    3 |
|    4 |
+------+
4 rows in set (0.01 sec)

mysql> select * from tab2;
+------+
| id   |
+------+
|  100 |
|  200 |
+------+
2 rows in set (0.00 sec)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324815682&siteId=291194637