myloader恢复主要流程
1、首先由myloader主线程完成建库建表,依次将备份目录下建库和建表文件执行应用到目标数据库实例中;
2、接着myloader主线程会生成多个工作线程,由这些工作线程将所有database.table.*.sql文件中的记录导入到对应表中,这个阶段是并行的,并行粒度为文件,工作线程完成所有database.table.*.sql 文件数据导入后销毁;
3、最后主线程将创建函数、存储进程、事件,以及创建表视图、触发器的文件执行导入对应数据库和表中。
myloader --help
Usage:
myloader [OPTION...] multi-threaded MySQL loader
Help Options:
-?, --help Show help options
Application Options:
-d, --directory Directory of the dump to import之前备份好的现在需要导入的文件夹
-q, --queries-per-transaction Number of queries per transaction, default 1000每次事物执行的查询数量,默认是1000
-o, --overwrite-tables Drop tables if they already exist如果要恢复的表存在,则先drop掉该表,使用该参数,需要备份时候要备份表结构
-B, --database An alternative database to restore into还原到指定的数据库
-s, --source-db Database to restore选择被还原的数据库,将这个数据库数据还原到--database指定的数据库里
-e, --enable-binlog Enable binary logging of the restore data启用还原数据的二进制日志
-h, --host 连接的主机名
-u, --user 用来备份的用户名
-p, --password 用户密码
-P, --port 连接端口
-S, --socket 使用socket通信时的socket文件
-t, --threads 开启的备份线程数,默认是4
-C, --compress-protocol 压缩与mysql通信的数据
-V, --version 显示版本号
-v, --verbose 输出信息模式, 0 = silent, 1 = errors, 2 = warnings, 3 = info, 默认为2
myloader恢复流程图
实战myloader进行数据恢复
单库的备份与恢复
#单库的备份与恢复
#创建测试的数据
mysql> create database test;
mysql> use test;
Database changed
mysql> create table mytest(id int,name varchar(10));
mysql> insert into mytest values (1,"lulei");
mysql> commit;
#mydumper备份单库
[root@localhost backup]# mydumper -u root -p root --database test --triggers -E --events --routines --less-locking --verbose=3 --outputdir test --logfile test.log
[root@localhost backup]# ls
test test.log
[root@localhost backup]# cat test.log
2020-06-25 14:08:37 [INFO] - Connected to a MySQL server
2020-06-25 14:08:37 [INFO] - Started dump at: 2020-06-25 14:08:37
2020-06-25 14:08:37 [INFO] - Written master status
2020-06-25 14:08:37 [INFO] - Thread 5 connected using MySQL connection ID 35
2020-06-25 14:08:37 [INFO] - Thread 6 connected using MySQL connection ID 36
2020-06-25 14:08:37 [INFO] - Thread 7 connected using MySQL connection ID 37
2020-06-25 14:08:37 [INFO] - Thread 8 connected using MySQL connection ID 38
2020-06-25 14:08:37 [INFO] - Thread 1 connected using MySQL connection ID 39
2020-06-25 14:08:37 [INFO] - Thread 2 connected using MySQL connection ID 40
2020-06-25 14:08:37 [INFO] - Thread 3 connected using MySQL connection ID 41
2020-06-25 14:08:37 [INFO] - Thread 4 connected using MySQL connection ID 42
2020-06-25 14:08:37 [INFO] - Thread 5 shutting down
2020-06-25 14:08:37 [INFO] - Thread 6 shutting down
2020-06-25 14:08:37 [INFO] - Thread 7 shutting down
2020-06-25 14:08:37 [INFO] - Thread 8 shutting down
2020-06-25 14:08:37 [INFO] - Thread 1 dumping data for `test`.`mytest`
2020-06-25 14:08:37 [INFO] - Thread 2 dumping schema for `test`.`mytest`
2020-06-25 14:08:37 [INFO] - Non-InnoDB dump complete, unlocking tables
2020-06-25 14:08:37 [INFO] - Thread 3 shutting down
2020-06-25 14:08:37 [INFO] - Thread 4 shutting down
2020-06-25 14:08:37 [INFO] - Thread 2 shutting down
2020-06-25 14:08:37 [INFO] - Thread 1 shutting down
2020-06-25 14:08:37 [INFO] - Finished dump at: 2020-06-25 14:08:37
[root@localhost test]# cat metadata
Started dump at: 2020-06-25 14:08:37
SHOW MASTER STATUS:
Log: mysql-bin.000001
Pos: 762
GTID:
Finished dump at: 2020-06-25 14:08:37
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 762 | | | |
+------------------+----------+--------------+------------------+-------------------+
[root@localhost test]# ll
total 16
-rw-r--r-- 1 root root 136 Jun 25 14:08 metadata
-rw-r--r-- 1 root root 230 Jun 25 14:08 test.mytest-schema.sql
-rw-r--r-- 1 root root 143 Jun 25 14:08 test.mytest.sql
-rw-r--r-- 1 root root 65 Jun 25 14:08 test-schema-create.sql
[root@localhost test]# cat test.mytest-schema.sql
/*!40101 SET NAMES binary*/;
/*!40014 SET FOREIGN_KEY_CHECKS=0*/;
/*!40103 SET TIME_ZONE='+00:00' */;
CREATE TABLE `mytest` (
`id` int(11) DEFAULT NULL,
`name` varchar(10) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
[root@localhost test]# cat test-schema-create.sql
CREATE DATABASE `test` /*!40100 DEFAULT CHARACTER SET latin1 */;
[root@localhost test]# cat test.mytest.sql
/*!40101 SET NAMES binary*/;
/*!40014 SET FOREIGN_KEY_CHECKS=0*/;
/*!40103 SET TIME_ZONE='+00:00' */;
INSERT INTO `mytest` VALUES
(1,"lulei");
#删除库,下面使用myloader进行恢复
mysql> drop database test;
Query OK, 1 row affected (0.01 sec)
[root@localhost backup]# myloader -u root -p root --database test --verbose=3 --directory test
** Message: 4 threads created
** Message: Creating database `test`
** Message: Creating table `test`.`mytest`
** Message: Thread 1 restoring `test`.`mytest` part 0
** Message: Thread 2 shutting down
** Message: Thread 3 shutting down
** Message: Thread 4 shutting down
** Message: Thread 1 shutting down
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test |
+--------------------+
mysql> use test;
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_test |
+----------------+
| mytest |
+----------------+
mysql> select * from mytest;
+------+-------+
| id | name |
+------+-------+
| 1 | lulei |
+------+-------+
#可以看到数据恢复成功
全库备份与多库恢复
[root@localhost backup]# mydumper -u root -p root --triggers -E --events --routines --less-locking --verbose=3 --outputdir alldatabase
mysql> drop database mysql;
mysql> drop database test;
[root@localhost backup]# myloader -u root -p root --verbose=3 --directory alldatabase
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test |
+--------------------+
如果是备份了多个数据库,建议恢复数据库的话,目录里面只含有需要恢复的数据库的数据文件
(在恢复多库的时候,要将全备份里面和需要恢复的数据库不相干的数据库文件删除,避免恢复了其他不需要恢复的数据库)
恢复表
-rw-r--r-- 1 root root 230 Jun 25 14:21 test.mytest-schema.sql
-rw-r--r-- 1 root root 143 Jun 25 14:21 test.mytest.sql
-rw-r--r-- 1 root root 65 Jun 25 14:21 test-schema-create.sql
#只需要将备份的表的.sql文件在数据库当中执行
[root@localhost alldatabase]# cat test.mytest-schema.sql
/*!40101 SET NAMES binary*/;
/*!40014 SET FOREIGN_KEY_CHECKS=0*/;
/*!40103 SET TIME_ZONE='+00:00' */;
CREATE TABLE `mytest` (
`id` int(11) DEFAULT NULL,
`name` varchar(10) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
[root@localhost alldatabase]# cat test.mytest.sql
/*!40101 SET NAMES binary*/;
/*!40014 SET FOREIGN_KEY_CHECKS=0*/;
/*!40103 SET TIME_ZONE='+00:00' */;
INSERT INTO `mytest` VALUES
(1,"lulei");
[root@localhost alldatabase]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 67
Server version: 5.7.30-log MySQL Community Server (GPL)
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use test;
Database changed
mysql> source test.mytest-schema.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)
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| mytest |
+----------------+
mysql> source test.mytest.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 (0.01 sec)
mysql> select * from mytest;
+------+-------+
| id | name |
+------+-------+
| 1 | lulei |
+------+-------+
myloader用法总结
myloader恢复案例:
1.单库还原
mydumper -u root -p 123456 --database db1 --outputdir db1
drop database db1;--删除数据库,然后进行恢复
myloader -u root -p 123456 --database db1 --directory db1 --verbose=3
如果是备份了多个数据库,建议恢复数据库的话,目录里面只含有需要恢复的数据库的数据文件
2.多个库还原
mydumper -u root -p 123456 --outputdir db12 --verbose=3
myloader -u root -p 123456 --directory db12 --verbose=3
3.还原表
source db1.tbs1-schema.sql 还原表结构
source db1.tbs1.sql 还原表数据
4.如果备份时候是压缩的,如何还原
mydumper -u root -p 123456 --database db1 --compress --outputdir db1 --verbose=3
myloader -u root -p 123456 --database db1 --directory db1 --verbose=3
5.如何跨库恢复
备份db1的数据,恢复到db2数据库里去。
mydumper -u root -p 123456 --database db1 --outputdir db1 --verbose=3
myloader -u root -p 123456 --database db2 --overwrite-tables --directory db1 -verbose=3