溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

使用Xtrabackup進(jìn)行MySQL備份:

發(fā)布時(shí)間:2020-07-16 07:22:07 來源:網(wǎng)絡(luò) 閱讀:645 作者:孤鴻子 欄目:數(shù)據(jù)庫

使用Xtrabackup進(jìn)行MySQL備份:


一、安裝


1、簡介

Xtrabackup是由percona提供的mysql數(shù)據(jù)庫備份工具,據(jù)官方介紹,這也是世界上惟一一款開源的能夠?qū)nnodb和xtradb數(shù)據(jù)庫進(jìn)行熱備的工具。特點(diǎn):

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

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

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

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

(5)還原速度快;


2、安裝

其最新版的軟件可從 http://www.percona.com/software/percona-xtrabackup/ 獲得。本文基于CentOS 6.5 的系統(tǒng),因此,直接下載相應(yīng)版本的rpm包安裝即可,這里不再演示其過程。


[root@localhost ~]# wget https://www.percona.com/downloads/XtraBackup/Percona-XtraBackup-2.4.7/binary/redhat/6/x86_64/percona-xtrabackup-24-2.4.7-1.el6.x86_64.rpm


[root@localhost ~]# yum install  percona-xtrabackup-24-2.4.7-1.el6.x86_64.rpm 


Xtrabackup中主要包含兩個(gè)工具:

xtrabackup:是用于熱備份innodb, xtradb表中數(shù)據(jù)的工具,不能備份其他類型的表,也不能備份數(shù)據(jù)表結(jié)構(gòu);

innobackupex:是將xtrabackup進(jìn)行封裝的perl腳本,提供了備份myisam表的能力。


二、備份的實(shí)現(xiàn)


1、完全備份


# innobackupex --user=DBUSER --password=DBUSERPASS  /path/to/BACKUP-DIR/


1)、為備份操作創(chuàng)建MySQL賬戶:

MariaDB [(none)]> grant reload,lock tables,replication client,create tablespace,super,process  on *.* to 'backup'@'%' identified by 'pancou';

Query OK, 0 rows affected (0.00 sec)

        

MariaDB [(none)]> flush privileges;

Query OK, 0 rows affected (0.00 sec)



2)、創(chuàng)建備份文件目錄:


[root@localhost ~]# mkdir -p /hotbackup


對數(shù)據(jù)庫進(jìn)行完整備份,如果在從庫上進(jìn)行備份,請?zhí)砑?-slave_info –safe-slave-backup參數(shù)來獲取復(fù)制主庫的binlog信息。默認(rèn)備份會(huì)將當(dāng)前庫的binlog信息保存到xtrabackup_binlog_info文件中,而--slave_info參數(shù)將復(fù)制主庫的binlog信息保存到xtrabackup_slave_info中,如果使用備份來添加復(fù)制,請注意選取正確的binlog信息。


3)、創(chuàng)建innobackupex的配置文件:

[root@localhost ~]# vim /usr/local/etc/my.cnf

[mysqld]

datadir = "/data/mysql/"

innodb_data_home_dir = "/data/mysql/"

innodb_data_home_path = "ibdata1:10M:autoextend"

innodb_log_group_home_dir = "/data/mysql/"

innodb_log_file_in_group = 2

innodb_log_file_size = 1G


4)、進(jìn)行完整備份:


        [root@localhost ~]# innobackupex --user=backup --password=pancou  /hotbackup/full

170427 17:00:51 innobackupex: Starting the backup operation


IMPORTANT: Please check that the backup run completes successfully.

          At the end of a successful backup run innobackupex

          prints "completed OK!".


170427 17:00:52  version_check Connecting to MySQL server with DSN 'dbi:mysql:;mysql_read_default_group=xtrabackup' as 'backup'  (using password: YES).

170427 17:00:52  version_check Connected to MySQL server

170427 17:00:52  version_check Executing a version check against the server...

170427 17:00:52  version_check Done.

170427 17:00:52 Connecting to MySQL server host: localhost, user: backup, password: set, port: not set, socket: not set

Using server version 10.1.22-MariaDB

innobackupex version 2.4.7 based on MySQL server 5.7.13 Linux (x86_64) (revision id: 6f7a799)

xtrabackup: uses posix_fadvise().

xtrabackup: cd to /data/mysql

xtrabackup: open files limit requested 0, set to 60000

