MySQL 的物理备份

物理备份的原理通常是基于物理文件的拷贝,和逻辑备份相比,它的最大优点是备份速度更快。本小节将主要介绍 MySQL 的物理备份方法。

1. 冷备

一般来说,冷备就是停止数据库服务,拷贝数据文件。这种方法对所有的存储引擎都适合,但一般很少使用到冷备,因为需要停机窗口。

冷备的操作步骤如下:

停止 MySQL 服务,在操作系统层面复制数据文件和日志文件到备份目录。

[mysql@localhost ~]$ service mysqld stop
Shutting down MySQL.......... SUCCESS! 

[mysql@localhost ~]$ scp -r /mysql/data/ /mysql/dbbackup/

热备则刚好相反,不影响数据库的正常运行,通过在线备份数据文件和 binlog 日志实现。

2. InnoDB 的热备

目前,比较流行的 InnoDB 存储引擎的热备工具是 Xtrabackup。 xtrabackup 是 Percona 公司开发的一个用于 MySQL 数据库物理热备的备份工具,支持 MySQL、Percona server 和 MariaDB,开源免费,是目前较为受欢迎的主流备份工具 ,它能对 InnoDB 数据库和 XtraDB 存储引擎的数据库非阻塞地备份(对于 MyISAM 的备份同样需要加表锁)。

Xtrabackup 优点:

  1. 备份速度快,物理备份可靠;
  2. 备份过程不会打断正在执行的事务(无需锁表);
  3. 能够基于压缩等功能节约磁盘空间和流量;
  4. 自动备份校验;
  5. 还原速度快;
  6. 可以流传将备份传输到另外一台机器上;
  7. 在不增加服务器负载的情况备份数据。

实际案例:Xtrabackup 全量备份:

[root@localhost ~]# innobackupex --defaults-file=/etc/my.cnf --user=root --password=MyNewPass4! --port=3306 --no-timestamp /mysql/dbbackup/
200823 21:11:33 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!".

200823 21:11:33  version_check Connecting to MySQL server with DSN 'dbi:mysql:;mysql_read_default_group=xtrabackup;port=3306;mysql_socket=/mysql/tmp/mysql.sock' as 'root'  (using password: YES).
200823 21:11:33  version_check Connected to MySQL server
200823 21:11:33  version_check Executing a version check against the server...
200823 21:11:33  version_check Done.
200823 21:11:33 Connecting to MySQL server host: localhost, user: root, password: set, port: 3306, socket: /mysql/tmp/mysql.sock

......

200823 21:11:48 Finished backing up non-InnoDB tables and files
200823 21:11:48 [00] Writing /mysql/dbbackup/xtrabackup_binlog_info
200823 21:11:48 [00]        ...done
200823 21:11:48 Executing FLUSH NO_WRITE_TO_BINLOG ENGINE LOGS...
xtrabackup: The latest check point (for incremental): '14533834254'
xtrabackup: Stopping log copying thread.
.200823 21:11:48 >> log scanned up to (14533834263)

200823 21:11:49 Executing UNLOCK TABLES
200823 21:11:49 All tables unlocked
200823 21:11:49 [00] Copying ib_buffer_pool to /mysql/dbbackup/ib_buffer_pool
200823 21:11:49 [00]        ...done
200823 21:11:49 Backup created in directory '/mysql/dbbackup/'
MySQL binlog position: filename 'mysql-bin.000022', position '190', GTID of the last change '294ae4cf-be0f-11e7-8269-fa163e665653:1-2769,
a373c879-3a2c-11e8-bb78-fa163e665653:1-5,
aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa:1-40:1000012-1000013'
200823 21:11:49 [00] Writing /mysql/dbbackup/backup-my.cnf
200823 21:11:49 [00]        ...done
200823 21:11:49 [00] Writing /mysql/dbbackup/xtrabackup_info
200823 21:11:49 [00]        ...done
xtrabackup: Transaction log of lsn (14533834254) to (14533834263) was copied.
200823 21:11:49 completed OK!

备份完成后,备份目录将产生如下文件:

[root@localhost ~]$ ls -lh /mysql/dbbackup/
total 101M
-rw-r-----  1 root root  433 Aug 23 21:11 backup-my.cnf
-rw-r-----  1 root root  42K Aug 23 21:11 ib_buffer_pool
-rw-r-----  1 root root 100M Aug 23 21:11 ibdata1
drwxr-x---  2 root root 4.0K Aug 23 21:11 mysql
drwxr-x---  2 root root 4.0K Aug 23 21:11 performance_schema
drwxr-x---  2 root root  12K Aug 23 21:11 sys
drwxr-x---  2 root root 4.0K Aug 23 21:11 tempdb
-rw-r-----  1 root root  166 Aug 23 21:11 xtrabackup_binlog_info
-rw-r-----  1 root root  121 Aug 23 21:11 xtrabackup_checkpoints
-rw-r-----  1 root root  703 Aug 23 21:11 xtrabackup_info
-rw-r-----  1 root root 2.5K Aug 23 21:11 xtrabackup_logfile

实际案例:Xtrabackup 增量备份:

在进行增量备份之前,首先要有一次全量备份,第一次增量是基于全备份,之后的增量是基于上一次的增量备份。

[root@localhost ~]# innobackupex --defaults-file=/etc/my.cnf --user=root --password=MyNewPass4! --port=3306 --no-timestamp /mysql/hotbackup/base

-- 从xtrabackup_checkpoints文件可以看出,备份类型为全备份,lsn号从0至14533834254
[root@localhost ~]# cat /mysql/hotbackup/base/xtrabackup_checkpoints 
backup_type = full-backuped
from_lsn = 0
to_lsn = 14533834254
last_lsn = 14533834263
compact = 0
recover_binlog_info = 0

表 customer 插入新的数据:

mysql> insert into customer(id,last_name,first_name,birth_date,gender,balance) values(3,333,333,'2020-08-10',1,30);
Query OK, 1 row affected (0.00 sec)

mysql> insert into customer(id,last_name,first_name,birth_date,gender,balance) values(4,444,444,'2020-09-10',1,40);
Query OK, 1 row affected (0.00 sec)

第一次增量备份:

[root@localhost ~]# innobackupex --defaults-file=/etc/my.cnf --user=root --password=MyNewPass4! --port=3306 --incremental --no-timestamp /mysql/hotbackup/inc1 --incremental-basedir=/mysql/hotbackup/base

-- 从xtrabackup_checkpoints文件可以看出,备份类型为增量备份,lsn号从lsn 14533834254至lsn 14533838790,14533834254为全备份的to_lsn,表示备份从lsn 14533834254以来的增量变化。
[root@localhost ~]# cat /mysql/hotbackup/inc1/xtrabackup_checkpoints 
backup_type = incremental
from_lsn = 14533834254
to_lsn = 14533838790
last_lsn = 14533838799
compact = 0
recover_binlog_info = 0

3.小结

本小节主要介绍 MySQL 的物理备份方法:冷备和热备。

冷备主要是停止 MySQL 服务,在操作系统层面复制数据文件和日志文件到备份目录。

热备主要是针对 InnoDB 存储引擎,介绍了开源备份工具 -Xtrabackup 的使用方法:全量备份和增量备份。