1. 完全备份
创建用于备份恢复的用户 xbK并赋予权限
mysql> create user xbk@'localhost' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
mysql> grant reload,process,lock tables,replication client on *.* to xbk@localhost;
Query OK, 0 rows affected (0.00 sec)
创建备份的目录
[root@qht26 u01]# mkdir backup
[root@qht26 u01]# chown -R mysql.mysql /u01/backup
进行数据库的全库备份
[root@qht26 u01]# innobackupex --defaults-file=/etc/my.cnf --user=xbk --password=123456 --stocket=/u01/mysql/mysql.sock /u01/backup
可以看到整个备份过程:连接数据库,开始拷贝redo log,拷贝innodb表文件,锁表、拷贝非innodb表文件,停止拷贝redo log,解锁。
查看生成的文件:
[root@qht26 backup]# cd 2018-04-24_11-54-46/
[root@qht26 2018-04-24_11-54-46]# ls -lth
total 13M
-rw-r----- 1 root root 424 Apr 24 11:55 backup-my.cnf
-rw-r----- 1 root root 350 Apr 24 11:55 ib_buffer_pool
drwxr-x--- 2 root root 4.0K Apr 24 11:55 mysql
-rw-r----- 1 root root 113 Apr 24 11:55 xtrabackup_checkpoints
-rw-r----- 1 root root 481 Apr 24 11:55 xtrabackup_info
-rw-r----- 1 root root 2.5K Apr 24 11:55 xtrabackup_logfile
drwxr-x--- 2 root root 12K Apr 24 11:54 sys
drwxr-x--- 2 root root 4.0K Apr 24 11:54 l5m
drwxr-x--- 2 root root 4.0K Apr 24 11:54 performance_schema
-rw-r----- 1 root root 12M Apr 24 11:54 ibdata1
其中几个目录文件mysql,sys,l5m,performance_schema是存放的各自数据库的文件。
backup-my.cnf,备份命令用到的配置选项信息;
[root@qht26 2018-04-24_11-54-46]# cat backup-my.cnf
# This MySQL options file was generated by innobackupex.
# The MySQL server
[mysqld]
innodb_checksum_algorithm=crc32
innodb_log_checksum_algorithm=strict_crc32
innodb_data_file_path=ibdata1:12M:autoextend
innodb_log_files_in_group=2
innodb_log_file_size=50331648
innodb_fast_checksum=false
innodb_page_size=16384
innodb_log_block_size=512
innodb_undo_directory=./
innodb_undo_tablespaces=0
server_id=0
redo_log_version=1
ib_buffer_pool, buffer pool 中的热数据,当设置 innodb_buffer_pool_dump_at_shutdown=1 ,在关闭 MySQL 时,会把内存中的热数据保存在磁盘里 ib_buffer_pool 文件中,位于数据目录下。
bdata1,备份的共享表空间文件;
xtrabackup_info,记录备份的基本信息,uuid、备份命令、备份时间、binlog、LSN、以及其他加密压缩等信息。
[root@qht26 2018-04-24_11-54-46]# cat xtrabackup_info
uuid = 442078de-4773-11e8-be9d-001a646dd15e
name =
tool_name = innobackupex
tool_command = --defaults-file=/etc/my.cnf --user=xbk --password=... --stocket=/u01/mysql/mysql.sock /u01/backup
tool_version = 2.4.10
ibbackup_version = 2.4.10
server_version = 5.7.21
start_time = 2018-04-24 11:54:46
end_time = 2018-04-24 11:55:02
lock_time = 0
binlog_pos =
innodb_from_lsn = 0
innodb_to_lsn = 2556615
partial = N
incremental = N
format = file
compact = N
compressed = N
encrypted = N
xtrabackup_checkpoints,备份类型(如完全或增量)、备份状态(如是否已经为prepared状态)和LSN(日志序列号)范围信息;
[root@qht26 2018-04-24_11-54-46]# cat xtrabackup_checkpoints
backup_type = full-backuped
from_lsn = 0
to_lsn = 2556615
last_lsn = 2556624
compact = 0
recover_binlog_info = 0
trabackup_logfile,这个文件是备份的重做日志文件。
2.完全恢复
关闭数据库并删除所有的数据文件
[root@qht26 ~]# service mysql stop
Shutting down MySQL.. [ OK ]
[root@qht26 u01]# cd /u01
[root@qht26 u01]# mv mysql/ mysql_bak
[root@qht26 u01]# mkdir mysql
[root@qht26 u01]# chown mysql.mysql mysql
准备(prepare)一个完全备份: --apply-log /u01/backup/2018-04-24_11-54-46为备份的目录,执行之后 xtrabackup_checkpoints 文件中的 backup_type = full-prepared )
--apply-log //应用 BACKUP-DIR 中的 xtrabackup_logfile 事务日志文件。一般情况下,在备份完成后,数据尚且不能用于恢复操作,因为备份的数据中可能会包含尚未提交的事务或已经提交但尚未同步至数据文件中的事务。因此,此时数据文件仍处于不一致状态。“准备”的主要作用正是通过回滚未提交的事务及同步已经提交的事务至数据文件使得数据文件处于一致性状态。
[root@qht26 u01]# innobackupex --apply-log /u01/backup/2018-04-24_11-54-46/
。。。
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: Waiting for purge to start
InnoDB: 5.7.19 started; log sequence number 2556949
xtrabackup: starting shutdown with innodb_fast_shutdown = 1
InnoDB: FTS optimize thread exiting.
InnoDB: Starting shutdown...
InnoDB: Shutdown completed; log sequence number 2556968
180424 13:15:12 completed OK!
[root@qht26 2018-04-24_11-54-46]# cat xtrabackup_checkpoints
backup_type = full-prepared
from_lsn = 0
to_lsn = 2556615
last_lsn = 2556624
compact = 0
recover_binlog_info = 0
执行恢复操作:
--copy-back //拷贝先前备份所有文件到它们的原始路径。但原路径下不能有任何文件或目录,除非指定 --force-non-empty-directories 选项。
[root@qht26 ~]# innobackupex --defaults-file=/etc/my.cnf --copy-back --rsync /u01/backup/2018-04-24_11-54-46/
。。。
180424 13:20:37 [01] Copying ./mysql/tables_priv.MYI to /u01/mysql/mysql/tables_priv.MYI
180424 13:20:37 [01] ...done
180424 13:20:37 [01] Copying ./mysql/ndb_binlog_index.MYD to /u01/mysql/mysql/ndb_binlog_index.MYD
180424 13:20:37 [01] ...done
180424 13:20:37 [01] Copying ./mysql/user.frm to /u01/mysql/mysql/user.frm
180424 13:20:37 [01] ...done
180424 13:20:37 [01] Copying ./mysql/event.MYI to /u01/mysql/mysql/event.MYI
180424 13:20:37 [01] ...done
180424 13:20:37 [01] Copying ./mysql/time_zone_transition_type.ibd to /u01/mysql/mysql/time_zone_transition_type.ibd
180424 13:20:37 [01] ...done
180424 13:20:37 [01] Copying ./mysql/gtid_executed.frm to /u01/mysql/mysql/gtid_executed.frm
180424 13:20:37 [01] ...done
180424 13:20:37 completed OK!
数据文件都已恢复回来:
[root@qht26 mysql]# ls -lth
total 121M
drwxr-x--- 2 root root 4.0K Apr 24 13:20 mysql
drwxr-x--- 2 root root 12K Apr 24 13:20 sys
drwxr-x--- 2 root root 4.0K Apr 24 13:20 l5m
-rw-r----- 1 root root 481 Apr 24 13:20 xtrabackup_info
-rw-r----- 1 root root 350 Apr 24 13:20 ib_buffer_pool
-rw-r----- 1 root root 12M Apr 24 13:20 ibtmp1
drwxr-x--- 2 root root 4.0K Apr 24 13:20 performance_schema
-rw-r----- 1 root root 12M Apr 24 13:20 ibdata1
-rw-r----- 1 root root 48M Apr 24 13:20 ib_logfile0
-rw-r----- 1 root root 48M Apr 24 13:20 ib_logfile1
修改权限后启动数据库:
[root@qht26 mysql]# chown -R mysql.mysql /u01/mysql
[root@qht26 mysql]# service mysql start
Starting MySQL.. [ OK ]
数据都已完全恢复回来
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| l5m |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
mysql> use l5m
Database changed
mysql> show tables;
+---------------+
| Tables_in_l5m |
+---------------+
| tab1 |
+---------------+
1 row in set (0.00 sec)
我们以之前做的全备为基准,在其基础上做增量备份:
对数据库先做一些更改:
mysql> use l5m
Database changed
mysql> create table tab2 (id int);
Query OK, 0 rows affected (0.25 sec)
mysql> insert into tab2 values(100);
Query OK, 1 row affected (0.06 sec)
mysql> insert into tab1 values(2);
Query OK, 1 row affected (0.05 sec)
mysql> commit;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from tab1;
+------+
| id |
+------+
| 1 |
| 2 |
+------+
2 rows in set (0.03 sec)
mysql> select * from tab2;
+------+
| id |
+------+
| 100 |
+------+
1 row in set (0.02 sec)
增量备份1,以上面的全备份为基准
[root@qht26 mysql]# innobackupex --defaults-file=/etc/my.cnf --user=xbk --password=123456 --socket=/u01/mysql/mysql.sock --incremental /u01/backup/inc --incremental-basedir=/u01/backup/2018-04-24_11-54-46/ --parallel=2
备份的文件如下:
[root@qht26 2018-04-24_14-36-17]# pwd
/u01/backup/inc/2018-04-24_14-36-17
[root@qht26 2018-04-24_14-36-17]# ls -lth
total 532K
-rw-r----- 1 root root 424 Apr 24 14:36 backup-my.cnf
-rw-r----- 1 root root 350 Apr 24 14:36 ib_buffer_pool
drwxr-x--- 2 root root 4.0K Apr 24 14:36 mysql
-rw-r----- 1 root root 117 Apr 24 14:36 xtrabackup_checkpoints
-rw-r----- 1 root root 572 Apr 24 14:36 xtrabackup_info
-rw-r----- 1 root root 2.5K Apr 24 14:36 xtrabackup_logfile
drwxr-x--- 2 root root 12K Apr 24 14:36 sys
drwxr-x--- 2 root root 4.0K Apr 24 14:36 l5m
drwxr-x--- 2 root root 4.0K Apr 24 14:36 performance_schema
-rw-r----- 1 root root 480K Apr 24 14:36 ibdata1.delta
-rw-r----- 1 root root 44 Apr 24 14:36 ibdata1.meta
再次往表里插入些数据:
mysql> insert into tab1 values(3);
Query OK, 1 row affected (0.06 sec)
mysql> insert into tab1 values(4);
Query OK, 1 row affected (0.01 sec)
mysql> insert into tab2 values(200);
Query OK, 1 row affected (0.06 sec)
mysql> commit;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from tab1;
+------+
| id |
+------+
| 1 |
| 2 |
| 3 |
| 4 |
+------+
4 rows in set (0.00 sec)
mysql> select * from tab2;
+------+
| id |
+------+
| 100 |
| 200 |
+------+
2 rows in set (0.00 sec)
再次做基于上次增量备份的增量备份(基于/u01/backup/inc/2018-04-24_14-36-17/的备份)
[root@qht26 ~]# innobackupex --defaults-file=/etc/my.cnf --user=xbk --password=123456 --socket=/u01/mysql/mysql.sock --incremental /u01/backup/inc --incremental-basedir=/u01/backup/inc/2018-04-24_14-36-17/ --parallel=2
[root@qht26 inc]# ls -lth
total 8.0K
drwxr-x--- 6 root root 4.0K Apr 24 14:42 2018-04-24_14-41-43
drwxr-x--- 6 root root 4.0K Apr 24 14:36 2018-04-24_14-36-17
[root@qht26 inc]# pwd
/u01/backup/inc
增量备份的恢复:
增量备份的恢复需要有3个步骤
#准备一个全备份 需要加redo-only
[root@qht26 backup]# innobackupex --apply-log --redo-only /u01/backup/2018-04-24_11-54-46/
#将增量1应用到全备份
需要加redo-only
[root@qht26 backup]# innobackupex --apply-log --redo-only /u01/backup/2018-04-24_11-54-46/ --incremental-dir=/u01/backup/inc/2018-04-24_14-36-17/
#将增量2应用到全备份
[root@qht26 backup]# innobackupex --apply-log --redo-only /u01/backup/2018-04-24_11-54-46/ --incremental-dir=/u01/backup/inc/2018-04-24_14-41-43/
##把所有合在一起的完全备份整体进行一次apply操作,回滚未提交的数据##
[root@qht26 2018-04-24_11-54-46]# innobackupex --apply-log /u01/backup/2018-04-24_11-54-46
xtrabackup: Log applied to lsn 2556977
xtrabackup: The intended lsn is 2564716
模拟数据损坏:
mysql> use l5m
Database changed
mysql> drop table tab2;
Query OK, 0 rows affected (0.15 sec)
mysql> drop table tab1;
Query OK, 0 rows affected (0.04 sec)
[root@qht26 ~]# service mysql stop
[root@qht26 ~]# mv /u01/mysql /u01/mysql_bk2
[root@qht26 ~]# mkdir /u01/mysql
[root@qht26 ~]# chown -R mysql.mysql /u01/mysql
[root@qht26 ~]# innobackupex --defaults-file=/etc/my.cnf --copy-back --rsync /u01/backup/2018-04-24_11-54-46/
[root@qht26 ~]# chown -R mysql.mysql /u01/mysql
[root@qht26 ~]# service mysql start
Starting MySQL.. [ OK ]
#数据已全部恢复
mysql> use l5m
Database changed
mysql> show tables;
+---------------+
| Tables_in_l5m |
+---------------+
| tab1 |
| tab2 |
+---------------+
2 rows in set (0.00 sec)
mysql> select * from tab1;
+------+
| id |
+------+
| 1 |
| 2 |
| 3 |
| 4 |
+------+
4 rows in set (0.01 sec)
mysql> select * from tab2;
+------+
| id |
+------+
| 100 |
| 200 |
+------+
2 rows in set (0.00 sec)
上述都没有说有关时间点的恢复,这个时间点恢复是指当你全量备份或增量备份后,下次备份还没开始,数据库发生故障后的备份和恢复。
这个时间点恢复的备份和恢复是通过binlog实现的。
备份
mysqlbinlog -j 245 binlog-log.xxxxxxx > /PATH/TO/BINLOG.sql
恢复
登录数据库的恢复
mysql > soure /PATH/TO/BINLOG.sql
or
mysql -uroot -p < /PATH/TO/BINLOG.sql