xtrabackup備份、還原

一、xtrabackup簡介

前面介紹mysqldump備份方式是採用邏輯備份,其最大的缺陷就是備份和恢復速度都慢,對於一個小於50G的數據庫而言,這個速度還是能接受的,但如果數據庫非常大,那再使用mysqldump備份就不太適合了。

這時就需要一種好用又高效的工具,xtrabackup就是其中一款,號稱免費版的InnoDB HotBackup。

Xtrabackup實現是物理備份,而且是物理熱備

目前主流的有兩個工具可以實現物理熱備:ibbackup和xtrabackup;ibbackup是商業軟件,需要授權,非常昂貴。而xtrabackup功能比ibbackup還要強大,但卻是開源的。因此我們這裏就來介紹xtrabackup的使用。

Xtrabackup提供了兩種命令行工具:

xtrabackup:專用於備份InnoDB和XtraDB引擎的數據;

innobackupex:這是一個perl腳本,在執行過程中會調用xtrabackup命令,這樣用該命令即可以實現備份InnoDB,也可以備份MyISAM引擎的對象。

Xtrabackup是由percona提供的mysql數據庫備份工具,特點:

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

(2)備份過程不會打斷正在執行的事務;

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

(4)自動實現備份檢驗;

(5)還原速度快。

官方鏈接地址:http://www.percona.com/software/percona-xtrabackup;可以下載源碼編譯安裝,也可以下載適合的RPM包或使用yum進行安裝或者下載二進制源碼包。

 

二、xtrabackup安裝

[root@localhost ~]# tar xf percona-xtrabackup-2.4.4-Linux-x86_64.tar.gz

[root@localhost ~]# cd percona-xtrabackup-2.4.4-Linux-x86_64

[[email protected]_64]# cp bin/*/usr/bin/

[root@localhost percona-xtrabackup-2.4.4-Linux-x86_64]# yum -y install perl-DBI perl-DBD-MySQL perl-Time-HiResperl-IO-Socket-SSL perl-TermReadKey.x86_64 perl-Digest-MD5

[root@localhost ~]# rpm -ivh percona-toolkit-2.2.19-1.noarch.rpm

warning: percona-toolkit-2.2.19-1.noarch.rpm:Header V4 DSA/SHA1 Signature, key ID cd2efd2a: NOKEY

Preparing...               ########################################### [100%]

   1:percona-toolkit       ########################################### [100%]

三、源碼編譯數據庫

