Xtrabacup如何備份

這篇文章將為大家詳細(xì)講解有關(guān)Xtrabacup如何備份,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

站在用戶的角度思考問題,與客戶深入溝通,找到色尼網(wǎng)站設(shè)計(jì)與色尼網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類型包括:成都做網(wǎng)站、成都網(wǎng)站制作、成都外貿(mào)網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、域名與空間、雅安服務(wù)器托管、企業(yè)郵箱。業(yè)務(wù)覆蓋色尼地區(qū)。

第1章 XTRABACKUP

1.1 xtrabackup介紹

InnoDB 有個(gè)商業(yè)的InnoDB Hotbackup,可以對InnoDB引擎的表實(shí)現(xiàn)在線熱備。而 percona出品的Xtrabackup,是InnoDB Hotbackup的一個(gè)開源替代品,可以在線對InnoDB/XtraDB引擎的表進(jìn)行物理備份。MySQLdump支持在線備份,不過是邏輯備份,效率比較差。xtrabackup是開源的MySQL備份工具,物理備份,效率很不錯(cuò)。

Xtrabackup有兩個(gè)主要的工具:xtrabackup、innobackupex,其中xtrabackup只能備份InnoDB和XtraDB兩種數(shù)據(jù)表,innobackupex則封裝了xtrabackup,同時(shí)可以備份MyISAM數(shù)據(jù)表。Xtrabackup做備份的時(shí)候不能備份表結(jié)構(gòu)、觸發(fā)器等等,智能紛紛.idb數(shù)據(jù)文件。另外innobackupex還不能完全支持增量備份,需要和xtrabackup結(jié)合起來實(shí)現(xiàn)全備的功能。

1.2 xtrabackup特點(diǎn)介紹

C語言編寫,可以備份Innodb、XtraD,不拷貝*.frm文件

支持的引擎有:

InnoDB、Xtradb:hotbackup

MyISAM: with read lock

具有的特點(diǎn)有如下幾點(diǎn):

§  1)備份過程快速、可靠;

§  (2)備份過程不會(huì)打斷正在執(zhí)行的事務(wù);

§  (3)能夠基于壓縮等功能節(jié)約磁盤空間和流量;

§  (4)自動(dòng)實(shí)現(xiàn)備份檢驗(yàn);

§  (5)還原速度快;

1.3 Xtrabackup的下載安裝

Xtrabackup工具由Percona公司開發(fā)的開源熱備工具,可以到官網(wǎng)下載到最新的版本文件。

https://www.percona.com/downloads/下載各種版本,也可以在系統(tǒng)中使用wget下載。

方法一:

wget https://www.percona.com/downloads/XtraBackup/Percona-XtraBackup-2.4.4/binary/redhat/6/x86_64/percona-xtrabackup-24-2.4.4-1.el6.x86_64.rpm

安裝依賴的包

yum install perl-DBI  perl-DBD-MySQL  perl-Time-HiRes perl-IO-Socket-SSL installperl perl-devel libaio libaio-devel perl-Time-HiRes perl-DBD-MySQL

方法二:

下載好的rpm包,直接使用yum 安裝,自動(dòng)安裝依賴包,但是需要有epel源的支持,所以要下載配置好epe'源

wget -O /etc/yum.repos.d/epel.repo  http://mirrors.aliyun.com/repo/epel-6.repo

yum install -y percona-xtrabackup-24-2.4.4-1.el6.x86_64.rpm

1.4 xtrabackup備份用戶需要的權(quán)限

RELOAD 和 LOCKTABLES 權(quán)限為了執(zhí)行 FLUSHTABLES WITH READ LOCK

REPLICATION CLIENT 為了獲取binary log位置

CREATE TABLESPACE 權(quán)限為了導(dǎo)入表,用戶表級別的恢復(fù)

SUPER權(quán)限在slave環(huán)境下備份用來啟動(dòng)和關(guān)閉slave線程

1.5 Xtrabackup備份

