学习目标
掌握openGauss数据库的逻辑备份和恢复技术。
----为逻辑备份准备环境
root@modb:~# su - omm
omm@modb:~$ mkdir /var/lib/opengauss/backup
omm@modb:~$ gsql -r
gsql ((openGauss 3.0.0 build 02c14696) compiled at 2022-04-01 18:12:00 commit 0 last mr )
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type "help" for help.
omm=# create user test identified by 'huawei@1234' sysadmin ;
NOTICE: The encrypted password contains MD5 ciphertext, which is not secure.
CREATE ROLE
omm=# create tablespace test_tbs relative location 'tablespace/test_tbs1' ;
CREATE TABLESPACE
omm=# create database testdb with tablespace = test_tbs ;
CREATE DATABASE
omm=# create table test1(col int) ;
omm=# CREATE TABLE
omm=# create table test2(col int) ;
CREATE TABLE
omm=# \q
omm@modb:~$ gsql -d omm -c "\dt"
List of relations
Schema | Name | Type | Owner | Storage
--------+-------+-------+-------+----------------------------------
public | test1 | table | omm | {orientation=row,compression=no}
public | test2 | table | omm | {orientation=row,compression=no}
(2 rows)
复制
课程作业
1.逻辑备份和恢复案例1:使用sql格式进行备份和恢复omm数据库
--使用test用户,备份数据库omm:
gs_dump -U test -W huawei@1234 omm -F p -f /var/lib/opengauss/backup/backup.sql
逻辑恢复:
--使用用户test,执行用gs_dump生成的sql脚本,将数据恢复到testdb数据库中:
gsql -d testdb -U test -W huawei@1234 -f /var/lib/opengauss/backup/backup.sql
恢复验证:
--验证数据库omm的备份已经被恢复到数据库testdb:
--源库(备份的数据库):
gsql -d omm -c "\dt"
--新库(恢复的数据库):
gsql -d testdb -U test -W huawei@1234 -c "\dt"
复制
---备份
omm@modb:~$ gs_dump -U test -W huawei@1234 omm -F p -f /var/lib/opengauss/backup/backup.sql
gs_dump[port='5432'][omm][2022-12-14 17:52:44]: The total objects number is 413.
gs_dump[port='5432'][omm][2022-12-14 17:52:44]: [100.00%] 413 objects have been dumped.
gs_dump[port='5432'][omm][2022-12-14 17:52:44]: dump database omm successfully
gs_dump[port='5432'][omm][2022-12-14 17:52:44]: total time: 1446 ms
---恢复
omm@modb:~$
omm@modb:~$ gsql -d testdb -U test -W huawei@1234 -f /var/lib/opengauss/backup/backup.sql
SET
SET
SET
SET
SET
SET
CREATE SCHEMA
ALTER SCHEMA
SET
SET
SET
CREATE TABLE
ALTER TABLE
CREATE TABLE
ALTER TABLE
REVOKE
REVOKE
GRANT
GRANT
total time: 19 ms
omm@modb:~$
---恢复验证
omm@modb:~$ gsql -d omm -c "\dt"
List of relations
Schema | Name | Type | Owner | Storage
--------+-------+-------+-------+----------------------------------
public | test1 | table | omm | {orientation=row,compression=no}
public | test2 | table | omm | {orientation=row,compression=no}
(2 rows)
omm@modb:~$ gsql -d testdb -U test -W huawei@1234 -c "\dt"
List of relations
Schema | Name | Type | Owner | Storage
--------+-------+-------+-------+----------------------------------
public | test1 | table | omm | {orientation=row,compression=no}
public | test2 | table | omm | {orientation=row,compression=no}
(2 rows)
复制
2.逻辑备份和恢复案例2:使用dump格式进行备份和恢复omm数据库
逻辑备份:使用gs_dump备份数据库,生成归档格式的备份文件
--创建测试数据
gsql -r
CREATE TABLE test3(col int);
CREATE TABLE test4(col int);
\q
--使用test用户,备份omm数据库,生成归档格式的备份文件:
gs_dump -U test -W huawei@1234 omm -F p -f /var/lib/opengauss/backup/backup.dump
逻辑恢复:
--使用gs_dump生成的归档文件恢复数据库
gsql -d testdb -U test -W huawei@1234 -f /var/lib/opengauss/backup/backup.dump
恢复验证:
--源库(备份的数据库):
gsql -d omm -c "\dt"
--新库(恢复的数据库):
gsql -d testdb -U test -W huawei@1234 -c "\dt"
复制
omm@modb:~$ gsql -r
gsql ((openGauss 3.0.0 build 02c14696) compiled at 2022-04-01 18:12:00 commit 0 last mr )
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type "help" for help.
omm=# create table test3(col int);
CREATE TABLE
omm=# create table test4(col int);
CREATE TABLE
omm=#
omm=# \q
--备份
omm@modb:~$ gs_dump -U test -W huawei@1234 omm -F p -f /var/lib/opengauss/backup/backup.dump
gs_dump[port='5432'][omm][2022-12-14 18:03:31]: The total objects number is 417.
gs_dump[port='5432'][omm][2022-12-14 18:03:31]: [100.00%] 417 objects have been dumped.
gs_dump[port='5432'][omm][2022-12-14 18:03:31]: dump database omm successfully
gs_dump[port='5432'][omm][2022-12-14 18:03:31]: total time: 1632 ms
omm@modb:~$
--恢复
omm@modb:~$ gsql -d testdb -U test -W huawei@1234 -f /var/lib/opengauss/backup/backup.dump
SET
SET
SET
SET
SET
SET
gsql:/var/lib/opengauss/backup/backup.dump:16: ERROR: schema "test" already exists
ALTER SCHEMA
SET
SET
SET
gsql:/var/lib/opengauss/backup/backup.dump:34: ERROR: relation "test1" already exists in schema "public"
DETAIL: creating new table with existing name in the same schema
ALTER TABLE
gsql:/var/lib/opengauss/backup/backup.dump:46: ERROR: relation "test2" already exists in schema "public"
DETAIL: creating new table with existing name in the same schema
ALTER TABLE
CREATE TABLE
ALTER TABLE
CREATE TABLE
ALTER TABLE
REVOKE
REVOKE
GRANT
GRANT
total time: 19 ms
--验证
omm@modb:~$ gsql -d omm -c "\dt"
public | test4 | table | omm | {orientation=row,compression=no}
(4 rows)
List of relations
Schema | Name | Type | Owner | Storage
--------+-------+-------+-------+----------------------------------
public | test1 | table | omm | {orientation=row,compression=no}
public | test2 | table | omm | {orientation=row,compression=no}
public | test3 | table | omm | {orientation=row,compression=no}
omm@modb:~$ gsql -d testdb -U test -W huawei@1234 -c "\dt"
List of relations
Schema | Name | Type | Owner | Storage
--------+-------+-------+-------+----------------------------------
public | test1 | table | omm | {orientation=row,compression=no}
public | test2 | table | omm | {orientation=row,compression=no}
public | test3 | table | omm | {orientation=row,compression=no}
public | test4 | table | omm | {orientation=row,compression=no}
(4 rows)
复制
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。
评论
相关阅读
openGauss荣获中国软件行业协会多奖项,技术升级再创行业新高度
openGauss
558次阅读
2025-04-30 14:30:58
MogDB 发布更新,解决 openGauss 数据库在长事务情况下Ustore表膨胀问题
MogDB
308次阅读
2025-04-17 10:41:41
MogDB 发布更新,解决 openGauss 数据库在长事务情况下Ustore表膨胀问题
云和恩墨
201次阅读
2025-04-16 09:52:02
GitCode 成 openGauss 新归宿,国产开源数据库里程碑事件
严少安
174次阅读
2025-04-27 11:37:53
荣誉时刻!openGauss认证证书快递已发,快来看看谁榜上有名!
墨天轮小教习
162次阅读
2025-04-23 17:39:13
单个执行机并行执行MySQL到openGauss数据迁移子任务
Clipnosis
151次阅读
2025-04-30 16:39:58
Postgresql数据库单个Page最多存储多少行数据
maozicb
96次阅读
2025-04-23 16:02:19
openGauss6.0.0适配操作系统自带的软件,不依赖三方库
来杯拿铁
96次阅读
2025-04-18 10:49:53
openGauss新特性 | openGauss-DataVec向量数据库特性介绍
openGauss
67次阅读
2025-04-17 10:41:47
RISC-V 首迎 openGauss 7.0.0-RC1 全量版适配!数据库核心功能完整落地开源架构
openGauss
50次阅读
2025-04-16 10:33:59