xtrabackup: using the following InnoDB configuration:

xtrabackup:   innodb_data_home_dir = .

xtrabackup:   innodb_data_file_path = ibdata1:12M:autoextend

xtrabackup:   innodb_log_group_home_dir = ./

        ......

        ....    .....

        170427 17:00:57 Executing UNLOCK TABLES

170427 17:00:57 All tables unlocked

170427 17:00:57 Backup created in directory '/hotbackup/full/2017-04-27_17-00-51/'

MySQL binlog position: filename 'mysql-bin.000007', position '941', GTID of the last change '0-1-270'

170427 17:00:57 [00] Writing backup-my.cnf

170427 17:00:57 [00]        ...done

170427 17:00:57 [00] Writing xtrabackup_info

170427 17:00:57 [00]        ...done

xtrabackup: Transaction log of lsn (21843393) to (21843393) was copied.

170427 17:00:57 completed OK!


        從信息中會(huì)發(fā)現(xiàn)備份被創(chuàng)建在'/hotbackup/full/2017-04-27_17-00-51/'


        [root@localhost ~]# innobackupex  --defaults-file=/etc/my.cnf --user=backup --password=pancou --socket=/var/lib/mysql/mysql.sock  /hotbackup/full

        

        其他選項(xiàng):

          --no-timestamp,指定了這個(gè)選項(xiàng)備份會(huì)直接備份在BACKUP-DIR,不再創(chuàng)建時(shí)間戳文件夾。

          --default-file,指定配置文件,用來配置innobackupex的選項(xiàng)。


        [root@localhost full]# innobackupex  --defaults-file=/etc/my.cnf --user=backup --password=pancou --socket=/var/lib/mysql/mysql.sock  /hotbackup/full --no-timestamp

        .....

        170427 17:14:43 Executing UNLOCK TABLES

170427 17:14:43 All tables unlocked

170427 17:14:43 Backup created in directory '/hotbackup/full/'

MySQL binlog position: filename 'mysql-bin.000007', position '941', GTID of the last change '0-1-270'

170427 17:14:43 [00] Writing backup-my.cnf

170427 17:14:43 [00]        ...done

170427 17:14:43 [00] Writing xtrabackup_info

170427 17:14:43 [00]        ...done

xtrabackup: Transaction log of lsn (21843393) to (21843393) was copied.

170427 17:14:43 completed OK!

        

    使用innobakupex備份時(shí),其會(huì)調(diào)用xtrabackup備份所有的InnoDB表,復(fù)制所有關(guān)于表結(jié)構(gòu)定義的相關(guān)文件(.frm)、以及MyISAM、MERGE、CSV和ARCHIVE表的相關(guān)文件,同時(shí)還會(huì)備份觸發(fā)器和數(shù)據(jù)庫配置信息相關(guān)的文件。這些文件會(huì)被保存至一個(gè)以時(shí)間命令的目錄中。


在備份的同時(shí),innobackupex還會(huì)在備份目錄中創(chuàng)建如下文件:

(1)xtrabackup_checkpoints —— 備份類型(如完全或增量)、備份狀態(tài)(如是否已經(jīng)為prepared狀態(tài))和LSN(日志序列號(hào))范圍信息;


每個(gè)InnoDB頁(通常為16k大小)都會(huì)包含一個(gè)日志序列號(hào),即LSN。LSN是整個(gè)數(shù)據(jù)庫系統(tǒng)的系統(tǒng)版本號(hào),每個(gè)頁面相關(guān)的LSN能夠表明此頁面最近是如何發(fā)生改變的。


(2)xtrabackup_binlog_info —— mysql服務(wù)器當(dāng)前正在使用的二進(jìn)制日志文件及至備份這一刻為止二進(jìn)制日志事件的位置。


(3)xtrabackup_binlog_pos_innodb —— 二進(jìn)制日志文件及用于InnoDB或XtraDB表的二進(jìn)制日志文件的當(dāng)前position。


(4)xtrabackup_binary —— 備份中用到的xtrabackup的可執(zhí)行文件;


(5)backup-my.cnf —— 備份命令用到的配置選項(xiàng)信息;


