MySQL備份與恢復-innobackupex


 :上一片myloder搞崩潰,為什么百度的博文都是抄襲一模一樣的,哎煩!

這一片文章我們來介紹物理備份工具xtracebackup!

首先是安裝可以percona官網下載安裝,下載rpm包直接yum安裝即可!

yum install -y perl-DBD-MySQL perl-DBI perl-Time-HiRes libaio* #安裝依賴包
yum
install -y percona-xtrabackup-24-2.4.4-1.el6.x86_64.rpm
[root@test2 ~]# xtrabackup -v
xtrabackup version 2.4.4 based on MySQL server 5.7.13 Linux (x86_64) (revision id: df58cf2)
#下載的時候盡量下載新版本的percoan,這樣會有對MySQL5.7的支持
#寫這個博文的時候,官網最新版本好像是8.0的吧,這個8.0版本應該是為了支持MySQL8.x吧,后續再測一下!

XtraceBackup是由知名數據庫軟件服務企業Percona提供的一款熱備工具,除了能夠支持最為常見的MyISAM,INNODB引擎對象外,還支持XtraDB引擎。

查看percona的安裝文件:

[root@test2 ~]# rpm -ql percona-xtrabackup-24-2.4.4-1.el6.x86_64
/usr/bin/innobackupex 
/usr/bin/xbcloud
/usr/bin/xbcloud_osenv
/usr/bin/xbcrypt
/usr/bin/xbstream                 #以專用的xbstream格式壓縮xtrabackup輸出的信息。也可以使用tar 
/usr/bin/xtrabackup               
#最經常使用的就是這兩個備份工具,其中之前的版本xtracebackup只能備份INNODB存儲引擎,而innobackupex對xtracebackup進行了封裝,不僅能備份INNODB存儲
引擎,也能備份myisam存儲引擎,但是查看官方2.4版本的文檔時候,percona說在之后的版本會放棄innobackupex這個工具,建議使用xtracebackup命令! /usr/share/doc/percona-xtrabackup-24-2.4.4 /usr/share/doc/percona-xtrabackup-24-2.4.4/COPYING /usr/share/man/man1/innobackupex.1.gz /usr/share/man/man1/xbcrypt.1.gz /usr/share/man/man1/xbstream.1.gz /usr/share/man/man1/xtrabackup.1.gz

 

XtraceBackup備份有以下幾個優點:

l  備份集高效,完整,可用。

l  備份任務執行過程中不會有阻塞任務

l  節省磁盤空間,降低網絡帶寬占用

l  備份集自動驗證機制

l  恢復更快

xtrabackup必須在MySQL服務端執行(但是創建的備份集不一定是保存在本地),特別是通過innobackupex命令創建備份集時,由於操作需要連接數據庫獲取信息,因此還要指定的連接參數(用戶名,密碼),而且連接所使用的的用戶,必須擁有正確的操作權限。

特別提醒:

percona官方在文檔中說要在之后的版本中放棄innobackupex工具,推薦使用xtracebackupex工具,但是截至目前為止,我們公司線上環境的備份還是使用的innobackupex。因此這里我們首先介紹innobackupex的備份使用,然后再介紹xtracebackup的使用。

xtraceback2.4.x的官方文檔地址:CLICK HEERE

innobackupex的備份與恢復

innobackupex的參數有很多,僅通過實例來講解經常使用的,若需要某些參數可以查看官方文檔!

一個完全備份

[root@test3 ~]# innobackupex --user=root --password=123456  /data/backup/          #備份很簡單,/data/backup/為備份的目錄
181128 18:50:47 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!".

