MySQL——全量,增量备份与恢复(实战篇!)
一,全量备份与恢复
1,进入数据库,创建表,插入表数据
[root@master2 ~]
Enter password:
mysql> create database school;
Query OK, 1 row affected (0.01 sec)
mysql> use school;
Database changed
mysql> create table info(
-> id int(3) not null primary key auto_increment,
-> name varchar(10) not null,
-> score decimal(4,1) not null);
Query OK, 0 rows affected (0.02 sec)
mysql> desc info;
+
| Field | Type | Null | Key | Default | Extra |
+
| id | int(3) | NO | PRI | NULL | auto_increment |
| name | varchar(10) | NO | | NULL | |
| score | decimal(4,1) | NO | | NULL | |
+
3 rows in set (0.00 sec)
mysql> insert into info (name,score) values ('stu01',88),('stu02',77);
Query OK, 2 rows affected (0.02 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from info;
+
| id | name | score |
+
| 1 | stu01 | 88.0 |
| 2 | stu02 | 77.0 |
+
2 rows in set (0.01 sec)
mysql> select * from info limit 1;
+
| id | name | score |
+
| 1 | stu01 | 88.0 |
+
1 row in set (0.00 sec)
2,对数据库进行物理的完全备份
[root@master2 ~]
[root@master2 data]
auto.cnf ibdata1 ib_logfile1 mysql school test
ib_buffer_pool ib_logfile0 ibtmp1 performance_schema sys
[root@master2 data]
[root@master2 school]
db.opt info.frm info.ibd
[root@master2 school]
[root@master2 data]
[root@master2 data]
[root@master2 opt]
mysql-2019-11-26.tar.xz mysql-5.7.20 rh
3,对单个数据库进行逻辑上的备份
[root@master2 opt]
Enter password:
[root@master2 opt]
mysql-2019-11-26.tar.xz mysql-5.7.20 rh school.sql
[root@master2 opt]
...
CREATE TABLE `info` (
`id` int(3) NOT NULL AUTO_INCREMENT,
`name` varchar(10) NOT NULL,
`score` decimal(4,1) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
...
LOCK TABLES `info` WRITE;
;
INSERT INTO `info` VALUES (1,'stu01',88.0),(2,'stu02',77.0);
...
4,对多个数据库进行备份
[root@master2 opt]
Enter password:
[root@master2 opt]
db_school_mysql.sql mysql-2019-11-26.tar.xz mysql-5.7.20 rh school.sql
5,对数据库进行完全备份
[root@master2 opt]
Enter password:
[root@master2 opt]
all.sql mysql-2019-11-26.tar.xz rh
db_school_mysql.sql mysql-5.7.20 school.sql
6,对数据库中的表进行备份
[root@master2 opt]
Enter password:
[root@master2 opt]
all.sql mysql-2019-11-26.tar.xz rh school.sql
db_school_mysql.sql mysql-5.7.20 school_info.sql
7,对数据库中的表结构进行备份
[root@master2 opt]
Enter password:
[root@master2 opt]
all.sql mysql-5.7.20 school_info.sql
db_school_mysql.sql rh school.sql
mysql-2019-11-26.tar.xz school_info_desc.sql
8,基于脚本恢复数据库
[root@master2 opt]
Enter password:
mysql> show databases;
+
| Database |
+
| information_schema |
| mysql |
| performance_schema |
| school |
| sys |
| test |
+
6 rows in set (0.00 sec)
mysql> use school;
Database changed
mysql> show tables;
+
| Tables_in_school |
+
| info |
+
1 row in set (0.00 sec)
mysql> drop table info;
Query OK, 0 rows affected (0.01 sec)
mysql> show tables;
Empty set (0.00 sec)
mysql> source /opt/school.sql
mysql> show tables;
+
| Tables_in_school |
+
| info |
+
1 row in set (0.00 sec)
9,基于外部MySQL命令恢复数据库
mysql> drop table info;
Query OK, 0 rows affected (0.01 sec)
mysql> show tables;
Empty set (0.00 sec)
mysql> quit
Bye
[root@master2 opt]
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@master2 opt]
mysql: [Warning] Using a password on the command line interface can be insecure.
mysql> use school;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
+
| Tables_in_school |
+
| info |
+
1 row in set (0.00 sec)
二,MySQL增量备份及恢复
1,开启二进制日志文件
[root@master2 opt]
[mysqld]
user = mysql
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
character_set_server=utf8
pid-file = /usr/local/mysql/mysql.pid
socket = /usr/local/mysql/mysql.sock
log-bin=mysql-bin
server-id = 1
[root@master2 opt]
[root@master2 opt]
[root@master2 data]
auto.cnf ib_logfile0 mysql performance_schema test
ib_buffer_pool ib_logfile1 mysql-bin.000001 school
ibdata1 ibtmp1 mysql-bin.index sys
2,进行完全备份
[root@master2 data]
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@master2 data]
auto.cnf ib_logfile0 mysql performance_schema test
ib_buffer_pool ib_logfile1 mysql-bin.000001 school
ibdata1 ibtmp1 mysql-bin.index sys
[root@master2 data]
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
[root@master2 data]
auto.cnf ib_logfile0 mysql mysql-bin.index sys
ib_buffer_pool ib_logfile1 mysql-bin.000001 performance_schema test
ibdata1 ibtmp1 mysql-bin.000002 school
3,进入数据库,模拟误操作
[root@master2 data]
mysql: [Warning] Using a password on the command line interface can be insecure.
mysql> use school;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select * from info;
+
| id | name | score |
+
| 1 | st01 | 88.0 |
| 2 | st02 | 77.0 |
+
2 rows in set (0.00 sec)
mysql> insert into info (name,score) values ('by01',66);
Query OK, 1 row affected (0.00 sec)
mysql> select * from info;
+
| id | name | score |
+
| 1 | st01 | 88.0 |
| 2 | st02 | 77.0 |
| 3 | by01 | 66.0 |
+
3 rows in set (0.00 sec)
mysql> delete from info where name='st01';
Query OK, 1 row affected (0.00 sec)
mysql> insert into info (name,score) values ('by02',99);
Query OK, 1 row affected (0.00 sec)
mysql> select * from info;
+
| id | name | score |
+
| 2 | st02 | 77.0 |
| 3 | by01 | 66.0 |
| 4 | by02 | 99.0 |
+
3 rows in set (0.00 sec)
[root@master2 data]
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
[root@master2 data]
[root@master2 data]
[root@master2 opt]
bak.txt mysql-5.7.20 rh school.sql
[root@master2 opt]
...
...
4,基于时间点进行断点恢复
[root@master2 opt]
mysql: [Warning] Using a password on the command line interface can be insecure.
mysql> use school;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> drop table info;
Query OK, 0 rows affected (0.01 sec)
mysql> select * from info;
ERROR 1146 (42S02): Table 'school.info' doesn't exist
mysql> source /opt/school.sql ##恢复完全备份数据库脚本
...
mysql> show tables; ##查看表
+------------------+
| Tables_in_school |
+------------------+
| info |
+------------------+
1 row in set (0.00 sec)
mysql> select * from info; ##查看表数据
+----+------+-------+
| id | name | score |
+----+------+-------+
| 1 | st01 | 88.0 |
| 2 | st02 | 77.0 |
+----+------+-------+
2 rows in set (0.00 sec)
[root@master2 opt]# mysqlbinlog --no-defaults --stop-datetime='2019-11-27 20:14:46' /usr/local/mysql/data/mysql-bin.000002 | mysql -uroot -p123123
##恢复bin.000002中前一个正确的执行语句(从第二个错误语句时间点停止)
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@master2 opt]# mysql -uroot -p123123 ##进入数据库
mysql: [Warning] Using a password on the command line interface can be insecure.
mysql> use school; ##使用数据库
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select * from info; ##查看表数据,恢复了第一次正确操作
+----+------+-------+
| id | name | score |
+----+------+-------+
| 1 | st01 | 88.0 |
| 2 | st02 | 77.0 |
| 3 | by01 | 66.0 |
+----+------+-------+
3 rows in set (0.00 sec)
[root@master2 opt]# mysqlbinlog --no-defaults --start-datetime='2019-11-27 20:15:16' /usr/local/mysql/data/mysql-bin.000002 | mysql -uroot -p123123
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@master2 opt]
mysql: [Warning] Using a password on the command line interface can be insecure.
mysql> use school;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select * from info;
+
| id | name | score |
+
| 1 | st01 | 88.0 |
| 2 | st02 | 77.0 |
| 3 | by01 | 66.0 |
| 4 | by02 | 99.0 |
+
4 rows in set (0.00 sec)
5,基于位置点进行断点恢复
mysql> delete from info where name='by01';
Query OK, 1 row affected (0.01 sec)
mysql> delete from info where name='by02';
Query OK, 1 row affected (0.00 sec)
mysql> select * from info;
+
| id | name | score |
+
| 1 | st01 | 88.0 |
| 2 | st02 | 77.0 |
+
2 rows in set (0.00 sec)
mysql> quit
Bye
[root@master2 opt]
[root@master2 opt]
mysql: [Warning] Using a password on the command line interface can be insecure.
mysql> use school;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select * from info;
+
| id | name | score |
+
| 1 | st01 | 88.0 |
| 2 | st02 | 77.0 |
| 3 | by01 | 66.0 |
+
3 rows in set (0.00 sec)
mysql> quit
Bye
[root@master2 opt]
[root@master2 opt]
mysql: [Warning] Using a password on the command line interface can be insecure.
mysql> use school;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select * from info;
+
| id | name | score |
+
| 1 | st01 | 88.0 |
| 2 | st02 | 77.0 |
| 3 | by01 | 66.0 |
| 4 | by02 | 99.0 |
+
4 rows in set (0.00 sec)
6,对于增量备份全部恢复
[root@master2 opt]