写在前面
gs_restore是对通过gs_dump导出的文件进行导入,主要功能可以导入到数据库,也可以导入到一个文本文件。
学习环境
NODE1(主库) | NODE2(从库) | |
Hostname | pkt_mogdb1 | pkt_mogdb2 |
IP | 10.80.9.249 | 10.80.9.250 |
磁盘 | 20G | 20G |
内存 | 2G | 2G |
数据导入
gs_restore默认是以追加的形式导入,为了避免主键冲突或者数据重复尽量使用-c参数,先删除表。如果执行只导入数据不导入表结构,需要先把表中的数据truncate掉,然后再执行导入。
官方文档中导入时-F文件格式只有c|d|t,并没有p选项,不知道支不支持文本文件格式,需要测试一下,测试用的导出文件都是通过上一篇笔记《mogdb学习-从3开始(gs_dump)》中导出的数据,另MogDB在导入的时候自动判断文件格式,不需要指定F。
1、测试文本文件格式导入
尝试使用-F p导出的全量数据库文件dump1.sql
dump1.sql中总共有4个表分别mytable、mytable1、dump_tables、dump_tables_1包括表的ddl和数据
[omm@pkt_mogdb1 data]$ gs_restore -d db_mogdb dump1.sql input file appears to be a text format dump. Please use gsql. |
提示 不支持文本文件格式,请使用gsql工具
[omm@pkt_mogdb1 data]$ gsql -d db_mogdb -h 10.80.9.249 -U zkh -p 26000 -W Zkh12345678 -f dump1.sql |
因为执行sql脚本之前没有删除表,并且我的表中没有主键,因此导入的时候创建表会失败,但是数据会导入成功
- 测试自定义归档模式
tempdump是通过-F=c导出的文件格式,也是按照数据库导出,包含4个表和4个表的数据。
2.1导入全库的时候不指定-c参数
导入前的数据情况
select 'dump_tables',count(*) from dump_tables union all select 'dump_tables_1',count(*) from dump_tables_1; #条数 dump_tables 256 dump_tables_1 258 |
备份文件中是dump_tables是128条,dump_tables_1是129条
gs_restore -d db_mogdb /home/omm/tempdump |
输出结果
start restore operation ... Error while PROCESSING TOC: Error from TOC entry 13; 2615 16392 SCHEMA zkh zkh could not execute query: ERROR: schema "zkh" already exists Command was: CREATE SCHEMA zkh; Error from TOC entry 505; 1259 16389 TABLE mytable zkh 创建表失败 could not execute query: ERROR: relation "mytable" already exists in schema "public" DETAIL: creating new table with existing name in the same schema Command was: CREATE TABLE mytable ( firstcol integer ) WITH (orientation=row, compression=no); Error from TOC entry 507; 1259 24596 TABLE dump_tables zkh could not execute query: ERROR: relation "dump_tables" already exists in schema "zkh" DETAIL: creating new table with existing name in the same schema Command was: CREATE TABLE dump_tables ( schemaname name, tablename name, tableowner name, tablespace name, hasindexes... Error from TOC entry 508; 1259 24599 TABLE dump_tables_1 zkh could not execute query: ERROR: relation "dump_tables_1" already exists in schema "zkh" DETAIL: creating new table with existing name in the same schema Command was: CREATE TABLE dump_tables_1 ( schemaname name, tablename name, tableowner name, tablespace name, hasindex... Error from TOC entry 506; 1259 16393 TABLE mytable1 zkh could not execute query: ERROR: relation "mytable1" already exists in schema "zkh" DETAIL: creating new table with existing name in the same schema Command was: CREATE TABLE mytable1 ( firstcol integer ) WITH (orientation=row, compression=no); table mytable complete data imported ! table dump_tables complete data imported ! table dump_tables_1 complete data imported ! table mytable1 complete data imported ! Finish reading 15 SQL statements! end restore operation ... WARNING: errors ignored on restore: 5 restore operation successful total time: 427 ms |
因为在数据库里表已经存在,所以创建表失败,查看数据是否导入成功
select 'dump_tables',count(*) from dump_tables union all select 'dump_tables',count(*) from dump_tables_1; #条数 dump_tables 512 dump_tables_1 516 |
可见表数据已经插入进去,因为数据默认是追加的方式,而且我的表中没有主键
2.2导入全库的时候指定-c参数
[omm@pkt_mogdb1 ~]$ gs_restore -d db_mogdb -c /home/omm/tempdump start restore operation ... table mytable complete data imported ! table dump_tables complete data imported ! table dump_tables_1 complete data imported ! table mytable1 complete data imported ! Finish reading 15 SQL statements! end restore operation ... restore operation successful total time: 209 ms |
这次没有报错,查看表数据
select 'dump_tables',count(*) from dump_tables union all select 'dump_tables',count(*) from dump_tables_1; #条数 dump_tables 256 dump_tables_1 258 |
可见-c参数是先删除表,再创建表,再导入数据
2.2导入单表并且把现在的表中增加一个字段
alter table dump_tables add column new_table name; |
从备份文件中导入dump_tables表
[omm@pkt_mogdb1 ~]$ gs_restore -d db_mogdb -c /home/omm/tempdump --table=dump_tables start restore operation ... table dump_tables complete data imported ! Finish reading 15 SQL statements! end restore operation ... restore operation successful total time: 47 ms |
并且重新查询表,发现新家的字段已经不存在。
- 测试目录归档格式
datadump1文件也是包含了整个数据库内容
gs_restore -d db_mogdb -c /home/omm/datadump1 |
结果与自定义归档模式类似,此处就不在赘述。
- 测试tar格式
gs_restore -d db_mogdb -c /home/omm/datat.tar |
结果与自定义归档模式类似,此处就不在赘述。
- 测试导出文件格式
gs_restore /home/omm/datat.tar --file=test.sql --list |
运行结果导出了归档文件的文件内容
[omm@pkt_mogdb1 data]$ cat test.sql ; ; Archive created at Wed Aug 10 20:26:30 2022 ; dbname: db_mogdb ; TOC Entries: 15 ; Compression: 0 ; Dump Version: 1.12-0 ; Format: TAR ; Integer: 4 bytes ; Offset: 8 bytes ; Dumped from database version: 9.2.4 ; Dumped by gs_dump version: 9.2.4 ; ; ; Selected TOC Entries: ; 4763; 1262 16388 DATABASE - db_mogdb zkh 8; 2615 2200 SCHEMA - public omm 4765; 0 0 COMMENT - SCHEMA public omm 4766; 0 0 ACL - public omm 14; 2615 16392 SCHEMA - zkh zkh 505; 1259 16389 TABLE public mytable zkh 507; 1259 24596 TABLE zkh dump_tables zkh 508; 1259 24599 TABLE zkh dump_tables_1 zkh 506; 1259 16393 TABLE zkh mytable1 zkh 4755; 0 16389 TABLE DATA public mytable zkh 4757; 0 24596 TABLE DATA zkh dump_tables zkh 4758; 0 24599 TABLE DATA zkh dump_tables_1 zkh 4756; 0 16393 TABLE DATA zkh mytable1 zkh |
最后
gs_restore主要是针对gs_dump导出的文件格式,如果是从异构数据库导出的带格式的文本文件,需要使用gs_loader。