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 的备份同样需要加表锁)。
- 备份速度快,物理备份可靠;
- 备份过程不会打断正在执行的事务(无需锁表);
- 能够基于压缩等功能节约磁盘空间和流量;
- 自动备份校验;
- 还原速度快;
- 可以流传将备份传输到另外一台机器上;
- 在不增加服务器负载的情况备份数据。
[root@localhost ~]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!
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
[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
在进行增量备份之前,首先要有一次全量备份,第一次增量是基于全备份,之后的增量是基于上一次的增量备份。
[root@localhost ~][root@localhost ~]backup_type = full-backuped
from_lsn = 0to_lsn = 14533834254last_lsn = 14533834263compact = 0recover_binlog_info = 0
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 ~][root@localhost ~]backup_type = incremental
from_lsn = 14533834254to_lsn = 14533838790last_lsn = 14533838799compact = 0recover_binlog_info = 0
3.小结
本小节主要介绍 MySQL 的物理备份方法:冷备和热备。
冷备主要是停止 MySQL 服务,在操作系统层面复制数据文件和日志文件到备份目录。
热备主要是针对 InnoDB 存储引擎,介绍了开源备份工具 -Xtrabackup 的使用方法:全量备份和增量备份。