浅谈mysql 针对单张表的备份与还原

时间:2022-05-15 20:48:30

A、MySQL 备份工具xtrabackup 的安装

1. percona 官方xtrabackup 的二进制版本;二进制版本解压就能用了。

2. 解压xtrabackup & 创建连接

tar -xzvf percona-xtrabackup-2.3.4-Linux-x86_64.tar.gz -C /usr/local/
ln -s /usr/local/percona-xtrabackup-2.3.4 /usr/local/xtrabackup

3. 设置PATH环境变量
export PATH=/usr/local/xtrabackup/bin/:$PATH    

B、在mysql数据库中创建一个用户备份的用户 & 授权

1. 创建用户

        create user backuper@'localhost' identified by 'backup123';
        create user backuper@'127.0.0.1' identified by 'backup123';

2. 授权

grant reload,lock tables,replication client,process,super on *.* to 'backuper'@'localhost';
        grant create,insert,select on percona_schema.xtrabackup_history to 'backuper'@'localhost';
        grant reload,lock tables,replication client,process,super on *.* to 'backuper'@'127.0.0.1';
        grant create,insert,select on percona_schema.xtrabackup_history to 'backuper'@'127.0.0.1';

C、备份前的检查,这一步的主要目地是在之后做还原操作时,验证还原是不是有效;(生产是没有这一步的,

1. select * from tempdb.dict__major;
   select * from dict__major;

        +--------------+-----------------+
        | column_value | column_mean     |
        +--------------+-----------------+
        |            1 | 汉语言文学      |
        |            2 | 精算            |
        |            3 | 生物制药        |
        |            4 | 材料化学        |
        |            5 | 商务英语        |
        |            6 | 考古            |
        |            7 | 外交            |
        |            8 | 导游            |
        +--------------+-----------------+

D、备份tempdb.dict__major 表

1. 备份命令

innobackupex --host=127.0.0.1 --user=backuper --password=backup123 --port=3306 --include='tempdb.dict__major' /tmp/tempdb   

2. 备份完成后会在备份目录(/tmp/tempdb) 下生成用当前时间命名的目录,里面保存的就是备份文件

        tree /tmp/tempdb/
        /tmp/tempdb/
        └── 2016-09-10_18-25-16
            ├── backup-my.cnf
            ├── ibdata1
            ├── tempdb
            │   ├── dict__major.frm
            │   └── dict__major.ibd
            ├── xtrabackup_binlog_info
            ├── xtrabackup_checkpoints
            ├── xtrabackup_info
            └── xtrabackup_logfile
           

E、备份完成后就可以删除tempdb.dict__major表了(注意这里一定要保存一份表的定义,还原时会用到)

mysql>drop table tempdb.dict__major;

F、为了得到一个一致的备份集 在还原操作前还要进行一次日志的前滚和回滚

1. 前滚&回滚日志

innobackupex --apply-log --export /tmp/tempdb/2016-09-10_18-25-16/ 

2.  与前滚& 回滚前的对比

        tree /tmp/tempdb/
        /tmp/tempdb/
        └── 2016-09-10_18-25-16
            ├── backup-my.cnf
            ├── ibdata1
            ├── ib_logfile0
            ├── ib_logfile1
            ├── tempdb
            │   ├── dict__major.cfg
            │   ├── dict__major.exp
            │   ├── dict__major.frm
            │   └── dict__major.ibd
            ├── xtrabackup_binlog_info
            ├── xtrabackup_binlog_pos_innodb
            ├── xtrabackup_checkpoints
            ├── xtrabackup_info
            └── xtrabackup_logfile
           

G、还原tempdb.dict__major表

1. 创建 tempdb.dict__major表

        create table dict__major(
        column_value tinyint not null,
        column_mean varchar(32) not null,
        constraint pk__dict__major primary key (column_value));

2. 删除 tempdb.dict__major的表空间文件

alter table tempdb.dict__major discard tablespace;

3. 把备份中的表空间文件复制到tempdb.dict__major 表空间应该在的位置

        cp /tmp/tempdb/2016-09-10_18-25-16/tempdb/dict__major.ibd /usr/local/mysql/data/tempdb/
        cp /tmp/tempdb/2016-09-10_18-25-16/tempdb/dict__major.exp /usr/local/mysql/data/tempdb/
        cp /tmp/tempdb/2016-09-10_18-25-16/tempdb/dict__major.cfg /usr/local/mysql/data/tempdb/
        chown -R mysql:mysql /usr/local/mysql/data/tempdb/*
       

4. 导入表空间文件

alter table tempdb.dict__major import tablespace;    

5. 查看dict__major表恢复情况

        select * from dict__major;
        +--------------+-----------------+
        | column_value | column_mean     |
        +--------------+-----------------+
        |            1 | 汉语言文学      |
        |            2 | 精算            |
        |            3 | 生物制药        |
        |            4 | 材料化学        |
        |            5 | 商务英语        |
        |            6 | 考古            |
        |            7 | 外交            |
        |            8 | 导游            |
        +--------------+-----------------+

---------------------------------------------------------------

上一节用的是xtrabackup 对表进行备份,它的应用场景是单表的数据量大且在备份的过程中还要支持对表的写操作;也就是说在目前的场景下mysqldump 这个简单的

备份工具也是可以满足要求的;


现给出mysqldump 备份的一般步骤

A:创建备份用户

1.
        create user dumper@'127.0.0.1' identified by 'dumper123';
        grant select on *.* to dumper@'127.0.0.1';
        grant show view on *.* to dumper@'127.0.0.1';
        grant lock tables on *.* to dumper@'127.0.0.1';
        grant trigger on *.* to dumper@'127.0.0.1';
       

B:备份tempdb.dict__major表

1.
mysqldump --host=127.0.0.1 --port=3306 --user=dumper --password=dumper123 --quick tempdb dict__major >/tmp/tempdb.dict__major.sql
   

C: 删除已经备份的表

1.
mysql>drop table tempdb.dict__major;

D:还原tempdb.dict__major表

1.
mysql -uroot -pxxxxx -h127.0.0.1 -p3306 tempdb </tmp/tempdb.dict__major.sql     

E:检证还原的有效性

1.
select * from dict__major;

        +--------------+-----------------+
        | column_value | column_mean     |
        +--------------+-----------------+
        |            1 | 汉语言文学      |
        |            2 | 精算            |
        |            3 | 生物制药        |
        |            4 | 材料化学        |
        |            5 | 商务英语        |
        |            6 | 考古            |
        |            7 | 外交            |
        |            8 | 导游            |
        +--------------+-----------------+

以上这篇浅谈mysql 针对单张表的备份与还原就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。