在使用innobackupex進(jìn)行備份時(shí),還可以使用--no-timestamp選項(xiàng)來阻止命令自動(dòng)創(chuàng)建一個(gè)以時(shí)間命名的目錄;如此一來,innobackupex命令將會(huì)創(chuàng)建一個(gè)BACKUP-DIR目錄來存儲(chǔ)備份數(shù)據(jù)。


     [root@localhost full]# ls /hotbackup/full/


backup-my.cnf  ib_logfile1  performance_schema  testdb                        xtrabackup_checkpoints

ibdata1        ibtmp1       sakila              xtrabackup_binlog_info        xtrabackup_info

ib_logfile0    mysql        test                xtrabackup_binlog_pos_innodb  xtrabackup_logfile


     MariaDB [testdb]> drop table emp;

     Query OK, 0 rows affected (0.10 sec)



2、恢復(fù)一個(gè)完全備份


   1)、準(zhǔn)備(prepare)一個(gè)完全備份


一般情況下,在備份完成后,數(shù)據(jù)尚且不能用于恢復(fù)操作,因?yàn)閭浞莸臄?shù)據(jù)中可能會(huì)包含尚未提交的事務(wù)或已經(jīng)提交但尚未同步至數(shù)據(jù)文件中的事務(wù)。因此,此時(shí)數(shù)據(jù)文件仍處理不一致狀態(tài)。“準(zhǔn)備”的主要作用正是通過回滾未提交的事務(wù)及同步已經(jīng)提交的事務(wù)至數(shù)據(jù)文件也使得數(shù)據(jù)文件處于一致性狀態(tài)。


innobakupex命令的--apply-log選項(xiàng)可用于實(shí)現(xiàn)上述功能。如下面的命令:


# innobackupex --apply-log  /path/to/BACKUP-DIR

如果執(zhí)行正確,其最后輸出的幾行信息通常如下:

xtrabackup: starting shutdown with innodb_fast_shutdown = 1

120407  9:01:36  InnoDB: Starting shutdown...

120407  9:01:40  InnoDB: Shutdown completed; log sequence number 92036620

120407 09:01:40  innobackupex: completed OK!


在實(shí)現(xiàn)“準(zhǔn)備”的過程中,innobackupex通常還可以使用--use-memory選項(xiàng)來指定其可以使用的內(nèi)存的大小,默認(rèn)通常為100M。如果有足夠的內(nèi)存可用,可以多劃分一些內(nèi)存給prepare的過程,以提高其完成速度。


[root@localhost full]# innobackupex --apply-log --use-memory=4G /hotbackup/full/

....    .......

InnoDB: 5.7.13 started; log sequence number 21843477

xtrabackup: starting shutdown with innodb_fast_shutdown = 1

InnoDB: FTS optimize thread exiting.

InnoDB: Starting shutdown...

InnoDB: Shutdown completed; log sequence number 21843496

170427 17:23:19 completed OK!


  2)、正式從一個(gè)完全備份中恢復(fù)數(shù)據(jù) 


     注意:恢復(fù)時(shí)要先關(guān)閉MySQL


innobackupex命令的--copy-back選項(xiàng)用于執(zhí)行恢復(fù)操作,其通過復(fù)制所有數(shù)據(jù)相關(guān)的文件至mysql服務(wù)器DATADIR目錄中來執(zhí)行恢復(fù)過程。innobackupex通過backup-my.cnf來獲取DATADIR目錄的相關(guān)信息。


# innobackupex --copy-back  /path/to/BACKUP-DIR

如果執(zhí)行正確,其輸出信息的最后幾行通常如下:

innobackupex: Starting to copy InnoDB log files

innobackupex: in '/backup/2012-04-07_08-17-03'

innobackupex: back to original InnoDB log directory '/mydata/data'

innobackupex: Finished copying back files.


120407 09:36:10  innobackupex: completed OK!


請確保如上信息的最行一行出現(xiàn)“innobackupex: completed OK!”。


當(dāng)數(shù)據(jù)恢復(fù)至DATADIR目錄以后,還需要確保所有數(shù)據(jù)文件的屬主和屬組均為正確的用戶,如mysql,否則,在啟動(dòng)mysqld之前還需要事先修改數(shù)據(jù)文件的屬主和屬組。如:


# chown -R  mysql:mysql  /mydata/data/


[root@localhost ~]# service mysql stop

Shutting down MySQL...                                     [  OK  ]

[root@localhost ~]# mv /data/mysql/ /opt/mysql_bak

[root@localhost ~]# ls /data/

