xtrabackup备份工具两种命令单库增量备份还原

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了xtrabackup备份工具两种命令单库增量备份还原相关的知识,希望对你有一定的参考价值。

Innobackupex 参数解释:

--defaults-file=[MY.CNF]该选项传递给xtrabackup子进程,从指定文件读取缺省选项

--apply-log 从备份恢复。

--redo-only 该选项强制跳过rollback阶段,只进行redo。这是有必要使用的,如果备份后,要使用增量改变的。

--copy-back 从备份目录拷贝数据和索引文件到datadir目录

--remote-host=HOSTNAME备份到远程主机上,使用ssh

--stream=[tar|cpio(notimplemented)] 指定备份标准输出格式

--tmpdir=DIRECTORY默认与tmpdir相同。使用—remote-host或—stream参数后,传输日志文件将存放在临时目录下

--use-memory=MB选项传递给xtrabackup子进程。恢复使用内存大小

--parallel=NUMBER-OF-THREADS选项传递给xtrabackup子进程,指定数据传输线程总数。默认为1

--throttle=ios选项传递给xtrabackup子进程,限制IO线程数量

--sleep=MS 选项传递给xtrabackup子进程。每拷贝1MB数据暂停多少MS时间

--compress[=LEVEL]选项传递给xtrabackup子进程。压缩级别在0-9.1快速压缩,9最佳压缩,0不压缩。默认为1.

--include=REGEXP选项传递给xtrabackup子进程。使用正则进行匹配

--databases=LIST指定备份数据库

--tables-file=FILE

--uncompress选项传递给xtrabackup子进程。对压缩过的InnoDB数据文件不进行压缩

--export 仅使用于prepare选项。选项传递给xtrabackup子进程。

--user=NAME

--password=WORD

--host=HOST

--port=PORT

--slave-info 备份复制从服务端,主从信息记录在ibbackup_slave_info文件中

--socket=SOCKET

--no-timestamp不在备份根目录下创建以当前时间戳为名称的新的备份目录

--ibbackup=IBBACKUP-BINARYibbackup二进制路径

--no-lock 禁止表级锁。全部是InnoDB引擎表和不关系二进制日志位置下使用

--scpopt=SCP-OPTIONS指定scp参数






                                    innobackupex单库增量恢复


[[email protected] ~]# mkdir -p /opt/mysqlbackup/{full,inc}

#完全备份

[[email protected] ~]# innobackupex --user=root --password=123456 --include=test /opt/mysqlbackup/full/

mysql> select * from test.t1;

+----+------+

| id | name |

+----+------+

|  1 | tom1 |

|  2 | tom1 |

+----+------+

#插入数据

mysql> insert into test.t1(name) values('tom2');

#增量备份1

[[email protected] ~]# innobackupex --user=root --password=123456 --include=test --incremental /opt/mysqlbackup/inc/ --incremental-basedir=/opt/mysqlbackup/full/2018-07-30_20-36-57/

#插入数据,添加新表

mysql> insert into test.t1(name) values('tom3');

mysql> create table test.t2(id int);

mysql> insert into test.t2 values(1);

mysql> select * from test.t2;

+------+

| id   |

+------+

|    1 |

#增量备份2

[[email protected] ~]# innobackupex --user=root --password=123456 --include=test --incremental /opt/mysqlbackup/inc/ --incremental-basedir=/opt/mysqlbackup/inc/2018-07-30_20-47-55/

[[email protected] ~]# ls /opt/mysqlbackup/full

2018-07-30_20-36-57

[[email protected] ~]# ls /opt/mysqlbackup/inc/

2018-07-30_20-47-55  2018-07-30_20-51-21

#删除数据库

mysql> drop database test;

[[email protected] ~]# service mysqld stop

[[email protected] ~]# cd /opt/mysqlbackup/full/2018-07-30_20-36-57/test/

[[email protected] test]# ls

db.opt  t1.frm  t1.ibd

[[email protected] ~]innobackupex --apply-log --redo-only /opt/mysqlbackup/full/2018-07-30_20-34-34/

[[email protected] ~]innobackupex --apply-log --redo-only /opt/mysqlbackup/full/2018-07-30_20-34-34/ --incremental-dir=/opt/mysqlbackup/inc/2018-07-30_20-38-12/

[[email protected] ~]innobackupex --apply-log --redo-only /opt/mysqlbackup/full/2018-07-30_20-34-34/ --incremental-dir=/opt/mysqlbackup/inc/2018-07-30_20-42-03/

[[email protected] ~]innobackupex --apply-log --export /opt/mysqlbackup/full/2018-07-30_20-34-34/

