layout: post
title: Mysql数据库备份—-Percona XtraBackup实现单表单库备份
date: 2018-02-27
tags: [“Mariadb”,”软件服务”]


一、Percona XtraBackup实现全量和增量备份

二、实验简单步骤

  1. 单表备份
  2. 删除表
  3. 恢复表

三、实验步骤

1、备份单表

  1. [root@localhost ~]# innobackupex --include=test.test /backup/test_table
  2. 180227 12:13:53 innobackupex: Starting the backup operation
  3.  
  4. IMPORTANT: Please check that the backup run completes successfully.
  5. At the end of a successful backup run innobackupex
  6. prints "completed OK!".
  7. .
  8. .
  9. .
  10. .
  11. 180227 12:13:55 Backup created in directory '/backup/test_table/2018-02-27_12-13-53/'
  12. MySQL binlog position: filename 'localhost-bin.000001', position '332', GTID of the last change ''
  13. 180227 12:13:55 [00] Writing /backup/test_table/2018-02-27_12-13-53/backup-my.cnf
  14. 180227 12:13:55 [00] ...done
  15. 180227 12:13:55 [00] Writing /backup/test_table/2018-02-27_12-13-53/xtrabackup_info
  16. 180227 12:13:55 [00] ...done
  17. xtrabackup: Transaction log of lsn (1622559) to (1622568) was copied.
  18. 180227 12:13:55 completed OK!
  19.  
  20. [root@localhost 2018-02-27_12-13-53]# cd /backup/test_table/2018-02-27_12-13-53/
  21. [root@localhost 2018-02-27_12-13-53]# ll
  22. 总用量 12308
  23. -rw-r----- 1 root root 424 2 27 12:13 backup-my.cnf
  24. -rw-r----- 1 root root 12582912 2 27 12:13 ibdata1
  25. drwxr-x--- 2 root root 38 2 27 12:13 test
  26. -rw-r----- 1 root root 26 2 27 12:13 xtrabackup_binlog_info
  27. -rw-r----- 1 root root 113 2 27 12:13 xtrabackup_checkpoints
  28. -rw-r----- 1 root root 508 2 27 12:13 xtrabackup_info
  29. -rw-r----- 1 root root 2560 2 27 12:13 xtrabackup_logfile

2、删除test表

  1. [root@localhost 2018-02-27_12-13-53]# mysql -e 'drop tables test.test;'

3、恢复test表

首先需要创建表结构

  1. MariaDB [(none)]> use test;
  2. MariaDB [test]> CREATE TABLE IF NOT EXISTS `test`(
  3. -> `runoob_id` INT UNSIGNED AUTO_INCREMENT,
  4. -> `runoob_title` VARCHAR(100) NOT NULL,
  5. -> `runoob_author` VARCHAR(40) NOT NULL,
  6. -> `submission_date` DATE,
  7. -> PRIMARY KEY ( `runoob_id` )
  8. -> )ENGINE=InnoDB DEFAULT CHARSET=utf8;
  9. Query OK, 0 rows affected, 1 warning (0.00 sec)

复制表数据到数据库中

  1. [root@localhost test]# innobackupex --apply-log --export /backup/test_table/2018-02-27_12-13-53/ #整理表数据
  2. [root@localhost test]# cd /backup/test_table/2018-02-27_12-13-53/test
  3. [root@localhost test]# cp ./* /var/lib/mysql/test/
  4. [root@localhost test]# chown mysql.mysql /var/lib/mysql/test/*
  5.  

注意权限问题!

登录mysql数据库,恢复test表

  1. [root@localhost test]# mysql

  2. MariaDB [(none)]> use test;

  3. MariaDB [test]> alter table test discard tablespace;

  4. MariaDB [test]> alter table test import tablespace;


 

注意: XtraBackup实现单表单库备份,需要基于mariadb10即以上版本才可以实现单表的备份,其他版本可以通过mysqldump来实现数据的备份。

文档更新时间: 2020-05-25 12:16   作者:张尚