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

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

原创 xiaocx 2022-12-13
364

学习目标

    掌握openGauss数据库的逻辑备份和恢复技术。

课程学习

1.为逻辑备份准备环境


--创建逻辑备份的存储目录:
su - omm
mkdir -p /openGauss/backup

--创建备份恢复用户,需要具有super或者sysadmin权限
gsql -r
create user test IDENTIFIED BY 'huawei@1234' sysadmin ;
--创建恢复测试数据库testdb
CREATE TABLESPACE test_tbs RELATIVE LOCATION 'tablespace/test_tbs1';
CREATE DATABASE testdb WITH TABLESPACE = test_tbs;

--在omm数据库上,创建测试表test1、test2:

CREATE TABLE test1(col int);
CREATE TABLE test2(col int);
\q
--查看数据
gsql -d omm -c "\dt"

[root@opengauss ~]# su - omm
上一次登录:二 12月 13 14:50:54 CST 2022pts/0 上
[omm@opengauss ~]$ mkdir -p /openGauss/backup
[omm@opengauss ~]$ gsql -r
gsql ((openGauss 3.1.0 build 4e931f9a) compiled at 2022-09-29 14:19:24 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 ;
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);
CREATE TABLE
omm=# CREATE TABLE test2(col int);
CREATE TABLE
omm=# \q
[omm@opengauss ~]$ 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@opengauss ~]$ 


2.逻辑备份和恢复案例1(sql格式)

逻辑备份,使用gs_dump备份数据库,生成sql文件:

--使用test用户,备份数据库omm:

gs_dump -U test -W huawei@1234 omm -F p -f /openGauss/backup/backup.sql


逻辑恢复:

--使用用户test,执行用gs_dump生成的sql脚本,将数据恢复到testdb数据库中:
gsql -d testdb -U test -W huawei@1234 -f /openGauss/backup/backup.sql
恢复验证:

--验证数据库omm的备份已经被恢复到数据库testdb:
--源库(备份的数据库):
gsql -d omm -c "\dt"

--新库(恢复的数据库):
gsql -d testdb -U test -W huawei@1234 -c "\dt"

[omm@opengauss ~]$ gs_dump -U test -W huawei@1234  omm -F p -f /openGauss/backup/backup.sql
gs_dump[port='5432'][omm][2022-12-13 14:53:16]: The total objects number is 433.
gs_dump[port='5432'][omm][2022-12-13 14:53:16]: [100.00%] 433 objects have been dumped.
gs_dump[port='5432'][omm][2022-12-13 14:53:16]: dump database omm successfully
gs_dump[port='5432'][omm][2022-12-13 14:53:16]: total time: 1340  ms
[omm@opengauss ~]$   gsql -d testdb   -U test  -W huawei@1234  -f /openGauss/backup/backup.sql
SET
SET
SET
SET
SET
SET
CREATE SCHEMA
ALTER SCHEMA
CREATE SCHEMA
ALTER SCHEMA
SET
SET
SET
CREATE TABLE
ALTER TABLE
CREATE TABLE
ALTER TABLE
REVOKE
REVOKE
GRANT
GRANT
total time: 13  ms
[omm@opengauss ~]$
[omm@opengauss ~]$
[omm@opengauss ~]$ 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@opengauss ~]$ 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@opengauss ~]$ 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)

[omm@opengauss ~]$ 


3.openGauss数据库逻辑备份和恢复案例2(dump格式)

逻辑备份:使用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 /openGauss/backup/backup.dump
逻辑恢复:

--使用gs_dump生成的归档文件恢复数据库

gsql -d testdb -U test -W huawei@1234 -f /openGauss/backup/backup.dump

恢复验证:

--源库(备份的数据库):
gsql -d omm -c "\dt"
--新库(恢复的数据库):
gsql -d testdb -U test -W huawei@1234 -c "\dt"

[omm@opengauss ~]$ gsql -r
gsql ((openGauss 3.1.0 build 4e931f9a) compiled at 2022-09-29 14:19:24 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=# \q
[omm@opengauss ~]$ gs_dump -U test -W huawei@1234  omm -F p -f /openGauss/backup/backup.dump
gs_dump[port='5432'][omm][2022-12-13 14:56:28]: The total objects number is 437.
gs_dump[port='5432'][omm][2022-12-13 14:56:28]: [100.00%] 437 objects have been dumped.
gs_dump[port='5432'][omm][2022-12-13 14:56:28]: dump database omm successfully
gs_dump[port='5432'][omm][2022-12-13 14:56:28]: total time: 886  ms
[omm@opengauss ~]$ gsql -d testdb   -U test  -W huawei@1234  -f /openGauss/backup/backup.dump
SET
SET
SET
SET
SET
SET
gsql:/openGauss/backup/backup.dump:16: ERROR:  schema "gaussdb" already exists
ALTER SCHEMA
gsql:/openGauss/backup/backup.dump:25: ERROR:  schema "test" already exists
ALTER SCHEMA
SET
SET
SET
gsql:/openGauss/backup/backup.dump:43: ERROR:  relation "test1" already exists in schema "public"
DETAIL:  creating new table with existing name in the same schema
ALTER TABLE
gsql:/openGauss/backup/backup.dump:55: 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: 10  ms
[omm@opengauss ~]$ 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}
 public | test3 | table | omm   | {orientation=row,compression=no}
 public | test4 | table | omm   | {orientation=row,compression=no}
(4 rows)

[omm@opengauss ~]$ 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)

[omm@opengauss ~]$



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

文章被以下合辑收录

评论