InnoDB 和非InnoDB文件的備份都是通過拷貝文件,但是實(shí)現(xiàn)的方式是不同的。

  • InnoDB是以page為粒度做的的(Xtrabackup),Xtrabackup在讀取每個(gè)page時(shí)會(huì)校驗(yàn)checksum值,保證數(shù)據(jù)塊是一致的

  • 非InnoDB是cp或者tar 命令,innobackupex在cp MyISAM文件時(shí)已經(jīng)做了flush(FTWRL),磁盤上的文件也是完整的,所以最終備份集里的數(shù)據(jù)文件都是寫入完整的

  • 1.5.1備份流程

Xtrabacup如何備份

1.5.2 全備流程圖

Xtrabacup如何備份

1.5.3 增量備份流程圖

Xtrabacup如何備份

1.6 Xtrabackup恢復(fù)

1.6.1 恢復(fù)流程圖

Xtrabacup如何備份

Xtrabacup如何備份

Xtrabacup如何備份

Xtrabacup如何備份

Xtrabacup如何備份

1.7 Xtrabackup的使用

規(guī)范操作,我們先建好全備和增量備份的路徑

[root@db01 backups]# pwd

/data/backups

[root@db01 backups]# ll

總用量 12

drwxr-xr-x 4 root root 4096 9月   4 17:28 full

drwxr-xr-x 4 root root 4096 9月   4 17:26 inc

操作過程:

 mkdir/data/backups/{full,inc} -p

1.7.1 全備

*****生產(chǎn)環(huán)境中為了數(shù)據(jù)一致性最好是ROW模式,因?yàn)槠渌哪J綌?shù)據(jù)會(huì)丟失*****

命令語法格式:

innobackupex --user=User--password=PWD/data/backup/full --slave-info --safe-slave-backup --parallel=4--safe-slave-backup-timeout=7200  --rsync

--rsync 這個(gè)參數(shù)一般用作分布式數(shù)據(jù)庫集群的時(shí)候

全備步驟:

1、準(zhǔn)備一個(gè)xtr_test數(shù)據(jù)庫,并在xtr_test數(shù)據(jù)庫中插入testbackup表,字段包含id,step兩個(gè)字段

mysql -uroot -p789 -S /data/3306/mysql.sock
show databases;
create database xtr_test;
use xtr_test;
select database();
create table testbackup(id int not nullauto_increment primary key,step varchar(50))engine=innodb;
show tables;
desc testbackup;
show create table testbackup\G

結(jié)果樣子:

mysql> desc testbackup;
+-------+-------------+------+-----+---------+----------------+
| Field | Type        | Null | Key | Default | Extra          |
+-------+-------------+------+-----+---------+----------------+
| id    |int(11)     | NO   | PRI | NULL    | auto_increment |
| step  |varchar(50) | YES  |     | NULL   |                |
+-------+-------------+------+-----+---------+----------------+
2 rows in set (0.02 sec)
mysql> show create table testbackup\G
*************************** 1. row***************************
       Table:testbackup