[[email protected] ~]cp -rf /opt/mysqlbackup/full/2018-07-30_20-34-34/* /usr/local/mysql/data/

[[email protected] data]# service mysqld restart 

mysql> select * from t1;

+----+------+

| id | name |

+----+------+

|  1 | tom1 |

|  2 | tom1 |

|  3 | tom1 |

|  4 | tom1 |

|  5 | tom1 |

|  6 | tom1 |

+----+------+

6 rows in set (0.00 sec)


mysql> select * from t2

    -> ;

+------+

| id   |

+------+

|    1 |

|    3 |

|    2 |

+------+


                                                          innobackupex备份多库

[[email protected] full]# innobackupex --user=root --password=123456 --databases="test test1" /opt/mysqlbackup/full/

 

 




xtrabackup备份单个表

[[email protected] ~]# mkdir -p /opt/mysqlbackup/{full,inc}

[[email protected] ~]# mysql -uroot -p123456

mysql> use test

mysql> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| mysql              |

| performance_schema |

| sys                |

| test               |

| test1              |

+--------------------+


mysql> select * from test.t1;

+----+------+

| id | name |

+----+------+

|  1 | tom1 |

|  2 | tom1 |

+----+------+

[[email protected] ~]# xtrabackup --user=root --password=123456 --backup --target-dir=/opt/mysqlbackup/full/full_$(date +%F_%H%M%S) --databases="test"

mysql> insert into test.t1(name) values('tom1');

[[email protected] ~]# xtrabackup --user=root --password=123456 --backup --target-dir=/opt/mysqlbackup/inc/inc_$(date +%F_%H%M%S) --incremental-basedir=/opt/mysqlbackup/full/full_2018-07-30_204200/ --databases='test'

mysql> insert into test.t1(name) values('tom1');

mysql> insert into test.t2(name) values('tom1');

[[email protected] ~]# xtrabackup --user=root --password=123456 --backup --target-dir=/opt/mysqlbackup/inc/inc_$(date +%F_%H%M%S) --incremental-basedir=/opt/mysqlbackup/inc/inc_2018-07-30_205007/ --databases='test'

mysql> drop table test.t1;

mysql> delete from test.t2 where id=1;

[[email protected] ~]# xtrabackup --prepare --apply-log-only --target-dir=/opt/mysqlbackup/full/full_2018-07-30_204200/

[[email protected] ~]# cat /opt/mysqlbackup/full/full_2018-07-30_204200/xtrabackup_checkpoints 

backup_type = log-applied

from_lsn = 0

to_lsn = 2609032

last_lsn = 2609041

compact = 0

recover_binlog_info = 0

[[email protected] ~]# xtrabackup --prepare --apply-log-only --target-dir=/opt/mysqlbackup/full/full_2018-07-30_204200/ --incremental-dir=/opt/mysqlbackup/inc/inc_2018-07-30_205007/

[[email protected] ~]# xtrabackup --prepare --apply-log-only --target-dir=/opt/mysqlbackup/full/full_2018-07-30_204200/ --incremental-dir=/opt/mysqlbackup/inc/inc_2018-07-30_205157/

[[email protected] ~]# xtrabackup --prepare --target-dir=/opt/mysqlbackup/full/full_2018-07-30_204200/

[[email protected] ~]# cat /opt/mysqlbackup/full/full_2018-07-30_204200/xtrabackup_checkpoints 

backup_type = full-prepared

from_lsn = 0

to_lsn = 2613778

last_lsn = 2613787

compact = 0

recover_binlog_info = 0

[[email protected] ~]# service mysqld stop 

[[email protected] ~]# cd /opt/mysqlbackup/full/full_2018-07-30_204200/

[[email protected] full_2018-07-30_204200]# rsync -rvt --exclude 'xtrabackup_checkpoints' --exclude 'xtrabackup_logfile' ./ /usr/local/mysql/data/

[[email protected] full_2018-07-30_204200]# chown -R mysql:mysql /usr/local/mysql/

[[email protected] full_2018-07-30_204200]# service mysqld start

mysql> show tables;

+----------------+

| Tables_in_test |

+----------------+

| t1             |

| t2             |

+----------------+

2 rows in set (0.00 sec)


mysql> select * from t1;

+----+------+

| id | name |

+----+------+

|  1 | tom1 |

|  2 | tom1 |

|  3 | tom1 |

|  4 | tom1 |

+----+------+

4 rows in set (0.00 sec)


mysql> select * from t2;

+----+------+

| id | name |

+----+------+

|  1 | tom1 |

|  2 | tom1 |

|  3 | tom1 |

+----+------+

3 rows in set (0.00 sec)






  xtrabackup备份多库

[[email protected] /]# xtrabackup --defaults-file=/etc/my.cnf --user=root --password=123456 --backup --target-dir=/opt/mysqlbackup/full/3 --databases='test test1'

mysql> insert into test.t1(name) values('tom1');


以上是关于xtrabackup备份工具两种命令单库增量备份还原的主要内容,如果未能解决你的问题,请参考以下文章

Xtrabackup 能不能做单库的备份恢复

xtrabackup 备份还原单库/表

xtrabackup安装进行全量备份增量备份

05: 实时增量备份 XtraBackup 备份 总结和答疑 MySQL 主从同步

MYSQL xtrabackup 增量备份

通过Xtrabackup实现MySQL实例的全库备份与按需单库恢复