mysql全量、增量备份与恢复
一、备份的概念
1.数据备份是指对重要数据进行备份,避免因丢失带来的损失。
数据丢失的原因
程序错误
人为操作错误
运算错误
磁盘故障
灾难和盗窃
2.备份类型
物理备份
对数据库操作系统物理文件(如数据文件、日志文件等)的备份。
适用于在出现问题时需要快速恢复的大型重要数据库。
物冷备:在数据库关闭状态下进行备份操作。
物热备:在数据库处于运行状态时进行备份操作,该备份方法依赖数据库的日志文件。
物温备:数据库锁定表格(不可写入但可读)的状态下进行备份操作。
逻辑备份
对数据库逻辑组件(如表等数据库对象)的备份,表示为逻辑数据库结构(create database,create table语句)和内容(insert 语句或分隔文本文件)的信息。
适用于可以编辑数据值或表结构较小的数据量,或者在不同的机器体系结构上重新创建数据。
按数据库的备份策略角度,数据库的备份可分为完全备份、差异备份和增量备份。
文字说明
完全备份:每次对数据进行完整备份。备份时间长,占用磁盘空间大。
差异备份:备份那些自从上次完全备份之后被修改过的所有文件,备份时间节点是从上次完整备份起。
增量备份:只有那些在上次完全备份或者增量备份后被修改的文件才会被备份。以上次完整备份或上次增量备份的时间为时间点,仅备份这之间的数据变化,因而备份的数据量小,占用空间小,备份速度快。
3.常见备份方法
直接打包数据库文件(物理冷备份)
专用备份工具(mysqldump)
二进制日志增量备份
第三方工具备份
mysqldump语法
格式1
备份指定库中的部分表
mysqldump [选项] 库名 [表名 1] [表名 2] … > /备份路径/备份文件名
格式2
备份一个或多个完整的库(包括其中所有的表)
mysqldump [选项] --databases 库名1 [库名 2] … > /备份路径/备份文件名
格式3
备份mysql服务器中所有的库
mysqldump [选项] --all-databases > /备份路径/备份文件名
恢复数据库
mysql [选项] [库名] [表名] < /备份路径/备份文件名 (恢复表时,该表所在库必须存在且必须在命令行指定库名
mysql增量恢复(使用binlog文件)
一般恢复
mysqlbinlog [--no-defaults] 增量备份文件 | mysql -u 用户名 -p 密码
基于位置的恢复
-恢复数据到指定位置
mysqlbinlog --stop-position='操作id' 二进制日志 | mysql -u 用户名 -p 密码
-从指定的位置开始恢复数据
mysqlbinlog --start-position='操作id' 二进制日志 | mysql -u 用户名 -p 密码
基于时间点的恢复
-从日志开头截止到某个时间点的恢复
mysqlbinlog [--no-defaults] --stop-datetime='年-月-日 小时:分钟:秒'
-从某个时间点到日志结尾的恢复
mysqlbinlog [--no-defaults] --start-datetime='年-月-日 小时:分钟:秒' 二进制日志 | mysql -u 用户名 -p 密码
-从某个时间点到某个时间点的恢复
mysqlbinlog [--no-defaults] --start-datetime='年-月-日 小时:分钟:秒' --stop-datetime='年-月-日 小时:分钟:秒' 二进制日志 | mysql -u 用户名 -p 密码
二、数据库的备份,还原演示
PS:binlog_format = MIXED # 定义二进制日志的记录格式为混合模式,不开启二进制日志文件不会记录insert、update、delete语句。1.物理冷备份与恢复[root@localhost ~]# systemctl stop mysqld
[root@localhost ~]# mkdir /backup
[root@localhost ~]# tar zcf /backup/mysql_all-$(date +%F).tar.gz /usr/local/mysql/data/ # $(date +%F) 表示使用日期函数,%F的格式是(YY-DD-MM)。
tar: 从成员名中删除开头的“/”
[root@localhost ~]# ls -l /backup
总计 2024
-rw-r--r--. 1 root root 2070063 4月21日 09:37 mysql_all-2025-04-21.tar.gz
[root@localhost ~]# mkdir bak
[root@localhost ~]# mv /usr/local/mysql/data/ /root/bak/ #将数据库的数据文件移动到/bak目录下,模拟故障。
[root@localhost ~]# mkdir restore
[root@localhost ~]# tar zxf /backup/mysql_all-2025-04-21.tar.gz -C restore/
[root@localhost ~]# mv restore/usr/local/mysql/data/ /usr/local/mysql/ # 还原数据。
[root@localhost ~]# systemctl start mysqld2.mysqldump备份与恢复
[root@localhost ~]# mysqldump -u root -pooos123 mysql user > mysql-user.sql # 备份mysql库的user表
[root@localhost ~]# mysqldump -uroot -pooos123 --databases aaa > aaa.sql # 备份aaa库(包含其中所有的表)
[root@localhost ~]# mysqldump -uroot -pooos123 --opt --all-databases > all-data.sql # 备份所有库
[root@localhost ~]# grep -v "^--" aaa.sql | grep -v "^/" | grep -v "^$" # 查看备份文件中的数据库操作语句。“/*…*”部分或以“--”开头的行表示注释信息。
CREATE DATABASE /*!32312 IF NOT EXISTS*/ `aaa` /*!40100 DEFAULT CHARACTER SET utf8mb3 */ /*!80016 DEFAULT ENCRYPTION='N' */;
USE `aaa`;
DROP TABLE IF EXISTS `oo`;
CREATE TABLE `oo` (`id` int DEFAULT NULL,`name` char(25) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;
LOCK TABLES `oo` WRITE;
UNLOCK TABLES;mysql> select database();
+------------+
| database() |
+------------+
| aaa |
+------------+
1 row in set (0.01 sec)
mysql> show tables;
+---------------+
| Tables_in_aaa |
+---------------+
| oo |
+---------------+
1 row in set (0.01 sec)
mysql> drop table oo;
Query OK, 1 row affected (0.03 sec)[root@localhost ~]# mysql -uroot -pooos123 aaa < aaa.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@localhost ~]# mysql -uroot -pooos123 -e 'show tables from aaa;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+---------------+
| Tables_in_aaa |
+---------------+
| oo |
+---------------+[root@localhost ~]# mysql -uroot -pooos123 -e 'drop database aaa;'
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@localhost ~]# mysql -uroot -pooos123 -e 'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| a |
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
[root@localhost ~]# mysql -uroot -pooos123 < aaa.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@localhost ~]# mysql -uroot -pooos123 -e 'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| a |
| aaa |
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+mysql> drop database aaa;
Query OK, 1 row affected (0.02 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| a |
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)mysql> source /root/aaa.sql
Query OK, 0 rows affected (0.00 sec)Query OK, 0 rows affected (0.00 sec)Query OK, 0 rows affected (0.00 sec)Query OK, 1 row affected, 1 warning (0.01 sec)Database changed
Query OK, 0 rows affected (0.01 sec)mysql> show databases;
+--------------------+
| Database |
+--------------------+
| a |
| aaa 3.一般、基于位置和基于时间点的恢复
mysql> create database client;
Query OK, 1 row affected (0.01 sec)mysql> use client;
Database changed
mysql> create table user_info(identityCard char(20) not null,name char(20) not null,gender char(4),UserID char(10) not null,expenses int(10));
Query OK, 0 rows affected, 1 warning (0.06 sec)mysql> insert into user_info values('000006','张三','男','016','10');
Query OK, 1 row affected (0.01 sec)mysql> insert into user_info values('000007','李四','女','017','91');
Query OK, 1 row affected (0.01 sec)mysql> insert into user_info values('000007','王五','女','018','23');
Query OK, 1 row affected (0.00 sec)mysql> select * from user_info-> ;
+--------------+--------+--------+--------+----------+
| identityCard | name | gender | UserID | expenses |
+--------------+--------+--------+--------+----------+
| 000006 | 张三 | 男 | 016 | 10 |
| 000007 | 李四 | 女 | 017 | 91 |
| 000007 | 王五 | 女 | 018 | 23 |
+--------------+--------+--------+--------+----------+
3 rows in set (0.00 sec)
先进行一次完全备份
[root@localhost data]# mkdir /mysql_bak
[root@localhost data]# mysqldump -uroot -pooos123 client user_info > /mysql_bak/client_userinfo-$(date +%F).sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@localhost data]# ls /mysql_bak
client_userinfo-2025-04-21.sql继续插入新的数据并进行增量备份
mysql> use client;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -ADatabase changed
mysql> insert into user_info values('000009','赵六','男','019','37');
Query OK, 1 row affected (0.01 sec)mysql> insert into user_info values('000010','孙七','男','020','36');
Query OK, 1 row affected (0.01 sec)mysql> select * from user_info;
+--------------+--------+--------+--------+----------+
| identityCard | name | gender | UserID | expenses |
+--------------+--------+--------+--------+----------+
| 000006 | 张三 | 男 | 016 | 10 |
| 000007 | 李四 | 女 | 017 | 91 |
| 000007 | 王五 | 女 | 018 | 23 |
| 000009 | 赵六 | 男 | 019 | 37 |
| 000010 | 孙七 | 男 | 020 | 36 |
+--------------+--------+--------+--------+----------+
5 rows in set (0.00 sec)[root@localhost mysql_bak]# mkdir /mysql_bak
[root@localhost mysql_bak]# mysqldump -uroot -pooos123 client user_info > /mysql_bak/client_userinfo-$(date +%F).sql[root@localhost mysql_bak]# mysqladmin -uroot -p flush-logs # 新日志为7,只保存了赵六和孙七的数据。[root@localhost mysql_bak]# mysql -uroot -pooos123
mysql> insert into user_info values('000009','赵六','男','019','37');
mysql> insert into user_info values('000009','孙七','男','020','36');[root@localhost mysql_bak]# mysqladmin -uroot -p flush-logs [root@localhost mysql_bak]# ls -l /usr/local/mysql/data/binlog.*[root@localhost mysql_bak]# cp binlog.000007 /mysql_bak[root@localhost mysql_bak]# mysql -uroot -pooos123 client </mysql_bak/client_userinfo-2025-04-21.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@localhost mysql_bak]# mysql -uroot -pooos123 -e 'select * from client.user_info;'
mysql: [Warning] Using a password on the command line interface can be insecure.
恢复完全备份
+--------------+--------+--------+--------+----------+
| identityCard | name | gender | UserID | expenses |
+--------------+--------+--------+--------+----------+
| 000006 | 张三 | 男 | 016 | 10 |
| 000007 | 李四 | 女 | 017 | 91 |
| 000007 | 王五 | 女 | 018 | 23 |
+--------------+--------+--------+--------+----------+
恢复增量备份
[root@localhost mysql_bak]# mysqlbinlog --no-defaults /mysql_bak/binlog.000007 | mysql -uroot -pooos123
[root@localhost mysql_bak]# mysql -uroot -pooos123 -e 'select * from client.user_info;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------+--------+--------+--------+----------+
| identityCard | name | gender | UserID | expenses |
+--------------+--------+--------+--------+----------+
| 000006 | 张三 | 男 | 016 | 10 |
| 000007 | 李四 | 女 | 017 | 91 |
| 000007 | 王五 | 女 | 018 | 23 |
| 000009 | 赵六 | 男 | 019 | 37 |
| 000009 | 孙七 | 男 | 020 | 36 |
+--------------+--------+--------+--------+----------+基于位置恢复
[root@localhost mysql_bak]# mysql -uroot -pooos123 -e 'drop table client.user_info;'
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@localhost mysql_bak]# mysql -uroot -pooos123 -e 'select * from client.user_info;'
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1146 (42S02) at line 1: Table 'client.user_info' doesn't exist
[root@localhost mysql_bak]# mysql -uroot -pooos123 client </mysql_bak/client_userinfo-2025-04-21.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@localhost mysql_bak]# mysql -uroot -pooos123 -e 'select * from client.user_info;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------+--------+--------+--------+----------+
| identityCard | name | gender | UserID | expenses |
+--------------+--------+--------+--------+----------+
| 000006 | 张三 | 男 | 016 | 10 |
| 000007 | 李四 | 女 | 017 | 91 |
| 000008 | 王五 | 女 | 018 | 23 |
+--------------+--------+--------+--------+----------+
[root@localhost mysql_bak]# mysqlbinlog --no-defaults --stop-position='498' /mysql_bak/binlog.000002 | mysql -uroot -pooos123
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@localhost mysql_bak]# mysql -uroot -pooos123 -e 'select * from client.user_info;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------+--------+--------+--------+----------+
| identityCard | name | gender | UserID | expenses |
+--------------+--------+--------+--------+----------+
| 000006 | 张三 | 男 | 016 | 10 |
| 000007 | 李四 | 女 | 017 | 91 |
| 000008 | 王五 | 女 | 018 | 23 |
| 000009 | 赵六 | 男 | 019 | 37 |
+--------------+--------+--------+--------+----------+[root@localhost mysql_bak]# mysqlbinlog --no-defaults --start-position='663' --stop-position='808' /mysql_bak/binlog.000002 | mysql -uroot -pooos123
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@localhost mysql_bak]# mysql -uroot -pooos123 -e 'select * from client.user_info;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------+--------+--------+--------+----------+
| identityCard | name | gender | UserID | expenses |
+--------------+--------+--------+--------+----------+
| 000006 | 张三 | 男 | 016 | 10 |
| 000007 | 李四 | 女 | 017 | 91 |
| 000008 | 王五 | 女 | 018 | 23 |
| 000009 | 赵六 | 男 | 019 | 37 ||
| 000010 | 孙七 | 男 | 020 | 36 |
+--------------+--------+--------+--------+----------+基于时间点恢复
[root@localhost mysql_bak]# mysql -uroot -pooos123 -e 'drop table client.user_info;'
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@localhost mysql_bak]# mysql -uroot -pooos123 client </mysql_bak/client_userinfo-2025-04-21.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@localhost mysql_bak]# mysql -uroot -pooos123 -e 'select * from client.user_info;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------+--------+--------+--------+----------+
| identityCard | name | gender | UserID | expenses |
+--------------+--------+--------+--------+----------+
| 000006 | 张三 | 男 | 016 | 10 |
| 000007 | 李四 | 女 | 017 | 91 |
| 000008 | 王五 | 女 | 018 | 23 |
+--------------+--------+--------+--------+----------+
[root@localhost mysql_bak]# mysqlbinlog --no-defaults --start-datetime='2025-04-21 12:26:17' --stop-datetime='2025-04-21 12:26:39' /mysql_bak/binlog.000002 | mysql -uroot -pooos123
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@localhost mysql_bak]# mysql -uroot -pooos123 -e 'select * from client.user_info;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------+--------+--------+--------+----------+
| identityCard | name | gender | UserID | expenses |
+--------------+--------+--------+--------+----------+
| 000006 | 张三 | 男 | 016 | 10 |
| 000007 | 李四 | 女 | 017 | 91 |
| 000008 | 王五 | 女 | 018 | 23 |
| 000010 | 孙七 | 男 | 020 | 36 |
+--------------+--------+--------+--------+----------+4.mysql的gtid和xtrabackup恢复
[root@localhost mysql_bak]# vim /etc/my.cnf
gtid_mode=ON
enforce_gtid_consistency=ONmysql> show global variables like 'gtid_mode'; # 查看gtid是否开启。
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| gtid_mode | ON |
+---------------+-------+mysql> create database test;
Query OK, 1 row affected (0.01 sec)mysql> use test;
Database changedmysql> create table user(id int);
Query OK, 0 rows affected (0.02 sec)mysql> insert into user values(1);
Query OK, 1 row affected (0.02 sec)mysql> insert into user values(2);
Query OK, 1 row affected (0.01 sec)mysql> insert into user values(3);
Query OK, 1 row affected (0.00 sec)mysql> show master status;
+---------------+----------+--------------+------------------+------------------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------+----------+--------------+------------------+------------------------------------------+
| binlog.000001 | 1417 | | | 5809cdd9-1e4b-11f0-b091-000c2923905d:1-5 |
+---------------+----------+--------------+------------------+------------------------------------------+
## 1-5表示第一个事务到第五个事务的所有操作。[root@localhost mysql_bak]# mysqldump -uroot -pooos123 --databases test > test.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
Warning: A partial dump from a server that has GTIDs will by default include the GTIDs of all transactions, even those that changed suppressed parts of the database. If you don't want to restore GTIDs, pass --set-gtid-purged=OFF. To make a complete dump, pass --all-databases --triggers --routines --events.
Warning: A dump from a server that has GTIDs enabled will by default include the GTIDs of all transactions, even those that were executed during its extraction and might not be represented in the dumped data. This might result in an inconsistent data dump.
In order to ensure a consistent backup of the database, pass --single-transaction or --lock-all-tables or --master-data. [root@localhost mysql_bak]# grep -i gtid test.sql
-- GTID state at the beginning of the backup
SET @@GLOBAL.GTID_PURGED=/*!80000 '+'*/ '5809cdd9-1e4b-11f0-b091-000c2923905d:1-5';mysql> drop database test;
Query OK, 1 row affected (0.04 sec)mysql> show master status;
+---------------+----------+--------------+------------------+------------------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------+----------+--------------+------------------+------------------------------------------+
| binlog.000001 | 2188 | | | 5809cdd9-1e4b-11f0-b091-000c2923905d:1-8 |
+---------------+----------+--------------+------------------+------------------------------------------+
1 row in set (0.00 sec)[root@localhost data]# mysqlbinlog --include-gtids='5809cdd9-1e4b-11f0-b091-000c2923905d:1-7' /usr/local/mysql/data/binlog.000001 > /mysqlbak.sql
[root@localhost data]# ls /
afs boot home lost+found mysql_bak proc sbin tmp
backup dev lib media mysqlbak.sql root srv usr
bin etc lib64 mnt opt run sys var[root@localhost data]# mysql -uroot -pooos123 -e 'reset master;'恢复全量
[root@localhost mysql_bak]# mysql -uroot -pooos123 < test.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@localhost mysql_bak]# mysql -uroot -pooos123 -e 'select * from test.user;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+------+
| id |
+------+
| 1 |
| 2 |
| 3 |
+------+
恢复增量
[root@localhost mysql_bak]# mysql -uroot -pooos123 < /mysqlbak.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@localhost mysql_bak]# mysql -uroot -pooos123 -e 'select * from test.user;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+------+
| id |
+------+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
+------+