Create Table: CREATE TABLE `testbackup` (
  `id`int(11) NOT NULL AUTO_INCREMENT,
  `step`varchar(50) DEFAULT NULL,
  PRIMARY KEY(`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

2、插入字段“firstfull backup”到testbackup表中

insert into testbackup(step) values('first fullbackup');

操作結(jié)果:

mysql> select * from testbackup;
+----+-------------------+
| id | step              |
+----+-------------------+
|  1 | firstfull backup |
+----+-------------------+
1 row in set (0.02 sec)

3、執(zhí)行全備

此時(shí),我們創(chuàng)建的xtr_test數(shù)據(jù)庫中的testbackup表中有數(shù)據(jù),而且我們創(chuàng)建的/data/backups/full文件下面沒有任何內(nèi)容。下面執(zhí)行全備,操作命令上面已經(jīng)給出,此次操作使用的多實(shí)例所以用到了--socket參數(shù)。

innobackupex --user=root --password=789/data/backups/full --slave-info --safe-slave-backup --parallel=4--safe-slave-backup-timeout=7200 --socket=/data/3306/mysql.sock

執(zhí)行結(jié)果一定要看到“160905 20:01:38 completedOK!" OK的字樣,才表示成功,從打出的日志我們也能看到備份的過程和上面給出的全備流程圖對比起來。

正確輸出以后會(huì)在/data/backups/full 指定的目錄中生成一個(gè)有時(shí)間和序號的目錄,這個(gè)目錄就是全備文件存放地方如:

[root@db01 ~]# ls -lrt /data/backups/full/
總用量 4
drwxr-x--- 8 root root 4096 9月   5 20:012016-09-05_20-01-32

1.7.2 增量備份

增量備份的前提是一定要有一個(gè)全備 ,只有有了全備才能進(jìn)行所謂的增量備份?。?!

增量備份的方式有兩種,分別為:

第一種:通過全備的文件

innobackupex --user=root --password=789 --incremental --incremental-lsn=1783249/data/backups/inc  --slave-info--safe-slave-backup --parallel=4 --safe-slave-backup-timeout=7200--socket=/data/3306/mysql.sock

第二種方法:通過LSN

innobackupex --user=root --password=789 --incremental --incremental-lsn=1783249/data/backups/inc  --slave-info--safe-slave-backup --parallel=4 --safe-slave-backup-timeout=7200--socket=/data/3306/mysql.sock

1、準(zhǔn)備好全備最新一次的全備文件,這里只做了一次,所以只有一個(gè)目錄

[root@db01 ~]# ls -lrt /data/backups/full/
總用量 4
drwxr-x--- 8 root root 4096 9月   5 20:01 2016-09-05_20-01-32

2、對xtr_test數(shù)據(jù)庫中的testbackup表,進(jìn)行數(shù)據(jù)的插入,插入內(nèi)容為“fist increment backup”

mysql> insert into testbackup(step)values('first increment backup');
Query OK, 1 row affected (0.02 sec)
 
mysql> select * from testbackup;
+----+------------------------+
| id | step                  |
+----+------------------------+
|  1 | firstfull backup      |
|  2 | firstincrement backup |
+----+------------------------+
2 rows in set (0.01 sec)

3、執(zhí)行增量備份

這里我們要確定好最新一次的全備(強(qiáng)調(diào))!?。?/p>

innobackupex --user=root --password=789--incremental  --incremental-basedir=/data/backups/full/
2016-09-05_20-01-32 
 /data/backups/inc --slave-info--safe-slave-backup --parallel=4 --safe-slave-backup-timeout=7200 --socket=/data/3306/mysql.sock

這里執(zhí)行結(jié)果一定要看到“completed OK!" OK的字樣,才表示成功如果是分布式也可以用--rsync,我們會(huì)在創(chuàng)建的inc目錄下發(fā)現(xiàn)創(chuàng)建了一個(gè)以時(shí)間和序號命名的目錄,這個(gè)目錄就是Xtrabackup備份的增量。

執(zhí)行結(jié)果:

[root@db01 ~]# ll /data/backups/inc
總用量 4
drwxr-x--- 8 root root 4096 9月   5 20:19 2016-09-05_20-19-48

TIP:innodb默認(rèn)是使用的系統(tǒng)表空間,我們可以通過配置參數(shù)來使idb文件到獨(dú)立空間中innodb_file_per_table = 1 這個(gè)是修改idb到獨(dú)立空間,如果數(shù)據(jù)庫是線上的,我們可以在配置文件my.cnf配置以上參數(shù)后,再使用set globalinnodb_file_per_table= 1 臨時(shí)生效。

alter table backupstep engine=innodb; 修改表的引擎,這個(gè)操作很費(fèi)時(shí)間,需要在業(yè)務(wù)低點(diǎn)的時(shí)候操作

1.7.3 再增量備份的基礎(chǔ)上再備份

在創(chuàng)建的xtr_test數(shù)據(jù)庫中的testbackup表中再次插入內(nèi)容“secondincrement backup”

mysql> insert into testbackup(step) values('secondincrement backup');
Query OK, 1 row affected (0.01 sec)
 
mysql> select * from testbackup;
+----+-------------------------+
| id | step                    |
+----+-------------------------+
|  1 | firstfull backup       |
|  2 | first incrementbackup  |
|  3 | secondincrement backup |
+----+-------------------------+
3 rows in set (0.00 sec)

1、確定最新的全備文件

[root@db01 full]# pwd
/data/backups/full
[root@db01 full]# ls -lrt
總用量 4
drwxr-x--- 8 root root 4096 9月   5 20:01 2016-09-05_20-01-32

   2、確定上一次增量備份的LSN值

[root@db01 inc]# ls -lrt
總用量 4
drwxr-x--- 8 root root 4096 9月   5 20:19 2016-09-05_20-19-48
[root@db01 inc]# cd 2016-09-05_20-19-48/
[root@db01 2016-09-05_20-19-48]# ls
backup-my.cnf krik    performance_schema      xtrabackup_checkpoints  xtr_test
ibdata1.delta mysql   test                    xtrabackup_info
ibdata1.meta  oldboy xtrabackup_binlog_info xtrabackup_logfile
[root@db01 2016-09-05_20-19-48]# cat xtrabackup_checkpoints 
backup_type = incremental
from_lsn = 1793716   #這個(gè)值一定是和上一次全備的last_lsn一樣,否則就是數(shù)據(jù)不全
to_lsn = 1794019
last_lsn = 1794019
compact = 0
recover_binlog_info = 0

4、
執(zhí)行增量備份命令

innobackupex  --user=root --password=789 --incremental--incremental-lsn=1794019 /data/backups/inc --slave-info --safe-slave-backup --parallel=4 --safe-slave-backup-timeout=7200--socket=/data/3306/mysql.sock

這個(gè)值是通過查看最近一次增量備份的xtrabackup_checkpoints得到

這里執(zhí)行結(jié)果一定要看到“completed OK!" OK的字樣,才表示成功

執(zhí)行結(jié)果會(huì)在指定的目錄/data/backups/inc下生成一個(gè)新的文件

[root@db01 2016-09-05_20-19-48]# cd ..
[root@db01 inc]# pwd
/data/backups/inc
[root@db01 inc]# ls -lrt
總用量 8
drwxr-x--- 8 root root 4096 9月   5 20:19 2016-09-05_20-19-48
drwxr-x--- 8 root root 4096 9月   5 21:18 2016-09-05_21-18-30

到此我們對數(shù)據(jù)進(jìn)行了完整的備份,下面會(huì)再創(chuàng)建一條語句,但是不會(huì)做增量備份,為了是下面測試二進(jìn)制日志恢復(fù)數(shù)據(jù)。接下來要玩大的了,刪庫,后面看看能恢復(fù)回來不

生產(chǎn)環(huán)境中為了數(shù)據(jù)一致性最好是ROW模式,因?yàn)槠渌哪J綌?shù)據(jù)會(huì)丟失

mysql> show variables like "log_bin";
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_bin      | ON    |
+---------------+-------+
1 row in set (0.00 sec)
 
mysql> show variables like"%binlog_format%";
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| binlog_format | ROW   |
+---------------+-------+
1 row in set (0.00 sec)
 
ot root   418 9月   4 15:34backup-my.cnf

第2章 數(shù)據(jù)庫恢復(fù)

Xtrabacup如何備份

Xtrabacup如何備份

Xtrabacup如何備份

這里只做redo,不做undo

2.1 恢復(fù)數(shù)據(jù)前準(zhǔn)備

數(shù)據(jù)庫的恢復(fù),是從全備--->1次增量--->2次增量......--->N次增量直到故障點(diǎn)的位置的順序來恢復(fù)的

如果是全備:最近一次的全備----> binlog

2.1.1 創(chuàng)建新的語句為二進(jìn)制日志恢復(fù)做準(zhǔn)備

mysql> insert into testbackup(step)values('second increment backup');
Query OK, 1 row affected (0.01 sec)
 
mysql> select * from testbackup;
+----+-------------------------+
| id | step                    |
+----+-------------------------+
|  1 | firstfull backup       |
|  2 | firstincrement backup  |
|  3 | secondincrement backup |
+----+-------------------------+
3 rows in set (0.00 sec)
 
mysql> insert into testbackup(step)values('last  backup');
Query OK, 1 row affected (0.00 sec)
 
mysql> select * from testbackup;
+----+-------------------------+
| id | step                    |
+----+-------------------------+
|  1 | firstfull backup       |
|  2 | firstincrement backup  |
|  3 | secondincrement backup |
|  4 |last  backup            |
+----+-------------------------+
4 rows in set (0.00 sec)
 
mysql> truncate table testbackup;
Query OK, 0 rows affected (0.02 sec)
 
mysql> select * from testbackup;
Empty set (0.00 sec)

2.2 恢復(fù)第一次的全備數(shù)據(jù)庫

恢復(fù)數(shù)據(jù)庫的時(shí)候一定會(huì)重啟一次數(shù)據(jù)庫的,所以我們可以選擇停庫操作(如果可以的情況下),下面以停庫操作練習(xí):

1、  停掉數(shù)據(jù)庫

[root@db01 inc]# /data/3306/mysql stop
Stoping MySQL...
[root@db01 inc]# netstat -lntup|grep 3306
[root@db01 inc]#

2、  恢復(fù)最近一次的全備,*****在操作前一定要先備份*****

[root@db01 backups]# for i in `ls/data/backups/`;do cp -r $i /data/backups/$i.bak;done
[root@db01 backups]# ls
full full.bak  inc  inc.bak
[root@db01 backups]# cp -r /data/3306/data/data/backups/3306_data.bak
[root@db01 backups]# ls
3306_data.bak full  full.bak  inc inc.bak

3、  導(dǎo)入全備數(shù)據(jù)

innobackupex --user=root --password=789 --apply-log--redo-only /data/backups/full/2016-09-05_20-01-32--socket=/data/3306/mysql.sock

要確認(rèn)看到OK字樣,表示導(dǎo)入成功

2.3 恢復(fù)第一次全備后的第一次增量備份

1、    確定第一次增量備份的文件目錄

[root@db01 inc]# ls -lrt
總用量 8
drwxr-x--- 8 root root 4096 9月   5 20:19 2016-09-05_20-19-48
drwxr-x--- 8 root root 4096 9月   5 21:18 2016-09-05_21-18-30

通過時(shí)間排序可以看到這里第一次是2016-09-05_20-19-48,工作中可以自定義一些名字

2、    執(zhí)行增量恢復(fù)

innobackupex --user=root --password=789 --apply-log--redo-only /data/backups/full/2016-09-05_20-01-32 --incremental-dir=/data/backups/inc/2016-09-05_20-19-48--socket=/data/3306/mysql.sock

 看到OK字樣表示成功

2.4 恢復(fù)第一次全備后的第二次增量備份

innobackupex --user=root --password=789 --apply-log--redo-only /data/backups/full/2016-09-05_20-01-32   --incremental-dir=/data/backups/inc/2016-09-05_21-18-30--socket=/data/3306/mysql.sock

到此Xtrabackup昨晚了redo操作,下面我們要把他寫入數(shù)據(jù)文件中。這一步才是把數(shù)據(jù)放到數(shù)據(jù)庫中

數(shù)據(jù)寫入數(shù)據(jù)文件:

innobackupex --user=root --password=789--apply-log /data/backups/full/2016-09-05_20-01-32  --socket=/data/3306/mysql.sock

同樣是看到OK字樣表示執(zhí)行成功。

2.5 恢復(fù)數(shù)據(jù)到實(shí)例3306的數(shù)據(jù)中

Xtrabackup恢復(fù)是把所有的增量都疊加到了全備的身上,也就是恢復(fù)用的全備。它就是數(shù)據(jù)庫data,我們把這個(gè)全備目錄替換了3306數(shù)據(jù)庫的data數(shù)據(jù)

[root@db01 full]# rm -fr /data/3306/data/
[root@db01 full]# mv 2016-09-05_20-01-32//data/3306/data
[root@db01 full]# ll /data/3306/data
總用量 163884
-rw-r----- 1 root root       418 9月   5 20:01 backup-my.cnf
-rw-r----- 1 root root 134217728 9月   5 22:12 ibdata1
-rw-r----- 1 root root   4194304 9月   5 22:12 ib_logfile0
-rw-r----- 1 root root   4194304 9月   5 22:12 ib_logfile1
-rw-r----- 1 root root   4194304 9月   5 22:12 ib_logfile2
-rw-r----- 1 root root  12582912 9月   5 22:12 ibtmp1
drwxr-x--- 2 root root      4096 9月   5 22:10 krik
drwxr-x--- 2 root root      4096 9月   5 22:10 mysql
drwxr-x--- 2 root root      4096 9月   5 22:10 oldboy
drwxr-x--- 2 root root      4096 9月   5 22:10 performance_schema
drwxr-x--- 2 root root      4096 9月   5 22:10 test
-rw-r----- 1 root root        22 9月   5 22:10 xtrabackup_binlog_info
-rw-r--r-- 1 root root        33 9月   5 22:12 xtrabackup_binlog_pos_innodb
-rw-r----- 1 root root       113 9月   5 22:12 xtrabackup_checkpoints
-rw-r----- 1 root root       630 9月   5 22:10 xtrabackup_info
-rw-r----- 1 root root   8388608 9月   5 22:04 xtrabackup_logfile
drwxr-x--- 2 root root      4096 9月   5 22:10 xtr_test
[root@db01 full]# chown -R mysql.mysql/data/3306/data/
[root@db01 full]# ll /data/3306/data
總用量 163884
-rw-r----- 1 mysql mysql       418 9月   5 20:01 backup-my.cnf
-rw-r----- 1 mysql mysql 134217728 9月   5 22:12 ibdata1
-rw-r----- 1 mysql mysql   4194304 9月   5 22:12 ib_logfile0
-rw-r----- 1 mysql mysql   4194304 9月   5 22:12 ib_logfile1
-rw-r----- 1 mysql mysql   4194304 9月   5 22:12 ib_logfile2
-rw-r----- 1 mysql mysql  12582912 9月   5 22:12 ibtmp1
drwxr-x--- 2 mysql mysql      4096 9月   5 22:10 krik
drwxr-x--- 2 mysql mysql      4096 9月   5 22:10 mysql
drwxr-x--- 2 mysql mysql      4096 9月   5 22:10 oldboy
drwxr-x--- 2 mysql mysql      4096 9月   5 22:10 performance_schema
drwxr-x--- 2 mysql mysql      4096 9月   5 22:10 test
-rw-r----- 1 mysql mysql        22 9月   5 22:10 xtrabackup_binlog_info
-rw-r--r-- 1 mysql mysql        33 9月   5 22:12 xtrabackup_binlog_pos_innodb
-rw-r----- 1 mysql mysql       113 9月   5 22:12 xtrabackup_checkpoints
-rw-r----- 1 mysql mysql       630 9月   5 22:10 xtrabackup_info
-rw-r----- 1 mysql mysql   8388608 9月   5 22:04 xtrabackup_logfile
drwxr-x--- 2 mysql mysql      4096 9月   5 22:10 xtr_test

重啟數(shù)據(jù)庫測試

[root@db01 full]# cd /data/3306
[root@db01 3306]# ./mysql start
Starting MySQL...
[root@db01 3306]# netstat -lntup|grep 3306
tcp       0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      56714/mysqld
mysql> select * from testbackup;
+----+-------------------------+
| id | step                    |
+----+-------------------------+
|  1 | firstfull backup       |
|  2 | firstincrement backup  |
|  3 | secondincrement backup |
+----+-------------------------+
3 rows in set (0.00 sec)
測試增量恢復(fù)正常,但是我們最后做的一次操作,沒有出現(xiàn),下面使用二進(jìn)制binglog來恢復(fù)

2.6 binlog日志恢復(fù)數(shù)據(jù)

使用binlog恢復(fù)數(shù)據(jù),我們需要來確定一個(gè)“范圍”這里我們需要確定從第二次增量備份后到執(zhí)行刪除表的sql語句之前這個(gè)范圍;查找范圍,我們可以使用時(shí)間范圍、位置點(diǎn)pos來取這個(gè)范圍。

2.6.1 查找第二次增量的位置點(diǎn)

[root@db01 2016-09-05_21-18-30]# catxtrabackup_binlog_info
mysql-bin.000010   1201

這里我們知道了第二次增量結(jié)束后的位置點(diǎn)事1201,我們將以這個(gè)位置點(diǎn)為起止位置點(diǎn)

2.6.2 查找刪表語句時(shí)候的位置點(diǎn)

由于測試binlog日志文件少,這里很好查找,生產(chǎn)中要根據(jù)時(shí)間點(diǎn)來截取一段時(shí)間范圍內(nèi),然后在去查找

[root@db01 3306]# mysqlbinlog--base64-output=decode-rows mysql-bin.000010
# at 1376
#160905 21:46:41 server id 1  end_log_pos 1403     Xid = 164
COMMIT/*!*/;
# at 1403
#160905 21:46:59 server id 1  end_log_pos 1495     Query    thread_id=7   exec_time=0   error_code=0
SET TIMESTAMP=1473083219/*!*/;
truncate table testbackup
/*!*/;
# at 1495
#160905 21:52:16 server id 1  end_log_pos 1514     Stop
DELIMITER ;
# End of log file
ROLLBACK /* added by mysqlbinlog */;
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;

通過查找到語句后,我們找到了位置點(diǎn),我們可以導(dǎo)出一個(gè)文件,通過vim編輯來搜索語句

2.6.3 binlog恢復(fù)數(shù)據(jù)

通過上面找出的起止點(diǎn)和終止點(diǎn)來恢復(fù)數(shù)據(jù)

mysqlbinlog --base64-output=decode-rows -v--start-position=1201 --stop-position=1403 mysql-bin.000010

來再次確認(rèn)

恢復(fù):

[root@db01 3306]# mysqlbinlog --start-position=1201--stop-position=1403 mysql-bin.000010|mysql -uroot -p789 -S/data/3306/mysql.sock

數(shù)據(jù)中查看:

mysql> select * from testbackup;
+----+-------------------------+
| id | step                    |
+----+-------------------------+
|  1 | firstfull backup       |
|  2 | firstincrement backup  |
|  3 | secondincrement backup |
|  4 |last  backup            |
+----+-------------------------+
4 rows in set (0.01 sec)

附錄---Xtrabackup參數(shù)詳解

Xtrabackup常用參數(shù)選項(xiàng)如下:

--slave-info:

   它會(huì)記錄master服務(wù)器的binary log的pos和name。會(huì)把記錄的信息記錄在xtrabackup_slave_info。在備份從庫時(shí),可以使用該參數(shù), 加上--slave-info備份目錄下會(huì)多生成一個(gè)xtrabackup_slave_info 文件, 這里會(huì)保存主日志文件以及偏移, 文件內(nèi)容類似于:CHANGE MASTER TOMASTER_LOG_FILE='', MASTER_LOG_POS=0。

    這個(gè)參數(shù)適用的場景:假設(shè)現(xiàn)在有主庫A和從庫B,目前想再添加一臺(tái)備庫C,并讓備庫C以主庫A為master;因?yàn)橹鲙霢是生產(chǎn)庫,壓力一般比較大,所以我們就在備庫B上備份一個(gè)數(shù)據(jù)庫,然后把這個(gè)備份拿到C服務(wù)器上 并導(dǎo)入到C庫,接下來再在C服務(wù)器上執(zhí)行change master的命令:其中 master_host是A的ip,而master_log_file和master_log_pos就是這個(gè)xtrabackup_slave_info里面的值

案例:http://blog.chinaunix.net/uid-26446098-id-3395111.html幫助理解

--safe-salve-backup:

   它會(huì)停止slave SQL 進(jìn)程,等備份完后,重新打開slave的SQL進(jìn)程

--force-tar --stream=tar /tmp

   這些命令是用來壓縮備份為tar文件。具體看官方文檔

--defaults-file=#

默認(rèn)配置文件的路徑,如果不該參數(shù),xtrabackup將從依次從以下位置查找配置文件/etc/my.cnf、/etc/mysql/my.cnf、/usr/local/etc/my.cnf、~/.my.cnf,并讀取配置文件中的[mysqld]和[xtrabackup]配置段。[mysqld]中只需要指定datadir、innodb_data_home_dir、innodb_data_file_path、innodb_log_group_home_dir、innodb_log_files_in_group、innodb_log_file_size6個(gè)參數(shù)即可讓xtrabackup正常工作。

--defaults-extra-file=#

如果使用了該參數(shù),在讀取了全局配置文件之后,會(huì)再讀取這里指定的配置文件

--target-dir=name

備份文件的存放目錄路徑

--backup

實(shí)施備份到target-dir

--prepare

實(shí)施對備份文件進(jìn)行恢復(fù)前的準(zhǔn)備(生成InnoDB log file)

--print-param

打印備份或恢復(fù)時(shí)需要的參數(shù)

--use-memory=#

該參數(shù)在 prepare 的時(shí)候使用,控制prepare時(shí)innodb實(shí)例使用的內(nèi)存量

--suspend-at-end

在target-dir目錄下產(chǎn)生一個(gè)xtrabackup_suspended文件,將xtrabackup進(jìn)程掛起,不停地將數(shù)據(jù)文件的變化同步到備份文件,直到用戶手工刪除xtrabackup_suspended文件

--throttle=#

每秒IO次數(shù),限制backup時(shí)使用的I/O操作量,使備份對數(shù)據(jù)庫正常業(yè)務(wù)的影響最小化

--log-stream

該參數(shù)在backup的時(shí)候使用,將xtrabackup_logfile的內(nèi)容輸出到標(biāo)準(zhǔn)輸出,使用該參數(shù)時(shí)會(huì)自動(dòng)使用suspend-at-end參數(shù),innobackupex腳本的stream 模式會(huì)使用該參數(shù)。

--incremental-lsn=name

增量備份時(shí)只拷貝LSN比該參數(shù)指定值新的ibd pages,前次備份到了哪個(gè)LSN可以看前次備份集的xtrabackup_checkpoints文件

--incremental-basedir=name

該參數(shù)在backup的時(shí)候使用,備份比該參數(shù)指定位置的備份集新的idb pages

--incremental-dir=name

該參數(shù)在prepare的時(shí)候使用,指定prepare時(shí)產(chǎn)生的.delta 文件和日志文件的存放路徑

--tables=name

在備份file-per-table類型的數(shù)據(jù)文件時(shí)使用,使用正則表達(dá)式指定需要備份的innodb表

--datadir=name

MySQL數(shù)據(jù)庫的數(shù)據(jù)文件目錄

關(guān)于“Xtrabacup如何備份”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請把它分享出去讓更多的人看到。

本文名稱:Xtrabacup如何備份
URL標(biāo)題:http://bm7419.com/article30/jdddso.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供小程序開發(fā)建站公司、網(wǎng)站制作、微信公眾號、企業(yè)建站面包屑導(dǎo)航

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)