寫在前面
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。




