openGauss每日一练第19天
今日目标:
openGauss数据库的逻辑备份和恢复技术。
gs_dump支持将数据库信息导出至纯文本格式的SQL脚本文件或其他归档文件中。
- 纯文本格式的SQL脚本文件:包含将数据库恢复为其保存时的状态所需的SQL语句。通过gsql运行该SQL脚本文件,可以恢复数据库。即使在其他主机和其他数据库产品上,只要对SQL脚本文件稍作修改,也可以用来重建数据库。
- 归档格式文件:包含将数据库恢复为其保存时的状态所需的数据,可以是tar格式、目录归档格式或自定义归档格式,详见下表。该导出结果必须与gs_restore配合使用来恢复数据库,gs_restore工具在导入时,系统允许用户选择需要导入的内容,甚至可以在导入之前对等待导入的内容进行排序。
| 格式名称 | -F的参数值 | 说明 | 建议 | 对应导入工具 |
|---|---|---|---|---|
| 纯文本格式 | p | 纯文本脚本文件包含SQL语句和命令。命令可以由gsql命令行终端程序执行,用于重新创建数据库对象并加载表数据。 | 小型数据库,一般推荐纯文本格式。 | 使用gsql工具恢复数据库对象前,可根据需要使用文本编辑器编辑纯文本导出文件。 |
| 自定义归档格式 | c | 一种二进制文件。支持从导出文件中恢复所有或所选数据库对象。 | 中型或大型数据库,推荐自定义归档格式。 | 使用gs_restore可以选择要从自定义归档导出文件中导入相应的数据库对象。 |
| 目录归档格式 | d | 该格式会创建一个目录,该目录包含两类文件,一类是目录文件,另一类是每个表和blob对象对应的数据文件。 | - | |
| tar归档格式 | t | tar归档文件支持从导出文件中恢复所有或所选数据库对象。tar归档格式不支持压缩且对于单独表大小应小于8GB。 | - |
2.课后作业
环境准备
-- 创建备份恢复用户,需具有super或者sysadmin权限
omm=# create user backupuser identified by 'gauss@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
-- 创建database
omm=# create database testdb with tablespace = test_tbs ;
CREATE DATABASE
omm=# \du
List of roles
Role name | Attributes
| Member of
omm | Sysadmin, Create role, Create DB, Replication, Administer audit, Monitoradmin, Operatoradmin, Policyadmin, UseFT
| {}
------------+-----------------------------------------------------------------------------------------------------------------
-+-----------
backupuser | Sysadmin
| {}
gaussdb | Sysadmin
| {}
omm=# \db
List of tablespaces
Name | Owner | Location
------------+-------+----------------------
pg_default | omm |
pg_global | omm |
test_tbs | omm | tablespace/test_tbs1
(3 rows)
omm=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+-------+----------+---------+-------+-------------------
omm | omm | UTF8 | C | C |
postgres | omm | UTF8 | C | C |
template0 | omm | UTF8 | C | C | =c/omm +
| | | | | omm=CTc/omm
template1 | omm | UTF8 | C | C | =c/omm +
| | | | | omm=CTc/omm
testdb | omm | UTF8 | C | C |
(5 rows)
-- 创建表
omm=# create table test1 (col int);
CREATE TABLE
omm=# create table test2 (col int);
CREATE TABLE
omm=# \dt
List of relations
Schema | Name | Type | Owner | Storage
omm=# --------+-------+-------+-------+----------------------------------
public | test1 | table | omm | {orientation=row,compression=no}
public | test2 | table | omm | {orientation=row,compression=no}
(2 rows)
2.1 逻辑备份和恢复案例1:使用sql格式进行备份和恢复omm数据库
omm@modb:/$ mkdir -p /var/lib/opengauss/data/backuptest
omm@modb:/$ ls /var/lib/opengauss/data/
PG_VERSION gs_gazelle.conf pg_csnlog pg_location pg_replslot pg_xlog sql_monitor
asp_data gs_profile pg_ctl.lock pg_log pg_serial postgresql.conf undo
backuptest gswlm_userinfo.cfg pg_errorinfo pg_logical pg_snapshots postgresql.conf.lock
base mot.conf pg_hba.conf pg_multixact pg_stat_tmp postmaster.opts
gaussdb.state pg_audit pg_ident.conf pg_notify pg_tblspc postmaster.pid
global pg_clog pg_llog pg_perf pg_twophase postmaster.pid.lock
omm@modb:/$ gs_dump -U backupuser -W gauss
connection to database "" failed: FATAL: Invalid username/password,login denied.
-- 备份
omm@modb:/$ gs_dump -U backupuser -W gauss@1234 omm -F p -f /var/lib/opengauss/data/backuptest/backup.sql
gs_dump[port='5432'][omm][2022-12-14 20:07:40]: The total objects number is 413.
gs_dump[port='5432'][omm][2022-12-14 20:07:40]: [100.00%] 413 objects have been dumped.
gs_dump[port='5432'][omm][2022-12-14 20:07:40]: dump database omm successfully
gs_dump[port='5432'][omm][2022-12-14 20:07:40]: total time: 1480 ms
omm@modb:/$ ls /var/lib/opengauss/data/backuptest/
backup.sql
-- 恢复
omm@modb:/$ gsql -d testdb -U backupuser -W gauss@1234 -f /var/lib/opengauss/data/backuptest/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: 18 ms
-- 恢复验证
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 backupuser -W gauss@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)
omm@modb:/$
2.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 test3(col int);
CREATE TABLE
omm=# create table test4(col int);
CREATE TABLE
omm=# \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)
omm=# \q
-- 备份
omm@modb:/$ gs_dump -U backupuser -W gauss@1234 omm -F p -f /var/lib/opengauss/data/backuptest/backup.dump
gs_dump[port='5432'][omm][2022-12-14 20:14:47]: The total objects number is 417.
gs_dump[port='5432'][omm][2022-12-14 20:14:47]: [100.00%] 417 objects have been dumped.
gs_dump[port='5432'][omm][2022-12-14 20:14:47]: dump database omm successfully
gs_dump[port='5432'][omm][2022-12-14 20:14:47]: total time: 1495 ms
omm@modb:/$ ls /var/lib/opengauss/data/backuptest/
backup.dump backup.sql
-- 恢复
omm@modb:/$ gsql -d testdb -U backupuser -W gauss@1234 -f /var/lib/opengauss/data/backuptest/backup.dump
SET
SET
SET
SET
SET
SET
gsql:/var/lib/opengauss/data/backuptest/backup.dump:16: ERROR: schema "backupuser" already exists
ALTER SCHEMA
SET
SET
SET
gsql:/var/lib/opengauss/data/backuptest/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/data/backuptest/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: 21 ms
-- 恢复验证
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}
public | test3 | table | omm | {orientation=row,compression=no}
public | test4 | table | omm | {orientation=row,compression=no}
(4 rows)
omm@modb:/$
omm@modb:/$ gsql -d testdb -U backupuser -W gauss@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)
omm@modb:/$
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




