一、学习目标
掌握openGauss数据库的逻辑备份和恢复技术。
二、课程学习
掌握openGauss数据库的逻辑备份和恢复技术,使用sql格式进行备份和恢复omm数据库,使用dump格式进行备份和恢复omm数据库.
三、课后作业
首先还是连接数据库,准备环境:
创建逻辑备份的存储目录:
su - omm
omm@modb:~$ ls /var/lib/opengauss/
data
omm@modb:~$ mkdir /var/lib/opengauss/backup
omm@modb:~$ ls /var/lib/opengauss/
backup data
创建备份恢复用户,需要具有super或者sysadmin权限
gsql -r
omm=# create user test identified by 'huawei@1234' sysadmin;
NOTICE: The encrypted password contains MD5 ciphertext, which is not secure.
omm=# CREATE ROLE
创建恢复测试数据库testdb
CREATE TABLESPACE test_tbs RELATIVE LOCATION 'tablespace/test_tbs1';
CREATE TABLESPACE
omm=# CREATE DATABASE testdb WITH TABLESPACE = test_tbs;
CREATE DATABASE
在omm数据库上,创建测试表test1、test2:
omm=# CREATE TABLE test1(col int);
CREATE TABLE
omm=# CREATE TABLE test2(col int);
CREATE TABLE
\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:
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-13 11:05:08]: The total objects number is 413.
gs_dump[port='5432'][omm][2022-12-13 11:05:08]: [100.00%] 413 objects have been dumped.
gs_dump[port='5432'][omm][2022-12-13 11:05:08]: dump database omm successfully
gs_dump[port='5432'][omm][2022-12-13 11:05:08]: total time: 1564 ms
omm@modb:~$ ls -lh /var/lib/opengauss/backup/
-rw------- 1 omm omm 1.3K Dec 13 11:05 backup.sql
total 4.0K
逻辑恢复:
使用用户test,执行用gs_dump生成的sql脚本,将数据恢复到testdb数据库中:
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: 25 ms
2.逻辑备份和恢复案例2:使用dump格式进行备份和恢复omm数据库
omm=# CREATE TABLE test3(col int);
omm=# CREATE TABLE
CREATE TABLE test4(col int);
CREATE TABLE
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-13 11:24:21]: The total objects number is 417.
gs_dump[port='5432'][omm][2022-12-13 11:24:21]: [100.00%] 417 objects have been dumped.
gs_dump[port='5432'][omm][2022-12-13 11:24:21]: dump database omm successfully
gs_dump[port='5432'][omm][2022-12-13 11:24:21]: total time: 1560 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
四、心得体会
通过今天学习掌握openGauss数据库的逻辑备份和恢复技术,实验了使用sql格式进行备份和恢复omm数据库,使用dump格式进行备份和恢复omm数据库.
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