181128 18:50:47  version_check Connecting to MySQL server with DSN 'dbi:mysql:;mysql_read_default_group=xtrabackup;host=localhost' as 'root'  (using password: YES).
181128 18:50:47  version_check Connected to MySQL server
181128 18:50:47  version_check Executing a version check against the server...
181128 18:50:47  version_check Done.
181128 18:50:47 Connecting to MySQL server host: localhost, user: root, password: set, port: 0, socket: (null)
Using server version 5.7.22-log
innobackupex version 2.4.4 based on MySQL server 5.7.13 Linux (x86_64) (revision id: df58cf2)
xtrabackup: uses posix_fadvise().
xtrabackup: cd to /data/mysql
xtrabackup: open files limit requested 0, set to 1024
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 = ./
xtrabackup:   innodb_log_files_in_group = 2
xtrabackup:   innodb_log_file_size = 50331648
InnoDB: Number of pools: 1
181128 18:50:47 >> log scanned up to (13741543758)
xtrabackup: Generating a list of tablespaces
InnoDB: Allocated tablespace ID 48 for mysql/servers, old maximum was 0
181128 18:50:48 [01] Copying ./ibdata1 to /data/backup/2018-11-28_18-50-47/ibdata1 #拷貝的是ibd文件,然后分別拷貝每張表的文件,然后再拷貝redo日志
181128 18:50:48 >> log scanned up to (13741543758)
181128 18:50:49 >> log scanned up to (13741543758)
181128 18:50:50 [01]        ...done
181128 18:50:50 [01] Copying ./mysql/servers.ibd to /data/backup/2018-11-28_18-50-47/mysql/servers.ibd
181128 18:50:50 [01]        ...done
181128 18:50:50 [01] Copying ./mysql/time_zone_name.ibd to /data/backup/2018-11-28_18-50-47/mysql/time_zone_name.ibd
...... MySQL binlog position: filename
'test3-bin.000001', position '21818' 181128 18:51:12 [00] Writing backup-my.cnf 181128 18:51:12 [00] ...done 181128 18:51:12 [00] Writing xtrabackup_info 181128 18:51:12 [00] ...done xtrabackup: Transaction log of lsn (13741543749) to (13741543758) was copied. 181128 18:51:12 completed OK! #出現completed OK表示備份完成!

因為過程比較長,沒有粘貼,可以看一下備份的過程!

innobackupex會在備份目錄下面創建以當前時間點命名的備份文件如下:

[root@test3 ~]# cd /data/backup/
[root@test3 backup]# ls
2018-11-28_18-50-47             #以當前時間點命名的備份文件集
[root@test3 backup]# cd 2018-11-28_18-50-47/
[root@test3 2018-11-28_18-50-47]# ls          #看到的備份之后的文件集
backup-my.cnf  employees   hostinfo        ibdata1  mysql   performance_schema  sys   tpcc_test  xtrabackup_binlog_info  xtrabackup_info
cmdb           financesys  ib_buffer_pool  lianxi   mytest  sbtest              test  ts1.ibd    xtrabackup_checkpoints  xtrabackup_logfile
[root@test3 2018-11-28_18-50-47]# cd employees; ls #每個數據庫中包含的表的結構文件(frm)和表數據文件(ibd)
current_dept_emp.frm  departments.frm  dept_emp.frm  dept_emp_latest_date.frm  dept_manager.ibd  employees.ibd  salaries.ibd  test2.frm  titles.frm
db.opt                departments.ibd  dept_emp.ibd  dept_manager.frm          employees.frm     salaries.frm   test1.frm     test3.frm  titles.ibd
[root@test3 employees]#


backup-my.cnf: 為當前數據庫配置文件的備份!
xtrabackup_binlog_info:當前備份文件對應的二進制日志文件和position位置,做PIT恢復時使用。

[root@test3 2018-11-28_18-50-47]# cat xtrabackup_info #包含信息比較多
uuid = 85662dd9-f2fb-11e8-a47c-fa336351fc00 #當前數據庫的UUID
name =
tool_name = innobackupex #包含備份的工具,備份的命令,備份工具的版本,MySQL的版本
tool_command = --user=root --password=... /data/backup/
tool_version = 2.4.4
ibbackup_version = 2.4.4
server_version = 5.7.22-log
start_time = 2018-11-28 18:50:47 #備份的開始時間
end_time = 2018-11-28 18:51:12 #備份的結束時間
lock_time = 0
binlog_pos = filename 'test3-bin.000001', position '21818' #二進制日志的位置
innodb_from_lsn = 0
innodb_to_lsn = 13741543749 #刷新到lsn的位置
partial = N
incremental = N #增量備份
format = file
compact = N
compressed = N
encrypted = N

[root@test3 2018-11-28_18-50-47]# cat xtrabackup_checkpoints #包含checkpoint的信息,以及備份方式
backup_type = full-backuped
from_lsn = 0
to_lsn = 13741543749
last_lsn = 13741543758
compact = 0
recover_binlog_info = 0