[root@localhost ~]# mkdir /data/mysql

[root@localhost ~]# rm -rf /data/mysql/

[root@localhost ~]# ls /data/

[root@localhost ~]# innobackupex --defaults-file=/usr/local/etc/my.cnf --copy-back /hotbackup/full/

[root@localhost ~]# chown -R mysql:mysql /data/mysql/

[root@localhost ~]# service mysql start

Starting MySQL.170427 17:35:13 mysqld_safe Logging to '/data/mysql/localhost.err'.

170427 17:35:13 mysqld_safe Starting mysqld daemon with databases from /data/mysql

                                                          [  OK  ]

MariaDB [testdb]> show tables;

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

| Tables_in_testdb |

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

| emp              |

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

1 row in set (0.00 sec)



3、使用innobackupex進(jìn)行增量備份和恢復(fù)


每個(gè)InnoDB的頁面都會(huì)包含一個(gè)LSN信息,每當(dāng)相關(guān)的數(shù)據(jù)發(fā)生改變,相關(guān)的頁面的LSN就會(huì)自動(dòng)增長。這正是InnoDB表可以進(jìn)行增量備份的基礎(chǔ),即innobackupex通過備份上次完全備份之后發(fā)生改變的頁面來實(shí)現(xiàn)。


要實(shí)現(xiàn)第一次增量備份,可以使用下面的命令進(jìn)行:


# innobackupex --incremental /backup --incremental-basedir=BASEDIR


其中,BASEDIR指的是完全備份所在的目錄,此命令執(zhí)行結(jié)束后,innobackupex命令會(huì)在/backup目錄中創(chuàng)建一個(gè)新的以時(shí)間命名的目錄以存放所有的增量備份數(shù)據(jù)。另外,在執(zhí)行過增量備份之后再一次進(jìn)行增量備份時(shí),其--incremental-basedir應(yīng)該指向(上一次的增量備份)所在的目錄。


需要注意的是,增量備份僅能應(yīng)用于InnoDB或XtraDB表,對于MyISAM表而言,執(zhí)行增量備份時(shí)其實(shí)進(jìn)行的是完全備份。

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


   MariaDB [testdb]> insert into emp(name) values('tom'),('jerry');

Query OK, 2 rows affected (0.03 sec)

Records: 2  Duplicates: 0  Warnings: 0


MariaDB [testdb]> select * from emp;

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

| id   | name  |

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

|    1 | z1    |

|    2 | z2    |

| NULL | tom   |

| NULL | jerry |

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

4 rows in set (0.00 sec)

        

[root@localhost full]# innobackupex  --user=backup --password=pancou --incremental /hotbackup/increment-1 --incremental-basedir=/hotbackup/full/ --no-timestamp

        

        MariaDB [testdb]> create table test(id int(3),name char(6));

Query OK, 0 rows affected (0.22 sec)


MariaDB [testdb]> insert into test(name) values('lucy'),('jerry');

Query OK, 2 rows affected (0.04 sec)

Records: 2  Duplicates: 0  Warnings: 0

        

        MariaDB [testdb]> select * from test;

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

| id   | name  |

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

| NULL | lucy  |

| NULL | jerry |

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

2 rows in set (0.00 sec)

        

        [root@localhost full]# innobackupex  --user=backup --password=pancou --incremental /hotbackup/increment-2 --incremental-basedir=/hotbackup/increment-1 --no-timestamp


2)、恢復(fù)增量備份

         

         增量備份的恢復(fù)大體需要三個(gè)步驟:

         恢復(fù)基礎(chǔ)備份(全備)

         恢復(fù)增量備份到基礎(chǔ)備份(開始恢復(fù)的增量備份需要加--redo-only參數(shù),到最后一次增量備份去掉--redo-only參數(shù))

         對整體的基礎(chǔ)備份進(jìn)行恢復(fù),回滾那些未提交的數(shù)據(jù)


“準(zhǔn)備”(prepare)增量備份與整理完全備份有著一些不同,尤其要注意的是:

(1)需要在每個(gè)備份(包括完全和各個(gè)增量備份)上,將已經(jīng)提交的事務(wù)進(jìn)行“重放”?!爸胤拧敝?,所有的備份數(shù)據(jù)將合并到完全備份上。

(2)基于所有的備份將未提交的事務(wù)進(jìn)行“回滾”。


