• 沒有找到結果。

参数说明

3.12 使用 gs_restore 命令导入数据

操作场景

gs_restore是GaussDB(DWS)提供的与gs_dump配套的导入工具。通过该工具,可将 gs_dump导出的文件导入至数据库。gs_restore支持导入的文件格式包含自定义归档格 式、目录归档格式和tar归档格式。

gs_restore具备如下两种功能。

● 导入至数据库

如果指定了数据库,则数据将被导入到指定的数据库中。其中,并行导入必须指 定连接数据库的密码。

● 导入至脚本文件

如果未指定导入数据库,则创建包含重建数据库所需的SQL语句脚本,并将其写 入至文件或者标准输出。该脚本文件等效于gs_dump导出的纯文本格式文件。

gs_restore工具在导入时,允许用户选择需要导入的内容,并支持在数据导入前对等待 导入的内容进行排序。

操作步骤

说明

gs_restore默认是以追加的方式进行数据导入。为避免多次导入造成数据异常,在进行导入时,

建议使用"-e"和"-c" 参数,即导入前删除已存在于待导入数据库中的数据库对象,同时当出现导 入错误时,忽略当前错误,继续执行导入任务,并在导入后会显示相应的错误信息。

步骤1 以root用户登录到服务器,执行如下命令进入数据存放路径。

cd /opt/bin

步骤2 使用gs_restore命令,从gaussdb整个数据库内容的导出文件中,将数据库的所有对象 的定义导入到backupdb。

gs_restore W password U jack /home//backup/MPPDB_backup.tar p 8000 h 10.10.10.100 d backupdb -s -e -c

3-14 常用参数说明

参数 参数说明 举例

-U 连接数据库的用户名。 -U jack

-W 指定用户连接的密码。

● 如果主机的认证策略是trust,

则不会对数据库管理员进行密码 验证,即无需输入-W选项;

● 如果没有-W选项,并且不是数 据库管理员,会提示用户输入密 码。

-W Passwd@123

-d 连接数据库dbname,并直接将数 据导入到该数据库中。

-d backupdb

-p 指定服务器所监听的TCP端口或本 地Unix域套接字后缀,以确保连 接。

-p 8000

-h “集群地址”如果通过公网地址连

接,请指定为集群“公网访问地 址”或“公网访问域名”,如果通 过内网地址连接,请指定为集群

“内网访问地址”或“内网访问域 名”。

-h 10.10.10.100

-e 当发送SQL语句到数据库时如果出 现错误,退出当前出现错误的任 务,并执行其他导入任务。即默认 状态下会忽略错误任务并继续执行 导入,且在导入后会显示一系列错 误信息。

--c 在重新创建数据库对象前,清理

(删除)已存在于将要导入的数据 库中的数据库对象。

--s 只导入模式定义,不导入数据。当

前的序列值也不会被导入。

-其他参数说明请参见《工具参考》中“服务端工具>gs_restore”章节。

----结束

示例

示例一:执行gs_restore,导入指定MPPDB_backup.dmp文件(自定义归档格式)中 gaussdb数据库的数据和对象定义。

gs_restore -W Passwd@123 backup/MPPDB_backup.dmp -p 8000 -h 10.10.10.100 -d backupdb gs_restore[2017-07-21 19:16:26]: restore operation successfu

gs_restore: total time: 13053 ms

示例二:执行gs_restore,导入指定MPPDB_backup.tar文件(tar归档格式)中 gaussdb数据库的数据和对象定义。

gs_restore backup/MPPDB_backup.tar -p 8000 -h 10.10.10.100 -d backupdb gs_restore[2017-07-21 19:21:32]: restore operation successful

gs_restore[2017-07-21 19:21:32]: total time: 21203 ms

示例三:执行gs_restore,导入指定MPPDB_backup目录文件(目录归档格式)中 gaussdb数据库的数据和对象定义。

gs_restore backup/MPPDB_backup -p 8000 -h 10.10.10.100 -d backupdb gs_restore[2017-07-21 19:26:46]: restore operation successful

gs_restore[2017-07-21 19:26:46]: total time: 21003 ms

示例四:执行gs_restore,将gaussdb数据库的所有对象的定义导入至backupdb数据 库。导入前,gaussdb存在完整的定义和数据,导入后,backupdb数据库只存在所有 对象定义,表没有数据。

gs_restore W Passwd@123 /home//backup/MPPDB_backup.tar p 8000 h 10.10.10.100 d backupdb s e -c gs_restore[2017-07-21 19:46:27]: restore operation successful

gs_restore[2017-07-21 19:46:27]: total time: 32993 ms

示例五:执行gs_restore,导入MPPDB_backup.dmp文件中PUBLIC模式的所有定义和 数据。在导入时会先删除已经存在的对象,如果原对象存在跨模式的依赖则需手工强 制干预。

gs_restore backup/MPPDB_backup.dmp -p 8000 -h 10.10.10.100 -d backupdb -e -c -n PUBLIC gs_restore: [archiver (db)] Error while PROCESSING TOC:

gs_restore: [archiver (db)] Error from TOC entry 313; 1259 337399 TABLE table1 gaussdba

gs_restore: [archiver (db)] could not execute query: ERROR: cannot drop table table1 because other objects depend on it

DETAIL: view t1.v1 depends on table table1

HINT: Use DROP ... CASCADE to drop the dependent objects too.

Command was: DROP TABLE public.table1;