xtrabackup_logfile:【未知】

執行恢復

恢復分為兩個步驟:

准備恢復:所謂准備恢復,就是要為恢復做准備。就是說備份集沒辦法直接拿來用,因為這中間可能存在未提交或未回滾的事務,數據文件不一致,所以需要一個隊備份集的准備過程。

[root@test3 ~]# innobackupex --apply-log /data/backup/2018-11-28_18-50-47/        #准備階段的命令   
181128 19:16:57 innobackupex: Starting the apply-log operation

IMPORTANT: Please check that the apply-log run completes successfully.
           At the end of a successful apply-log run innobackupex
           prints "completed OK!".

innobackupex version 2.4.4 based on MySQL server 5.7.13 Linux (x86_64) (revision id: df58cf2)
xtrabackup: cd to /data/backup/2018-11-28_18-50-47
xtrabackup: This target seems to be not prepared yet.
InnoDB: Number of pools: 1
.....
InnoDB: 5.7.13 started; log sequence number 13741544981
xtrabackup: starting shutdown with innodb_fast_shutdown = 1
InnoDB: FTS optimize thread exiting.
InnoDB: Starting shutdown...
InnoDB: Shutdown completed; log sequence number 13741545378
181128 19:17:02 completed OK!

執行恢復:innobackupex提供了--copy-back參數,就是將指定的備份集,恢復到指定的路徑下面(這個指定的路徑是配置文件中datadir指定的路徑)!

[root@test3 ~]# service mysqld stop       #首先停掉數據庫
Shutting down MySQL.... SUCCESS! 
[root@test3 ~]# rm -fr /data/mysql/*      #情況datadir指定的目錄,若是線上環境,確保一定備份過 [root@test3 ~]# innobackupex --copy-back /data/backup/2018-11-28_18-50-47/ #備份命令
181128 19:20:58 innobackupex: Starting the copy-back operation

IMPORTANT: Please check that the copy-back run completes successfully.
           At the end of a successful copy-back run innobackupex
           prints "completed OK!".

innobackupex version 2.4.4 based on MySQL server 5.7.13 Linux (x86_64) (revision id: df58cf2)
......
181128 19:21:30 [01] Copying ./xtrabackup_info to /data/mysql/xtrabackup_info
181128 19:21:30 [01]        ...done
181128 19:21:30 [01] Copying ./xtrabackup_binlog_pos_innodb to /data/mysql/xtrabackup_binlog_pos_innodb
181128 19:21:30 [01]        ...done
181128 19:21:30 [01] Copying ./xtrabackup_galera_info to /data/mysql/xtrabackup_galera_info
181128 19:21:30 [01]        ...done
181128 19:21:30 [01] Copying ./ibtmp1 to /data/mysql/ibtmp1
181128 19:21:30 [01]        ...done
181128 19:21:30 completed OK!        #恢復完成

數據庫恢復之后,啟動數據庫:

[root@test3 ~]# chown -R mysql:mysql /data/mysql/
[root@test3 ~]# service mysqld start
Starting MySQL.. SUCCESS!

至此一個完全備份的數據恢復工作完成!

在使用innobackupex進行備份時,還可以使用--no-timestamp選項來阻止命令自動創建一個以時間命名的目錄,我們可以自定義備份集的名字如下:

[root@test3 ~]# innobackupex --user=root --password=123456 --no-timestamp /data/backup/test_`date +%F`  #指定備份集的名字
[root@test3 ~]# cd /data/backup/
[root@test3 backup]# ls           #查看備份集
 test_2018-11-28
[root@test3 backup]#

有時候因為socket文件不在默認位置,可以使用--socket指定位置:

[root@test2 ~]# innobackupex --user=root --password="7abec53701c3eefb" --no-timestamp /data/backup/testdb
181128 19:40:10 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!".

181128 19:40:10  version_check Connecting to MySQL server with DSN 'dbi:mysql:;mysql_read_default_group=xtrabackup' as 'root'  (using password: YES).
181128 19:40:10  version_check Connected to MySQL server
181128 19:40:10  version_check Executing a version check against the server...
181128 19:40:10  version_check Done.
181128 19:40:10 Connecting to MySQL server host: localhost, user: root, password: set, port: 0, socket: (null)
Failed to connect to MySQL server: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2).
[root@test2 ~]# innobackupex --user=root --password="7abec53701c3eefb" --no-timestamp --socket=/var/lib/mysql/mysql.sock  data/backup/testdb

 增量備份與恢復

