--环境创建
--创建逻辑备份的存储目录:
su - omm
mkdir /var/lib/opengauss/backup
--创建备份恢复用户,需要具有super或者sysadmin权限
gsql -r
create user mytest IDENTIFIED BY 'mygstest@1234' sysadmin;
--创建恢复测试数据库mytestdb
CREATE TABLESPACE mytest_tbs RELATIVE LOCATION 'tablespace/mytest_tbs1';
CREATE DATABASE mytestdb WITH TABLESPACE = mytest_tbs;
--在omm数据库上,创建测试表mytest1、mytest2:
CREATE TABLE mytest1(col int);
CREATE TABLE mytest2(col int);
insert into mytest1 values(11),(12),(13),(14);
insert into mytest2 values(21),(22),(23),(24);
\q
--查看数据
gsql -d omm -c "\dt"
root@modb:~# su - omm
omm@modb:~$ mkdir /var/lib/opengauss/backup
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=# create user mytest IDENTIFIED BY 'mygstest@1234' sysadmin;
NOTICE: The encrypted password contains MD5 ciphertext, which is not secure.
CREATE ROLE
omm=# CREATE TABLESPACE mytest_tbs RELATIVE LOCATION 'tablespace/mytest_tbs1';
CREATE TABLESPACE
omm=# CREATE DATABASE mytestdb WITH TABLESPACE = mytest_tbs;
CREATE DATABASE
omm=# CREATE TABLE mytest1(col int);
CREATE TABLE
omm=# CREATE TABLE mytest2(col int);
CREATE TABLE
omm=# insert into mytest1 values(11),(12),(13),(14);
INSERT 0 4
omm=# insert into mytest2 values(21),(22),(23),(24);
INSERT 0 4
omm=# \q
omm@modb:~$ gsql -d omm -c "\dt"
List of relations
Schema | Name | Type | Owner | Storage
--------+---------+-------+-------+----------------------------------
public | mytest1 | table | omm | {orientation=row,compression=no}
public | mytest2 | table | omm | {orientation=row,compression=no}
(2 rows)
omm@modb:~$
1.逻辑备份和恢复案例1:使用sql格式进行备份和恢复omm数据库。
--使用mytest用户,备份数据库omm:
gs_dump -U mytest -W mygstest@1234 omm -F p -f /var/lib/opengauss/backup/backup.sql
--使用用户mytest,执行用gs_dump生成的sql脚本,将数据恢复到mytestdb数据库中:
gsql -d mytestdb -U mytest -W mygstest@1234 -f /var/lib/opengauss/backup/backup.sql
--验证数据库omm的备份已经被恢复到数据库mytestdb:
--源库(备份的数据库):
gsql -d omm -c "\dt"
--新库(恢复的数据库):
gsql -d mytestdb -U mytest -W mygstest@1234 -c "\dt"
omm@modb:~$ gs_dump -U mytest -W mygstest@1234 omm -F p -f /var/lib/opengauss/backup/backup.sql
gs_dump[port='5432'][omm][2022-12-13 10:26:35]: The total objects number is 413.
gs_dump[port='5432'][omm][2022-12-13 10:26:35]: [100.00%] 413 objects have been dumped.
gs_dump[port='5432'][omm][2022-12-13 10:26:35]: dump database omm successfully
gs_dump[port='5432'][omm][2022-12-13 10:26:35]: total time: 1432 ms
omm@modb:~$ gsql -d mytestdb -U mytest -W mygstest@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: 21 ms
omm@modb:~$ gsql -d omm -c "\dt"
List of relations
Schema | Name | Type | Owner | Storage
--------+---------+-------+-------+----------------------------------
public | mytest1 | table | omm | {orientation=row,compression=no}
public | mytest2 | table | omm | {orientation=row,compression=no}
(2 rows)
omm@modb:~$ gsql -d mytestdb -U mytest -W mygstest@1234 -c "\dt"
List of relations
Schema | Name | Type | Owner | Storage
--------+---------+-------+-------+----------------------------------
public | mytest1 | table | omm | {orientation=row,compression=no}
public | mytest2 | table | omm | {orientation=row,compression=no}
(2 rows)
omm@modb:~$
2.逻辑备份和恢复案例2:使用dump格式进行备份和恢复omm数据库。
--清理mytestdb库数据
gsql -d mytestdb -U mytest -W mygstest@1234
drop table mytest1;
drop table mytest2;
--使用mytest用户,备份omm数据库,生成归档格式的备份文件:
gs_dump -U mytest -W mygstest@1234 omm -F p -f /var/lib/opengauss/backup/backup.dump
--使用gs_dump生成的归档文件恢复数据库
gsql -d mytestdb -U mytest -W mygstest@1234 -f /var/lib/opengauss/backup/backup.dump
--源库(备份的数据库):
gsql -d omm -c "\dt"
--新库(恢复的数据库):
gsql -d mytestdb -U mytest -W mygstest@1234 -c "\dt"
omm@modb:~$ gsql -d mytestdb -U mytest -W mygstest@1234
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.
mytestdb=> drop table mytest1;
DROP TABLE
mytestdb=> drop table mytest2;
DROP TABLE
mytestdb-> \dt
No relations found.
mytestdb-> \q
omm@modb:~$ gs_dump -U mytest -W mygstest@1234 omm -F p -f /var/lib/opengauss/backup/backup.dump
gs_dump[port='5432'][omm][2022-12-13 10:29:40]: The total objects number is 413.
gs_dump[port='5432'][omm][2022-12-13 10:29:40]: [100.00%] 413 objects have been dumped.
gs_dump[port='5432'][omm][2022-12-13 10:29:40]: dump database omm successfully
gs_dump[port='5432'][omm][2022-12-13 10:29:40]: total time: 1643 ms
omm@modb:~$ gsql -d mytestdb -U mytest -W mygstest@1234 -f /var/lib/opengauss/backup/backup.dump
SET
SET
SET
SET
SET
SET
gsql:/var/lib/opengauss/backup/backup.dump:16: ERROR: schema "mytest" already exists
ALTER SCHEMA
SET
SET
SET
CREATE TABLE
ALTER TABLE
CREATE TABLE
ALTER TABLE
REVOKE
REVOKE
GRANT
GRANT
total time: 16 ms
omm@modb:~$ gsql -d omm -c "\dt"
List of relations
Schema | Name | Type | Owner | Storage
--------+---------+-------+-------+----------------------------------
public | mytest1 | table | omm | {orientation=row,compression=no}
public | mytest2 | table | omm | {orientation=row,compression=no}
(2 rows)
omm@modb:~$ gsql -d mytestdb -U mytest -W mygstest@1234 -c "\dt"
List of relations
Schema | Name | Type | Owner | Storage
--------+---------+-------+-------+----------------------------------
public | mytest1 | table | omm | {orientation=row,compression=no}
public | mytest2 | table | omm | {orientation=row,compression=no}
(2 rows)
omm@modb:~$
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




