open Gauss 数据库-05 openGauss数据库备份恢复指导手册

发文章是为了证明自己真的掌握了一个知识,同时给他人带来帮助,如有问题,欢迎指正,祝大家万事胜意! 

目录

前言

openGauss数据库备份恢复 

1 实验介绍

1.1 关于本实验

1.2 实验目的

2 实验前提

3 物理备份和恢复

3.1 实验前提

3.2 物理备份

3.3 物理备份恢复

4 逻辑备份和恢复

4.1 实验前提

4.2 gs_dump

4.2.1 介绍

4.2.2 注意事项

4.2.4 gs_dump 备份示例 2

4.2.5 gs_dump 备份示例 3

4.2.6 gs_dump 备份示例 4

4.2.6 gs_dump 备份示例 4

4.3 gs_dumpall

4.3.1 介绍

4.3.2 操作步骤

4.4 gs_restore

​​​​​​​4.4.1 介绍

4.4.2 gs_restore 导入示例 1

4.4.3 gs_restore 导入示例 2

4.4.4 gs_restore 导入示例 3

4.4.5 gs_restore 导入示例 4

4.4.6 gs_restore 导入示例 5


前言

我的环境:

设备名称设备型号软件版本
虚拟机VMwareVMware-workstation-full-17.5.1
操作系统openEuler   openEuler 22.3LTS
数据库openGauss  openGauss 5.0.0

 需要的工具,大家不用现在下,后面用到了再下也可以,如果需要相关文件,可以评论,其实大多数都是可以去官网下的哈,因为我只能通过网盘给大家,文件又有点大,网盘的速度大家都是清楚的哈哈,所以还是推荐大家去官网,如果实在找不到可以找我

openGauss数据库备份恢复 

1 实验介绍

1.1 关于本实验

本实验主要描述 openGauss 数据库支持的两种备份恢复类型、多种备份恢复方案,并能够在备
份和恢复过程中提供数据的可靠性保障机制。

1.2 实验目的

掌握 openGauss 数据库中逻辑备份与恢复方法;
掌握 openGauss 数据库物理备份与恢复的方法;
能够在备份和恢复过程中提供数据的可靠性保障机制。

2 实验前提

在对数据库进行备份前,对数据库进行如下操作:
步骤 1 切换到 omm 用户,以操作系统用户 omm 登录数据库主节点。
[root@node0 ~]# su - omm
Last login: Mon Apr  8 19:47:35 CST 2024 on tty1


Welcome to 5.10.0-153.12.0.92.oe2203sp2.x86_64

System information as of time: 	2024年 04月 08日 星期一 20:18:18 CST

