前言:
最近参加了由opengauss、墨天轮、鲲鹏社区一起推出的活动《每日一练 opengauss 3.0.0 数据库在线实训课程》,共21天,墨天轮提供实操环境,特此记录学习笔记。
活动详情:https://www.modb.pro/db/551619
主题:
掌握openGauss数据库的逻辑备份和恢复技术。。
学习笔记
1.为逻辑备份准备环境
–创建逻辑备份的存储目录:
root@modb:~# su - omm
[omm@opengauss install]$ mkdir /oracle/huawei/install/backup
–创建备份恢复用户,需要具有super或者sysadmin权限
[omm@opengauss install]$ gsql -d mydb1 -p 15400 -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.
mydb1=# \conninfo
You are connected to database "mydb1" as user "omm" via socket in "/oracle/huawei/tmp" at port "15400".
mydb1=# create user test IDENTIFIED BY 'huawei@1234' sysadmin ;
CREATE ROLE
mydb1=#
–创建恢复测试数据库testdb
mydb1=# CREATE TABLESPACE test_tbs RELATIVE LOCATION 'tablespace/test_tbs1';
CREATE TABLESPACE
mydb1=# CREATE DATABASE testdb WITH TABLESPACE = test_tbs;
CREATE DATABASE
mydb1=#
mydb1=#
--在mydb1数据库上,创建测试表test1、test2:
mydb1=# CREATE TABLE test1(col int);
CREATE TABLE
mydb1=# CREATE TABLE test2(col int);
CREATE TABLE
mydb1=#
–查看数据
mydb1=# \q
[omm@opengauss install]$ gsql -d omm -c "\dt"
failed to connect /oracle/huawei/tmp:5432.
[omm@opengauss install]$ gsql -d mydb1 -p 15400 -c "\dt"
List of relations
Schema | Name | Type | Owner | Storage
---------+---------------+-------+-------+----------------------------------
testsm1 | check_table | table | test1 | {orientation=row,compression=no}
testsm1 | foreign_table | table | test1 | {orientation=row,compression=no}
testsm1 | not_null | table | test1 | {orientation=row,compression=no}
testsm1 | primary_table | table | test1 | {orientation=row,compression=no}
testsm1 | table | table | test1 | {orientation=row,compression=no}
testsm1 | test1 | table | omm | {orientation=row,compression=no}
testsm1 | test2 | table | omm | {orientation=row,compression=no}
testsm1 | testsm2 | table | test1 | {orientation=row,compression=no}
testsm1 | unique_test | table | test1 | {orientation=row,compression=no}
(9 rows)
[omm@opengauss install]$
2.逻辑备份和恢复案例1(sql格式)
逻辑备份,使用gs_dump备份数据库,生成sql文件:
--使用test用户,备份数据库mydb1:
[omm@opengauss backup]$ gs_dump -p 15400 -U test -W huawei@1234 mydb1 -F p -f /oracle/huawei/install/backup/backup.sql
gs_dump[port='15400'][mydb1][2022-12-14 10:16:50]: The total objects number is 464.
gs_dump[port='15400'][mydb1][2022-12-14 10:16:50]: [100.00%] 464 objects have been dumped.
gs_dump[port='15400'][mydb1][2022-12-14 10:16:50]: dump database mydb1 successfully
gs_dump[port='15400'][mydb1][2022-12-14 10:16:50]: total time: 9017 ms
[omm@opengauss backup]$
逻辑恢复:
--使用用户test,执行用gs_dump生成的sql脚本,将数据恢复到testdb数据库中:
[omm@opengauss backup]$ gsql -d testdb -p 15400 -U test -W huawei@1234 -f /oracle/huawei/install/backup/backup.sql
...
total time: 436 ms
恢复验证
--验证数据库mydb1的备份已经被恢复到数据库testdb:
--源库(备份的数据库):
gsql -d mydb1 -p 15400 -c "\dt"
[omm@opengauss backup]$ gsql -d mydb1 -p 15400 -c "\dt"
List of relations
Schema | Name | Type | Owner | Storage
---------+---------------+-------+-------+----------------------------------
testsm1 | check_table | table | test1 | {orientation=row,compression=no}
testsm1 | foreign_table | table | test1 | {orientation=row,compression=no}
testsm1 | not_null | table | test1 | {orientation=row,compression=no}
testsm1 | primary_table | table | test1 | {orientation=row,compression=no}
testsm1 | table | table | test1 | {orientation=row,compression=no}
testsm1 | test1 | table | omm | {orientation=row,compression=no}
testsm1 | test2 | table | omm | {orientation=row,compression=no}
testsm1 | testsm2 | table | test1 | {orientation=row,compression=no}
testsm1 | unique_test | table | test1 | {orientation=row,compression=no}
(9 rows)
[omm@opengauss backup]$
--新库(恢复的数据库):
gsql -d testdb -p 15400 -U test -W huawei@1234 -c "\dt"
[omm@opengauss backup]$ gsql -d testdb -p 15400 -U test -W huawei@1234 -c "\dt"
List of relations
Schema | Name | Type | Owner | Storage
--------+------+-------+-------+----------------------------------
public | test | table | omm | {orientation=row,compression=no}
(1 row)
[omm@opengauss backup]$
3.openGauss数据库逻辑备份和恢复案例2(dump格式)
逻辑备份:使用gs_dump备份数据库,生成归档格式的备份文件
--创建测试数据
[omm@opengauss backup]$ gsql -p 15400 -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=# \dt
List of relations
Schema | Name | Type | Owner | Storage
--------+-------+-------+-------+----------------------------------
public | test3 | table | omm | {orientation=row,compression=no}
public | test4 | table | omm | {orientation=row,compression=no}
(2 rows)
omm=#
--使用test用户,备份omm数据库,生成归档格式的备份文件:
[omm@opengauss backup]$ gs_dump -p 15400 -U test -W huawei@1234 omm -F p -f /oracle/huawei/install/backup/backup.dump
gs_dump[port='15400'][omm][2022-12-14 10:31:50]: The total objects number is 432.
gs_dump[port='15400'][omm][2022-12-14 10:31:50]: [100.00%] 432 objects have been dumped.
gs_dump[port='15400'][omm][2022-12-14 10:31:50]: dump database omm successfully
gs_dump[port='15400'][omm][2022-12-14 10:31:50]: total time: 6189 ms
[omm@opengauss backup]$
逻辑恢复:
--使用gs_dump生成的归档文件恢复数据库
[omm@opengauss backup]$ gsql -d testdb -p 15400 -U test -W huawei@1234 -f /oracle/huawei/install/backup/backup.dump
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: 46 ms
[omm@opengauss backup]$
恢复验证:
--源库(备份的数据库):
[omm@opengauss backup]$ gsql -d omm -c "\dt"
failed to connect /oracle/huawei/tmp:5432.
[omm@opengauss backup]$ gsql -d omm -p 15400 -c "\dt"
List of relations
Schema | Name | Type | Owner | Storage
--------+-------+-------+-------+----------------------------------
public | test3 | table | omm | {orientation=row,compression=no}
public | test4 | table | omm | {orientation=row,compression=no}
(2 rows)
[omm@opengauss backup]$
--新库(恢复的数据库):
[omm@opengauss backup]$ gsql -d testdb -p 15400 -U test -W huawei@1234 -c "\dt"
List of relations
Schema | Name | Type | Owner | Storage
--------+-------+-------+-------+----------------------------------
public | test3 | table | omm | {orientation=row,compression=no}
public | test4 | table | omm | {orientation=row,compression=no}
(2 rows)
[omm@opengauss backup]$
总结:
第20天的学习中,我们学会了opengauss的逻辑备份和恢复技术。
可以用sql格式或者dmmp格式。
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




