xtrabackup全量、增量備份恢復mysql數據庫
阿新 • • 發佈:2019-05-16
多少 prepare crypt seq shutdown info 備份恢復 NPU locked 一. 全量備份恢復:
- 查看原表內容:
MariaDB [(none)]> select * from testdb.students; +----+------------+------+--------+ | id | name | age | gender | +----+------------+------+--------+ | 1 | zhangsan | 15 | f | | 2 | lisi | 15 | m | | 3 | wanger | 25 | m | | 4 | liuwu | 24 | f | | 5 | wangermazi | 28 | f | +----+------------+------+--------+ 5 rows in set (0.00 sec)
1. 備份:
[[email protected] ~]# innobackupex --user=lxk --host=localhost --password=lxkpass /tmp 180916 11:56:18 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!". ...... 中間省略 ...... 180916 11:56:22 Executing UNLOCK TABLES 180916 11:56:22 All tables unlocked 180916 11:56:22 Backup created in directory ‘/tmp/2018-09-16_11-56-18‘ 180916 11:56:22 [00] Writing backup-my.cnf 180916 11:56:22 [00] ...done 180916 11:56:22 [00] Writing xtrabackup_info 180916 11:56:22 [00] ...done xtrabackup: Transaction log of lsn (1602080) to (1602080) was copied. 180916 11:56:23 completed OK! [[email protected] ~]# cat /tmp/2018-09-16_11-56-18/xtrabackup_checkpoints backup_type = full-backuped #備份類型:全量備份 from_lsn = 0 #起始lsn to_lsn = 1602080 #結束lsn last_lsn = 1602080 #總共多少個lsn compact = 0 recover_binlog_info = 0 [[email protected] ~]# cat /tmp/2018-09-16_11-56-18/xtrabackup_info uuid = 7a05430c-b964-11e8-889e-000c29080758 name = tool_name = innobackupex #備份工具名稱 tool_command = --user=lxk --host=localhost --password=... /tmp #備份時使用的命令 tool_version = 2.3.6 #工具版本 ibbackup_version = 2.3.6 server_version = 5.5.60-MariaDB start_time = 2018-09-16 11:56:18 #備份開始時間 end_time = 2018-09-16 11:56:22 #備份結束時間 lock_time = 0 binlog_pos = innodb_from_lsn = 0 innodb_to_lsn = 1602080 partial = N incremental = N format = file compact = N compressed = N #是否啟用壓縮 encrypted = N #是否加密
2. 準備(apply)備份
[[email protected] ~]# innobackupex --apply-log /tmp/2018-09-16_11-56-18/ 180916 12:06:16 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!". ...... 中間省略 ...... xtrabackup: starting shutdown with innodb_fast_shutdown = 1 InnoDB: FTS optimize thread exiting. InnoDB: Starting shutdown... InnoDB: Shutdown completed; log sequence number 1602582 180916 12:06:19 completed OK! #此處顯示completed OK即表示完成
3. 恢復備份:
(1)停止mysql服務
(2)刪庫
[[email protected] ~]# rm -rf /var/lib/mysql/*
(3) 通過全量備份恢復數據
[[email protected] ~]# innobackupex --copy-back /tmp/2018-09-16_11-56-18/
180916 12:11:19 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.3.6 based on MySQL server 5.6.24 Linux (x86_64) (revision id: )
180916 12:11:19 [01] Copying ib_logfile0 to /var/lib/mysql/ib_logfile0
180916 12:11:19 [01] ...done
.....
中間省略
.....
180916 12:11:20 [01] ...done
180916 12:11:20 [01] Copying ./xtrabackup_info to /var/lib/mysql/xtrabackup_info
180916 12:11:20 [01] ...done
180916 12:11:20 [01] Copying ./test/db.opt to /var/lib/mysql/test/db.opt
180916 12:11:20 [01] ...done
180916 12:11:20 completed OK! #顯示completed OK即為完成
(4) 修改恢復後文件的屬主,屬組為mysql
[[email protected] ~]# ls /var/lib/mysql -l
total 28692
-rw-r----- 1 root root 18874368 Sep 16 12:11 ibdata1
-rw-r----- 1 root root 5242880 Sep 16 12:11 ib_logfile0
-rw-r----- 1 root root 5242880 Sep 16 12:11 ib_logfile1
drwx------ 2 root root 4096 Sep 16 12:11 mysql
drwx------ 2 root root 4096 Sep 16 12:11 performance_schema
drwx------ 2 root root 4096 Sep 16 12:11 test
drwx------ 2 root root 4096 Sep 16 12:11 testdb
-rw-r----- 1 root root 437 Sep 16 12:11 xtrabackup_info
[[email protected] ~]# chown -R mysql.mysql /var/lib/mysql/*
[[email protected] ~]# ll /var/lib/mysql/
total 28692
-rw-r----- 1 mysql mysql 18874368 Sep 16 12:11 ibdata1
-rw-r----- 1 mysql mysql 5242880 Sep 16 12:11 ib_logfile0
-rw-r----- 1 mysql mysql 5242880 Sep 16 12:11 ib_logfile1
drwx------ 2 mysql mysql 4096 Sep 16 12:11 mysql
drwx------ 2 mysql mysql 4096 Sep 16 12:11 performance_schema
drwx------ 2 mysql mysql 4096 Sep 16 12:11 test
drwx------ 2 mysql mysql 4096 Sep 16 12:11 testdb
-rw-r----- 1 mysql mysql 437 Sep 16 12:11 xtrabackup_info
(5) 啟動MySQL並查看
[ro[email protected] ~]# systemctl start mariadb
[[email protected] ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.60-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.
MariaDB [(none)]> select * from testdb.students; #恢復完成
+----+------------+------+--------+
| id | name | age | gender |
+----+------------+------+--------+
| 1 | zhangsan | 15 | f |
| 2 | lisi | 15 | m |
| 3 | wanger | 25 | m |
| 4 | liuwu | 24 | f |
| 5 | wangermazi | 28 | f |
+----+------------+------+--------+
5 rows in set (0.00 sec)
二. 增量備份及恢復:
1. 全量備份:
[[email protected] ~]# innobackupex --user=lxk --host=localhost --password=lxkpass /tmp/
180916 12:17:01 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!".
.....
中間省略
.....
180916 12:17:03 Executing UNLOCK TABLES
180916 12:17:03 All tables unlocked
180916 12:17:03 Backup created in directory ‘/tmp//2018-09-16_12-17-01‘
180916 12:17:03 [00] Writing backup-my.cnf
180916 12:17:03 [00] ...done
180916 12:17:03 [00] Writing xtrabackup_info
180916 12:17:03 [00] ...done
xtrabackup: Transaction log of lsn (1602592) to (1602592) was copied.
180916 12:17:03 completed OK!
2. 修改數據庫,進行第一次增量備份
- 在testdb.students中添加一條數據:
MariaDB [testdb]> insert into students values (6,‘xiaoming‘,20,‘f‘);
Query OK, 1 row affected (0.00 sec)
MariaDB [testdb]> select * from students;
+----+------------+------+--------+
| id | name | age | gender |
+----+------------+------+--------+
| 1 | zhangsan | 15 | f |
| 2 | lisi | 15 | m |
| 3 | wanger | 25 | m |
| 4 | liuwu | 24 | f |
| 5 | wangermazi | 28 | f |
| 6 | xiaoming | 20 | f |
+----+------------+------+--------+
6 rows in set (0.00 sec)
- 增量備份:
[[email protected] ~]# innobackupex --incremental /tmp --incremental-basedir=/tmp/2018-09-16_12-17-01/
180916 12:23:28 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!".
.....
中間省略
.....
180916 12:23:30 [00] ...done
xtrabackup: Transaction log of lsn (1602735) to (1602735) was copied.
180916 12:23:30 completed OK!
3. 添加一條數據,進行第二次增量備份:
- 增加一條數據
MariaDB [testdb]> insert into students values (8,‘daming‘,20,‘m‘);
Query OK, 1 row affected (0.00 sec)
- 第二次增量備份(若此時--incremental-basedir指的是第一次全量備份路徑,則為差異備份):
[[email protected] ~]# innobackupex --incremental /tmp --incremental-basedir=/tmp/2018-09-16_12-23-28/
180916 12:29:08 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!".
.....
中間省略
.....
180916 12:29:10 [00] Writing xtrabackup_info
180916 12:29:10 [00] ...done
xtrabackup: Transaction log of lsn (1603615) to (1603615) was copied.
180916 12:29:10 completed OK!
4. 恢復數據:
(1) 準備(prepare)數據:
- 需要在每個備份(包括完全和各個增量備份)上,將已經提交的事務進行“重放”。“重放”之後,所有的備份數據將合並到完全備份上。
- 基於所有的備份將未提交的事務進行“回滾”
(2)準備全量備份文件
[[email protected] tmp]# innobackupex --apply-log --redo-only 2018-09-16_12-17-01
180916 12:34:06 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!".
.....
中間省略
.....
InnoDB: Starting shutdown...
InnoDB: Shutdown completed; log sequence number 1602592
180916 12:34:06 completed OK!
(3) 準備第一次增量備份文件:
- 註: --incremental-dir所指的目錄必須為絕對路徑
[[email protected] 2018-09-16_12-17-01]# innobackupex --apply-log --redo-only ./ --incremental-dir=/tmp/2018-09-16_12-23-28
180916 12:38:17 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!".
.....
中間省略
.....
180916 12:38:18 [00] Copying /tmp/2018-09-16_12-23-28/xtrabackup_info to ./xtrabackup_info
180916 12:38:18 [00] ...done
180916 12:38:18 completed OK!
(4) 準備第二次增量備份文件:
[[email protected] 2018-09-16_12-17-01]# innobackupex --apply-log --redo-only ./ --incremental-dir=/tmp/2018-09-16_12-29-08/
180916 12:42:56 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!".
.....
中間省略
.....
180916 12:42:57 [01] ...done
180916 12:42:57 [00] Copying /tmp/2018-09-16_12-29-08//xtrabackup_info to ./xtrabackup_info
180916 12:42:57 [00] ...done
180916 12:42:57 completed OK!
(5) 執行回滾操作
[[email protected] tmp]# innobackupex --apply-log /tmp/2018-09-16_12-17-01
180916 12:46:15 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!".
.....
中間省略
.....
xtrabackup: starting shutdown with innodb_fast_shutdown = 1
InnoDB: FTS optimize thread exiting.
InnoDB: Starting shutdown...
InnoDB: Shutdown completed; log sequence number 1604128
180916 12:46:18 completed OK!
(6) 關閉MySQL並刪除/var/lib/mysql/下所有文件
(7) 恢復數據:
[[email protected] tmp]# innobackupex --copy-back 2018-09-16_12-17-01/
180916 12:48:39 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!".
.....
中間省略
.....
180916 12:48:40 [01] Copying ./test/db.opt to /var/lib/mysql/test/db.opt
180916 12:48:40 [01] ...done
180916 12:48:40 completed OK!
(8) 修改/var/lib/mysql/下文件的屬主、屬組並啟動數據庫並查看
[[email protected] mysql]# chown -R mysql.mysql /var/lib/mysql/*
[[email protected] mysql]# ll
total 28692
-rw-r----- 1 mysql mysql 18874368 Sep 16 12:48 ibdata1
-rw-r----- 1 mysql mysql 5242880 Sep 16 12:48 ib_logfile0
-rw-r----- 1 mysql mysql 5242880 Sep 16 12:48 ib_logfile1
drwx------ 2 mysql mysql 4096 Sep 16 12:48 mysql
drwx------ 2 mysql mysql 4096 Sep 16 12:48 performance_schema
drwx------ 2 mysql mysql 4096 Sep 16 12:48 test
drwx------ 2 mysql mysql 4096 Sep 16 12:48 testdb
-rw-r----- 1 mysql mysql 462 Sep 16 12:48 xtrabackup_info
[[email protected] mysql]# systemctl start mariadb
[[email protected] mysql]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.60-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.
MariaDB [(none)]> select * from testdb.students; #恢復完成
+----+------------+------+--------+
| id | name | age | gender |
+----+------------+------+--------+
| 1 | zhangsan | 15 | f |
| 2 | lisi | 15 | m |
| 3 | wanger | 25 | m |
| 4 | liuwu | 24 | f |
| 5 | wangermazi | 28 | f |
| 6 | xiaoming | 20 | f |
| 8 | daming | 20 | m |
+----+------------+------+--------+
7 rows in set (0.00 sec)
三. xtrabackup備份目錄下文件解讀
在備份的同時,innobackupex還會在備份目錄中創建如下文件:
- xtrabackup_checkpoints
backup_type = full-backuped 本次備份類型 = 全量備份
from_lsn = 0 起始日誌序列號
to_lsn = 258476374114 結束日誌序列號
last_lsn = 258476374114
compact = 0 是否壓縮
recover_binlog_info = 0 復制恢復時binlog信息
-
其中包括:備份類型(如完全或增量)、備份狀態(如是否已經為prepared狀態)和LSN(日誌序列號)範圍信息;
- 每個InnoDB頁(通常為16k大小)都會包含一個日誌序列號,即LSN。LSN是整個數據庫系統的系統版本號,每個頁面相關的LSN能夠表明此頁面最近是如何發生改變的。
- xtrabackup_binlog_info
- mysql服務器當前正在使用的二進制日誌文件及至備份這一刻為止二進制日誌事件的位置。
[[email protected] 11.27]# cat xtrabackup_binlog_info
mysql-bin.001102 379212660 ac7a95b5-6507-11e8-b052-702084fbc6aa:1-6694,
e755a417-6507-11e8-b054-702084fbc7b6:1-38396411
- xtrabackup_binlog_pos_innodb —— 二進制日誌文件及用於InnoDB或XtraDB表的二進制日誌文件的當前position。
- xtrabackup_binary —— 備份中用到的xtrabackup的可執行文件;
- backup-my.cnf —— 備份命令用到的配置選項信息;
另外在使用innobackupex進行備份時,還可以使用--no-timestamp選項來阻止命令自動創建一個以時間命名的目錄;如此一來,innobackupex命令將會創建一個BACKUP-DIR目錄來存儲備份數據。
xtrabackup全量、增量備份恢復mysql數據庫