兩個(gè)參數(shù):

         --apply-log:表示只應(yīng)用XtraBackup日志中已提交的事務(wù)日志,回滾未提交的事務(wù)數(shù)據(jù)。 

         --redo-only:表示只應(yīng)用XtraBackup日志中已提交的事務(wù)日志,不回滾未提交的事務(wù)數(shù)據(jù)

于是,操作就變成了:

# innobackupex --apply-log --redo-only BASE-DIR


接著執(zhí)行:

# innobackupex --apply-log --redo-only BASE-DIR --incremental-dir=INCREMENTAL-DIR-1


而后是第二個(gè)增量:


# innobackupex --apply-log --redo-only BASE-DIR --incremental-dir=INCREMENTAL-DIR-2

        

        而后是第三個(gè)增量(注意如果是最后一個(gè)增量備份時(shí)去掉--redo-only參數(shù),回滾xtrabackup日志中那些還未提交的數(shù)據(jù)):


        # innobackupex --apply-log  BASE-DIR --incremental-dir=INCREMENTAL-DIR-3

        

        把所有合在一起的基礎(chǔ)備份整體進(jìn)行一次apply操作,回滾未提交的數(shù)據(jù):


        # innobackupex --apply-log --use-memory=4G BASE-DIR


其中BASE-DIR指的是完全備份所在的目錄,而INCREMENTAL-DIR-1指的是第一次增量備份的目錄,INCREMENTAL-DIR-2指的是第二次增量備份的目錄,其它依次類推,即如果有多次增量備份,每一次都要執(zhí)行如上操作;

        

        一個(gè)示例:


        全備:

        [root@localhost ~]# innobackupex  --defaults-file=/etc/my.cnf --user=backup --password=pancou --socket=/var/lib/mysql/mysql.sock  /hotbackup/full --no-timestamp

        ....        .....

        170428 12:58:31 Executing UNLOCK TABLES

170428 12:58:31 All tables unlocked

170428 12:58:31 Backup created in directory '/hotbackup/full/'

MySQL binlog position: filename 'mysql-bin.000001', position '1818', GTID of the last change '0-1-9'

170428 12:58:31 [00] Writing backup-my.cnf

170428 12:58:31 [00]        ...done

170428 12:58:31 [00] Writing xtrabackup_info

170428 12:58:31 [00]        ...done

xtrabackup: Transaction log of lsn (21885782) to (21885782) was copied.

170428 12:58:31 completed OK!



        第一次增量備份:

        MariaDB [testdb]> create table t3(id int(3),name char(8));

Query OK, 0 rows affected (0.21 sec)


MariaDB [testdb]> insert into t3 values(1,'tom'),(2,'jerry');

Query OK, 2 rows affected (0.05 sec)

Records: 2  Duplicates: 0  Warnings: 0


        

        MariaDB [testdb]> select * from t3;

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

| id   | name  |

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

|    1 | tom   |

|    2 | jerry |

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

2 rows in set (0.00 sec)



        [root@localhost ~]# innobackupex  --defaults-file=/etc/my.cnf --user=backup --password=pancou --socket=/var/lib/mysql/mysql.sock  --incremental-basedir=/hotbackup/full --incremental /hotbackup/increment-1 --no-timestamp

        ....      ....

        170428 13:02:52 Executing UNLOCK TABLES

170428 13:02:52 All tables unlocked

170428 13:02:52 Backup created in directory '/hotbackup/increment-1/'

MySQL binlog position: filename 'mysql-bin.000001', position '2304', GTID of the last change '0-1-12'

170428 13:02:52 [00] Writing backup-my.cnf

170428 13:02:52 [00]        ...done

170428 13:02:52 [00] Writing xtrabackup_info

170428 13:02:52 [00]        ...done

xtrabackup: Transaction log of lsn (21894630) to (21894630) was copied.

170428 13:02:52 completed OK!




        

        第二次增量備份:

        MariaDB [testdb]> insert into t3 values(3,'lucy'),(4,'linda');

Query OK, 2 rows affected (0.04 sec)

Records: 2  Duplicates: 0  Warnings: 0


MariaDB [testdb]> select * from t3;

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

| id   | name  |

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

|    1 | tom   |

|    2 | jerry |

|    3 | lucy  |

|    4 | linda |

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

4 rows in set (0.00 sec)



        [root@localhost ~]# innobackupex  --defaults-file=/etc/my.cnf --user=backup --password=pancou --socket=/var/lib/mysql/mysql.sock  --incremental-basedir=/hotbackup/increment-1 --incremental /hotbackup/increment-2 --no-timestamp


        ..... ......

        170428 13:04:52 Executing UNLOCK TABLES

170428 13:04:52 All tables unlocked

170428 13:04:52 Backup created in directory '/hotbackup/increment-2/'

MySQL binlog position: filename 'mysql-bin.000001', position '2477', GTID of the last change '0-1-13'

170428 13:04:52 [00] Writing backup-my.cnf

170428 13:04:52 [00]        ...done

170428 13:04:52 [00] Writing xtrabackup_info

170428 13:04:52 [00]        ...done

xtrabackup: Transaction log of lsn (21896433) to (21896433) was copied.

170428 13:04:52 completed OK!


              

        第三次增量備份:

        MariaDB [testdb]> insert into t3 values(5,'xiaofeng'),(6,'linchong');

Query OK, 2 rows affected (0.05 sec)

Records: 2  Duplicates: 0  Warnings: 0


MariaDB [testdb]> select * from t3;

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

| id   | name     |

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

|    1 | tom      |

|    2 | jerry    |

|    3 | lucy     |

|    4 | linda    |

|    5 | xiaofeng |

|    6 | linchong |

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

6 rows in set (0.00 sec)


        

        [root@localhost ~]# innobackupex  --defaults-file=/etc/my.cnf --user=backup --password=pancou --socket=/var/lib/mysql/mysql.sock  --incremental-basedir=/hotbackup/increment-2 --incremental /hotbackup/increment-3 --no-timestamp


        .... ....

        170428 13:09:24 Executing UNLOCK TABLES

170428 13:09:24 All tables unlocked

170428 13:09:24 Backup created in directory '/hotbackup/increment-3/'

MySQL binlog position: filename 'mysql-bin.000001', position '2657', GTID of the last change '0-1-14'

170428 13:09:24 [00] Writing backup-my.cnf

170428 13:09:24 [00]        ...done

170428 13:09:24 [00] Writing xtrabackup_info

170428 13:09:24 [00]        ...done

xtrabackup: Transaction log of lsn (21898226) to (21898226) was copied.

170428 13:09:24 completed OK!



        [root@localhost ~]# ls /hotbackup/

        full  increment-1  increment-2  increment-3


        預(yù)備恢復(fù):

        [root@localhost ~]# innobackupex --apply-log --redo-only --use-memory=4 /hotbackup/full/

       

xtrabackup: starting shutdown with innodb_fast_shutdown = 1

InnoDB: Starting shutdown...

InnoDB: Shutdown completed; log sequence number 21860430

InnoDB: Number of pools: 1

170428 12:38:26 completed OK!



        [root@localhost ~]# innobackupex --apply-log --redo-only --use-memory=4 /hotbackup/full/ --incremental-dir=/hotbackup/increment-1/


        170428 13:11:12 [01] Copying /hotbackup/increment-1/testdb/test.frm to ./testdb/test.frm

170428 13:11:12 [01]        ...done

170428 13:11:12 [00] Copying /hotbackup/increment-1//xtrabackup_binlog_info to ./xtrabackup_binlog_info

170428 13:11:12 [00]        ...done

170428 13:11:12 [00] Copying /hotbackup/increment-1//xtrabackup_info to ./xtrabackup_info

170428 13:11:12 [00]        ...done

170428 13:11:12 completed OK!



        [root@localhost ~]# innobackupex --apply-log --redo-only --use-memory=4 /hotbackup/full/ --incremental-dir=/hotbackup/increment-2/


        170428 13:11:22 [01] Copying /hotbackup/increment-2/testdb/db.opt to ./testdb/db.opt

170428 13:11:22 [01]        ...done

170428 13:11:22 [01] Copying /hotbackup/increment-2/testdb/t1.frm to ./testdb/t1.frm

170428 13:11:22 [01]        ...done

170428 13:11:22 [01] Copying /hotbackup/increment-2/testdb/test.frm to ./testdb/test.frm

170428 13:11:22 [01]        ...done

170428 13:11:22 [00] Copying /hotbackup/increment-2//xtrabackup_binlog_info to ./xtrabackup_binlog_info

170428 13:11:22 [00]        ...done