增量備份時與全備一起結合使用的,因此這里我們會說明一個全部的過程。

1:首先對目前的數據庫做一個全備

[root@test3 ~]# innobackupex --user=root --password=123456 --no-timestamp /data/backup/full_backup
[root@test3 ~]# cd /data/backup/
[root@test3 backup]# ls #全部備份的文件
full_backup

2:對數據做一些修改,如下

mysql> use sbtest;              #創建了一張表,然后插入了兩條記錄
Database changed
mysql> create table tb3(a varchar(5), sysTime datetime);
Query OK, 0 rows affected (0.02 sec)

mysql> insert into tb3 select "aa", now();
Query OK, 1 row affected (0.01 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> insert into tb3 select "bb", now();
Query OK, 1 row affected (0.01 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> select * from tb3;
+------+---------------------+
| a    | sysTime             |
+------+---------------------+
| aa   | 2018-11-29 09:14:00 |
| bb   | 2018-11-29 09:14:11 |
+------+---------------------+
2 rows in set (0.00 sec)

3:開始增量備份

[root@test3 backup]# innobackupex --user=root --password=123456 --no-timestamp --incremental /data/backup/incre1 --incremental-basedir=/data/backup/full_backup

--no-timestamp: 上面已經說過,自己定義備份集的名字
--incremental /data/backup/incre1 :參數說明這次備份時一個增量備份,后面的路徑指定增量備份的目錄
--incremental-basedir=/data/backup/full_backup :增量備份時基於全備的,這個參數就是說明本次的增量備份時基於那次的全備,指向全備的目錄文件

查看增量備份的文件記錄:

[root@test3 incre1]# cat xtrabackup_checkpoints
backup_type = incremental                 #備份類型
from_lsn = 13741583205                    #需要說明增量備份只能INNODB,xtrack存儲引擎,備份是基於lsn的改變的
to_lsn = 13741590096
last_lsn = 13741590105
compact = 0
recover_binlog_info = 0

備份完成之后,我們需要恢復操作,記得上面恢復第一步是什么?准備數據,也就是redo已經提交的事務,回滾未提交的事務。增量備份也是這樣,但是操作有些不同。

把全備文件備份一下,把數據庫二進制日志也保存一下:【這一步為了二進制增備恢復做准備

[root@test3 backup]# cp -r  full_backup full_backup_bak
[root@test3 backup]# cp /data/mysql/test3-bin.000001 /data/backup/
[root@test3 backup]# ls
full_backup  full_backup_bak  incre1  test3-bin.000001

恢復操作:

准備數據

第一准備全備:
[root@test3 ~]# innobackupex  --apply-log --redo-only --use-memory=1G /data/backup/full_backup

--use-memory=1G #表示使用的內存大小

第二步:把增量備份在全備上面回放,也就是把增量備份附加到全備上。
[root@test3 ~]# innobackupex --apply-log --redo-only /data/backup/full_backup --incremental-dir=/data/backup/incre1

整個恢復操作分為兩步
1:准備全部備份,保證文件的一致性。
2:把所有的增量備份附加到全備備份上。如果有多個增量備份,則每一個增量備份文件都要執行附加操作。但是要注意順序,可以查看增量備份的xtrabackup_checkpoints文件
根據lsn來確定文件的順序

開始恢復

[root@test3 ~]# service mysqld  stop
[root@test3 ~]# rm -fr /data/mysql/* [root@test3 ~]# innobackupex --copy-back /data/backup/full_backup
[root@test3 ~]# chown -R mysql:mysql /data/mysql/

#啟動數據庫,若是之前插入的兩條數據存在,則說明增量備份恢復成功!
[root@test3 ~]# service mysqld start
Starting MySQL... SUCCESS!
[root@test3 ~]# mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.22-log MySQL Community Server (GPL)

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

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

mysql> use sbtest;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select * from tb3; #增量備份插入的數據存在
+------+---------------------+
| a    | sysTime             |
+------+---------------------+
| aa   | 2018-11-29 09:14:00 |
| bb   | 2018-11-29 09:14:11 |
+------+---------------------+
2 rows in set (0.00 sec)

特別提示:

若有多個增量備份,則每一個增量備份都要重放到全備的文件中,一定要注意重放增量備份時候的順序,可以在xtrabackup_checkpoints文件中根據lsn的大小來判斷文件的順序。

利用二進制日志做增量恢復

上面innobackupex的增量恢復,有點不方便,若是要恢復到某個時間點,顯得有點無能為力了!但是二進制日志可以,二進制日志可以恢復指定時間點。

在上面的插入數據中,我們插入兩條數據,但是一個要求,我們只要恢復到2018-11-29 09:14:00的數據,也就是插入的第二條數據不需要了!

我們上面備份了全備和二進制日志,現在就可以使用了!

基於全備恢復所有的數據,然后根據二進制日志把數據庫恢復到我們要的時間點!

#執行全部備份的恢復
[root@test3 ~]#  innobackupex  --apply-log /data/backup/full_backup_bak
[root@test3 ~]# service mysqld stop [root@test3 ~]# rm -fr /data/mysql/ [root@test3 ~]# innobackupex --copy-back /data/backup/full_backup_bak [root@test3 ~]# chown -R mysql:mysql [root@test3 ~]# chown -R mysql:mysql /data/mysql/ [root@test3 ~]# service mysqld start Starting MySQL.. SUCCESS! [root@test3 ~]# mysql -uroot -p123456 mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.7.22-log MySQL Community Server (GPL) Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> use sbtest; #全備的時候,我們並沒有在sbtest下面創建tb3這張表 Database changed mysql> show tables; Empty set (0.00 sec)

 #執行基於二進制時間點的恢復

innobackupex備份的是,命令結束的時候的全部數據文件,可以在如下文件查看全備的時間信息:

#有兩種恢復方式基於時間點恢復
[root@test3 mysql]# cat xtrabackup_info uuid = e44e6a70-f373-11e8-a5cd-fa336351fc00 name = tool_name = innobackupex tool_command = --user=root --password=... --no-timestamp /data/backup/full_backup tool_version = 2.4.4 ibbackup_version = 2.4.4 server_version = 5.7.22-log start_time = 2018-11-29 09:12:17 end_time = 2018-11-29 09:12:51 #全部結束的時間 lock_time = 0 binlog_pos = filename 'test3-bin.000001', position '1782' innodb_from_lsn = 0 innodb_to_lsn = 13741583205 partial = N incremental = N format = file compact = N compressed = N encrypted = N
#基於二進制的日志點恢復 [root@test3 mysql]#
cat xtrabackup_binlog_pos_innodb #全部備份結束的時間點 test3-bin.000001 1227 [root@test3 mysql]#

導出要恢復時間段的二進制日志文件:

我們要截取的日志時間段是從 全備結束 到 2018-11-29 09:14:00 這個時間點的文件。全備結束時間點作為起始點,起始點有兩種確定方式,第一種上面文件中

的end_time指定的時間點;第二種二進制日志的position位置。這兩種方式都可以。

 

[root@test3 mysql]# cd /data/backup/
[root@test3 backup]# mysqlbinlog --start-datetime="2018-11-29 09:12:51" --stop-datetime="2018-11-29 09:14:00" test3-bin.000001 > /root/bak.sql
--start-datetime : 指定起始的時間點。
--stop-datetime: 指定結束的時間點。
--start-position: 指定二進制日志開始日志點。
--stop-position : 指定二進制日志結束的日志點。
上面四個參數可以配合使用

 

然后把上面導出sql文件導入到MySQL服務中。

mysql> set sql_log_bin=0;          #導入時,若文件較大會產生很多二進制日志文件,可以先把二進制日志文件關閉,

mysql> source bak.sql;             #導入文件,可以使用mysql直接導入

mysql> set sql_log_bin=1;           #導入完成之后,開啟二進制日志文件 

查看數據:

mysql> use sbtest;   #完了演砸了,但是數據庫已經存在了,說明這種方法是正確的!
Database changed
mysql> select * from tb3;
Empty set (0.01 sec)

mysql> show tables;
+------------------+
| Tables_in_sbtest |
+------------------+
| tb3              |
+------------------+
1 row in set (0.00 sec)

查看一下二進制日志:

[root@test3 backup]# mysqlbinlog test3-bin.000001
.......
#181129  9:13:47 server id 5  end_log_pos 1973 CRC32 0xcad0188f     Query    thread_id=13    exec_time=0    error_code=0
SET TIMESTAMP=1543454027/*!*/;
create table tb3(a varchar(5), sysTime datetime)
/*!*/;
# at 1973        #用時間截取的時候很可能是截取到了這個時間點就停止了,因此只有tb3這個表,沒有后面的插入的第一條數據,因此我們選擇用日志點截取數據
#181129  9:14:00 server id 5  end_log_pos 2038 CRC32 0x670a3d93     Anonymous_GTID    last_committed=9    sequence_number=10    rbr_only=yes
/*!50718 SET TRANSACTION ISOLATION LEVEL READ COMMITTED*//*!*/;
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 2038
#181129  9:14:00 server id 5  end_log_pos 2120 CRC32 0x6a82446d     Query    thread_id=13    exec_time=0    error_code=0
SET TIMESTAMP=1543454040/*!*/;
BEGIN
/*!*/;
# at 2120
#181129  9:14:00 server id 5  end_log_pos 2172 CRC32 0x7c9e9e8c     Table_map: `sbtest`.`tb3` mapped to number 117
# at 2172
#181129  9:14:00 server id 5  end_log_pos 2216 CRC32 0xda4461be     Write_rows: table id 117 flags: STMT_END_F

BINLOG '
WD3/WxMFAAAANAAAAHwIAAAAAHUAAAAAAAEABnNidGVzdAADdGIzAAIPEgMFAAADjJ6efA==
WD3/Wx4FAAAALAAAAKgIAAAAAHUAAAAAAAEAAgAC//wCYWGZoXqTgL5hRNo=
'/*!*/;
# at 2216
#181129  9:14:00 server id 5  end_log_pos 2247 CRC32 0x3b63c077     Xid = 92
COMMIT/*!*/;
# at 2247            #選擇這個日志點
#181129  9:14:11 server id 5  end_log_pos 2312 CRC32 0xbd634458     Anonymous_GTID    last_committed=10    sequence_number=11    rbr_only=yes
/*!50718 SET TRANSACTION ISOLATION LEVEL READ COMMITTED*//*!*/;
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 2312
#181129  9:14:11 server id 5  end_log_pos 2394 CRC32 0xb5eba130     Query    thread_id=13    exec_time=0    error_code=0
SET TIMESTAMP=1543454051/*!*/;
BEGIN
/*!*/;
# at 2394
#181129  9:14:11 server id 5  end_log_pos 2446 CRC32 0xfcaf0343     Table_map: `sbtest`.`tb3` mapped to number 117
# at 2446
#181129  9:14:11 server id 5  end_log_pos 2490 CRC32 0x080cd871     Write_rows: table id 117 flags: STMT_END_F

BINLOG '
Yz3/WxMFAAAANAAAAI4JAAAAAHUAAAAAAAEABnNidGVzdAADdGIzAAIPEgMFAAADQwOv/A==
Yz3/Wx4FAAAALAAAALoJAAAAAHUAAAAAAAEAAgAC//wCYmKZoXqTi3HYDAg=
'/*!*/;
# at 2490
#181129  9:14:11 server id 5  end_log_pos 2521 CRC32 0x14fd7636     Xid = 93
COMMIT/*!*/;
SET @@SESSION.GTID_NEXT= 'AUTOMATIC' /* added by mysqlbinlog */ /*!*/;
DELIMITER ;
# End of log file
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;

 

用日志點重新導入一下

mysqlbinlog --start-position=1227 --stop-position=2247 test3-bin.000001 > /root/bak.sql  #截取二進制日志

導入數據,查看數據:

mysql> source bak.sql;
mysql> use sbtest; Database changed mysql> show tables; +------------------+ | Tables_in_sbtest | +------------------+ | tb3 | +------------------+ 1 row in set (0.00 sec) mysql> select * from tb3; #數據已經存在 +------+---------------------+ | a | sysTime | +------+---------------------+ | aa | 2018-11-29 09:14:00 | +------+---------------------+ 1 row in set (0.00 sec)

 

 

增量備份有兩種方法,一個是利用innobackupex的方式進行增量備份另一個是利用二進制日志進行增量備份;建議使用二進制日志增量備份!

我們公司的備份策略時,每一天凌晨全部備份,然后備份當天的二進制日志文件,這樣就可以把數據恢復到任何一個時間點!

流壓縮

上面備份的數據比較少,並沒有考慮磁盤空間問題。下面我們來備份一個大庫!

[root@test2 mariadb]# du -sh
47G    .
#datadir的數據文件時47G,好吧也不是太大,能說明問題了!

查看備份之后的文件大小:

[root@test2 ~] innobackupex --user=root --password="123456" --no-timestamp --socket=/var/lib/mysql/mysql.sock  /test/backup/testdb
[root@test2 ~]# cd /test/backup/
[root@test2 backup]# ls
testdb
[root@test2 backup]# du -sh #可以看到備份的文件也是47G
47G    .
[root@test2 backup]#

#innobackupex是邏輯備份,就是拷貝數據,因此備份文件和源文件的大小相差不大

引入流壓縮:

[root@test2 backup]# innobackupex --user=root --password="7abec53701c3eefb" --no-timestamp --socket=/var/lib/mysql/mysql.sock --stream=tar /tmp |gzip -> testdb.tar.gz

--stram=tar : 指定標准輸出格式,一種是tar,另一種是xbstream;xbstream是xtraceback提供的,解壓的時候需要專用命令處理,考慮通用性,選擇用tar!

/tmp : 流格式標准輸出,臨時存放於/tmp目錄下面

| gzip -> : 通過管道把流格式輸出傳給gzip進行壓縮

#壓縮備份完之后,查看一下壓縮之后文件的大小:
[root@test2 backup]# ll -h testdb.tar.gz
-rw-r--r-- 1 root root 18G Nov 29 11:27 testdb.tar.gz

壓縮之前文件是47個G
未壓縮的備份文件是47個G,
壓縮之后備份文件是18個G。

之前的使用流壓縮備份出來的命令,解壓時必須帶上 “-i” 參數!

[root@test3 backup]# tar -ixvf testdb.tar.gz -C db/        #之前的版本解壓時候必須帶上-i參數,否則會報錯的

-C :直接把解壓的文件放到指定的目錄中,在恢復時,我們可以直接放到datadir指定的目錄當中、

[root@test3 backup]# tar -zxvf testdb.tar.gz -C db/ #但是2.4版本直接使用zxvf解壓也是可以的!percona優化了很多參數

文件解壓之后,然后需要准備文件也就是需要使用--apply-log選項,之后進行恢復就可以了!恢復過程可以參照上面的恢復過程!

percona還有一種壓縮方式,官方文檔給出的就是這種壓縮方式:CLICK HERE!

#備份指令如下
[root@test2 ~]# innobackupex --user=root --password="7abec53701c3eefb" --socket="/var/lib/mysql/mysql.sock" --no-timestamp --compress --compress-threads=4  /test/backup/compress_db

--compress:開啟壓縮

--compress-threads=4 : 使用4個壓縮線程

備份的文件如下:
[root@test2 backup]# cd compress_db
[root@test2 compress_db]# ls #文件如下
backup-my.cnf.qp  ib_buffer_pool.qp  mysql               xtrabackup_binlog_info.qp  xtrabackup_info.qp
financesys        ibdata1.qp         performance_schema  xtrabackup_checkpoints     xtrabackup_logfile.qp
[root@test2 compress_db]# du -sh #文件大小為23G
23G    .

#這個壓縮文件,對表結構文件(frm)和表數據文件(ibd)都進行了壓縮,壓縮后的后綴名為 .qp!

在xtraceback2.1.4之前需要使用一個簡短的shell來解壓備份出來的文件,在2.1.4之后可以使用如下命令解壓!

[root@test2 backup]# innobackupex --decompress /test/backup/compress_db/    
181129 13:55:50 innobackupex: Starting the decrypt and decompress operation

IMPORTANT: Please check that the decrypt and decompress run completes successfully.
           At the end of a successful decrypt and decompress run innobackupex
           prints "completed OK!".

innobackupex version 2.4.4 based on MySQL server 5.7.13 Linux (x86_64) (revision id: df58cf2)
181129 13:55:50 [01] decompressing ./ibdata1.qp
sh: qpress: command not found
cat: write error: Broken pipe
Error: thread 0 failed.
[root@test2 backup]# yum install -y qpress
Loaded plugins: fastestmirror
Setting up Install Process
Loading mirror speeds from cached hostfile
base                                                                                                                                                 | 3.7 kB     00:00     
epel                                                                                                                                                 | 4.4 kB     00:00     
mariadb                                                                                                                                              | 2.9 kB     00:00     
zabbix                                                                                                                                               | 2.9 kB     00:00     
No package qpress available.       #這里報錯了沒有qpress這個命令,這個命令需要從percona官網下載!網絡有點渣,隨后補上!
Error: Nothing to do

compress壓縮備份之后的數據是23個G,而流壓縮備份是18個G,源文件大小是47個G!根據存儲來說建議使用流壓縮吧!

 

#備份指定的庫,而不是全部的庫!

#!/bin/bash
#在備份指定庫時,可以使用--include參數,也可以使用--tables-file參數,把指定的庫寫在文件中
User="root"
Hostip=x.x.x.x
Passwd="x.x.x.x.x.x"
Tbname="fi_repayplan.fi_reapyplan,fi_repayplan.fi_reapyplan_bank_once,fi_repayplan.fi_reapyplan_boc,fi_repayplan.fi_reapyplan_ccb,fi_repayplan.fi_reapyplan_irr,fi_repayplan.fi_reapyplan_irrmoney,fi_repayplan.fi_reapyplan_money,fi_repayplan.fi_reapyplan_once,fi_repayplan.fi_reapyplan_oncemoney"

innobackupex --user=${User} --password=${Passwd} --host=${Hostip} --no-timestamp --include=${Tbname} --stream=tar /tmp | gzip > /data/fi_repayplan-`date +%F`.tar.gz 
備份指定的庫

 

#一個用於恢復的腳步

#!/bin/badh
#Auth: wangxz
#需要把要解壓的文件作為腳本執行的參數
#例如:sh huifu.sh xxx_2017-07-08_03-01-01.tar.gz
DB_DIR=/data/mariadb
DB_PORT=`netstat -lntp |grep 3306 | wc -l`
PROCESS_NUM=`ps aux |grep mysql |wc -l`
recover_log=/tmp/recover.log
print_str() ##打印說明信息
{
    echo -e "------------\e[0;31;1m $1 \e[0m-------------------"    
    sleep 1
}

check_ok()  ##一個檢測函數
{
    if [ $? != 0 ]; then
        print_str "Some error has happened"
        exit
    else
        sleep 1
    fi
}
##檢測是否給出要解壓的文件
if [ $# !=  1 ]; then
    print_str "Please give a tar file"
    exit
fi
 
if [ $DB_PORT == 1 ] && [ $PROCESS_NUM == 3 ]; then
    print_str "It's stopping the mysql service"
    service mysql stop >> $recover_log
    check_ok
    sleep 3
fi

if [ $DB_PORT == 0 ] && [ $PROCESS_NUM == 1 ]; then
    print_str "The mysql service is not running!"
fi

###清楚datadir目錄
print_str "Delete the datadir"
rm -fr $DB_DIR/*
check_ok
sleep 3

###解壓數據庫
print_str "Start Uncompress the backup file"
tar -ixvf $1 -C $DB_DIR/>> $recover_log
check_ok
sleep 3 

###准備數據庫備份
print_str "apply log the datadir"
innobackupex --apply-log $DB_DIR  >> $recover_log
check_ok
sleep 3

###修改權限
chown -R mysql:mysql $DB_DIR
sleep 3

###啟動數據庫
service mysql start >>$recover_log
check_ok
###檢測是否啟動成功
PROCESSLIST=`ps aux |grep mysql |wc -l`
PORT_NUM=`netstat -lntp |grep 3306 | wc -l`
if [ $PROCESSLIST -gt 1 ] && [ $PORT_NUM -le 1 ]; then
    print_str "the mysql service has started successfully!"
    rm -fr $recover_log
    check_ok
    print_str "The log was deleted successfully!"
else
    print_str  "Some unexcept things has happened"
fi  
恢復腳本

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM