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

openGauss 每日一练第20天

原创 酷酷咖啡 2022-12-13
260

学习目标

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

课程学习

1.为逻辑备份准备环境

--创建逻辑备份的存储目录:
su - omm
mkdir /var/lib/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"

2.逻辑备份和恢复案例1(sql格式)
逻辑备份,使用gs_dump备份数据库,生成sql文件:

--使用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"
  1. 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 /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"

课程作业

准 备

su - omm gsql -r -- 创建备份用户 create user backuser IDENTIFIED BY 'abcd@1234' sysadmin ; -- 创建表空间和数据库 CREATE TABLESPACE test_tbs RELATIVE LOCATION 'tablespace/test_tbs'; CREATE DATABASE testdb WITH TABLESPACE = test_tbs; -- 创建sql恢复数据库 test_re_db CREATE DATABASE test_re_db WITH TABLESPACE = test_tbs; -- 创建dump恢复数据库 test_dumpre_db CREATE DATABASE test_dumpre_db WITH TABLESPACE = test_tbs; -- 切换到新创建的库 \c testdb -- 创建表 create table t1(col int); create table t2(col int); create table t3(col int); -- 插入一些数据 insert into t1 values (1); insert into t1 values (2); insert into t1 values (3); insert into t1 values (4); insert into t1 values (5); insert into t2 values (11); insert into t2 values (22); insert into t2 values (33); insert into t2 values (44); insert into t2 values (55); insert into t3 values (111); insert into t3 values (222); insert into t3 values (333); insert into t3 values (444); insert into t3 values (555);

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

  • 创建备份路径

    mkdir /var/lib/opengauss/backup
  • 创建备份文件

    gs_dump -U backuser -W abcd@1234 testdb -F p -f /var/lib/opengauss/backup/backup.sql
  • 恢复数据到 test_re_db

    # 恢复备份的sql 文件到 新数据库 test_re_db gsql -d test_re_db -U backuser -W abcd@1234 -f /var/lib/opengauss/backup/backup.sql SET SET SET SET SET SET SET SET SET CREATE TABLE ALTER TABLE CREATE TABLE ALTER TABLE CREATE TABLE ALTER TABLE REVOKE REVOKE GRANT GRANT total time: 24 ms
    -- 进入到数据库,查看数据是否恢复到新的库 test_re_db中 omm@modb:/var/lib/opengauss/backup$ 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=# \c test_re_db Non-SSL connection (SSL connection is recommended when requiring high-security) You are now connected to database "test_re_db" as user "omm". test_re_db=# \dt List of relations Schema | Name | Type | Owner | Storage --------+------+-------+----------+---------------------------------- public | t1 | table | backuser | {orientation=row,compression=no} public | t2 | table | backuser | {orientation=row,compression=no} public | t3 | table | backuser | {orientation=row,compression=no} (3 rows) -- 查看恢复后的表中的数据,由此可以看到数据均已恢复到新库的表中了 test_re_db=# select * from t1; col ----- 1 2 3 4 5 (5 rows) test_re_db=# select * from t2; col ----- 11 22 33 44 55 (5 rows) test_re_db=# select * from t3; col ----- 111 222 333 444 555 (5 rows)

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

  • dump 格式备份数据库

    # 将testdb备份成dump格式 gs_dump -U backuser -W abcd@1234 testdb -F c -f /var/lib/opengauss/backup/backup.dump
  • dump 格式恢复数据库

    -- 使用 gs_restore 命令恢复二进制备份文件 omm@modb:~$ gs_restore -d test_dumpre_db -U backuser -W abcd@1234 /var/lib/opengauss/backup/backup.dump start restore operation ... table t1 complete data imported ! table t2 complete data imported ! table t3 complete data imported ! Finish reading 12 SQL statements! end restore operation ... restore operation successful total time: 44 ms -- 进入数据库查看恢复情况 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=# \c test_dumpre_db Non-SSL connection (SSL connection is recommended when requiring high-security) You are now connected to database "test_dumpre_db" as user "omm". -- 查看库中表是否恢复,可以看到 t1,t2,t3均已恢复回来 test_dumpre_db=# \dt List of relations Schema | Name | Type | Owner | Storage --------+------+-------+----------+---------------------------------- public | t1 | table | backuser | {orientation=row,compression=no} public | t2 | table | backuser | {orientation=row,compression=no} public | t3 | table | backuser | {orientation=row,compression=no} (3 rows) -- 查询 三个表中的数据是否恢复,可以看到数据也都恢复回来了 test_dumpre_db=# select * from t1; col ----- 1 2 3 4 5 (5 rows) test_dumpre_db=# select * from t2; col ----- 11 22 33 44 55 (5 rows) test_dumpre_db=# select * from t3; col ----- 111 222 333 444 555 (5 rows)

    ==注意:==课程学习中备份方式仅后缀名称不一样,实际还是备份的文本类型的;作业中使用了二进制 也就是 -F 中 c 参数的配置,更多的备份类型可以使用 --help 查看

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

评论