暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

openGauss每日一练第20天 | openGauss数据库的逻辑备份和恢复

原创 2022-12-13
476

0.学习内容与环境进入

掌握openGauss数据库的逻辑备份和恢复技术。
gs_dump
gsql -f <dumpfile> 恢复数据
gsql -c "\dt" 运行"\dt"命令

  • 进入环境
su - omm 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.

  • 查看全部数据库
\l
List of databases

Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+-------+----------+---------+-------+-------------------
omm | omm | UTF8 | C | C |
postgres | omm | UTF8 | C | C |
template0 | omm | UTF8 | C | C | =c/omm +
| | | | | omm=CTc/omm
| template1 | omm | UTF8 | C | C | =c/omm +
| | | | | omm=CTc/omm
| (4 rows)

1.逻辑备份和恢复案例1:使用sql格式进行备份和恢复omm数据库

  • 创建表t1,t2以验证数据
CREATE TABLE t1(id integer, col2 char(30)); INSERT into t1 values(1, 't1 testdata'); CREATE TABLE t2(id integer, col2 char(30)); INSERT into t2 values(1, 't2 testdata'); \dt \q
List of relations

Schema | Name | Type | Owner | Storage
--------+------+-------+-------+----------------------------------
public | t1 | table | omm | {orientation=row,compression=no}
public | t2 | table | omm | {orientation=row,compression=no}
(2 rows)

  • 导出数据为omm-bk.sql
mkdir /var/lib/opengauss/backup gs_dump -U omm omm -f /var/lib/opengauss/backup/omm-bk.sql
gs_dump[port='5432'][omm][2022-12-13 23:44:51]: The total objects number is 412.
gs_dump[port='5432'][omm][2022-12-13 23:44:51]: [100.00%] 412 objects have been dumped.
gs_dump[port='5432'][omm][2022-12-13 23:44:51]: dump database omm successfully
gs_dump[port='5432'][omm][2022-12-13 23:44:51]: total time: 1330 ms

  • 在交互命令中删除数据
drop table t1; drop table t2; \dt \q
  • 利用omm-bk.sql恢复数据
gsql -d omm -U omm -f /var/lib/opengauss/backup/omm-bk.sql
SET
SET
SET
SET
SET
SET
SET
SET
SET
CREATE TABLE
ALTER TABLE
CREATE TABLE
ALTER TABLE
REVOKE
REVOKE
GRANT
GRANT
total time: 16 ms

  • 查看恢复后数据
gsql -c "\dt"
                    <center>List of relations</center><br/>

Schema | Name | Type | Owner | Storage

--------±-----±------±------±---------------------------------

public | t1 | table | omm | {orientation=row,compression=no}

public | t2 | table | omm | {orientation=row,compression=no}

(2 rows)


  • 验证数据库omm的备份已经被恢复
gsql -d omm -c "\dt"

2.逻辑备份和恢复案例2:使用dump格式进行备份和恢复omm数据库

  • 备份为dump格式
gs_dump -U omm omm -F p -f /var/lib/opengauss/backup/omm-bk.dump
gs_dump[port='5432'][omm][2022-12-14 00:21:39]: The total objects number is 412.
gs_dump[port='5432'][omm][2022-12-14 00:21:39]: [100.00%] 412 objects have been dumped.
gs_dump[port='5432'][omm][2022-12-14 00:21:39]: dump database omm successfully
gs_dump[port='5432'][omm][2022-12-14 00:21:39]: total time: 1301 ms

  • 删除数据表
gsql -c "drop table t1; drop table t2;\dt"
DROP TABLE
DROP TABLE
No relations found.

  • 利用dump文件恢复数据
gsql -d omm -U omm -f /var/lib/opengauss/backup/omm-bk.dump
SET
SET
SET
SET
SET
SET
SET
SET
SET
CREATE TABLE
ALTER TABLE
CREATE TABLE
ALTER TABLE
REVOKE
REVOKE
GRANT
GRANT
total time: 23 ms

  • 查看表是否恢复成功
gsql -c "\dt"
List of relations

Schema | Name | Type | Owner | Storage
--------+------+-------+-------+----------------------------------
public | t1 | table | omm | {orientation=row,compression=no}
public | t2 | table | omm | {orientation=row,compression=no}
(2 rows)

最后修改时间:2022-12-14 00:57:28
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论