zoukankan      html  css  js  c++  java
  • 如何用Percona XtraBackup进行MySQL从库的单表备份和恢复【转】

    前提

    应该确定采用的是单表一个表空间,否则不支持单表的备份与恢复。

    在配置文件里边的mysqld段加上

    innodb_file_per_table = 1

    环境说明:
    主库:192.168.0.1
    从库1:192.168.0.2
    从库2:192.168.0.3
    备份工具 : Percona xtrabackup version 2.4.8 based on MySQL server 5.7.13 Linux (x86_64) (revision id: 97330f7)


    在主库上创建chenfeng库:

    mysql> create database chenfeng;
    Query OK, 1 row affected (0.08 sec)
    
    
    mysql> use chenfeng
    Database changed
    
    
    mysql> create table duansf(id int (11),name varchar(10));
    Query OK, 0 rows affected (0.14 sec)
    
    
    mysql> insert into duansf values(1,'duansf');
    Query OK, 1 row affected (0.01 sec)
    
    
    mysql> insert into duansf values(2,'duansf');
    Query OK, 1 row affected (0.01 sec)


    只备份chenfeng库的duansf表:

    [root@localhost backup]# innobackupex --defaults-file=/etc/my.cnf --user=root --password=dsf0723 -S /tmp/mysql.sock --slave-info --safe-slave-backup --include=chenfeng.duansf /data/backup


    innobackup部分参数解释:
    --slave-info会将master的binary log文件名和偏移量保存到xtrabackup_slave_info文件中
    --slave-info,备份从库, 加上 --slave-info 备份目录下会多生成一个 xtrabackup_slave_info 文件,
     这里会保存主日志文件以及偏移, 文件内容类似于: 
    CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000006', MASTER_LOG_POS=826270;

    --safe-slave-backup会暂停slave的sql线程,待备份结束后再启动

    --include=REGEXP
    对xtrabackup参数--tables的封装,也支持ibbackup。备份包含的库表,例如:--include="test.*",意思是要备份test库中所有的表。
    如果需要全备份,则省略这个参数;如果需要备份test库下的2个表:test1和test2,则写成:--include="test.test1|test.test2"。也可以使用通配符,如:--include="test.test*"。
    本例中只备份chenfeng库下的duansf表,可以这么写--include=chenfeng.duansf


    由于只备份了chenfeng库的duansf表,所以我们在生成的时间目录里2017-10-15_20-33-07只看到了chenfeng文件夹.
    [root@localhost 2017-10-15_20-33-07]# ll /data/backup
    总用量 12316
    -rw-r-----. 1 root root      424 10月 15 20:33 backup-my.cnf
    -rw-r-----. 1 root root      593 10月 15 20:33 ib_buffer_pool
    -rw-r-----. 1 root root 12582912 10月 15 20:33 ibdata1
    drwxr-x---. 2 root root       42 10月 15 20:33 chenfeng
    -rw-r-----. 1 root root       21 10月 15 20:33 xtrabackup_binlog_info
    -rw-r-----. 1 root root      117 10月 15 20:33 xtrabackup_checkpoints
    -rw-r-----. 1 root root      573 10月 15 20:33 xtrabackup_info
    -rw-r-----. 1 root root     2560 10月 15 20:33 xtrabackup_logfile
    -rw-r-----. 1 root root       76 10月 15 20:33 xtrabackup_slave_info

    [root@localhost 2017-10-15_20-33-07]# cd chenfeng
    [root@localhost chenfeng]# ll
    总用量 108
    -rw-r-----. 1 root root  8586 10月 15 20:33 duansf.frm
    -rw-r-----. 1 root root 98304 10月 15 20:33 duansf.ibd


    把chenfeng目录打包放到/data/backup/bak目录下:
    [root@localhost 2017-10-15_20-33-07]# tar czvf chenfeng.tar.gz chenfeng


    [root@localhost 2017-10-15_20-33-07]# mv chenfeng.tar.gz /data/backup/bak/


    解压缩做恢复用:


    恢复数据的时候,要经过prepare(recovery)和restore两个步骤,
    prepare导出表步骤:

    [root@localhost backup]# innobackupex --defaults-file=/etc/my.cnf --user=root --password=dsf0723 -S /tmp/mysql.sock --apply-log --export /data/backup/2017-10-15_20-33-07
    171015 20:49:32 innobackupex: Starting the apply-log operation


    在从库2上删除duansf表:

    mysql> use chenfeng
    Reading table information for completion of table and column names
    You can turn off this feature to get a quicker startup with -A
    
    
    Database changed
    mysql> show tables;
    +-----------------+
    | Tables_in_chenfeng |
    +-----------------+
    | duansf          |
    +-----------------+
    1 row in set (0.00 sec)
    
    
    
    
    mysql> show tables;
    +-----------------+
    | Tables_in_chenfeng |
    +-----------------+
    | duansf          |
    +-----------------+
    1 row in set (0.00 sec)
    
    
    mysql> delete from duansf;
    Query OK, 2 rows affected (0.06 sec)
    
    
    mysql> select * from duansf;
    Empty set (0.00 sec)



    从xtrabackup备份里恢复出duansf表数据:
    删除表:
    mysql> drop table duansf;
    Query OK, 0 rows affected (0.06 sec)


    重建表结构:
    mysql> CREATE TABLE `duansf` (
        ->   `id` int(11) DEFAULT NULL,
        ->   `name` varchar(10) DEFAULT NULL
        -> ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    Query OK, 0 rows affected (0.05 sec)


    卸载表空间:
    mysql> ALTER TABLE chenfeng.duansf DISCARD TABLESPACE;
    Query OK, 0 rows affected (0.01 sec)




    从备份里恢复:
    [root@localhost chenfeng]# cp /data/backup/2017-10-15_20-33-07/chenfeng/{duansf.ibd,duansf.cfg,duansf.frm} /usr/local/mysql/data/chenfeng
    [root@localhost chenfeng]# ll
    总用量 116
    -rw-r-----. 1 mysql mysql    65 10月 15 19:00 db.opt
    -rw-r--r--. 1 root  root    426 10月 15 21:13 duansf.cfg
    -rw-r-----. 1 mysql mysql  8586 10月 15 21:06 duansf.frm
    -rw-r-----. 1 root  root  98304 10月 15 21:13 duansf.ibd




    root@localhost data]# chown -R mysql:mysql /usr/local/mysql/data/chenfeng


    装载表空间:
    mysql> ALTER TABLE chenfeng.duansf import TABLESPACE;
    Query OK, 0 rows affected, 1 warning (0.20 sec)


    查看duansf表数据:
    mysql> select * from duansf;
    +------+--------+
    | id   | name   |
    +------+--------+
    |    1 | duansf |
    |    2 | duansf |
    +------+--------+
    2 rows in set (0.00 sec)

    数据已恢复.

    转自

    http://blog.itpub.net/15498/viewspace-2146003/

  • 相关阅读:
    Windows Server 2008上安装 Windows SharePoint Services 3.0
    自定义Unity 容器的扩展 Unity Application Block Event Broker
    .NET Migration工具
    ASP.NET 应用程序的扩展策略[MSDN 杂志]
    命令行解析的规则以及Command Line Parser Library
    Visual Studio 2008 SP1和.NET FX 3.5 SP1发布了
    Entity Framework samples For RTM
    PowerShell的开源实现
    Enterprise Library 4.0缓存应用程序块
    Microsoft SQL Server Community & Samples
  • 原文地址:https://www.cnblogs.com/paul8339/p/8023761.html
Copyright © 2011-2022 走看看