GaussDB 逻辑备份实操
1 逻辑备份定义
逻辑备份是指与业务有关的对象进行备份,这个对象包括表、表的数据、视图、索引、过程、函数等等。GaussDB支持逻辑备份的工具为gs_dump、gs_restore,以下举例说明。
2 创建举例数据
以下创建testdb库,创建test1模式,创建test表作为举例数据,并初始化数据:
gaussdb=> create database testdb encoding 'UTF8' dbcompatibility='A';
CREATE DATABASE
gaussdb=> \c testdb
Password for user root:
Non-SSL connection (SSL connection is recommended when requiring high-security)
You are now connected to database "testdb" as user "root".
testdb=> create schema test1;
CREATE SCHEMA
testdb=> create table test1.test(id int);
CREATE TABLE
testdb=> insert into test1.test values(1),(2),(3);
INSERT 0 3
testdb=>
csdn=> create schema test2;
CREATE SCHEMA
csdn=> create table test2.t2(id int);
CREATE TABLE
csdn=>
csdn=> insert into test2.t2 values(100),(200);
INSERT 0 2
3 gs_dump工具
- GaussDB提供服务器端工具gs_dump,在线导出数据库的数据,这些数据包括整个数据库或数据库 中指定的对象(如:模式、表、视图等),并且支持导出完整一致的数据。
- gs_dump可以创建四种不同的导出文件格式,通过[-F或者--format=]选项指定。
格式名称 | -F的参数值 | 说明 | 建议 | 对应导入工具 |
---|---|---|---|---|
纯文本格式 | p | 纯文本脚本文件包含SQL语句和命令。命令可以由gsql命令行终端程序执行,用于重新创建数据库对象并加载表数据。 | 小型数据库,一般推荐纯文本格式。 | 使用gsql工具恢复数据库对象前,可根据需要使用文本编辑器编辑纯文本导出文件。 |
自定义归档格式 | c | 一种二进制文件。支持从导出文件中恢复所有或所选的数据库对象。 | 中型或大型数据库,推荐自定义归档格式。 | 使用gs_restore可以选择要从自定义归档/目录归档/tar归档导出文件中导入相应的数据库对象。 |
目录归档格式 | d | 该格式会创建一个目录,该目录包含两类文件,一类是目录文件,另一类是每个表和blob对象对应的数据文件。 | - | |
tar归档格式 | t | tar归档文件支持从导出文件中恢复所有或所选的数据库对象。tar归档格式不支持压缩且对于单独表大小应小于8GB。 |
- gs_dump 常用的参数
-U:指定所连接主机的用户名。
-f:将输出发送至指定文件或目录。
-p:指定主机端口。
-F:选择输出格式。
-s:仅导出定义。
-a:仅备份数据。
-Z:指定使用的压缩比级别。
取值范围:0~9
针对自定义归档格式,该选项指定单个表数据片段的压缩,默认方式是以中等级别进行压缩。tar归档格式和纯文本格式目前不支持压缩。
0表示无压缩。
1表示压缩比最小,处理速度最快。
9表示压缩比最大,处理速度最慢。
-n:多指模式名,如果多个模式名的话,分别指定
-N:排除哪些模式不用备份
-t:指定表名称
-T:排除要导出的表名称
4 gs_dump使用举例
1)备份整个库
导出纯文本格式
gs_dump -h xxx.xx.0.71 -p 8000 -U root -W xxxx testdb -f /home/Ruby/gs_dump/testdb_bak0725.sql -F p
结果:
导出tar格式
gs_dump -h xxx.xxx.0.71 -p 8000 -U xxx -W xxxx testdb -f /home/Ruby/gs_dump/testdb_bak0725.tar -F t
结果:
导出自定义归档模式
gs_dump -h xxx.xxx.0.71 -p 8000 -U xxxx -W ****xx testdb -f /home/Ruby/gs_dump/testdb_bak0725.dmp -F c
结果:
仅备份定义
gs_dump -h xxx.xxx.0.71 -p 8000 -U root -W h****xxx testdb -f /home/Ruby/gs_dump/testdb_define_bak0725.sql -s -F -c
结果:
仅导出数据
gs_dump -h xxx.xxx.0.71 -p 8000 -U root -W h******@1Pwd testdb -f /home/Ruby/gs_dump/testdb_data_bak0725.dmp -a -F c
结果:
2) 备份schema
压缩导出schema:test1、test2
gs_dump -h xxx.xxx.0.71 -p 8000 -U root -W xxx**** testdb -n test1 -n test2 -Z 9 -f /home/Ruby/gs_dump/schema_bak.tar.gz -a -F c
结果:
压缩导出testdb但是排除test2模式
gs_dump -h xxx.xxx.0.71 -p 8000 -U root -W h***sxxx testdb -n test1 -N test2 -Z 9 -f /home/Ruby/gs_dump/schema_bak2.tar.gz -a -F c
结果:
3)备份表
导出表test,包含表定义与数据
gs_dump -h xxx.xxx.0.71 -p 8000 -U root -W h*****Pwd testdb -t test1.test -f /home/Ruby/gs_dump/test1_test1_tab.sql -F p
结果:
5 gs_resore工具
gs_restore是GaussDB提供的针对gs_dump导出数据的导入工具,通过此工具可将gs_dump 导出生成的文件进行导入。
1)主要功能:
- 导入到数据库:如果连接参数中指定了数据库,则数据将被导入到指定的数据库中。其中,并行导入必须指定连接的密码。导入时生成列会自动更新,并像普通列一样保存。
- 导入到脚本文件:如果未指定导入数据库,则创建包含重建数据库所必须的SQL语句脚本并写 入到文件或者标准输出。等效于直接使用gs_dump导出为纯文本格式。
2)常见参数:
-p:指定服务器所侦听的TCP端口或本地Unix域套接字后缀。
-d:指定连接数据库的dbname并直接导入到该数据库中。
6 gs_restore使用举例
1)恢复数据库
将testdb库的数据恢复到test_db1;
首先:创建test_db1库
[Ruby@dtest1 gs_dump]$ gsql -dpostgres -p8000 -Uroot -Wxxxx** -ar
gsql ((GaussDB Kernel 505.2.1 build 159cea95) compiled at 2024-12-27 09:22:44 commit 10161 last mr 21504 release)
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type "help" for help.gaussdb=> create database test_db1 encoding 'UTF8' dbcompatibility='A';
CREATE DATABASE
gaussdb=>
再次:执行恢复
gs_restore -h 192.168.0.71 -p 8000 -U root -W huawei@1Pwd /home/Ruby/gs_dump/testdb_bak0725.tar -d test_db1
执行结果:
验证数据:
将testdb库恢复到test_db1
2)恢复schema
步骤1:删除schema
[Ruby@dtest1 gs_dump]$ gsql -dpostgres -p8000 -Uroot -W*****1Pwd -ar
gsql ((GaussDB Kernel 505.2.1 build 159cea95) compiled at 2024-12-27 09:22:44 commit 10161 last mr 21504 release)
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type "help" for help.gaussdb=> \c testdb
Password for user root:
Non-SSL connection (SSL connection is recommended when requiring high-security)
You are now connected to database "testdb" as user "root".
testdb=>
testdb=>
testdb=>
testdb=> set search_path=test1;
SET
testdb=> \d+List of relationsSchema | Name | Type | Owner | Size | Storage | Description
--------+------+-------+-------+------------+------------------------------------------------------------------+-------------test1 | test | table | root | 8192 bytes | {orientation=row,compression=no,storage_type=ustore,segment=off} |
(1 row)testdb=> drop table test;
DROP TABLE
testdb=>
testdb=> drop schema test1;
DROP SCHEMA
testdb=>
testdb=>
testdb=>
步骤2:恢复schema
gs_restore -h xxx.xxx.0.71 -p 8000 -U root -W xxxxx -d testdb -F c /home/Ruby/gs_dump/testdb_bak0725.dmp
结果:
步骤3:验证数据:
原来test1模式下面的对象导入。
3)恢复表定义
步骤1:删除表test
步骤2:恢复表定义
gs_restore -h xxx.xxx.0.71 -p 8000 -U root -W h******Pwd -d testdb -n test1 -t test -e -c -s /home/Ruby/gs_dump/testdb_bak0725.dmp
执行结果:
步骤3:验证
原来表结构已导入
4)恢复表数据
步骤1:执行恢复命令
gs_restore -h xxx.xxx.0.71 -p 8000 -U root -W h******1Pwd -d testdb -n test1 -t test -e -a /home/Ruby/gs_dump/testdb_bak0725.dmp
步骤2:执行验证
原来表的3个记录已恢复
7 批注
GaussDB逻辑备份在一定的场景下非常的有用,如开发有SQL脚本与数据迁移到测试库,还有分布式环境下分布键重新定义的场景。