170428 13:11:22 [00] Copying /hotbackup/increment-2//xtrabackup_info to ./xtrabackup_info

170428 13:11:22 [00]        ...done

170428 13:11:22 completed OK!


        [root@localhost ~]# innobackupex --apply-log --use-memory=4 /hotbackup/full/ --incremental-dir=/hotbackup/increment-3/

        .... ....


        InnoDB: Setting file './ibtmp1' size to 12 MB. Physically writing the file full; Please wait ...

InnoDB: File './ibtmp1' size is now 12 MB.

InnoDB: 96 redo rollback segment(s) found. 1 redo rollback segment(s) are active.

InnoDB: 32 non-redo rollback segment(s) are active.

InnoDB: 5.7.13 started; log sequence number 21898773

xtrabackup: starting shutdown with innodb_fast_shutdown = 1

InnoDB: FTS optimize thread exiting.

InnoDB: Starting shutdown...

InnoDB: Shutdown completed; log sequence number 21898885

170428 13:13:23 completed OK!


[root@localhost ~]# innobackupex --apply-log  --use-memory=4 /hotbackup/full/ 

        

        InnoDB: 96 redo rollback segment(s) found. 1 redo rollback segment(s) are active.

InnoDB: 32 non-redo rollback segment(s) are active.

InnoDB: 5.7.13 started; log sequence number 21898904

xtrabackup: starting shutdown with innodb_fast_shutdown = 1

InnoDB: FTS optimize thread exiting.

InnoDB: Starting shutdown...

InnoDB: Shutdown completed; log sequence number 21898923

170428 13:15:33 completed OK!



        [root@localhost ~]# service mysql stop

Shutting down MySQL..                                      [  OK  ]

[root@localhost ~]# mv /data/mysql/ /tmp/data_bak

[root@localhost ~]# innobackupex --defaults-file=/usr/local/etc/my.cnf --copy-back /hotbackup/full/

..... ......

170428 13:16:55 [01] Copying ./testdb/test.ibd to /data/mysql/testdb/test.ibd

170428 13:16:55 [01]        ...done

170428 13:16:55 [01] Copying ./testdb/t1.ibd to /data/mysql/testdb/t1.ibd

170428 13:16:55 [01]        ...done

170428 13:16:55 [01] Copying ./testdb/t2.ibd to /data/mysql/testdb/t2.ibd

170428 13:16:55 [01]        ...done

170428 13:16:55 completed OK!


        [root@localhost ~]# chown -R mysql:mysql /data/mysql/

[root@localhost ~]# service mysql start

Starting MySQL.170428 12:21:59 mysqld_safe Logging to '/data/mysql/localhost.err'.

170428 12:21:59 mysqld_safe Starting mysqld daemon with databases from /data/mysql

                                                          [  OK  ]

        MariaDB [testdb]> select * from t3;

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

| id   | name     |

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

|    1 | tom      |

|    2 | jerry    |

|    3 | lucy     |

|    4 | linda    |

|    5 | xiaofeng |

|    6 | linchong |

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

6 rows in set (0.00 sec)

    

4、不完全恢復(fù)


        找到記錄備份結(jié)束時(shí)刻的binlog的位置文件xtrabackup_binlog_info,查看結(jié)束時(shí)刻binglogd 名稱和位置:


        [root@localhost ~]# cat /hotbackup/increment-3/xtrabackup_binlog_info 

mysql-bin.000001 2657 0-1-14


        查看當(dāng)前數(shù)據(jù)庫的binglog文件和位置:


        MariaDB [(none)]> show master logs;

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

| Log_name         | File_size |

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

| mysql-bin.000001 |       313 |

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

1 row in set (0.00 sec)

        

        從全備中恢復(fù)數(shù)據(jù)庫,恢復(fù)全備,之后再從熱備結(jié)束時(shí)刻binlog的位置開始,恢復(fù)到誤操作時(shí)刻14:00之前的binlog:

        mysqlbinlog --start-position='2657' --stop-datetime='2017-05-10 13:59:59' /data/mysql/mysql-bin.000001 | mysql -uroot -ppancou


        跳過故障點(diǎn)的誤操作時(shí)間點(diǎn),應(yīng)用之后的日志:

        mysqlbinlog  --stop-datetime='2016-05-10 14:01:00' /data/mysql/mysql-bin.000001 | mysql -uroot -ppancou


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI