学习目标
掌握openGauss数据库的逻辑备份和恢复技术。
1.逻辑备份和恢复案例1:使用sql格式进行备份和恢复omm数据库
--为逻辑备份准备环境,创建逻辑备份的存储目录:
root@modb:~# su - omm
omm@modb:~$ mkdir /var/lib/opengauss/backup
--创建备份恢复用户,需要具有super或者sysadmin权限
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 'aabb@123' sysadmin;
NOTICE: The encrypted password contains MD5 ciphertext, which is not secure.
CREATE ROLE
--创建恢复测试数据库testdb
omm=# 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
omm=# \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)
--逻辑备份,使用gs_dump备份数据库,生成sql文件:
--使用test用户,备份数据库omm:
omm@modb:~$ gs_dump -U test omm -F p -f /var/lib/opengauss/backup/backup.sql
Password:
gs_dump[port='5432'][omm][2022-12-13 10:08:34]: The total objects number is 413.
gs_dump[port='5432'][omm][2022-12-13 10:08:34]: [100.00%] 413 objects have been dumped.
gs_dump[port='5432'][omm][2022-12-13 10:08:34]: dump database omm successfully
gs_dump[port='5432'][omm][2022-12-13 10:08:34]: total time: 7775 ms
--查看SQL备份的数据文件:
omm@modb:~$ cat /var/lib/opengauss/backup/backup.sql
--
-- openGauss database dump
--
SET statement_timeout = 0;
SET xmloption = content;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SET check_function_bodies = false;
SET client_min_messages = warning;
--
-- Name: test; Type: SCHEMA; Schema: -; Owner: test
--
CREATE SCHEMA test;
ALTER SCHEMA test OWNER TO test;
SET search_path = public;
SET default_tablespace = '';
SET default_with_oids = false;
--
-- Name: test1; Type: TABLE; Schema: public; Owner: omm; Tablespace:
--
CREATE TABLE test1 (
col integer
)
WITH (orientation=row, compression=no);
ALTER TABLE public.test1 OWNER TO omm;
--
-- Name: test2; Type: TABLE; Schema: public; Owner: omm; Tablespace:
--
CREATE TABLE test2 (
col integer
)
WITH (orientation=row, compression=no);
ALTER TABLE public.test2 OWNER TO omm;
--
-- Data for Name: test1; Type: TABLE DATA; Schema: public; Owner: omm
--
COPY test1 (col) FROM stdin;
\.
;
--
-- Data for Name: test2; Type: TABLE DATA; Schema: public; Owner: omm
--
COPY test2 (col) FROM stdin;
\.
;
--
-- Name: public; Type: ACL; Schema: -; Owner: omm
--
REVOKE ALL ON SCHEMA public FROM PUBLIC;
REVOKE ALL ON SCHEMA public FROM omm;
GRANT CREATE,USAGE ON SCHEMA public TO omm;
GRANT USAGE ON SCHEMA public TO PUBLIC;
--
-- openGauss database dump complete
--
--逻辑恢复:
--使用用户test,执行用gs_dump生成的sql脚本,将数据恢复到testdb数据库中:
omm@modb:~$ gsql -d testdb -U test -f /var/lib/opengauss/backup/backup.sql
Password for user test:
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: 19 ms
--验证数据库omm的备份已经被恢复到数据库testdb:
--源库(备份的数据库):
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)
--新库(恢复的数据库):
omm@modb:~$ gsql -d testdb -U test -c "\dt"
Password for user test:
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备份数据库,生成归档格式的备份文件
--创建测试数据
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 test3(col int);
CREATE TABLE
omm=# create table test4(col int);
CREATE TABLE
omm=# \q
--使用test用户,备份omm数据库,生成归档格式的备份文件:
omm@modb:~$ gs_dump -U test omm -F p -f /var/lib/opengauss/backup/backup.dump
Password:
gs_dump[port='5432'][omm][2022-12-13 10:11:08]: The total objects number is 417.
gs_dump[port='5432'][omm][2022-12-13 10:11:08]: [100.00%] 417 objects have been dumped.
gs_dump[port='5432'][omm][2022-12-13 10:11:08]: dump database omm successfully
gs_dump[port='5432'][omm][2022-12-13 10:11:08]: total time: 4218 ms
--使用gs_dump生成的归档文件恢复数据库
omm@modb:~$ gsql -d testdb -U test -f /var/lib/opengauss/backup/backup.dump
Password for user test:
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: 18 ms
--源库(备份的数据库):
omm@modb:~$ gsql -d omm -c "\dt"
(4 rows)
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}
--新库(恢复的数据库):
omm@modb:~$ gsql -d testdb -U test -c "\dt"
Password for user test:
(4 rows)
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}