手工删除依赖,导入完成后再重新创建。

gs_restore backup/MPPDB_backup.dmp -p 8000 -h 10.10.10.100 -d backupdb -e -c -n PUBLIC gs_restore[2017-07-21 19:52:26]: restore operation successful

gs_restore[2017-07-21 19:52:26]: total time: 2203 ms

示例六:执行gs_restore,导入MPPDB_backup.dmp文件中PUBLIC模式下表hr.staffs 的定义。在导入之前,hr.staffs表不存在。

gs_restore backup/MPPDB_backup.dmp -p 8000 -h 10.10.10.100 -d backupdb -e -c -s -n PUBLIC -t hr.staffs gs_restore[2017-07-21 19:56:29]: restore operation successful

gs_restore[2017-07-21 19:56:29]: total time: 21000 ms

示例七:执行gs_restore,导入MPPDB_backup.dmp文件中PUBLIC模式下表hr.staffs 的数据。在导入之前,hr.staffs表不存在数据。

gs_restore backup/MPPDB_backup.dmp -p 8000 -h 10.10.10.100 -d backupdb -e -a -n PUBLIC -t hr.staffs gs_restore[2017-07-21 20:12:32]: restore operation successful

gs_restore[2017-07-21 20:12:32]: total time: 20203 ms

示例八:执行gs_restore,导入指定表hr.staffs的定义。在导入之前,hr.staffs表的数 据是存在的。

human_resource=# select * from hr.staffs;

staff_id | first_name | last_name | email | phone_number | hire_date | employment_id | salary | commission_pct | manager_id | section_id

gsql -d human_resource -p 8000

gsql ((GaussDB 8.1.1 build af002019) compiled at 2020-01-10 05:43:20 commit 6995 last mr 11566 ) Non-SSL connection (SSL connection is recommended when requiring high-security)

Type "help" for help.

human_resource=# drop table hr.staffs CASCADE;

NOTICE: drop cascades to view hr.staff_details_view DROP TABLE

gs_restore -W Passwd@123 /home//backup/MPPDB_backup.tar -p 8000 -h 10.10.10.100 -d human_resource -n hr -t staffs -s -e

restore operation successful total time: 904 ms

human_resource=# select * from hr.staffs;

staff_id | first_name | last_name | email | phone_number | hire_date | employment_id | salary | commission_pct | manager_id | section_id

hr | employment_history | table | | {orientation=row,compression=no}

hr | employments | table | | {orientation=row,compression=no}

hr | places | table | | {orientation=row,compression=no}

hr | sections | table | | {orientation=row,compression=no}

hr | states | table | | {orientation=row,compression=no}

(5 rows)

gs_restore -W Passwd@123 /home/mppdb/backup/MPPDB_backup.tar -p 8000 -h 10.10.10.100 -d human_resource -n hr -t staffs -n hr -t areas

restore operation successful total time: 724 ms

hr | employment_history | table | | {orientation=row,compression=no}

hr | employments | table | | {orientation=row,compression=no}

human_resource=# select * from hr.areas;

area_id | area_name

gs_restore -W Passwd@123 /home//backup/MPPDB_backup1.sql -p 8000 -h 10.10.10.100 -d backupdb -n hr -e -c

restore operation successful total time: 702 ms

示例十一:执行gs_restore,同时导入hr和hr1两个模式,仅导入模式下的所有对象定 义。gs_restore -W Passwd@123 /home//backup/MPPDB_backup2.dmp -p 8000 -h 10.10.10.100 -d backupdb -n hr -n hr1 -s

restore operation successful total time: 665 ms

示例十二:执行gs_restore,将human_resource数据库导出文件进行解密并导入至 backupdb数据库中。

create database backupdb;

CREATE DATABASE

gs_restore /home//backup/MPPDB_backup.tar -p 8000 -h 10.10.10.100 -d backupdb --with-key=1234567812345678

restore operation successful total time: 23472 ms gsql -d backupdb -p 8000 -r

gsql ((GaussDB 8.1.1 build af002019) compiled at 2020-01-10 05:43:20 commit 6995 last mr 11566 ) Non-SSL connection (SSL connection is recommended when requiring high-security)

Type "help" for help.

backupdb=# select * from hr.areas;

area_id | area_name

human_resource=# CREATE USER user1 IDENTIFIED BY 'password';

gs_restore -U user1 -W password /home//backup/MPPDB_backup.tar -p 8000 -h 10.10.10.100 -d backupdb --role role1 --rolepassword password

restore operation successful total time: 554 ms

gsql -d backupdb -p 8000 -r

gsql ((GaussDB 8.1.1 build af002019) compiled at 2020-01-10 05:43:20 commit 6995 last mr 11566 ) Non-SSL connection (SSL connection is recommended when requiring high-security)

Type "help" for help.

backupdb=# select * from hr.areas;

area_id | area_name

DSC(Database Schema Convertor)是一款运行在Linux或Windows操作系统上的命 令行工具,致力于向客户提供简单、快速、可靠的应用程序SQL脚本迁移服务,通过 内置的语法迁移逻辑解析源数据库应用程序SQL脚本,并迁移为适用于

GaussDB(DWS) 数据库的应用程序SQL脚本。DSC不需要连接数据库,可在离线模式 下实现零停机迁移。在GaussDB(DWS) 中通过执行迁移后的SQL脚本即可恢复数据 库,从而实现线下数据库轻松上云。