学习目标
掌握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”
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”
课程作业
1.逻辑备份和恢复案例1:使用sql格式进行备份和恢复omm数据库
root@modb:~# su - omm
omm@modb:~$ mkdir /var/lib/opengauss/bachup
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 test identified by 'kunpeng@1234' sysadmin;
NOTICE: The encrypted password contains MD5 ciphertext, which is not secure.
CREATE ROLE
omm=# create tablespace test\_tbs relative location 'tablespace/test\_tbs1';
CREATE TABLESPACE
omm=# create database testdb with tablespace = test\_tbs ;
CREATE DATABASE
omm=# create table t1(col int);
CREATE TABLE
omm=# create table t2(col int);
CREATE TABLE
omm=# \\q
omm@modb:~$ gsql -d omm -c "\\dt"
List of relations
Schema | Name | Type | Owner | Storage
\--------+------+-------+-------+----------------------------------
public | t1 | table | omm | {orientation=row,compression=no}
public | t2 | table | omm | {orientation=row,compression=no}
(2 rows)
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 test identified by 'kunpeng@1234' sysadmin;
NOTICE: The encrypted password contains MD5 ciphertext, which is not secure.
CREATE ROLE
omm=# create tablespace test\_tbs relative location 'tablespace/test\_tbs1';
CREATE TABLESPACE
omm=# create database testdb with tablespace = test\_tbs ;
CREATE DATABASE
omm=# \\q
omm@modb:~$ gs\_dump -U test -W kunpeng@1234 omm -F p -f /var/lib/opengauss/bachup/backup.sql
gs\_dump\[port='5432'\]\[omm\]\[2022-12-14 10:40:59\]: The total objects number is 413.
gs\_dump\[port='5432'\]\[omm\]\[2022-12-14 10:40:59\]: \[100.00%\] 413 objects have been dumped.
gs\_dump\[port='5432'\]\[omm\]\[2022-12-14 10:40:59\]: dump database omm successfully
gs\_dump\[port='5432'\]\[omm\]\[2022-12-14 10:40:59\]: total time: 1483 ms
omm@modb:~$ gsql -d testdb -U test -W kunpeng@1234 -f /var/lib/opengauss/bachup/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: 23 ms
omm@modb:~$ gsql -d omm -c "\\dt"
List of relations
Schema | Name | Type | Owner | Storage
\--------+------+-------+-------+----------------------------------
public | t1 | table | omm | {orientation=row,compression=no}
public | t2 | table | omm | {orientation=row,compression=no}
(2 rows)
omm@modb:~$ gsql -d testdb -U test -W kunpeng@1234 -c "\\dt"
List of relations
Schema | Name | Type | Owner | Storage
\--------+------+-------+-------+----------------------------------
public | t1 | table | omm | {orientation=row,compression=no}
public | t2 | table | omm | {orientation=row,compression=no}
(2 rows)
2.逻辑备份和恢复案例2:使用dump格式进行备份和恢复omm数据库
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 table t3(col int);
CREATE TABLE
omm=# create table t4(col int);
omm=# CREATE TABLE
omm=# \\q
omm@modb:~$ gs\_dump -U test -W kunpeng@1234 omm -F p -f /var/lib/opengauss/bachup/backup.dump
gs\_dump\[port='5432'\]\[omm\]\[2022-12-14 12:08:31\]: The total objects number is 417.
gs\_dump\[port='5432'\]\[omm\]\[2022-12-14 12:08:31\]: \[100.00%\] 417 objects have been dumped.
gs\_dump\[port='5432'\]\[omm\]\[2022-12-14 12:08:31\]: dump database omm successfully
gs\_dump\[port='5432'\]\[omm\]\[2022-12-14 12:08:31\]: total time: 1683 ms
omm@modb:~$ gsql -d testdb -U test -W kunpeng@1234 -f /var/lib/opengauss/bachup/backup.dump
SET
SET
SET
SET
SET
SET
gsql:/var/lib/opengauss/bachup/backup.dump:16: ERROR: schema "test" already exists
ALTER SCHEMA
SET
SET
SET
gsql:/var/lib/opengauss/bachup/backup.dump:34: ERROR: relation "t1" already exists in schema "public"
DETAIL: creating new table with existing name in the same schema
ALTER TABLE
gsql:/var/lib/opengauss/bachup/backup.dump:46: ERROR: relation "t2" 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
total time: 20 ms
GRANT
omm@modb:~$ gsql -d omm -c "\\dt"
List of relations
Schema | Name | Type | Owner | Storage
\--------+------+-------+-------+----------------------------------
public | t1 | table | omm | {orientation=row,compression=no}
public | t2 | table | omm | {orientation=row,compression=no}
public | t3 | table | omm | {orientation=row,compression=no}
public | t4 | table | omm | {orientation=row,compression=no}
(4 rows)
omm@modb:~$ gsql -d testdb -U test -W kunpeng@1234 -c "\\dt"
List of relations
Schema | Name | Type | Owner | Storage
\--------+------+-------+-------+----------------------------------
public | t1 | table | omm | {orientation=row,compression=no}
public | t2 | table | omm | {orientation=row,compression=no}
public | t3 | table | omm | {orientation=row,compression=no}
public | t4 | table | omm | {orientation=row,compression=no}
(4 rows)




