学习目标
掌握openGauss数据库的逻辑备份和恢复技术。
课程作业
1.逻辑备份和恢复案例1:使用sql格式进行备份和恢复omm数据库
--创建备份目录root@modb:~# su - omm
omm@modb:~$ mkdir /var/lib/opengauss/backup
--在omm数据库上创建测试表omm@modb:~$ gsql -d omm
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=# CREATE TABLE test1(txt char(30));
CREATE TABLE
omm=# INSERT INTO TEST1 VALUES('2022年12月13日21:04:39');
INSERT 0 1
omm=# \dt
List of relations
Schema | Name | Type | Owner | Storage
--------+-------+-------+-------+----------------------------------
public | test1 | table | omm | {orientation=row,compression=no}
(1 row)
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 21:06:40]: The total objects number is 411.
gs_dump[port='5432'][omm][2022-12-13 21:06:40]: [100.00%] 411 objects have been dumped.
gs_dump[port='5432'][omm][2022-12-13 21:06:40]: dump database omm successfully
gs_dump[port='5432'][omm][2022-12-13 21:06:40]: total time: 1726 ms
omm@modb:~$ ls /var/lib/opengauss/backup/
backup.sql
omm@modb:~$ 
--删除omm数据库上创建的测试表testomm@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=# drop table test1 ;
DROP TABLE
omm=# \dt
omm=# No relations found.
omm=#
--使用用户test,执行用gs_dump生成的sql脚本,将数据恢复omm数据库
omm@modb:~$ gsql -d omm -U test -W huawei@1234 -f /var/lib/opengauss/backup/backup.sql
SET
SET
SET
SET
SET
SET
gsql:/var/lib/opengauss/backup/backup.sql:16: ERROR: schema "test" already exists
ALTER SCHEMA
SET
SET
SET
CREATE TABLE
ALTER TABLE
REVOKE
REVOKE
GRANT
GRANT
total time: 11 ms
--查看恢复后的omm数据库,查看数据是否一致
omm@modb:~$ gsql -d omm
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=# \dt
omm=# List of relations
Schema | Name | Type | Owner | Storage
--------+-------+-------+-------+----------------------------------
public | test1 | table | omm | {orientation=row,compression=no}
(1 row)
omm=# select * from test1;
txt
--------------------------------
2022年12月13日21:04:39
(1 row)
omm=#
2.逻辑备份和恢复案例2:使用dump格式进行备份和恢复omm数据库
--使用gs_dump备份数据库,生成dump文件
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 21:34:31]: The total objects number is 411.
gs_dump[port='5432'][omm][2022-12-13 21:34:31]: [100.00%] 411 objects have been dumped.
gs_dump[port='5432'][omm][2022-12-13 21:34:31]: dump database omm successfully
gs_dump[port='5432'][omm][2022-12-13 21:34:31]: total time: 1608 ms
omm@modb:~$ ls /var/lib/opengauss/backup/
backup.dump backup.sql
omm@modb:~$ 
--删除omm数据库的测试表test1omm@modb:~$ gsql -d omm
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=# \dt
omm=# List of relations
Schema | Name | Type | Owner | Storage
--------+-------+-------+-------+----------------------------------
public | test1 | table | omm | {orientation=row,compression=no}
(1 row)
omm=# drop table test1;
DROP TABLE
omm=# \dt
No relations found.
--使用用户test,执行用gs_dump生成的sql脚本,将数据恢复到omm数据库中
omm@modb:~$ gsql -d omm -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
CREATE TABLE
ALTER TABLE
REVOKE
REVOKE
GRANT
GRANT
total time: 11 ms
omm@modb:~$
--查看恢复后的omm数据库,查看数据是否一致
omm@modb:~$ gsql -d omm
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=# \dt
List of relations
Schema | Name | Type | Owner | Storage
--------+-------+-------+-------+----------------------------------
public | test1 | table | omm | {orientation=row,compression=no}
(1 row)
omm=# select * from test1;
txt
--------------------------------
2022年12月13日21:04:39
(1 row)
omm=#
最后修改时间:2022-12-13 21:38:07
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




