为逻辑备份准备环境
--创建逻辑备份的存储目录:
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"
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数据库
逻辑备份,使用gs_dump备份数据库,生成sql文件:
--使用test用户,备份数据库omm:
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 10:32:46]: The total objects number is 413.p/backup.sqlgs_dump[port='5432'][omm][2022-12-13 10:32:46]: [100.00%] 413 objects have been dumped.
gs_dump[port='5432'][omm][2022-12-13 10:32:46]: dump database omm successfully
gs_dump[port='5432'][omm][2022-12-13 10:32:46]: total time: 1671 ms
逻辑恢复:
--使用用户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"
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)
--新库(恢复的数据库):
gsql -d testdb -U test -W huawei@1234 -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)
2.逻辑备份和恢复案例2:使用dump格式进行备份和恢复omm数据库
逻辑备份:使用gs_dump备份数据库,生成归档格式的备份文件
--创建测试数据
gsql -r
create table test3(col int);
create table test4(col int);
\q
--删除testdb下test1和test2gsql -d testdb -U test -W huawei@1234 -c 'drop table test1'
gsql -d testdb -U test -W huawei@1234 -c 'drop table test2'--使用test用户,备份omm数据库,生成归档格式的备份文件:
gs_dump -U test -W huawei@1234 omm -F c -f /var/lib/opengauss/backup/backup.dump
逻辑恢复:gs_restore /var/lib/opengauss/backup/backup.dump -d testdb -U test -Whuawei@1234
恢复验证:
--源库(备份的数据库):
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}
public | test3 | table | omm | {orientation=row,compression=no}
public | test4 | table | omm | {orientation=row,compression=no}
(4 rows)
--新库(恢复的数据库):
gsql -d testdb -U test -W huawei@1234 -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}
public | test3 | table | omm | {orientation=row,compression=no}
public | test4 | table | omm | {orientation=row,compression=no}
(4 rows)