[roo[root@localhost ~]# yum -y install ncurses-devel

[root@localhost ~]# tar xf cmake-2.8.6.tar.gz

[root@localhost ~]# cd cmake-2.8.6

[[email protected]]# ./configure && gmake&& gmake install

[[email protected]]# cd

[root@localhost ~]# groupadd mysql

[root@localhost ~]# useradd -M -s /sbin/nologin mysql -g mysql

[root@localhost ~]# tar xf mysql-5.5.22.tar.gz -C /usr/src/

[root@localhost ~]# cd /usr/src/mysql-5.5.22/

[[email protected]]# cmake-DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DSYSCONFDIR=/etc-DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci-DWITH_EXTRA_CHARSETS=all && make && make instal

[[email protected]]# chown -R mysql:mysql/usr/local/mysql

[[email protected]]# rm -rf /etc/my.cnf

[[email protected]]# cp support-files/my-medium.cnf/etc/my.cnf

[[email protected]]# /usr/local/mysql/scripts/mysql_install_db--user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data

[[email protected]]# echo "PATH=$PATH:/usr/local/mysql/bin">> /etc/profile

[[email protected]]# . /etc/profile

[[email protected]]# cp/usr/src/mysql-5.5.22/support-files/mysql.server /etc/rc.d/init.d/mysqld

[[email protected]]# chmod +x /etc/rc.d/init.d/mysqld

[[email protected]]# chkconfig --add mysqld

[[email protected]]# vim /etc/my.cnf

27 datadir=/usr/local/mysql/data

[[email protected]]# servicemysqld start

Starting MySQL...                                          [確定]

[[email protected]]# mysqladmin-uroot password '123456'

四、xtrabackup完全備份+binlog增量備份

1、完全備份

基本語法:innobackupex --user=DBUSER--password=DBUSERPASS /path/to/BACKUP

:--defaults-file=/etc/my.cnf 指定mysql的配置文件my.cfg,如果指定則必須是第一個參數。

/path/to/BACKUP-DIR/指定備份所存放的目標目錄,備份過程會創建一個以當時備份時間命名的目錄存放備份文件。

創建備份目錄,full爲完全備份目錄,inc爲增量備份目錄

[root@localhost ~]# mkdir -p/opt/mysqlbackup/{full,inc}

[root@localhost ~]# innobackupex --user=root--password=123456 /opt/mysqlbackup/full/

MySQL binlog position:filename 'mysql-bin.000004', position '107'

161203 21:03:23 [00]Writing backup-my.cnf

161203 21:03:23 [00]        ...done

161203 21:03:23 [00]Writing xtrabackup_info

161203 21:03:23 [00]        ...done

xtrabackup: Transactionlog of lsn (1595675) to (1595675) was copied.

161203 21:03:24 completedOK!

[root@localhost ~]# ls /opt/mysqlbackup/full

2016-12-03_21-03-06

[root@localhost ~]# ls /opt/mysqlbackup/full/2016-12-03_21-03-06/

backup-my.cnf  mysql               xtrabackup_binlog_info  xtrabackup_info

ibdata1        performance_schema  xtrabackup_checkpoints  xtrabackup_logfile

各文件說明:

(1)xtrabackup_checkpoints—— 備份類型(如完全或增量)、備份狀態(如是否已經爲prepared狀態)和LSN(日誌序列號)範圍信息;

每個InnoDB頁(通常爲16k大小)都會包含一個日誌序列號,即LSN。LSN是整個數據庫系統的系統版本號,每個頁面相關的LSN能夠表明此頁面最近是如何發生改變的。

(2)xtrabackup_binlog_info—— mysql服務器當前正在使用的二進制日誌文件及至備份這一刻爲止二進制日誌事件的位置。

(3)xtrabackup_binlog_pos_innodb—— 二進制日誌文件及用於InnoDB或XtraDB表的二進制日誌文件的當前position。

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

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

在使用innobackupex進行備份時,還可以使用--no-timestamp選項來阻止命令自動創建一個以時間命名的目錄;如此一來,innobackupex命令將會創建一個BACKUP-DIR目錄來存儲備份數據

注意:相關選項說明:

其中,--user指定連接數據庫的用戶名,--password指定連接數據庫的密碼,--defaults-file指定數據庫的配置文件,innobackupex要從其中獲取datadir等信息;--database指定要備份的數據庫,這裏指定的數據庫只對MyISAM表有效,對於InnoDB 數據來說都是全備(所有數據庫中的InnoDB數據都進行了備份,不是隻備份指定的數據庫,恢復時也一樣);/opt/mysqlbackup/full是備份文件的存放位置。

注意:備份數據庫的用戶需要具有相應權限,如果要使用一個最小權限的用戶進行備份,則可基於如下命令創建此類用戶:

[root@localhost ~]# mysql -uroot -p

Enter password:

Welcome to the MySQLmonitor.  Commands end with ; or \g.

Your MySQL connection idis 3

Server version: 5.5.22-logSource distribution

 

Copyright (c) 2000, 2011,Oracle and/or its affiliates. All rights reserved.

 

Oracle is a registeredtrademark of Oracle Corporation and/or its

affiliates. Other namesmay be trademarks of their respective

owners.

 

Type 'help;' or '\h' forhelp. Type '\c' to clear the current input statement.

 

mysql> create user 'bkpuser'@'localhost'identified by '123456';

Query OK, 0 rows affected(0.06 sec)

 

mysql> revoke all privileges,grantoption from 'bkpuser'@'localhost';

Query OK, 0 rows affected(0.00 sec)

 

mysql>  grant reload,lock tables,replication client,process on *.* to 'bkpuser'@'localhost';

Query OK, 0 rows affected(0.00 sec)

 

mysql> flush privileges;

Query OK, 0 rows affected(0.05 sec)

 

2、增量備份

向mysql添加一個benet庫,然後插入數據進行增量備份,對完全備份後的數據庫更改進行二進制日誌備份。

查看日誌位置

[root@localhost ~]# cat/opt/mysqlbackup/full/2016-12-03_21-03-06/xtrabackup_binlog_info

mysql-bin.000004   107

[root@localhost ~]# mysql -uroot -p

Enter password:

Welcome to the MySQLmonitor.  Commands end with ; or \g.

Your MySQL connection idis 4

Server version: 5.5.22-logSource distribution

 

Copyright (c) 2000, 2011,Oracle and/or its affiliates. All rights reserved.

 

Oracle is a registeredtrademark of Oracle Corporation and/or its

affiliates. Other namesmay be trademarks of their respective

owners.

 

Type 'help;' or '\h' forhelp. Type '\c' to clear the current input statement.

 

mysql> create database benet;

Query OK, 1 row affected(0.00 sec)

 

mysql> use benet

Database changed

mysql> create table tb1 (id int,namechar(16) not null);

Query OK, 0 rows affected(0.05 sec)

 

mysql> insert into tb1values(1,'zhangsan');

Query OK, 1 row affected(0.02 sec)

 

mysql> insert into tb1 values(2,'lisi');

Query OK, 1 row affected(0.02 sec)

 

mysql> select * fromtb1;

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

| id   | name    |

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

|    1 | zhangsan |

|    2 | lisi    |

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

2 rows in set (0.00 sec)

mysql> quit

Bye

 

增量備份二進制文件

[root@localhost ~]# mysqlbinlog --start-position=107 /usr/local/mysql/data/mysql-bin.000004> /opt/mysqlbackup/inc/`date +%F`.sql

[root@localhost ~]# ls/opt/mysqlbackup/inc/

2016-12-03.sql

 

模擬數據庫損壞

[root@localhost ~]# mv /usr/local/mysql/data/*  /tmp/

一般情況下,在備份完成後,數據尚且不能用於恢復操作,因爲備份的數據中可能會包含尚未提交的事務或已經提交但尚未同步至數據文件中的事務。因此,此時數據文件仍處於不一致狀態。“準備”的主要作用正是通過回滾未提交的事務及同步已經提交的事務至數據文件也使得數據文件處於一致性狀態。

在準備(prepare)過程結束後,InnoDB表數據已經前滾到整個備份結束的點,而不是回滾到xtrabackup剛開始時的點。

innobakupex命令的--apply-log選項可用於實現上述功能。如下面的命令:

[root@localhost ~]# innobackupex --apply-log/opt/mysqlbackup/full/2016-12-03_21-03-06/

.........

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

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

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

InnoDB: 5.7.13 started;log sequence number 1595925

xtrabackup: startingshutdown with innodb_fast_shutdown = 1

InnoDB: FTS optimizethread exiting.

InnoDB: Startingshutdown...

InnoDB: Shutdowncompleted; log sequence number 1595944

161203 21:33:32 completedOK!

[root@localhost ~]# innobackupex --copy-back/opt/mysqlbackup/full/2016-12-03_21-03-06/

...........

161203 21:36:07 [01]        ...done

161203 21:36:07 [01]Copying ./performance_schema/events_waits_current.frm to/usr/local/mysql/data/performance_schema/events_waits_current.frm

161203 21:36:07 [01]        ...done

161203 21:36:07 completedOK!

[root@localhost ~]# ll /usr/local/mysql/data/

總用量 40972

-rw-r-----. 1 root root18874368 12  3 21:36 ibdata1

-rw-r-----. 1 rootroot  5242880 12  3 21:36 ib_logfile0

-rw-r-----. 1 rootroot  5242880 12  3 21:36 ib_logfile1

-rw-r-----. 1 root root12582912 12  3 21:36 ibtmp1

drwxr-x---. 2 rootroot     4096 12  3 21:36 mysql

drwxr-x---. 2 rootroot     4096 12  3 21:36 performance_schema

-rw-r-----. 1 rootroot      478 12  3 21:36 xtrabackup_info

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

[root@localhost ~]# killall mysqld

[root@localhost ~]# service mysqld start

Starting MySQL..                                           [確定]

[root@localhost ~]# mysql -uroot -p

Enter password:

Welcome to the MySQLmonitor.  Commands end with ; or \g.

Your MySQL connection idis 1

Server version: 5.5.22-logSource distribution

 

Copyright (c) 2000, 2011,Oracle and/or its affiliates. All rights reserved.

 

Oracle is a registeredtrademark of Oracle Corporation and/or its

affiliates. Other namesmay be trademarks of their respective

owners.

 

Type 'help;' or '\h' forhelp. Type '\c' to clear the current input statement.

 

mysql> show databases;

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

| Database           |

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

| information_schema |

| mysql              |

| performance_schema |

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

3 rows in set (0.01 sec)

我們可以看到在完全備份後創建的數據並沒有恢復,這時就需要用增量備份來還原

爲了避免產生大量的二進制日誌,可以把二進制日誌關掉

mysql> set sql_log_bin=0;

Query OK, 0 rows affected(0.00 sec)

 

mysql> source/opt/mysqlbackup/inc/2016-12-03.sql

Query OK, 0 rows affected(0.00 sec)

 

Query OK, 0 rows affected(0.00 sec)

 

Query OK, 0 rows affected(0.00 sec)

 

Query OK, 0 rows affected (0.00sec)

mysql> set sql_log_bin=1;

Query OK, 0 rows affected(0.00 sec)

mysql> show databases;

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

| Database           |

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

| information_schema |

| benet              |

| mysql              |

| performance_schema |

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

4 rows in set (0.00 sec)

 

mysql> use benet

Database changed

mysql> select * from tb1;

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

| id   | name    |

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

|    1 | zhangsan |

|    2 | lisi    |

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

2 rows in set (0.01 sec)

 

五、xtrabackup完全備份+xtrabacup增量備份

前面我們進行增量備份時,使用的還是老方法:備份二進制日誌。其實xtrabackup還支持進行增量備份。

先介紹下xtrabackup的備份原理

在InnoDB內部會維護一個redo日誌文件,我們也可以叫做事務日誌文件(transaction log,事務日誌)。事務日誌會存儲每一個InnoDB表數據的記錄修改。當InnoDB啓動時,InnoDB會檢查數據文件和事務日誌,並執行兩個步驟:它應用已經提交的事務日誌到數據文件,並將修改過但沒有提交的數據進行回滾操作。

xtrabackup在啓動時會記住log sequence number(LSN),並且複製所有的數據文件。複製過程需要一些時間,所以這期間如果數據文件有改動,那麼將會使數據庫處於一個不同的時間點。這時,xtrabackup會運行一個後臺進程,用於監視事務日誌,並從事務日誌複製最新的修改。xtrabackup必須持續的做這個操作,是因爲事務日誌是會輪轉重複的寫入,並且事務日誌可以被重用。所以xtrabackup自啓動開始,就不停的將事務日誌中每個數據文件的修改都記錄下來。這就是xtrabackup的備份過程

所以每個InnoDB的頁面都會包含一個LSN信息,每當相關的數據發生改變,相關的頁面的LSN就會自動增長。這正是InnoDB表可以進行增量備份的基礎。

xtraBackup基於InnoDB的crash-recovery功能。它會複製innodb的data file,由於不鎖表,複製出來的數據是不一致的,在恢復的時候使用crash-recovery,使得數據恢復一致。

當InnoDB啓動的時候,它會先去檢查data file和transaction log,並且會做二步操作:

1.It applies committed transaction logentries to the data files

2.it performs an undo operation on anytransactions that modifieddata but did not commit.

所以在prepare過程中,XtraBackup使用複製到的transactions log對備份出來的innodb data file進行crash recovery。

測試環境準備

mysql> create database test;

Query OK, 1 row affected(0.00 sec)

 

mysql> use test;

Database changed

mysql> create table xx(id int,namevarchar(20));

Query OK, 0 rows affected(0.04 sec)

 

mysql> insert into xx values(1,'tom1');

Query OK, 1 row affected(0.02 sec)

 

mysql> insert into xx values(2,'tom2');

Query OK, 1 row affected(0.03 sec)

mysql> quit

Bye

 

執行完全備份

[root@localhost ~]# xtrabackup--defaults-file=/etc/my.cnf --user=root --password='123456' --port=3306--backup --target-dir=/opt/mysqlbackup/full/full_incre_$(date +%Y%m%d_%H%M%s)

..........

161203 22:01:55 [00]Writing backup-my.cnf

161203 22:01:55 [00]        ...done

161203 22:01:55 [00]Writing xtrabackup_info

161203 22:01:55 [00]        ...done

xtrabackup: Transactionlog of lsn (1603986) to (1603986) was copied.

161203 22:01:55 completedOK!

[root@localhost ~]# ls -l /opt/mysqlbackup/full/

總用量 8

drwxr-x---. 4 root root4096 12  3 21:33 2016-12-03_21-03-06

drwxr-x---. 6 root root4096 12  3 22:01 full_incre_20161203_22011480773711

 

繼續往數據庫增加數據,進行第一次增量備份

[root@localhost ~]# mysql -uroot -p

Enter password:

Welcome to the MySQLmonitor.  Commands end with ; or \g.

Your MySQL connection idis 4

Server version: 5.5.22-logSource distribution

 

Copyright (c) 2000, 2011,Oracle and/or its affiliates. All rights reserved.

 

Oracle is a registeredtrademark of Oracle Corporation and/or its

affiliates. Other namesmay be trademarks of their respective

owners.

 

Type 'help;' or '\h' forhelp. Type '\c' to clear the current input statement.

 

mysql> use test

Database changed

mysql> insert into xx values(3,'tom3');

Query OK, 1 row affected(0.03 sec)

mysql> quit

Bye

[root@localhost ~]# xtrabackup--dafaults-file=/etc/my.cnf --user=root --password="123456" --port=3306--backup --target-dir=/opt/mysqlbackup/inc/incre_$(date +%Y%m%d_%H%M%S)--incremental-basedir=/opt/mysqlbackup/full/full_incre_20161203_22011480773711/

........

161203 22:09:54 [00]Writing xtrabackup_info

161203 22:09:54 [00]        ...done

xtrabackup: Transactionlog of lsn (1605014) to (1605014) was copied.

161203 22:09:54 completedOK!

[root@localhost ~]# ls -l /opt/mysqlbackup/inc/

總用量 8

-rw-r--r--. 1 root root3041 12  3 21:24 2016-12-03.sql

drwxr-x---. 6 root root4096 12  3 22:09 incre_20161203_220950

 

注意:這裏的增量備份只針對Innodb,而MyISAM仍然是完整備份

向表中再添入數據,進行第二次增量備份

[root@localhost ~]# mysql -uroot -p

Enter password:

Welcome to the MySQLmonitor.  Commands end with ; or \g.

Your MySQL connection idis 7

Server version: 5.5.22-logSource distribution

 

Copyright (c) 2000, 2011,Oracle and/or its affiliates. All rights reserved.

 

Oracle is a registeredtrademark of Oracle Corporation and/or its

affiliates. Other namesmay be trademarks of their respective

owners.

 

Type 'help;' or '\h' forhelp. Type '\c' to clear the current input statement.

 

mysql> use test

Database changed

mysql> insert into xx values(4,'tom4');

Query OK, 1 row affected(0.03 sec)

 

mysql> quit

Bye

[root@localhost ~]# xtrabackup--defaults-file=/etc/my.cnf --user=root --password="123456"--port=3306 --backup --target-dir=/opt/mysqlbackup/inc/incre_$(date+%Y%m%d_%H%M%S)--incremental-basedir=/opt/mysqlbackup/inc/incre_20161203_220950/

.........

161203 22:18:34 [00]        ...done

161203 22:18:34 [00]Writing xtrabackup_info

161203 22:18:34 [00]        ...done

xtrabackup: Transactionlog of lsn (1606042) to (1606042) was copied.

161203 22:18:34 completedOK!

注意:第二次增量備份--incremental--basedir指的是向上一次增量備份文件的位置

 

[root@localhost ~]# ls -l /opt/mysqlbackup/inc/

總用量 12

-rw-r--r--. 1 root root3041 12  3 21:24 2016-12-03.sql

drwxr-x---. 6 root root4096 12  3 22:09 incre_20161203_220950

drwxr-x---. 6 root root4096 12  3 22:18 incre_20161203_221830

 

Xtrabackup進行增量恢復

爲了驗證對比,先刪除兩個增量備份前表裏面的數據

[root@localhost ~]# mysql -uroot -p

Enter password:

Welcome to the MySQLmonitor.  Commands end with ; or \g.

Your MySQL connection idis 10

Server version: 5.5.22-logSource distribution

 

Copyright (c) 2000, 2011,Oracle and/or its affiliates. All rights reserved.

 

Oracle is a registered trademarkof Oracle Corporation and/or its

affiliates. Other namesmay be trademarks of their respective

owners.

 

Type 'help;' or '\h' forhelp. Type '\c' to clear the current input statement.

 

mysql> use test;

Database changed

mysql> delete from xx where id=3;

Query OK, 1 row affected(0.03 sec)

mysql> quit

Bye

[root@localhost ~]# xtrabackup--defaults-file=/etc/my.cnf --prepare --user=root --password="123456"--apply-log-only--target-dir=/opt/mysqlbackup/full/full_incre_20161203_22011480773711/

[root@localhost ~]# xtrabackup--defaults-file=/etc/my.cnf --prepare --user=root --password="123456"--apply-log-only--target-dir=/opt/mysqlbackup/full/full_incre_20161203_22011480773711/--incremental-dir=/opt/mysqlbackup/inc/incre_20161203_220950/

[root@localhost ~]# xtrabackup--defaults-file=/etc/my.cnf --prepare --user=root --password="123456"--apply-log-only--target-dir=/opt/mysqlbackup/full/full_incre_20161203_22011480773711/--incremental-dir=/opt/mysqlbackup/inc/incre_20161203_221830/

[root@localhost ~]# xtrabackup--defaults-file=/etc/my.cnf --prepare --user=root --password="123456"--target-dir=/opt/mysqlbackup/full/full_incre_20161203_22011480773711/

[root@localhost ~]# service mysqld stop

Shutting down MySQL..                                      [確定]

[root@localhost ~]# cd/opt/mysqlbackup/full/full_incre_20161203_22011480773711/

[root@localhostfull_incre_20161203_22011480773711]# rsync -rvt --exclude 'xtrabackup_checkpoints' --exclude'xtrabackup_logfile' ./ /usr/local/mysql/data/

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

[root@localhost ~]# service mysqld start

Starting MySQL...                                          [確定]

[root@localhost ~]# mysql -uroot -p123456 -e"select * from test.xx"

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

| id   | name |

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

|    1 | tom1 |

|    2 | tom2 |

|    3 | tom3 |

|    4 | tom4 |

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

 

六、innobackupex全庫備份+innobackupex增量備份

測試環境準備

[root@localhost ~]# mysql -uroot -p

Enter password:

Welcome to the MySQLmonitor.  Commands end with ; or \g.

Your MySQL connection idis 3

Server version: 5.5.22-logSource distribution

 

Copyright (c) 2000, 2011,Oracle and/or its affiliates. All rights reserved.

 

Oracle is a registeredtrademark of Oracle Corporation and/or its

affiliates. Other namesmay be trademarks of their respective

owners.

 

Type 'help;' or '\h' forhelp. Type '\c' to clear the current input statement.

 

mysql> create database test2;

Query OK, 1 row affected(0.01 sec)

 

mysql> use test2

Database changed

mysql> create table yy(id int,namevarchar(20));

Query OK, 0 rows affected(0.05 sec)

 

mysql> insert into yy values(1,'kim1');

Query OK, 1 row affected(0.03 sec)

 

mysql> insert into yy values(2,'kim2');

Query OK, 1 row affected(0.02 sec)

 

mysql> quit

Bye

 

先做完全備份

[root@localhost ~]# innobackupex--defaults-file=/etc/my.cnf --user=root --password="123456"/opt/mysqlbackup/full/fulll_incre_$(date +%Y%m%d_%H%M%S) --no-timestamp

........

161203 22:57:11 [00]Writing xtrabackup_info

161203 22:57:11 [00]        ...done

xtrabackup: Transactionlog of lsn (1609292) to (1609292) was copied.

161203 22:57:12 completedOK!

 

接下來做第一次增量備份,先進入數據庫插入數據

mysql> use test2;

Database changed

mysql> insert into yy values(3,'kim3');

Query OK, 1 row affected(0.02 sec)

[root@localhost ~]# innobackupex --incremental/opt/mysqlbackup/inc/incree_$(date +%Y%m%d_%H%M%S)--incremental-basedir=/opt/mysqlbackup/full/fulll_incre_20161203_225708/--user=root --password="123456" --no-timestamp

 

基於全備和第一次增量備份來做第二次增量備份

mysql> use test2;

Database changed

mysql> insert into yy values(4,'kim4');

Query OK, 1 row affected(0.02 sec)

[root@localhost ~]# innobackupex --incremental/opt/mysqlbackup/inc/increee_$(date +%Y%m%d_%H%M%S)--incremental-basedir=/opt/mysqlbackup/inc/incree_20161203_230236/ --user=root--password="123456" --no-timestamp

 

 

刪除增量的數據,進行增量恢復

mysql> use test2;

Database changed

mysql> delete from yy where id=3;

Query OK, 1 row affected(0.03 sec)

[root@localhost ~]#innobackupex --apply-log --redo-only /opt/mysqlbackup/full/fulll_incre_20161203_225708/

[root@localhost ~]#innobackupex --apply-log --redo-only/opt/mysqlbackup/full/fulll_incre_20161203_225708/--incremental-dir=/opt/mysqlbackup/inc/incree_20161203_230236/

[root@localhost ~]#innobackupex --apply-log --redo-only/opt/mysqlbackup/full/fulll_incre_20161203_225708/--incremental-dir=/opt/mysqlbackup/inc/increee_20161203_230810/

[root@localhost ~]#service mysqld stop

Shutting down MySQL.                                       [確定]

[root@localhost ~]# mkdir/tmp/mysqlbak

[root@localhost ~]# mv/usr/local/mysql/data/* /tmp/mysqlbak/

[root@localhost ~]#innobackupex --defaults-file=/etc/my.cnf --user=root --password="123456" --copy-back/opt/mysqlbackup/full/fulll_incre_20161203_225708/

啓動數據庫,查看數據是否恢復

 

 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章