System load: 	0.64
Processes: 	204
Memory used: 	42.8%
Swap used: 	13.5%
Usage On: 	25%
IP address: 	192.168.28.131
Users online: 	2
To run a command as administrator(user "root"),use "sudo <command>".
步骤 2 连接 openGauss 数据库。
[omm@node0 ~]$ gsql -d postgres -p 15400 -r
gsql ((openGauss 5.0.0 build a07d57c3) compiled at 2023-03-29 03:37:13 commit 0 last mr  )
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type "help" for help.
步骤 3 创建 customer_t1 表。
openGauss=# DROP TABLE IF EXISTS customer_t1;
DROP TABLE
openGauss=# CREATE TABLE customer_t1 
openGauss-# ( 
openGauss(#  c_customer_sk integer, 
openGauss(#  c_customer_id char(5), 
openGauss(#  c_first_name char(6), 
openGauss(#  c_last_name char(8) 
openGauss(# );
CREATE TABLE
步骤 4 向表中插入数据。
openGauss=#  INSERT INTO customer_t1 (c_customer_sk, c_customer_id, c_first_name) VALUES 
openGauss-# (3769, 'hello', DEFAULT) , 
openGauss-# (6885, 'maps', 'Joes'), 
openGauss-# (4321, 'tpcds', 'Lily'), 
openGauss-# (9527, 'world', 'James');
INSERT 0 4
步骤 5 查看表中的数据。
openGauss=# select * from customer_t1;
 c_customer_sk | c_customer_id | c_first_name | c_last_name 
---------------+---------------+--------------+-------------
          3769 | hello         |              | 
          6885 | maps          | Joes         | 
          4321 | tpcds         | Lily         | 
          9527 | world         | James        | 
(4 rows)
步骤 6 创建 customer_t2
openGauss=# DROP TABLE IF EXISTS customer_t2;
NOTICE:  table "customer_t2" does not exist, skipping
DROP TABLE
openGauss=# CREATE TABLE customer_t2 
openGauss-# ( 
openGauss(#  c_customer_sk integer, 
openGauss(#  c_customer_id char(5), 
openGauss(#  c_first_name char(6), 
openGauss(#  c_last_name char(8) 
openGauss(# );
CREATE TABLE
步骤 7 向表中插入数据
openGauss=#  INSERT INTO customer_t2 (c_customer_sk, c_customer_id, c_first_name) VALUES 
openGauss-# (3769, 'hello', DEFAULT) , 
openGauss-# (6885, 'maps', 'Joes'), 
openGauss-# (9527, 'world', 'James');
INSERT 0 3
步骤 8 查看表中数据。
openGauss=# select * from customer_t2;
 c_customer_sk | c_customer_id | c_first_name | c_last_name 
---------------+---------------+--------------+-------------
          3769 | hello         |              | 
          6885 | maps          | Joes         | 
          9527 | world         | James        | 
(3 rows)
步骤 9 创建用户 lucy
openGauss=# DROP user IF EXISTS LUCY;
NOTICE:  role "lucy" does not exist, skipping
DROP ROLE
openGauss=# CREATE USER lucy WITH PASSWORD  "Bigdata@123";
CREATE ROLE
步骤 10 切换到 Lucy 用户
openGauss=# \c - lucy
Password for user lucy: 
Non-SSL connection (SSL connection is recommended when requiring high-security)
You are now connected to database "postgres" as user "lucy".
步骤 11 创建 lucy shema 的表。
openGauss=> DROP TABLE IF EXISTS lucy.mytable;
NOTICE:  table "mytable" does not exist, skipping
DROP TABLE
openGauss=> CREATE TABLE mytable (firstcol int);
CREATE TABLE
步骤 12 向表中插入数据
openGauss=> INSERT INTO mytable values (100);
INSERT 0 1
步骤 13 查看表中数据
openGauss=> SELECT * from mytable;
 firstcol 
----------
      100
(1 row)
步骤 14 退出数据库:

openGauss=> \q
[omm@node0 ~]$ 

物理备份和恢复

openGauss 部署成功后,在数据库运行的过程中,会遇到各种问题及异常状态。 openGauss
提供了 gs_basebackup 工具做基础备份。 gs_basebackup 工具由操作系统用户 omm 执行。

3.1 实验前提

在数据库备份之前创建存储备份文件的文件夹。
步骤 1 切换到 omm 用户,以操作系统用户 omm 登录数据库主节点。

[root@node0 ~]# su - omm
Last login: Sat Apr 13 23:05:21 CST 2024 on tty1


Welcome to 5.10.0-153.12.0.92.oe2203sp2.x86_64

System information as of time: 	2024年 04月 13日 星期六 23:14:02 CST

System load: 	0.01
Processes: 	198
Memory used: 	37.9%
Swap used: 	9.5%
Usage On: 	25%
IP address: 	192.168.28.131
Users online: 	2
To run a command as administrator(user "root"),use "sudo <command>".
[omm@node0 ~]$ 
步骤 2 创建存储备份文件的文件夹。
[omm@node0 ~]$ mkdir -p /home/omm/physical/backup
[omm@node0 ~]$ 

3.2 物理备份

步骤 1 如果数据库服务没有启动,就启动数据库服务 ( 务必以操作系统用户 omm 启动数据库服务, 如果没有请切换用户)
[omm@node0 ~]$ gs_om -t start;
Starting cluster.
=========================================
[SUCCESS] node0:
[2024-04-13 23:16:40.080][8226][][gs_ctl]: gs_ctl started,datadir is /opt/huawei/install/data/dn 
[2024-04-13 23:16:40.086][8226][][gs_ctl]:  another server might be running; Please use the restart command
=========================================
Successfully started.
步骤 2 将数据库进行物理备份。
[omm@node0 ~]$ gs_basebackup -D /home/omm/physical/backup -p 15400
INFO:  The starting position of the xlog copy of the full build is: 0/3000028. The slot minimum LSN is: 0/0. The disaster slot minimum LSN is: 0/0. The logical slot minimum LSN is: 0/0.
[2024-04-13 23:18:08]:begin build tablespace list
[2024-04-13 23:18:08]:finish build tablespace list
[2024-04-13 23:18:08]:begin get xlog by xlogstream
                                                                              [2024-04-13 23:18:08]: check identify system success
                                                                              [2024-04-13 23:18:08]: send START_REPLICATION 0/3000000 success
                                                                              [2024-04-13 23:18:08]: keepalive message is received
                                                                              [2024-04-13 23:18:08]: keepalive message is received
[2024-04-13 23:18:13]:gs_basebackup: base backup successfully
参数 -D directory 表示备份文件输出的目录, 必选项
步骤 3 切换到存储备份文件夹查看备份文件。
[omm@node0 ~]$ cd /home/omm/physical/backup
[omm@node0 backup]$ ls
backup_label        pg_ident.conf  pg_xlog
base                pg_llog        postgresql.conf
cacert.pem          pg_location    postgresql.conf.bak
global              pg_logical     postgresql.conf.guc.bak
gswlm_userinfo.cfg  pg_multixact   postgresql.conf.lock
mot.conf            pg_notify      postmaster.pid.lock
pg_clog             pg_replslot    server.crt
pg_csnlog           pg_serial      server.key
pg_ctl.lock         pg_snapshots   server.key.cipher
pg_errorinfo        pg_stat_tmp    server.key.rand
pg_hba.conf         pg_tblspc      undo
pg_hba.conf.bak     pg_twophase
pg_hba.conf.lock    PG_VERSION

3.3 物理备份恢复

当数据库发生故障时需要从备份文件进行恢复。因为 gs_basebackup 是对数据库按二进制进行
备份,因此恢复时可以直接拷贝替换原有的文件, 或者直接在备份的库上启动数据库。
说明:
若当前数据库实例正在运行,直接从备份文件启动数据库可能会存在端口冲突,这时,需
要修配置文件的 port 参数,或者在启动数据库时指定一下端口。
若当前备份文件为主备数据库,可能需要修改一下主备之间的复制连接。即配置文件中的
postgre.conf 中的 replconninfo1 replconninfo2 等。
当数据库发生问题需要从备份进行恢复时,步骤如下:
步骤 1 停止 openGauss(( 务必以操作系统用户 omm 停止数据库服务,如果没有请切换用户 )
[omm@node0 backup]$ gs_om -t stop;
Stopping cluster.
=========================================
Successfully stopped cluster.
=========================================
End stop cluster.
[omm@node0 backup]$ 
步骤 2 清理原库中的所有或部分文件
查看数据库节点文件夹名称 ( 文件夹名称是数据库安装时定义的,不同数据可能不同 )
[omm@node0 ~]$ cd /opt/huawei/install/data
[omm@node0 data]$ ls
dn
查看文件列表如下:
[omm@node0 data]$ cd dn
[omm@node0 dn]$ ls
backup_label.old    pg_ident.conf  postgresql.conf
base                pg_llog        postgresql.conf.bak
cacert.pem          pg_location    postgresql.conf.guc.bak
gaussdb.state       pg_logical     postgresql.conf.lock
global              pg_multixact   postmaster.opts
gswlm_userinfo.cfg  pg_notify      postmaster.pid
mot.conf            pg_replslot    postmaster.pid.lock
pg_clog             pg_serial      server.crt
pg_csnlog           pg_snapshots   server.key
pg_ctl.lock         pg_stat_tmp    server.key.cipher
pg_errorinfo        pg_tblspc      server.key.rand
pg_hba.conf         pg_twophase    undo
pg_hba.conf.bak     PG_VERSION
pg_hba.conf.lock    pg_xlog
删除文件,对数据库文件进行破坏。
[omm@node0 dn]$ rm -rf *
[omm@node0 dn]$ ls
[omm@node0 dn]$ 
删除文件后,列表为空
[omm@node0 dn]$ rm -rf *
[omm@node0 dn]$ ls
[omm@node0 dn]$ 
删除文件后,列表为空
步骤 3 使用数据库系统用户权限从备份中还原需要的数据库文件, "/opt/huawei/install/data/d n "
dn 是数据库节点文件夹名称,不同数据库可能不同请查看确认。
[omm@node0 dn]$ cp -r /home/omm/physical/backup/. /opt/huawei/install/data/dn
[omm@node0 dn]$ 
备份时间大概需要几分钟,恢复后文件列表如下
[omm@node0 dn]$ cd /opt/huawei/install/data/dn
[omm@node0 dn]$ ls
backup_label        pg_ident.conf  pg_xlog
base                pg_llog        postgresql.conf
cacert.pem          pg_location    postgresql.conf.bak
global              pg_logical     postgresql.conf.guc.bak
gswlm_userinfo.cfg  pg_multixact   postgresql.conf.lock
mot.conf            pg_notify      postmaster.pid.lock
pg_clog             pg_replslot    server.crt
pg_csnlog           pg_serial      server.key
pg_ctl.lock         pg_snapshots   server.key.cipher
pg_errorinfo        pg_stat_tmp    server.key.rand
pg_hba.conf         pg_tblspc      undo
pg_hba.conf.bak     pg_twophase
pg_hba.conf.lock    PG_VERSION
若数据库中存在链接文件 , 需要修改使其链接到正确的文件。
步骤 4 重启数据库服务器, 并检查数据库内容,确保数据库已经恢复到所需的状态。
[omm@node0 dn]$ gs_om -t start;
Starting cluster.
=========================================

=========================================
Successfully started.

4 逻辑备份和恢复

通过逻辑导出对数据进行备份,逻辑备份只能基于备份时刻进行数据转储,所以恢复时也只能恢复到备份时保存的数据。对于故障点和备份点之间的数据,逻辑备份无能为力,逻辑备份适合备份那些很少变化的数据,当这些数据因误操作被损坏时,可以通过逻辑备份进行快速恢复。如果通过逻辑备份进行全库恢复,通常需要重建数据库,导入备份数据来完成,对于可用性要求很高的数据库,这种恢复时间太长,通常不被采用。由于逻辑备份具有平台无关性,所以更为常见的是逻辑备份被作为一个数据迁移及移动的主要手段。

4.1 实验前提

在数据库备份之前创建存储备份文件的文件夹。

步骤 1 切换到 omm 用户,以操作系统用户 omm 登录数据库主节点。

[root@node0 ~]# su - omm
Last login: Mon Apr 15 16:46:11 CST 2024 on pts/1


Welcome to 5.10.0-153.12.0.92.oe2203sp2.x86_64

System information as of time: 	2024年 04月 15日 星期一 17:16:22 CST

System load: 	0.06
Processes: 	199
Memory used: 	38.4%
Swap used: 	10.1%
Usage On: 	26%
IP address: 	192.168.28.131
Users online: 	2
To run a command as administrator(user "root"),use "sudo <command>".
步骤 2 创建存储备份文件的文件夹。
[omm@node0 ~]$ mkdir -p /home/omm/logical/backup
[omm@node0 ~]$ 

4.2 gs_dump

4.2.1 介绍

gs_dump openGauss 用于导出数据库相关信息的工具,用户可以自定义导出一个数据库或其中的对象(模式、表、视图等)。支持导出的数据库可以是默认数据库 postgres,也可以是自定义数据库。

主要功能:

gs_dump 可以创建四种不同的导出文件格式,通过[-F 或者--format=]选项指定,具体见官方文档

4.2.2 注意事项

如果 openGauss 有任何本地数据要添加到 template1 数据库,请将 gs_dump 的输出恢复

到一个真正的空数据库中,否则可能会因为被添加对象的定义被复制,出现错误。要创建

一个无本地添加的空数据库,需从 template0 而非 template1 复制,例如:


openGauss=# CREATE DATABASE foo WITH TEMPLATE template0;
CREATE DATABASE
tar 归档形式的文件大小不得超过 8GB tar 文件格式的固有限制)。 tar 文档整体大小和任
何其他输出格式没有限制,操作系统可能对此有要求。
gs_dump 生成的转储文件不包含优化程序用来做执行计划决定的统计数据。因此,最
好从某转储文件恢复之后运行 ANALYZE 以确保最佳效果。转储文件不包含任何 ALTER
DATABASE…SET 命令,这些设置由 gs_dumpall 转储,还有数据库用户和其他完成安装设
置。
使用 gs_dump 转储数据库为 SQL 文本文件或其它格式的操作:示例中 “Bigdata@123”
示数据库用户密码 “/home/omm/logical/backup/MPPDB_backup.sql” 表示导出的文件;
“26000” 表示数据库服务器端口; “postgres” 表示要访问的数据库名。
说明:导出操作时,请确保该目录存在并且当前的操作系统用户对其具有读写权限。

4.2.3 gs_dump 备份示例 1

执行 gs_dump,导出 postgres 数据库全量信息,导出的 MPPDB_backup.sql 文件格式为纯文本格式。

步骤 1 以操作系统用户 omm 登录数据库主节点。

[root@node0 ~]# su - omm
Last login: Mon Apr 15 17:16:22 CST 2024 on pts/1


Welcome to 5.10.0-153.12.0.92.oe2203sp2.x86_64

System information as of time: 	2024年 04月 15日 星期一 17:32:59 CST

System load: 	0.02
Processes: 	196
Memory used: 	39.3%
Swap used: 	11.6%
Usage On: 	26%
IP address: 	192.168.28.131
Users online: 	2
步骤 2 执行 gs_dump ,导出的 MPPDB_backup.sql 文件格式为纯文本格式。
[omm@node0 ~]$ gs_dump -U omm -W Bigdata@123 -f /home/omm/logical/backup/MPPDB_backup.sql -p 15400 postgres -F p
gs_dump[port='15400'][postgres][2024-04-15 17:34:35]: The total objects number is 516.
gs_dump[port='15400'][postgres][2024-04-15 17:34:35]: [100.00%] 516 objects have been dumped.
gs_dump[port='15400'][postgres][2024-04-15 17:34:35]: dump database postgres successfully
gs_dump[port='15400'][postgres][2024-04-15 17:34:35]: total time: 2624  ms
步骤 3 切换到 backup 文件夹,查看 MPPDB_backup.sql 文件。
[omm@node0 ~]$ ll /home/omm/logical/backup/
total 772
-rw------- 1 omm dbgrp 127566 Apr 15 09:48 bkp2.sql
-rw------- 1 omm dbgrp    972 Apr 15 09:35 bkp_shl2.sql
drwx------ 2 omm dbgrp   4096 Apr 15 09:34 MPPDB_backup
-rw------- 1 omm dbgrp 122523 Apr 15 09:45 MPPDB_backup2.sql
-rw------- 1 omm dbgrp 121108 Apr 15 09:33 MPPDB_backup.dmp
-rw------- 1 omm dbgrp 123098 Apr 15 17:34 MPPDB_backup.sql
-rw------- 1 omm dbgrp 277504 Apr 15 09:32 MPPDB_backup.tar
[omm@node0 ~]$ cat /home/omm/logical/backup/MPPDB_backup.sql
……
--
-- Name: inx_stu01; Type: INDEX; Schema: public; Owner: omm; Tablespace: 
--

CREATE INDEX inx_stu01 ON student USING btree (std_name) TABLESPACE pg_default;


--
-- Name: public; Type: ACL; Schema: -; Owner: omm
--

REVOKE ALL ON SCHEMA public FROM PUBLIC;
REVOKE ALL ON SCHEMA public FROM omm;
GRANT CREATE,USAGE ON SCHEMA public TO omm;
GRANT USAGE ON SCHEMA public TO PUBLIC;


--
-- openGauss database dump complete
--
4.2.4 gs_dump 备份示例 2

执行 gs_dump,导出 postgres 数据库全量信息,导出的 MPPDB_backup.tar 文件格式tar格式。

步骤 1 以操作系统用户 omm 登录数据库主节点

[root@node0 ~]# su - omm
Last login: Mon Apr 15 17:32:59 CST 2024 on pts/1


Welcome to 5.10.0-153.12.0.92.oe2203sp2.x86_64

System information as of time: 	2024年 04月 15日 星期一 17:41:09 CST

System load: 	0.00
Processes: 	197
Memory used: 	39.4%
Swap used: 	12.1%
Usage On: 	26%
IP address: 	192.168.28.131
Users online: 	2
To run a command as administrator(user "root"),use "sudo <command>".
[omm@node0 ~]$ 

步骤 2 执行 gs_dump,导出的 MPPDB_backup.tar 文件格式为 tar 格式。

[omm@node0 ~]$ gs_dump -U omm -W Bigdata@123 -f /home/omm/logical/backup/MPPDB_backup.tar -p 15400 postgres -F t
gs_dump[port='15400'][postgres][2024-04-15 17:43:14]: The total objects number is 516.
gs_dump[port='15400'][postgres][2024-04-15 17:43:14]: [100.00%] 516 objects have been dumped.
gs_dump[port='15400'][postgres][2024-04-15 17:43:14]: dump database postgres successfully
gs_dump[port='15400'][postgres][2024-04-15 17:43:14]: total time: 1422  ms
[omm@node0 ~]$ 

步骤 3 查看生成的文件信息。

[omm@node0 ~]$ ll /home/omm/logical/backup/
total 772
-rw------- 1 omm dbgrp 127566 Apr 15 09:48 bkp2.sql
-rw------- 1 omm dbgrp    972 Apr 15 09:35 bkp_shl2.sql
drwx------ 2 omm dbgrp   4096 Apr 15 09:34 MPPDB_backup
-rw------- 1 omm dbgrp 122523 Apr 15 09:45 MPPDB_backup2.sql
-rw------- 1 omm dbgrp 121108 Apr 15 09:33 MPPDB_backup.dmp
-rw------- 1 omm dbgrp 123098 Apr 15 17:34 MPPDB_backup.sql
-rw------- 1 omm dbgrp 277504 Apr 15 17:43 MPPDB_backup.tar
4.2.5 gs_dump 备份示例 3

执行 gs_dump,导出 postgres 数据库全量信息,导出的 MPPDB_backup.dmp 文件格式为自定义归档格式。

步骤 1 以操作系统用户 omm 登录数据库主节点。

[root@node0 ~]# su - omm
Last login: Mon Apr 15 17:41:09 CST 2024 on pts/1


Welcome to 5.10.0-153.12.0.92.oe2203sp2.x86_64

System information as of time: 	2024年 04月 15日 星期一 17:45:32 CST

System load: 	0.93
Processes: 	199
Memory used: 	39.6%
Swap used: 	12.3%
Usage On: 	26%
IP address: 	192.168.28.131
Users online: 	2
To run a command as administrator(user "root"),use "sudo <command>".

步骤 2 执行 gs_dump,导出的 MPPDB_backup.dmp 文件格式为自定义归档格式。

[omm@node0 ~]$ gs_dump -U omm -W Bigdata@123 -f /home/omm/logical/backup/MPPDB_backup.dmp -p 15400 postgres -F c
gs_dump[port='15400'][postgres][2024-04-15 17:47:39]: The total objects number is 516.
gs_dump[port='15400'][postgres][2024-04-15 17:47:39]: [100.00%] 516 objects have been dumped.
gs_dump[port='15400'][postgres][2024-04-15 17:47:39]: dump database postgres successfully
gs_dump[port='15400'][postgres][2024-04-15 17:47:39]: total time: 1420  ms

步骤 3 查看生成的文件信息。

[omm@node0 ~]$ ll /home/omm/logical/backup/
total 772
-rw------- 1 omm dbgrp 127566 Apr 15 09:48 bkp2.sql
-rw------- 1 omm dbgrp    972 Apr 15 09:35 bkp_shl2.sql
drwx------ 2 omm dbgrp   4096 Apr 15 17:49 MPPDB_backup
-rw------- 1 omm dbgrp 122523 Apr 15 09:45 MPPDB_backup2.sql
-rw------- 1 omm dbgrp 121108 Apr 15 17:47 MPPDB_backup.dmp
-rw------- 1 omm dbgrp 123098 Apr 15 17:34 MPPDB_backup.sql
-rw------- 1 omm dbgrp 277504 Apr 15 17:43 MPPDB_backup.tar
4.2.6 gs_dump 备份示例 4

执行 gs_dump,导出 postgres 数据库全量信息,导出的 MPPDB_backup 文件格式为目录格式

步骤 1 以操作系统用户 omm 登录数据库主节点。

[root@node0 ~]# su - omm
Last login: Mon Apr 15 17:45:31 CST 2024 on pts/1


Welcome to 5.10.0-153.12.0.92.oe2203sp2.x86_64

System information as of time: 	2024年 04月 15日 星期一 20:02:07 CST

System load: 	0.10
Processes: 	196
Memory used: 	40.0%
Swap used: 	14.0%
Usage On: 	26%
IP address: 	192.168.28.131
Users online: 	2
To run a command as administrator(user "root"),use "sudo <command>".

步骤 2 执行 gs_dump,导出的 MPPDB_backup 文件格式为目录格式

[omm@node0 ~]$ gs_dump -U omm -W Bigdata@123 -f /home/omm/logical/backup/MPPDB_backup -p 15400 postgres -F d
gs_dump[port='15400'][postgres][2024-04-15 09:33:53]: The total objects number is 516.
gs_dump[port='15400'][postgres][2024-04-15 09:33:54]: [100.00%] 516 objects have been dumped.
gs_dump[port='15400'][postgres][2024-04-15 09:33:54]: dump database postgres successfully
gs_dump[port='15400'][postgres][2024-04-15 09:33:54]: total time: 2341 ms

步骤 3 查看生成的文件信息。

[omm@node0 ~]$ ll /home/omm/logical/backup/
total 772
-rw------- 1 omm dbgrp 127566 Apr 15 09:48 bkp2.sql
-rw------- 1 omm dbgrp    972 Apr 15 09:35 bkp_shl2.sql
drwx------ 2 omm dbgrp   4096 Apr 15 17:49 MPPDB_backup
-rw------- 1 omm dbgrp 122523 Apr 15 09:45 MPPDB_backup2.sql
-rw------- 1 omm dbgrp 121108 Apr 15 17:47 MPPDB_backup.dmp
-rw------- 1 omm dbgrp 123098 Apr 15 17:34 MPPDB_backup.sql
-rw------- 1 omm dbgrp 277504 Apr 15 17:43 MPPDB_backup.tar
4.2.6 gs_dump 备份示例 4

执行 gs_dump,导出 postgres 数据库全量信息,导出的 MPPDB_backup 文件格式为目录格式

步骤 1 以操作系统用户 omm 登录数据库主节点。

[root@node0 ~]# su - omm
Last login: Mon Apr 15 20:02:07 CST 2024 on pts/1


Welcome to 5.10.0-153.12.0.92.oe2203sp2.x86_64

System information as of time: 	2024年 04月 15日 星期一 20:10:39 CST

System load: 	0.15
Processes: 	197
Memory used: 	40.0%
Swap used: 	14.0%
Usage On: 	26%
IP address: 	192.168.28.131
Users online: 	2
To run a command as administrator(user "root"),use "sudo <command>".
[omm@node0 ~]$ 

步骤 2 执行 gs_dump,导出的 MPPDB_backup 文件格式为目录格式

[omm@node0 ~]$ gs_dump -U omm -W Bigdata@123 -f /home/omm/logical/backup/MPPDB_backup -p 15400 postgres -F d
gs_dump[port='15400'][postgres][2024-04-15 09:34:57]: The total objects number is 516.
gs_dump[port='15400'][postgres][2024-04-15 09:34:57]: [100.00%] 516 objects have been dumped.
gs_dump[port='15400'][postgres][2024-04-15 09:34:57]: dump database postgres successfully
gs_dump[port='15400'][postgres][2024-04-15 09:34:57]: total time: 1646 ms

步骤 3 查看生成的文件信息。

[omm@node0 ~]$ ll /home/omm/logical/backup/
total 772
-rw------- 1 omm dbgrp 127566 Apr 15 09:48 bkp2.sql
-rw------- 1 omm dbgrp    972 Apr 15 09:35 bkp_shl2.sql
drwx------ 2 omm dbgrp   4096 Apr 15 17:49 MPPDB_backup
-rw------- 1 omm dbgrp 122523 Apr 15 09:45 MPPDB_backup2.sql
-rw------- 1 omm dbgrp 121108 Apr 15 17:47 MPPDB_backup.dmp
-rw------- 1 omm dbgrp 123098 Apr 15 17:34 MPPDB_backup.sql
-rw------- 1 omm dbgrp 277504 Apr 15 17:43 MPPDB_backup.tar
4.2.7 gs_dump 备份示例 5
执行 gs_dump ,导出 postgres 数据库的 表(或视图、或序列、或外表)对象 ,例如表
customer_t1
步骤 1 以操作系统用户 omm 登录数据库主节点。
[root@node0 ~]# su - omm
Last login: Mon Apr 15 20:10:39 CST 2024 on pts/1


Welcome to 5.10.0-153.12.0.92.oe2203sp2.x86_64

System information as of time: 	2024年 04月 15日 星期一 20:16:26 CST

System load: 	0.14
Processes: 	196
Memory used: 	40.0%
Swap used: 	14.0%
Usage On: 	26%
IP address: 	192.168.28.131
Users online: 	2
To run a command as administrator(user "root"),use "sudo <command>".
[omm@node0 ~]$ 
步骤 2 执行 gs_dump ,导出的 MPPDB_backup 文件格式为目录格式
[omm@node0 ~]$ gs_dump -U omm -W Bigdata@123 -f /home/omm/logical/backup/bkp_shl2.sql-t public.customer_t1 -p 15400
gs_dump[port='15400'][postgres][2024-04-15 09:35:44]: The total objects number is 421.
gs_dump[port='15400'][postgres][2024-04-15 09:35:44]: [100.00%] 421 objects have been dumped.
gs_dump[port='15400'][postgres][2024-04-15 09:35:44]: dump database postgres successfully
gs_dump[port='15400'][postgres][2024-04-15 09:35:44]: total time: 1161 ms
步骤 3 查看生成的文件信息
[omm@node0 ~]$ ll /home/omm/logical/backup/
total 524
-rw------- 1 omm dbgrp 972 Apr 15 09:35 bkp_shl2.sql
drwx------ 2 omm dbgrp 4096 Apr 15 09:34 MPPDB_backup
-rw------- 1 omm dbgrp 121108 Apr 15 09:33 MPPDB_backup.dmp
-rw------- 1 omm dbgrp 123098 Apr 15 09:24 MPPDB_backup.sql
-rw------- 1 omm dbgrp 277504 Apr 15 09:32 MPPDB_backup.tar
步骤 4 查看生成的 sql 文件
[omm@node0 ~]$ cat /home/omm/logical/backup/bkp_shl2.sql
……
CREATE TABLE customer_t1 (
    c_customer_sk integer,
    c_customer_id character(5),
    c_first_name character(6),
    c_last_name character(8)
)
WITH (orientation=row, compression=no);


ALTER TABLE public.customer_t1 OWNER TO omm;

--
-- Data for Name: customer_t1; Type: TABLE DATA; Schema: public; Owner: omm
--

COPY customer_t1 (c_customer_sk, c_customer_id, c_first_name, c_last_name) FROM stdin;
3769	hello	\N	\N
6885	maps 	Joes  	\N
4321	tpcds	Lily  	\N
9527	world	James 	\N
\.
;

--
-- openGauss database dump complete
--

4.2.8 gs_dump 备份示例 6

执行 gs_dump,导出 postgres 数据库信息,但不导出/home/MPPDB_temp.sql 中指定的表信息。导出的 MPPDB_backup.sql 文件格式为纯文本格式。

步骤 1 以操作系统用户 omm 登录数据库主节点

[root@node0 ~]# su - omm
Last login: Mon Apr 15 20:16:26 CST 2024 on pts/1


Welcome to 5.10.0-153.12.0.92.oe2203sp2.x86_64

System information as of time: 	2024年 04月 15日 星期一 20:40:08 CST

System load: 	0.04
Processes: 	197
Memory used: 	40.1%
Swap used: 	14.2%
Usage On: 	26%
IP address: 	192.168.28.131
Users online: 	2
To run a command as administrator(user "root"),use "sudo <command>".
步骤 2 创建 MPPDB_temp.sql ,填写不导出的表的表名。例如 customer_t1
[omm@node0 ~]$ vi /home/omm/logical/MPPDB_temp.sql
[omm@node0 ~]$ 

输入”i”,切换到 INSERT 模式,输入public.customer_t1”,按下”ESC”,并输入”:wq”后回车

保存后退出。

[omm@node0 ~]$ ll /home/omm/logical/backup/
total 644
-rw------- 1 omm dbgrp 972 Apr 15 09:35 bkp_shl2.sql
drwx------ 2 omm dbgrp 4096 Apr 15 09:34 MPPDB_backup
-rw------- 1 omm dbgrp 122523 Apr 15 09:45 MPPDB_backup2.sql
-rw------- 1 omm dbgrp 121108 Apr 15 09:33 MPPDB_backup.dmp
-rw------- 1 omm dbgrp 123098 Apr 15 09:24 MPPDB_backup.sql
-rw------- 1 omm dbgrp 277504 Apr 15 09:32 MPPDB_backup.tar

步骤 3 执行 gs_dump ,导出 postgres 数据库信息,但不导出 MPPDB_temp.sql 中指定的表信息
[omm@node0 ~]$ gs_dump -U omm -W Bigdata@123 -p 15400 postgres --exclude-tablefile=/home/omm/logical/MPPDB_temp.sql -f /home/omm/logical/backup/MPPDB_backup2.sql
gs_dump[port='15400'][postgres][2024-04-15 09:45:42]: The total objects number is 514.
gs_dump[port='15400'][postgres][2024-04-15 09:45:42]: [100.00%] 514 objects have been dumped.
gs_dump[port='15400'][postgres][2024-04-15 09:45:42]: dump database postgres successfully
gs_dump[port='15400'][postgres][2024-04-15 09:45:42]: total time: 1394 ms
步骤 4 查看生成的文件信息。
[omm@node0 ~]$ ll /home/omm/logical/backup/
total 644
-rw------- 1 omm dbgrp 972 Apr 15 09:35 bkp_shl2.sql
drwx------ 2 omm dbgrp 4096 Apr 15 09:34 MPPDB_backup
-rw------- 1 omm dbgrp 122523 Apr 15 09:45 MPPDB_backup2.sql
-rw------- 1 omm dbgrp 121108 Apr 15 09:33 MPPDB_backup.dmp
-rw------- 1 omm dbgrp 123098 Apr 15 09:24 MPPDB_backup.sql
-rw------- 1 omm dbgrp 277504 Apr 15 09:32 MPPDB_backup.tar

步骤 5 查看生成的 sql 文件,确认没有备份 customer_t1

[omm@node0 ~]$ cat /home/omm/logical/backup/MPPDB_backup2.sql
……

SET search_path = public;

--
-- Name: inx_stu01; Type: INDEX; Schema: public; Owner: omm; Tablespace: 
--

CREATE INDEX inx_stu01 ON student USING btree (std_name) TABLESPACE pg_default;


--
-- Name: public; Type: ACL; Schema: -; Owner: omm
--

REVOKE ALL ON SCHEMA public FROM PUBLIC;
REVOKE ALL ON SCHEMA public FROM omm;
GRANT CREATE,USAGE ON SCHEMA public TO omm;
GRANT USAGE ON SCHEMA public TO PUBLIC;


--
-- openGauss database dump complete
--

4.3 gs_dumpall

4.3.1 介绍

gs_dumpall openGauss 用于导出所有数据库相关信息工具,它可以导出 openGauss

据库的所有数据,包括默认数据库 postgres 的数据、自定义数据库的数据、以及

openGauss 所有数据库公共的全局对象。

gs_dumpall 在导出 openGauss 所有数据库时分为两部分:

gs_dumpall 自身对所有数据库公共的全局对象进行导出,包括有关数据库用户和组,表

空间以及属性(例如,适用于数据库整体的访问权限)信息。

gs_dumpall 通过调用 gs_dump 来完成 openGauss 中各数据库的 SQL 脚本文件导出,该脚本文件包含将数据库恢复为其保存时的状态所需要的全部 SQL 语句。

以上两部分导出的结果为纯文本格式的 SQL 脚本文件,使用 gsql 运行该脚本文件可以恢复openGauss 数据库。

4.3.2 操作步骤
步骤 1 切换到 omm 用户,以操作系统用户 omm 登录数据库主节点。
[root@node0 ~]# su - omm
Last login: Mon Apr 15 20:40:08 CST 2024 on pts/1


Welcome to 5.10.0-153.12.0.92.oe2203sp2.x86_64

System information as of time: 	2024年 04月 17日 星期三 23:08:22 CST

System load: 	0.00
Processes: 	196
Memory used: 	11.8%
Swap used: 	0%
Usage On: 	26%
IP address: 	192.168.28.131
Users online: 	1
To run a command as administrator(user "root"),use "sudo <command>".
步骤 2 使用 gs_dumpall 一次导出 openGauss 的所有数据库。
[omm@node0 ~]$ gs_dumpall -f /home/omm/logical/backup/bkp2.sql -p 15400
gs_dump[port='15400'][dbname='db_tpcc02'][2024-04-17 23:10:58]: The total objects number is 427.
gs_dump[port='15400'][dbname='db_tpcc02'][2024-04-17 23:10:58]: [100.00%] 427 objects have been dumped.
gs_dump[port='15400'][dbname='db_tpcc02'][2024-04-17 23:10:58]: dump database dbname='db_tpcc02' successfully
gs_dump[port='15400'][dbname='db_tpcc02'][2024-04-17 23:10:58]: total time: 1494  ms
gs_dump[port='15400'][dbname='foo'][2024-04-17 23:11:00]: The total objects number is 427.
gs_dump[port='15400'][dbname='foo'][2024-04-17 23:11:00]: [100.00%] 427 objects have been dumped.
gs_dump[port='15400'][dbname='foo'][2024-04-17 23:11:00]: dump database dbname='foo' successfully
gs_dump[port='15400'][dbname='foo'][2024-04-17 23:11:00]: total time: 1504  ms
gs_dump[port='15400'][dbname='postgres'][2024-04-17 23:11:02]: The total objects number is 516.
gs_dump[port='15400'][dbname='postgres'][2024-04-17 23:11:02]: [100.00%] 516 objects have been dumped.
gs_dump[port='15400'][dbname='postgres'][2024-04-17 23:11:02]: dump database dbname='postgres' successfully
gs_dump[port='15400'][dbname='postgres'][2024-04-17 23:11:02]: total time: 1930  ms
gs_dumpall[port='15400'][2024-04-17 23:11:02]: dumpall operation successful
gs_dumpall[port='15400'][2024-04-17 23:11:02]: total time: 5055  ms
步骤 3 查看生成的文件信息。
[omm@node0 ~]$ ll /home/omm/logical/backup/
total 776
-rw------- 1 omm dbgrp 127566 Apr 17 23:11 bkp2.sql
-rw------- 1 omm dbgrp    972 Apr 15 09:35 bkp_shl2.sql
drwx------ 2 omm dbgrp   4096 Apr 15 17:49 MPPDB_backup
-rw------- 1 omm dbgrp 122523 Apr 15 09:45 MPPDB_backup2.sql
-rw------- 1 omm dbgrp 123098 Apr 15 20:32 MPPDB_backup.dmp
-rw------- 1 omm dbgrp 123098 Apr 15 17:34 MPPDB_backup.sql
-rw------- 1 omm dbgrp 277504 Apr 15 17:43 MPPDB_backup.tar

4.4 gs_restore

​​​​​​​4.4.1 介绍

gs_restore openGauss 提供的针对 gs_dump 导出数据的导入工具。通过此工具可由gs_dump 生成的导出文件进行导入。

gs_restore 工具由操作系统用户 omm 执行。

主要功能包含:

导入到数据库:

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

指定连接的密码。

导入到脚本文件:

如果未指定导入数据库,则创建包含重建数据库所必须的 SQL 语句脚本并写入到文件或者标准输出。等效于直接使用 gs_dump 导出为纯文本格式。

4.4.2 gs_restore 导入示例 1
执行 gs_restore ,将导出的 MPPDB_backup.tar 文件( tar 格式) 导入到 db_tpcc01 数据库。
步骤 1 切换到 omm 用户,以操作系统用户 omm 登录数据库主节点。
[root@node0 ~]# su - omm
Last login: Wed Apr 17 23:08:22 CST 2024 on pts/0


Welcome to 5.10.0-153.12.0.92.oe2203sp2.x86_64

System information as of time: 	2024年 04月 17日 星期三 23:14:53 CST

System load: 	0.06
Processes: 	196
Memory used: 	38.3%
Swap used: 	11.0%
Usage On: 	26%
IP address: 	192.168.28.131
Users online: 	1
To run a command as administrator(user "root"),use "sudo <command>".
步骤 2 连接 openGauss 数据库。
[omm@node0 ~]$ gsql -d postgres -p 15400 -r
gsql ((openGauss 5.0.0 build a07d57c3) compiled at 2023-03-29 03:37:13 commit 0 last mr  )
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type "help" for help.
步骤 3 创建数据库。

openGauss=# DROP DATABASE IF EXISTS db_tpcc01;
NOTICE:  database "db_tpcc01" does not exist, skipping
DROP DATABASE
openGauss=# CREATE DATABASE db_tpcc01;
CREATE DATABASE
步骤 4 退出数据库。
openGauss=# \q
[omm@node0 ~]$ 

​​​​​​​步骤 5 执行 gs_restore,将导出的 MPPDB_backup.tar 文件(tar 格式)导入到 db_tpcc01 数据库。

[omm@node0 ~]$ gs_restore /home/omm/logical/backup/MPPDB_backup.tar -p 15400 -d db_tpcc01

如果成功,显示如下:

start restore operation ...
table mytable complete data imported !
table pmk_configuration complete data imported !
table pmk_meta_data complete data imported !
table pmk_snapshot complete data imported !
table pmk_snapshot_datanode_stat complete data imported !
table a complete data imported !
table class complete data imported !
table course complete data imported !
table customer_t1 complete data imported !
table customer_t2 complete data imported !
table mytable complete data imported !
table school_department complete data imported !
table student complete data imported !
table t1 complete data imported !
table t2 complete data imported !
table t_test complete data imported !
table teacher complete data imported !
table web_returns_p2 complete data imported !
Finish reading 87 SQL statements!
end restore operation ...
restore operation successful
total time: 1165  ms
步骤 6 连接 db_tpcc01 数据库。 ​​​​​​​
[omm@node0 ~]$ gsql -d db_tpcc01 -p 15400 -r
gsql ((openGauss 5.0.0 build a07d57c3) compiled at 2023-03-29 03:37:13 commit 0 last mr  )
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type "help" for help.
步骤 7 查看数据库中恢复的 customer_t1 表。
db_tpcc01=#  select * from customer_t1;
 c_customer_sk | c_customer_id | c_first_name | c_last_name 
---------------+---------------+--------------+-------------
          3769 | hello         |              | 
          6885 | maps          | Joes         | 
          4321 | tpcds         | Lily         | 
          9527 | world         | James        | 
(4 rows)
步骤 8 退出 db_tpcc01 数据库。
db_tpcc01=# \q
[omm@node0 ~]$ 
4.4.3 gs_restore 导入示例 2

执行 gs_restore,将导出的 MPPDB_backup.dmp 文件(自定义归档格式)导入到 db_tpcc02数据库。

步骤 1 切换到 omm 用户,以操作系统用户 omm 登录数据库主节点

[root@node0 ~]# su - omm
Last login: Wed Apr 17 23:14:53 CST 2024 on pts/0


Welcome to 5.10.0-153.12.0.92.oe2203sp2.x86_64

System information as of time: 	2024年 04月 17日 星期三 23:25:56 CST

System load: 	0.06
Processes: 	197
Memory used: 	39.1%
Swap used: 	11.9%
Usage On: 	26%
IP address: 	192.168.28.131
Users online: 	1
To run a command as administrator(user "root"),use "sudo <command>".
步骤 2 连接 openGauss 数据库
[omm@node0 ~]$ gsql -d postgres -p 15400 -r
gsql ((openGauss 5.0.0 build a07d57c3) compiled at 2023-03-29 03:37:13 commit 0 last mr  )
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type "help" for help.
步骤 3 创建数据库
openGauss=# DROP DATABASE IF EXISTS db_tpcc02;
NOTICE:  database "db_tpcc02" does not exist, skipping
DROP DATABASE
openGauss=#  CREATE DATABASE db_tpcc02;
CREATE DATABASE
步骤 4 退出数据库。
openGauss=# \q
[omm@node0 ~]$ 
步骤 5 执行 gs_restore ,将导出的 MPPDB_backup.dmp 文件(自定义归档格式)导入到 db_tpcc02数据库
[omm@node0 ~]$ gs_restore /home/omm/logical/backup/MPPDB_backup.dmp -p 15400 -d db_tpcc02
start restore operation ...
table mytable complete data imported !
table pmk_configuration complete data imported !
table pmk_meta_data complete data imported !
table pmk_snapshot complete data imported !
table pmk_snapshot_datanode_stat complete data imported !
table a complete data imported !
table class complete data imported !
table course complete data imported !
table customer_t1 complete data imported !
table customer_t2 complete data imported !
table mytable complete data imported !
table school_department complete data imported !
table student complete data imported !
table t1 complete data imported !
table t2 complete data imported !
table t_test complete data imported !
table teacher complete data imported !
table web_returns_p2 complete data imported !
Finish reading 87 SQL statements!
end restore operation ...
restore operation successful
total time: 862 ms
步骤 6 连接 db_tpcc02 数据库。
[omm@node0 ~]$ gsql -d db_tpcc02 -p 15400 -r
gsql ((openGauss 5.0.0 build a07d57c3) compiled at 2023-03-29 03:37:13 commit 0 last mr  )
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type "help" for help.

步骤 7 查看数据库中恢复的 customer_t1 表。
db_tpcc02=# select * from customer_t1;
c_customer_sk | c_customer_id | c_first_name | c_last_name 
---------------+---------------+--------------+-------------
 3769 | hello | | 
 6885 | maps | Joes | 
 4321 | tpcds | Lily | 
 9527 | world | James | 
(4 rows)
步骤 8 退出 db_tpcc02 数据库
db_tpcc02=# \q
[omm@node0 ~]$ 
4.4.4 gs_restore 导入示例 3
执行 gs_restore ,将导出的 MPPDB_backup 文件(目录格式) 导入到 postgres 数据库。
步骤 1 切换到 omm 用户,以操作系统用户 omm 登录数据库主节点。
[root@node0 ~]# su - omm
Last login: Wed Apr 17 23:25:56 CST 2024 on pts/0


Welcome to 5.10.0-153.12.0.92.oe2203sp2.x86_64

System information as of time: 	2024年 04月 17日 星期三 23:43:58 CST

System load: 	0.30
Processes: 	196
Memory used: 	39.1%
Swap used: 	14.9%
Usage On: 	26%
IP address: 	192.168.28.131
Users online: 	1
To run a command as administrator(user "root"),use "sudo <command>".
步骤 2 连接 openGauss 数据库。
[omm@node0 ~]$ gsql -d postgres -p 15400 -r
gsql ((openGauss 5.0.0 build a07d57c3) compiled at 2023-03-29 03:37:13 commit 0 last mr  )
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type "help" for help.
步骤 3 创建数据库。
openGauss=# DROP DATABASE IF EXISTS db_tpcc03;
NOTICE:  database "db_tpcc03" does not exist, skipping
DROP DATABASE
openGauss=# CREATE DATABASE db_tpcc03;
CREATE DATABASE

步骤 4 退出数据库

openGauss=# \q
步骤 5 执行 gs_restore ,将导出的 MPPDB_backup 文件(目录格式)导入到 db_tpcc03 数据库。
[omm@node0 ~]$ gs_restore /home/omm/logical/backup/MPPDB_backup -p 15400 -d db_tpcc03
start restore operation ...
table mytable complete data imported !
table pmk_configuration complete data imported !
table pmk_meta_data complete data imported !
table pmk_snapshot complete data imported !
table pmk_snapshot_datanode_stat complete data imported !
table a complete data imported !
table class complete data imported !
table course complete data imported !
table customer_t1 complete data imported !
table customer_t2 complete data imported !
table mytable complete data imported !
table school_department complete data imported !
table student complete data imported !
table t1 complete data imported !
table t2 complete data imported !
table t_test complete data imported !
table teacher complete data imported !
table web_returns_p2 complete data imported !
Finish reading 87 SQL statements!
end restore operation ...
restore operation successful
total time: 1836  ms
[omm@node0 ~]$ 
步骤 6 连接 db_tpcc03 数据库。
[omm@node0 ~]$ gsql -d db_tpcc03 -p 15400 -r
gsql ((openGauss 5.0.0 build a07d57c3) compiled at 2023-03-29 03:37:13 commit 0 last mr  )
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type "help" for help.

步骤 7 查看数据库中恢复的 customer_t1 表。
db_tpcc03=#  select * from customer_t1;
 c_customer_sk | c_customer_id | c_first_name | c_last_name 
---------------+---------------+--------------+-------------
          3769 | hello         |              | 
          6885 | maps          | Joes         | 
          4321 | tpcds         | Lily         | 
          9527 | world         | James        | 
(4 rows)

步骤 8 退出 db_tpcc03 数据库。 ​​​​​​​
db_tpcc03=# \q
[omm@node0 ~]$ 
4.4.5 gs_restore 导入示例 4
执行 gs_restore ,使用自定义归档格式的 MPPDB_backup.dmp 文件来进行如下导入操作。
导入时在重新创建数据库对象前,清理(删除)已存在于将要还原的数据库中的数据库对象。
步骤 1 由于在备份示例 1 已经将所有对象恢复到 db_tpcc01 数据库中,所以在再次导入时如果不先删除已经存在的对象,会出现报错信息。
[omm@node0 ~]$ gs_restore -W Bigdata@123 /home/omm/logical/backup/MPPDB_backup.dmp -p 15400 -d db_tpcc01
start restore operation ...
……
Error from TOC entry 3728; 1259 41282 INDEX inx_stu01 omm
could not execute query: ERROR: relation "inx_stu01" already exists
 Command was: CREATE INDEX inx_stu01 ON student USING btree (std_name) TABLESPACE 
pg_default;
Finish reading 87 SQL statements!
end restore operation ...
WARNING: errors ignored on restore: 69
restore operation successful
total time: 1166 ms
步骤 2 连接 db_tpcc01 数据库
[omm@node0 ~]$ gsql -d db_tpcc01 -p 15400 -r
gsql ((openGauss 5.0.0 build a07d57c3) compiled at 2023-03-29 03:37:13 commit 0 last mr  )
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type "help" for help.
步骤 3 查看数据库中恢复的 customer_t1 表。

db_tpcc01=# select * from customer_t1;
 c_customer_sk | c_customer_id | c_first_name | c_last_name 
---------------+---------------+--------------+-------------
          3769 | hello         |              | 
          6885 | maps          | Joes         | 
          4321 | tpcds         | Lily         | 
          9527 | world         | James        | 
(4 rows)
原有数据表并没有被删除,导入的数据表以数据追加的方式导入数据。
步骤 4 退出数据库
db_tpcc01=# \q
[omm@node0 ~]$ 
步骤 5 输入以下命令,使用 -c 参数手工删除依赖,导入完成后再重新创建
[omm@node0 ~]$ gs_restore -W Bigdata@123 /home/omm/logical/backup/MPPDB_backup.dmp -p 15400 -d db_tpcc01 -e -c
start restore operation ...
table mytable complete data imported !
table pmk_configuration complete data imported !
table pmk_meta_data complete data imported !
table pmk_snapshot complete data imported !
table pmk_snapshot_datanode_stat complete data imported !
table a complete data imported !
table class complete data imported !
table course complete data imported !
table customer_t1 complete data imported !
table customer_t2 complete data imported !
table mytable complete data imported !
table school_department complete data imported !
table student complete data imported !
table t1 complete data imported !
table t2 complete data imported !
table t_test complete data imported !
table teacher complete data imported !
table web_returns_p2 complete data imported !
Finish reading 87 SQL statements!
end restore operation ...
restore operation successful
total time: 1285 ms
注:如果原对象存在跨模式的依赖则需手工强制干预。
步骤 6 连接 db_tpcc01 数据库
[omm@node0 ~]$ gsql -d db_tpcc01 -p 15400 -r
gsql ((openGauss 5.0.0 build a07d57c3) compiled at 2023-03-29 03:37:13 commit 0 last mr  )
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type "help" for help.
步骤 7 查看数据库中恢复的 customer_t1
db_tpcc01=# select * from customer_t1;
 c_customer_sk | c_customer_id | c_first_name | c_last_name 
---------------+---------------+--------------+-------------
          3769 | hello         |              | 
          6885 | maps          | Joes         | 
          4321 | tpcds         | Lily         | 
          9527 | world         | James        | 
(4 rows)
步骤 8 退出数据库
db_tpcc01=# \q
[omm@node0 ~]$ 
原有数据表已经被清除,恢复的是导入的数据表
4.4.6 gs_restore 导入示例 5
执行 gs_restore ,使用自定义归档格式的 MPPDB_backup.dmp 文件来进行如下导入操作。
只导入 PUBLIC 模式下表 customer_t1 的定义。
步骤 1 切换到 omm 用户,以操作系统用户 omm 登录数据库主节点。
[root@node0 ~]# su - omm
Last login: Thu Apr 18 11:39:13 CST 2024 on pts/0


Welcome to 5.10.0-153.12.0.92.oe2203sp2.x86_64

System information as of time: 	2024年 04月 18日 星期四 11:48:17 CST

System load: 	0.08
Processes: 	196
Memory used: 	38.3%
Swap used: 	10.5%
Usage On: 	26%
IP address: 	192.168.28.131
Users online: 	1
To run a command as administrator(user "root"),use "sudo <command>".
步骤 2 连接 openGauss 数据库。
[omm@node0 ~]$ gsql -d postgres -p 15400 -r
gsql ((openGauss 5.0.0 build a07d57c3) compiled at 2023-03-29 03:37:13 commit 0 last mr  )
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type "help" for help.
步骤 3 创建数据库。
openGauss=# DROP DATABASE IF EXISTS db_tpcc04;
NOTICE:  database "db_tpcc04" does not exist, skipping
DROP DATABASE
openGauss=# CREATE DATABASE db_tpcc04;
CREATE DATABASE
openGauss=# 
步骤 4 退出数据库。
openGauss=# \q
[omm@node0 ~]$ 
步骤 5 执行 gs_restore ,只导入 PUBLIC 模式下表 customer_t1 的定义。
[omm@node0 ~]$ gs_restore /home/omm/logical/backup/MPPDB_backup.dmp -p 15400 -d db_tpcc04 -n public -t customer_t1
table customer_t1 complete data imported !
Finish reading 87 SQL statements!
end restore operation ...
restore operation successful
total time: 34 ms
步骤 6 连接 db_tpcc04 数据库。
[omm@node0 ~]$ gsql -d db_tpcc04 -p 15400 -r
gsql ((openGauss 5.0.0 build a07d57c3) compiled at 2023-03-29 03:37:13 commit 0 last mr  )
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type "help" for help.
步骤 7 查看数据库中恢复的 customer_t1 表。
db_tpcc04=# select * from customer_t1;
c_customer_sk | c_customer_id | c_first_name | c_last_name 
---------------+---------------+--------------+-------------
 3769 | hello | | 
 6885 | maps | Joes | 
 4321 | tpcds | Lily | 
 9527 | world | James | 
(4 rows)
步骤 8 查看 postgres 数据库中 LUCY 模式下的表。
db_tpcc04=#  select * from lucy.mytable;
ERROR:  schema "lucy" does not exist
LINE 1: select * from lucy.mytable;
                      ^
db_tpcc04=# 
步骤 9 查看 postgres 数据库中 PUBLIC 模式下的 customer_t2
db_tpcc04=# select * from customer_t2;
ERROR:  relation "customer_t2" does not exist on dn_6001
LINE 1: select * from customer_t2;
                      ^
db_tpcc04=# 
说明只恢复了 PUBLIC shema 模式下的的 customer_t1 表。
本实验结束。
  • 11
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值