发文章是为了证明自己真的掌握了一个知识,同时给他人带来帮助,如有问题,欢迎指正,祝大家万事胜意!
目录
前言
openGauss数据库备份恢复
1 实验介绍
1.1 关于本实验
1.2 实验目的
2 实验前提
3 物理备份和恢复
3.1 实验前提
3.2 物理备份
3.3 物理备份恢复
4 逻辑备份和恢复
4.1 实验前提
4.2 gs_dump
4.2.1 介绍
4.2.2 注意事项
4.2.4 gs_dump 备份示例 2
4.2.5 gs_dump 备份示例 3
4.2.6 gs_dump 备份示例 4
4.2.6 gs_dump 备份示例 4
4.3 gs_dumpall
4.3.1 介绍
4.3.2 操作步骤
4.4 gs_restore
4.4.1 介绍
4.4.2 gs_restore 导入示例 1
4.4.3 gs_restore 导入示例 2
4.4.4 gs_restore 导入示例 3
4.4.5 gs_restore 导入示例 4
4.4.6 gs_restore 导入示例 5
前言
我的环境:
设备名称 | 设备型号 | 软件版本 |
虚拟机 | VMware | VMware-workstation-full-17.5.1 |
操作系统 | openEuler | openEuler 22.3LTS |
数据库 | openGauss | openGauss 5.0.0 |
需要的工具,大家不用现在下,后面用到了再下也可以,如果需要相关文件,可以评论,其实大多数都是可以去官网下的哈,因为我只能通过网盘给大家,文件又有点大,网盘的速度大家都是清楚的哈哈,所以还是推荐大家去官网,如果实在找不到可以找我
openGauss数据库备份恢复
1 实验介绍
1.1 关于本实验
1.2 实验目的
2 实验前提
[root@node0 ~]# su - omm
Last login: Mon Apr 8 19:47:35 CST 2024 on tty1Welcome to 5.10.0-153.12.0.92.oe2203sp2.x86_64System information as of time: 2024年 04月 08日 星期一 20:18:18 CSTSystem load: 0.64
Processes: 204
Memory used: 42.8%
Swap used: 13.5%
Usage On: 25%
IP address: 192.168.28.131
Users online: 2
To run a command as administrator(user "root"),use "sudo <command>".
[omm@node0 ~]$ gsql -d postgres -p 15400 -r
gsql ((openGauss 5.0.0 build a07d57c3) compiled at 2023-03-29 03:37:13 commit 0 last mr )
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type "help" for help.
openGauss=# DROP TABLE IF EXISTS customer_t1;
DROP TABLE
openGauss=# CREATE TABLE customer_t1
openGauss-# (
openGauss(# c_customer_sk integer,
openGauss(# c_customer_id char(5),
openGauss(# c_first_name char(6),
openGauss(# c_last_name char(8)
openGauss(# );
CREATE TABLE
openGauss=# INSERT INTO customer_t1 (c_customer_sk, c_customer_id, c_first_name) VALUES
openGauss-# (3769, 'hello', DEFAULT) ,
openGauss-# (6885, 'maps', 'Joes'),
openGauss-# (4321, 'tpcds', 'Lily'),
openGauss-# (9527, 'world', 'James');
INSERT 0 4
openGauss=# select * from customer_t1;c_customer_sk | c_customer_id | c_first_name | c_last_name
---------------+---------------+--------------+-------------3769 | hello | | 6885 | maps | Joes | 4321 | tpcds | Lily | 9527 | world | James |
(4 rows)
openGauss=# DROP TABLE IF EXISTS customer_t2;
NOTICE: table "customer_t2" does not exist, skipping
DROP TABLE
openGauss=# CREATE TABLE customer_t2
openGauss-# (
openGauss(# c_customer_sk integer,
openGauss(# c_customer_id char(5),
openGauss(# c_first_name char(6),
openGauss(# c_last_name char(8)
openGauss(# );
CREATE TABLE
openGauss=# INSERT INTO customer_t2 (c_customer_sk, c_customer_id, c_first_name) VALUES
openGauss-# (3769, 'hello', DEFAULT) ,
openGauss-# (6885, 'maps', 'Joes'),
openGauss-# (9527, 'world', 'James');
INSERT 0 3
openGauss=# select * from customer_t2;c_customer_sk | c_customer_id | c_first_name | c_last_name
---------------+---------------+--------------+-------------3769 | hello | | 6885 | maps | Joes | 9527 | world | James |
(3 rows)
openGauss=# DROP user IF EXISTS LUCY;
NOTICE: role "lucy" does not exist, skipping
DROP ROLE
openGauss=# CREATE USER lucy WITH PASSWORD "Bigdata@123";
CREATE ROLE
openGauss=# \c - lucy
Password for user lucy:
Non-SSL connection (SSL connection is recommended when requiring high-security)
You are now connected to database "postgres" as user "lucy".
openGauss=> DROP TABLE IF EXISTS lucy.mytable;
NOTICE: table "mytable" does not exist, skipping
DROP TABLE
openGauss=> CREATE TABLE mytable (firstcol int);
CREATE TABLE
openGauss=> INSERT INTO mytable values (100);
INSERT 0 1
openGauss=> SELECT * from mytable;firstcol
----------100
(1 row)
openGauss=> \q
[omm@node0 ~]$
3 物理备份和恢复
3.1 实验前提
[root@node0 ~]# su - omm
Last login: Sat Apr 13 23:05:21 CST 2024 on tty1Welcome to 5.10.0-153.12.0.92.oe2203sp2.x86_64System information as of time: 2024年 04月 13日 星期六 23:14:02 CSTSystem load: 0.01
Processes: 198
Memory used: 37.9%
Swap used: 9.5%
Usage On: 25%
IP address: 192.168.28.131
Users online: 2
To run a command as administrator(user "root"),use "sudo <command>".
[omm@node0 ~]$
[omm@node0 ~]$ mkdir -p /home/omm/physical/backup
[omm@node0 ~]$
3.2 物理备份
[omm@node0 ~]$ gs_om -t start;
Starting cluster.
=========================================
[SUCCESS] node0:
[2024-04-13 23:16:40.080][8226][][gs_ctl]: gs_ctl started,datadir is /opt/huawei/install/data/dn
[2024-04-13 23:16:40.086][8226][][gs_ctl]: another server might be running; Please use the restart command
=========================================
Successfully started.
[omm@node0 ~]$ gs_basebackup -D /home/omm/physical/backup -p 15400
INFO: The starting position of the xlog copy of the full build is: 0/3000028. The slot minimum LSN is: 0/0. The disaster slot minimum LSN is: 0/0. The logical slot minimum LSN is: 0/0.
[2024-04-13 23:18:08]:begin build tablespace list
[2024-04-13 23:18:08]:finish build tablespace list
[2024-04-13 23:18:08]:begin get xlog by xlogstream[2024-04-13 23:18:08]: check identify system success[2024-04-13 23:18:08]: send START_REPLICATION 0/3000000 success[2024-04-13 23:18:08]: keepalive message is received[2024-04-13 23:18:08]: keepalive message is received
[2024-04-13 23:18:13]:gs_basebackup: base backup successfully
[omm@node0 ~]$ cd /home/omm/physical/backup
[omm@node0 backup]$ ls
backup_label pg_ident.conf pg_xlog
base pg_llog postgresql.conf
cacert.pem pg_location postgresql.conf.bak
global pg_logical postgresql.conf.guc.bak
gswlm_userinfo.cfg pg_multixact postgresql.conf.lock
mot.conf pg_notify postmaster.pid.lock
pg_clog pg_replslot server.crt
pg_csnlog pg_serial server.key
pg_ctl.lock pg_snapshots server.key.cipher
pg_errorinfo pg_stat_tmp server.key.rand
pg_hba.conf pg_tblspc undo
pg_hba.conf.bak pg_twophase
pg_hba.conf.lock PG_VERSION
3.3 物理备份恢复
[omm@node0 backup]$ gs_om -t stop;
Stopping cluster.
=========================================
Successfully stopped cluster.
=========================================
End stop cluster.
[omm@node0 backup]$
[omm@node0 ~]$ cd /opt/huawei/install/data
[omm@node0 data]$ ls
dn
[omm@node0 data]$ cd dn
[omm@node0 dn]$ ls
backup_label.old pg_ident.conf postgresql.conf
base pg_llog postgresql.conf.bak
cacert.pem pg_location postgresql.conf.guc.bak
gaussdb.state pg_logical postgresql.conf.lock
global pg_multixact postmaster.opts
gswlm_userinfo.cfg pg_notify postmaster.pid
mot.conf pg_replslot postmaster.pid.lock
pg_clog pg_serial server.crt
pg_csnlog pg_snapshots server.key
pg_ctl.lock pg_stat_tmp server.key.cipher
pg_errorinfo pg_tblspc server.key.rand
pg_hba.conf pg_twophase undo
pg_hba.conf.bak PG_VERSION
pg_hba.conf.lock pg_xlog
[omm@node0 dn]$ rm -rf *
[omm@node0 dn]$ ls
[omm@node0 dn]$
[omm@node0 dn]$ rm -rf *
[omm@node0 dn]$ ls
[omm@node0 dn]$
[omm@node0 dn]$ cp -r /home/omm/physical/backup/. /opt/huawei/install/data/dn
[omm@node0 dn]$
[omm@node0 dn]$ cd /opt/huawei/install/data/dn
[omm@node0 dn]$ ls
backup_label pg_ident.conf pg_xlog
base pg_llog postgresql.conf
cacert.pem pg_location postgresql.conf.bak
global pg_logical postgresql.conf.guc.bak
gswlm_userinfo.cfg pg_multixact postgresql.conf.lock
mot.conf pg_notify postmaster.pid.lock
pg_clog pg_replslot server.crt
pg_csnlog pg_serial server.key
pg_ctl.lock pg_snapshots server.key.cipher
pg_errorinfo pg_stat_tmp server.key.rand
pg_hba.conf pg_tblspc undo
pg_hba.conf.bak pg_twophase
pg_hba.conf.lock PG_VERSION
[omm@node0 dn]$ gs_om -t start;
Starting cluster.
==================================================================================
Successfully started.
4 逻辑备份和恢复
通过逻辑导出对数据进行备份,逻辑备份只能基于备份时刻进行数据转储,所以恢复时也只能恢复到备份时保存的数据。对于故障点和备份点之间的数据,逻辑备份无能为力,逻辑备份适合备份那些很少变化的数据,当这些数据因误操作被损坏时,可以通过逻辑备份进行快速恢复。如果通过逻辑备份进行全库恢复,通常需要重建数据库,导入备份数据来完成,对于可用性要求很高的数据库,这种恢复时间太长,通常不被采用。由于逻辑备份具有平台无关性,所以更为常见的是逻辑备份被作为一个数据迁移及移动的主要手段。
4.1 实验前提
在数据库备份之前创建存储备份文件的文件夹。
步骤 1 切换到 omm 用户,以操作系统用户 omm 登录数据库主节点。
[root@node0 ~]# su - omm
Last login: Mon Apr 15 16:46:11 CST 2024 on pts/1Welcome to 5.10.0-153.12.0.92.oe2203sp2.x86_64System information as of time: 2024年 04月 15日 星期一 17:16:22 CSTSystem load: 0.06
Processes: 199
Memory used: 38.4%
Swap used: 10.1%
Usage On: 26%
IP address: 192.168.28.131
Users online: 2
To run a command as administrator(user "root"),use "sudo <command>".
[omm@node0 ~]$ mkdir -p /home/omm/logical/backup
[omm@node0 ~]$
4.2 gs_dump
4.2.1 介绍
gs_dump 是 openGauss 用于导出数据库相关信息的工具,用户可以自定义导出一个数据库或其中的对象(模式、表、视图等)。支持导出的数据库可以是默认数据库 postgres,也可以是自定义数据库。
主要功能:
gs_dump 可以创建四种不同的导出文件格式,通过[-F 或者--format=]选项指定,具体见官方文档
4.2.2 注意事项
如果 openGauss 有任何本地数据要添加到 template1 数据库,请将 gs_dump 的输出恢复
到一个真正的空数据库中,否则可能会因为被添加对象的定义被复制,出现错误。要创建
一个无本地添加的空数据库,需从 template0 而非 template1 复制,例如:
openGauss=# CREATE DATABASE foo WITH TEMPLATE template0;
CREATE DATABASE
4.2.3 gs_dump 备份示例 1
执行 gs_dump,导出 postgres 数据库全量信息,导出的 MPPDB_backup.sql 文件格式为纯文本格式。
步骤 1 以操作系统用户 omm 登录数据库主节点。
[root@node0 ~]# su - omm
Last login: Mon Apr 15 17:16:22 CST 2024 on pts/1Welcome to 5.10.0-153.12.0.92.oe2203sp2.x86_64System information as of time: 2024年 04月 15日 星期一 17:32:59 CSTSystem load: 0.02
Processes: 196
Memory used: 39.3%
Swap used: 11.6%
Usage On: 26%
IP address: 192.168.28.131
Users online: 2
[omm@node0 ~]$ gs_dump -U omm -W Bigdata@123 -f /home/omm/logical/backup/MPPDB_backup.sql -p 15400 postgres -F p
gs_dump[port='15400'][postgres][2024-04-15 17:34:35]: The total objects number is 516.
gs_dump[port='15400'][postgres][2024-04-15 17:34:35]: [100.00%] 516 objects have been dumped.
gs_dump[port='15400'][postgres][2024-04-15 17:34:35]: dump database postgres successfully
gs_dump[port='15400'][postgres][2024-04-15 17:34:35]: total time: 2624 ms
[omm@node0 ~]$ ll /home/omm/logical/backup/
total 772
-rw------- 1 omm dbgrp 127566 Apr 15 09:48 bkp2.sql
-rw------- 1 omm dbgrp 972 Apr 15 09:35 bkp_shl2.sql
drwx------ 2 omm dbgrp 4096 Apr 15 09:34 MPPDB_backup
-rw------- 1 omm dbgrp 122523 Apr 15 09:45 MPPDB_backup2.sql
-rw------- 1 omm dbgrp 121108 Apr 15 09:33 MPPDB_backup.dmp
-rw------- 1 omm dbgrp 123098 Apr 15 17:34 MPPDB_backup.sql
-rw------- 1 omm dbgrp 277504 Apr 15 09:32 MPPDB_backup.tar
[omm@node0 ~]$ cat /home/omm/logical/backup/MPPDB_backup.sql
……
--
-- Name: inx_stu01; Type: INDEX; Schema: public; Owner: omm; Tablespace:
--CREATE INDEX inx_stu01 ON student USING btree (std_name) TABLESPACE pg_default;--
-- 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
--
4.2.4 gs_dump 备份示例 2
执行 gs_dump,导出 postgres 数据库全量信息,导出的 MPPDB_backup.tar 文件格式为 tar格式。
步骤 1 以操作系统用户 omm 登录数据库主节点
[root@node0 ~]# su - omm
Last login: Mon Apr 15 17:32:59 CST 2024 on pts/1Welcome to 5.10.0-153.12.0.92.oe2203sp2.x86_64System information as of time: 2024年 04月 15日 星期一 17:41:09 CSTSystem load: 0.00
Processes: 197
Memory used: 39.4%
Swap used: 12.1%
Usage On: 26%
IP address: 192.168.28.131
Users online: 2
To run a command as administrator(user "root"),use "sudo <command>".
[omm@node0 ~]$
步骤 2 执行 gs_dump,导出的 MPPDB_backup.tar 文件格式为 tar 格式。
[omm@node0 ~]$ gs_dump -U omm -W Bigdata@123 -f /home/omm/logical/backup/MPPDB_backup.tar -p 15400 postgres -F t
gs_dump[port='15400'][postgres][2024-04-15 17:43:14]: The total objects number is 516.
gs_dump[port='15400'][postgres][2024-04-15 17:43:14]: [100.00%] 516 objects have been dumped.
gs_dump[port='15400'][postgres][2024-04-15 17:43:14]: dump database postgres successfully
gs_dump[port='15400'][postgres][2024-04-15 17:43:14]: total time: 1422 ms
[omm@node0 ~]$
步骤 3 查看生成的文件信息。
[omm@node0 ~]$ ll /home/omm/logical/backup/
total 772
-rw------- 1 omm dbgrp 127566 Apr 15 09:48 bkp2.sql
-rw------- 1 omm dbgrp 972 Apr 15 09:35 bkp_shl2.sql
drwx------ 2 omm dbgrp 4096 Apr 15 09:34 MPPDB_backup
-rw------- 1 omm dbgrp 122523 Apr 15 09:45 MPPDB_backup2.sql
-rw------- 1 omm dbgrp 121108 Apr 15 09:33 MPPDB_backup.dmp
-rw------- 1 omm dbgrp 123098 Apr 15 17:34 MPPDB_backup.sql
-rw------- 1 omm dbgrp 277504 Apr 15 17:43 MPPDB_backup.tar
4.2.5 gs_dump 备份示例 3
执行 gs_dump,导出 postgres 数据库全量信息,导出的 MPPDB_backup.dmp 文件格式为自定义归档格式。
步骤 1 以操作系统用户 omm 登录数据库主节点。
[root@node0 ~]# su - omm
Last login: Mon Apr 15 17:41:09 CST 2024 on pts/1Welcome to 5.10.0-153.12.0.92.oe2203sp2.x86_64System information as of time: 2024年 04月 15日 星期一 17:45:32 CSTSystem load: 0.93
Processes: 199
Memory used: 39.6%
Swap used: 12.3%
Usage On: 26%
IP address: 192.168.28.131
Users online: 2
To run a command as administrator(user "root"),use "sudo <command>".
步骤 2 执行 gs_dump,导出的 MPPDB_backup.dmp 文件格式为自定义归档格式。
[omm@node0 ~]$ gs_dump -U omm -W Bigdata@123 -f /home/omm/logical/backup/MPPDB_backup.dmp -p 15400 postgres -F c
gs_dump[port='15400'][postgres][2024-04-15 17:47:39]: The total objects number is 516.
gs_dump[port='15400'][postgres][2024-04-15 17:47:39]: [100.00%] 516 objects have been dumped.
gs_dump[port='15400'][postgres][2024-04-15 17:47:39]: dump database postgres successfully
gs_dump[port='15400'][postgres][2024-04-15 17:47:39]: total time: 1420 ms
步骤 3 查看生成的文件信息。
[omm@node0 ~]$ ll /home/omm/logical/backup/
total 772
-rw------- 1 omm dbgrp 127566 Apr 15 09:48 bkp2.sql
-rw------- 1 omm dbgrp 972 Apr 15 09:35 bkp_shl2.sql
drwx------ 2 omm dbgrp 4096 Apr 15 17:49 MPPDB_backup
-rw------- 1 omm dbgrp 122523 Apr 15 09:45 MPPDB_backup2.sql
-rw------- 1 omm dbgrp 121108 Apr 15 17:47 MPPDB_backup.dmp
-rw------- 1 omm dbgrp 123098 Apr 15 17:34 MPPDB_backup.sql
-rw------- 1 omm dbgrp 277504 Apr 15 17:43 MPPDB_backup.tar
4.2.6 gs_dump 备份示例 4
执行 gs_dump,导出 postgres 数据库全量信息,导出的 MPPDB_backup 文件格式为目录格式。
步骤 1 以操作系统用户 omm 登录数据库主节点。
[root@node0 ~]# su - omm
Last login: Mon Apr 15 17:45:31 CST 2024 on pts/1Welcome to 5.10.0-153.12.0.92.oe2203sp2.x86_64System information as of time: 2024年 04月 15日 星期一 20:02:07 CSTSystem load: 0.10
Processes: 196
Memory used: 40.0%
Swap used: 14.0%
Usage On: 26%
IP address: 192.168.28.131
Users online: 2
To run a command as administrator(user "root"),use "sudo <command>".
步骤 2 执行 gs_dump,导出的 MPPDB_backup 文件格式为目录格式
[omm@node0 ~]$ gs_dump -U omm -W Bigdata@123 -f /home/omm/logical/backup/MPPDB_backup -p 15400 postgres -F d
gs_dump[port='15400'][postgres][2024-04-15 09:33:53]: The total objects number is 516.
gs_dump[port='15400'][postgres][2024-04-15 09:33:54]: [100.00%] 516 objects have been dumped.
gs_dump[port='15400'][postgres][2024-04-15 09:33:54]: dump database postgres successfully
gs_dump[port='15400'][postgres][2024-04-15 09:33:54]: total time: 2341 ms
步骤 3 查看生成的文件信息。
[omm@node0 ~]$ ll /home/omm/logical/backup/
total 772
-rw------- 1 omm dbgrp 127566 Apr 15 09:48 bkp2.sql
-rw------- 1 omm dbgrp 972 Apr 15 09:35 bkp_shl2.sql
drwx------ 2 omm dbgrp 4096 Apr 15 17:49 MPPDB_backup
-rw------- 1 omm dbgrp 122523 Apr 15 09:45 MPPDB_backup2.sql
-rw------- 1 omm dbgrp 121108 Apr 15 17:47 MPPDB_backup.dmp
-rw------- 1 omm dbgrp 123098 Apr 15 17:34 MPPDB_backup.sql
-rw------- 1 omm dbgrp 277504 Apr 15 17:43 MPPDB_backup.tar
4.2.6 gs_dump 备份示例 4
执行 gs_dump,导出 postgres 数据库全量信息,导出的 MPPDB_backup 文件格式为目录格式。
步骤 1 以操作系统用户 omm 登录数据库主节点。
[root@node0 ~]# su - omm
Last login: Mon Apr 15 20:02:07 CST 2024 on pts/1Welcome to 5.10.0-153.12.0.92.oe2203sp2.x86_64System information as of time: 2024年 04月 15日 星期一 20:10:39 CSTSystem load: 0.15
Processes: 197
Memory used: 40.0%
Swap used: 14.0%
Usage On: 26%
IP address: 192.168.28.131
Users online: 2
To run a command as administrator(user "root"),use "sudo <command>".
[omm@node0 ~]$
步骤 2 执行 gs_dump,导出的 MPPDB_backup 文件格式为目录格式
[omm@node0 ~]$ gs_dump -U omm -W Bigdata@123 -f /home/omm/logical/backup/MPPDB_backup -p 15400 postgres -F d
gs_dump[port='15400'][postgres][2024-04-15 09:34:57]: The total objects number is 516.
gs_dump[port='15400'][postgres][2024-04-15 09:34:57]: [100.00%] 516 objects have been dumped.
gs_dump[port='15400'][postgres][2024-04-15 09:34:57]: dump database postgres successfully
gs_dump[port='15400'][postgres][2024-04-15 09:34:57]: total time: 1646 ms
步骤 3 查看生成的文件信息。
[omm@node0 ~]$ ll /home/omm/logical/backup/
total 772
-rw------- 1 omm dbgrp 127566 Apr 15 09:48 bkp2.sql
-rw------- 1 omm dbgrp 972 Apr 15 09:35 bkp_shl2.sql
drwx------ 2 omm dbgrp 4096 Apr 15 17:49 MPPDB_backup
-rw------- 1 omm dbgrp 122523 Apr 15 09:45 MPPDB_backup2.sql
-rw------- 1 omm dbgrp 121108 Apr 15 17:47 MPPDB_backup.dmp
-rw------- 1 omm dbgrp 123098 Apr 15 17:34 MPPDB_backup.sql
-rw------- 1 omm dbgrp 277504 Apr 15 17:43 MPPDB_backup.tar
[root@node0 ~]# su - omm
Last login: Mon Apr 15 20:10:39 CST 2024 on pts/1Welcome to 5.10.0-153.12.0.92.oe2203sp2.x86_64System information as of time: 2024年 04月 15日 星期一 20:16:26 CSTSystem load: 0.14
Processes: 196
Memory used: 40.0%
Swap used: 14.0%
Usage On: 26%
IP address: 192.168.28.131
Users online: 2
To run a command as administrator(user "root"),use "sudo <command>".
[omm@node0 ~]$
[omm@node0 ~]$ gs_dump -U omm -W Bigdata@123 -f /home/omm/logical/backup/bkp_shl2.sql-t public.customer_t1 -p 15400
gs_dump[port='15400'][postgres][2024-04-15 09:35:44]: The total objects number is 421.
gs_dump[port='15400'][postgres][2024-04-15 09:35:44]: [100.00%] 421 objects have been dumped.
gs_dump[port='15400'][postgres][2024-04-15 09:35:44]: dump database postgres successfully
gs_dump[port='15400'][postgres][2024-04-15 09:35:44]: total time: 1161 ms
[omm@node0 ~]$ ll /home/omm/logical/backup/
total 524
-rw------- 1 omm dbgrp 972 Apr 15 09:35 bkp_shl2.sql
drwx------ 2 omm dbgrp 4096 Apr 15 09:34 MPPDB_backup
-rw------- 1 omm dbgrp 121108 Apr 15 09:33 MPPDB_backup.dmp
-rw------- 1 omm dbgrp 123098 Apr 15 09:24 MPPDB_backup.sql
-rw------- 1 omm dbgrp 277504 Apr 15 09:32 MPPDB_backup.tar
[omm@node0 ~]$ cat /home/omm/logical/backup/bkp_shl2.sql
……
CREATE TABLE customer_t1 (c_customer_sk integer,c_customer_id character(5),c_first_name character(6),c_last_name character(8)
)
WITH (orientation=row, compression=no);ALTER TABLE public.customer_t1 OWNER TO omm;--
-- Data for Name: customer_t1; Type: TABLE DATA; Schema: public; Owner: omm
--COPY customer_t1 (c_customer_sk, c_customer_id, c_first_name, c_last_name) FROM stdin;
3769 hello \N \N
6885 maps Joes \N
4321 tpcds Lily \N
9527 world James \N
\.
;--
-- openGauss database dump complete
--
4.2.8 gs_dump 备份示例 6
执行 gs_dump,导出 postgres 数据库信息,但不导出/home/MPPDB_temp.sql 中指定的表信息。导出的 MPPDB_backup.sql 文件格式为纯文本格式。
步骤 1 以操作系统用户 omm 登录数据库主节点
[root@node0 ~]# su - omm
Last login: Mon Apr 15 20:16:26 CST 2024 on pts/1Welcome to 5.10.0-153.12.0.92.oe2203sp2.x86_64System information as of time: 2024年 04月 15日 星期一 20:40:08 CSTSystem load: 0.04
Processes: 197
Memory used: 40.1%
Swap used: 14.2%
Usage On: 26%
IP address: 192.168.28.131
Users online: 2
To run a command as administrator(user "root"),use "sudo <command>".
[omm@node0 ~]$ vi /home/omm/logical/MPPDB_temp.sql
[omm@node0 ~]$
输入”i”,切换到 INSERT 模式,输入”public.customer_t1”,按下”ESC”,并输入”:wq”后回车
保存后退出。
[omm@node0 ~]$ ll /home/omm/logical/backup/
total 644
-rw------- 1 omm dbgrp 972 Apr 15 09:35 bkp_shl2.sql
drwx------ 2 omm dbgrp 4096 Apr 15 09:34 MPPDB_backup
-rw------- 1 omm dbgrp 122523 Apr 15 09:45 MPPDB_backup2.sql
-rw------- 1 omm dbgrp 121108 Apr 15 09:33 MPPDB_backup.dmp
-rw------- 1 omm dbgrp 123098 Apr 15 09:24 MPPDB_backup.sql
-rw------- 1 omm dbgrp 277504 Apr 15 09:32 MPPDB_backup.tar
[omm@node0 ~]$ gs_dump -U omm -W Bigdata@123 -p 15400 postgres --exclude-tablefile=/home/omm/logical/MPPDB_temp.sql -f /home/omm/logical/backup/MPPDB_backup2.sql
gs_dump[port='15400'][postgres][2024-04-15 09:45:42]: The total objects number is 514.
gs_dump[port='15400'][postgres][2024-04-15 09:45:42]: [100.00%] 514 objects have been dumped.
gs_dump[port='15400'][postgres][2024-04-15 09:45:42]: dump database postgres successfully
gs_dump[port='15400'][postgres][2024-04-15 09:45:42]: total time: 1394 ms
[omm@node0 ~]$ ll /home/omm/logical/backup/
total 644
-rw------- 1 omm dbgrp 972 Apr 15 09:35 bkp_shl2.sql
drwx------ 2 omm dbgrp 4096 Apr 15 09:34 MPPDB_backup
-rw------- 1 omm dbgrp 122523 Apr 15 09:45 MPPDB_backup2.sql
-rw------- 1 omm dbgrp 121108 Apr 15 09:33 MPPDB_backup.dmp
-rw------- 1 omm dbgrp 123098 Apr 15 09:24 MPPDB_backup.sql
-rw------- 1 omm dbgrp 277504 Apr 15 09:32 MPPDB_backup.tar
步骤 5 查看生成的 sql 文件,确认没有备份 customer_t1 表
[omm@node0 ~]$ cat /home/omm/logical/backup/MPPDB_backup2.sql
……SET search_path = public;--
-- Name: inx_stu01; Type: INDEX; Schema: public; Owner: omm; Tablespace:
--CREATE INDEX inx_stu01 ON student USING btree (std_name) TABLESPACE pg_default;--
-- 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
--
4.3 gs_dumpall
4.3.1 介绍
gs_dumpall 是 openGauss 用于导出所有数据库相关信息工具,它可以导出 openGauss 数
据库的所有数据,包括默认数据库 postgres 的数据、自定义数据库的数据、以及
openGauss 所有数据库公共的全局对象。
gs_dumpall 在导出 openGauss 所有数据库时分为两部分:
gs_dumpall 自身对所有数据库公共的全局对象进行导出,包括有关数据库用户和组,表
空间以及属性(例如,适用于数据库整体的访问权限)信息。
gs_dumpall 通过调用 gs_dump 来完成 openGauss 中各数据库的 SQL 脚本文件导出,该脚本文件包含将数据库恢复为其保存时的状态所需要的全部 SQL 语句。
以上两部分导出的结果为纯文本格式的 SQL 脚本文件,使用 gsql 运行该脚本文件可以恢复openGauss 数据库。
4.3.2 操作步骤
[root@node0 ~]# su - omm
Last login: Mon Apr 15 20:40:08 CST 2024 on pts/1Welcome to 5.10.0-153.12.0.92.oe2203sp2.x86_64System information as of time: 2024年 04月 17日 星期三 23:08:22 CSTSystem load: 0.00
Processes: 196
Memory used: 11.8%
Swap used: 0%
Usage On: 26%
IP address: 192.168.28.131
Users online: 1
To run a command as administrator(user "root"),use "sudo <command>".
[omm@node0 ~]$ gs_dumpall -f /home/omm/logical/backup/bkp2.sql -p 15400
gs_dump[port='15400'][dbname='db_tpcc02'][2024-04-17 23:10:58]: The total objects number is 427.
gs_dump[port='15400'][dbname='db_tpcc02'][2024-04-17 23:10:58]: [100.00%] 427 objects have been dumped.
gs_dump[port='15400'][dbname='db_tpcc02'][2024-04-17 23:10:58]: dump database dbname='db_tpcc02' successfully
gs_dump[port='15400'][dbname='db_tpcc02'][2024-04-17 23:10:58]: total time: 1494 ms
gs_dump[port='15400'][dbname='foo'][2024-04-17 23:11:00]: The total objects number is 427.
gs_dump[port='15400'][dbname='foo'][2024-04-17 23:11:00]: [100.00%] 427 objects have been dumped.
gs_dump[port='15400'][dbname='foo'][2024-04-17 23:11:00]: dump database dbname='foo' successfully
gs_dump[port='15400'][dbname='foo'][2024-04-17 23:11:00]: total time: 1504 ms
gs_dump[port='15400'][dbname='postgres'][2024-04-17 23:11:02]: The total objects number is 516.
gs_dump[port='15400'][dbname='postgres'][2024-04-17 23:11:02]: [100.00%] 516 objects have been dumped.
gs_dump[port='15400'][dbname='postgres'][2024-04-17 23:11:02]: dump database dbname='postgres' successfully
gs_dump[port='15400'][dbname='postgres'][2024-04-17 23:11:02]: total time: 1930 ms
gs_dumpall[port='15400'][2024-04-17 23:11:02]: dumpall operation successful
gs_dumpall[port='15400'][2024-04-17 23:11:02]: total time: 5055 ms
[omm@node0 ~]$ ll /home/omm/logical/backup/
total 776
-rw------- 1 omm dbgrp 127566 Apr 17 23:11 bkp2.sql
-rw------- 1 omm dbgrp 972 Apr 15 09:35 bkp_shl2.sql
drwx------ 2 omm dbgrp 4096 Apr 15 17:49 MPPDB_backup
-rw------- 1 omm dbgrp 122523 Apr 15 09:45 MPPDB_backup2.sql
-rw------- 1 omm dbgrp 123098 Apr 15 20:32 MPPDB_backup.dmp
-rw------- 1 omm dbgrp 123098 Apr 15 17:34 MPPDB_backup.sql
-rw------- 1 omm dbgrp 277504 Apr 15 17:43 MPPDB_backup.tar
4.4 gs_restore
4.4.1 介绍
gs_restore 是 openGauss 提供的针对 gs_dump 导出数据的导入工具。通过此工具可由gs_dump 生成的导出文件进行导入。
gs_restore 工具由操作系统用户 omm 执行。
主要功能包含:
导入到数据库:
如果连接参数中指定了数据库,则数据将被导入到指定的数据库中。其中,并行导入必须
指定连接的密码。
导入到脚本文件:
如果未指定导入数据库,则创建包含重建数据库所必须的 SQL 语句脚本并写入到文件或者标准输出。等效于直接使用 gs_dump 导出为纯文本格式。
4.4.2 gs_restore 导入示例 1
[root@node0 ~]# su - omm
Last login: Wed Apr 17 23:08:22 CST 2024 on pts/0Welcome to 5.10.0-153.12.0.92.oe2203sp2.x86_64System information as of time: 2024年 04月 17日 星期三 23:14:53 CSTSystem load: 0.06
Processes: 196
Memory used: 38.3%
Swap used: 11.0%
Usage On: 26%
IP address: 192.168.28.131
Users online: 1
To run a command as administrator(user "root"),use "sudo <command>".
[omm@node0 ~]$ gsql -d postgres -p 15400 -r
gsql ((openGauss 5.0.0 build a07d57c3) compiled at 2023-03-29 03:37:13 commit 0 last mr )
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type "help" for help.
openGauss=# DROP DATABASE IF EXISTS db_tpcc01;
NOTICE: database "db_tpcc01" does not exist, skipping
DROP DATABASE
openGauss=# CREATE DATABASE db_tpcc01;
CREATE DATABASE
openGauss=# \q
[omm@node0 ~]$
步骤 5 执行 gs_restore,将导出的 MPPDB_backup.tar 文件(tar 格式)导入到 db_tpcc01 数据库。
[omm@node0 ~]$ gs_restore /home/omm/logical/backup/MPPDB_backup.tar -p 15400 -d db_tpcc01
如果成功,显示如下:
start restore operation ...
table mytable complete data imported !
table pmk_configuration complete data imported !
table pmk_meta_data complete data imported !
table pmk_snapshot complete data imported !
table pmk_snapshot_datanode_stat complete data imported !
table a complete data imported !
table class complete data imported !
table course complete data imported !
table customer_t1 complete data imported !
table customer_t2 complete data imported !
table mytable complete data imported !
table school_department complete data imported !
table student complete data imported !
table t1 complete data imported !
table t2 complete data imported !
table t_test complete data imported !
table teacher complete data imported !
table web_returns_p2 complete data imported !
Finish reading 87 SQL statements!
end restore operation ...
restore operation successful
total time: 1165 ms
[omm@node0 ~]$ gsql -d db_tpcc01 -p 15400 -r
gsql ((openGauss 5.0.0 build a07d57c3) compiled at 2023-03-29 03:37:13 commit 0 last mr )
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type "help" for help.
db_tpcc01=# select * from customer_t1;c_customer_sk | c_customer_id | c_first_name | c_last_name
---------------+---------------+--------------+-------------3769 | hello | | 6885 | maps | Joes | 4321 | tpcds | Lily | 9527 | world | James |
(4 rows)
db_tpcc01=# \q
[omm@node0 ~]$
4.4.3 gs_restore 导入示例 2
执行 gs_restore,将导出的 MPPDB_backup.dmp 文件(自定义归档格式)导入到 db_tpcc02数据库。
步骤 1 切换到 omm 用户,以操作系统用户 omm 登录数据库主节点
[root@node0 ~]# su - omm
Last login: Wed Apr 17 23:14:53 CST 2024 on pts/0Welcome to 5.10.0-153.12.0.92.oe2203sp2.x86_64System information as of time: 2024年 04月 17日 星期三 23:25:56 CSTSystem load: 0.06
Processes: 197
Memory used: 39.1%
Swap used: 11.9%
Usage On: 26%
IP address: 192.168.28.131
Users online: 1
To run a command as administrator(user "root"),use "sudo <command>".
[omm@node0 ~]$ gsql -d postgres -p 15400 -r
gsql ((openGauss 5.0.0 build a07d57c3) compiled at 2023-03-29 03:37:13 commit 0 last mr )
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type "help" for help.
openGauss=# DROP DATABASE IF EXISTS db_tpcc02;
NOTICE: database "db_tpcc02" does not exist, skipping
DROP DATABASE
openGauss=# CREATE DATABASE db_tpcc02;
CREATE DATABASE
openGauss=# \q
[omm@node0 ~]$
[omm@node0 ~]$ gs_restore /home/omm/logical/backup/MPPDB_backup.dmp -p 15400 -d db_tpcc02
start restore operation ...
table mytable complete data imported !
table pmk_configuration complete data imported !
table pmk_meta_data complete data imported !
table pmk_snapshot complete data imported !
table pmk_snapshot_datanode_stat complete data imported !
table a complete data imported !
table class complete data imported !
table course complete data imported !
table customer_t1 complete data imported !
table customer_t2 complete data imported !
table mytable complete data imported !
table school_department complete data imported !
table student complete data imported !
table t1 complete data imported !
table t2 complete data imported !
table t_test complete data imported !
table teacher complete data imported !
table web_returns_p2 complete data imported !
Finish reading 87 SQL statements!
end restore operation ...
restore operation successful
total time: 862 ms
[omm@node0 ~]$ gsql -d db_tpcc02 -p 15400 -r
gsql ((openGauss 5.0.0 build a07d57c3) compiled at 2023-03-29 03:37:13 commit 0 last mr )
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type "help" for help.
db_tpcc02=# select * from customer_t1;
c_customer_sk | c_customer_id | c_first_name | c_last_name
---------------+---------------+--------------+-------------3769 | hello | | 6885 | maps | Joes | 4321 | tpcds | Lily | 9527 | world | James |
(4 rows)
db_tpcc02=# \q
[omm@node0 ~]$
4.4.4 gs_restore 导入示例 3
[root@node0 ~]# su - omm
Last login: Wed Apr 17 23:25:56 CST 2024 on pts/0Welcome to 5.10.0-153.12.0.92.oe2203sp2.x86_64System information as of time: 2024年 04月 17日 星期三 23:43:58 CSTSystem load: 0.30
Processes: 196
Memory used: 39.1%
Swap used: 14.9%
Usage On: 26%
IP address: 192.168.28.131
Users online: 1
To run a command as administrator(user "root"),use "sudo <command>".
[omm@node0 ~]$ gsql -d postgres -p 15400 -r
gsql ((openGauss 5.0.0 build a07d57c3) compiled at 2023-03-29 03:37:13 commit 0 last mr )
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type "help" for help.
openGauss=# DROP DATABASE IF EXISTS db_tpcc03;
NOTICE: database "db_tpcc03" does not exist, skipping
DROP DATABASE
openGauss=# CREATE DATABASE db_tpcc03;
CREATE DATABASE
步骤 4 退出数据库
openGauss=# \q
[omm@node0 ~]$ gs_restore /home/omm/logical/backup/MPPDB_backup -p 15400 -d db_tpcc03
start restore operation ...
table mytable complete data imported !
table pmk_configuration complete data imported !
table pmk_meta_data complete data imported !
table pmk_snapshot complete data imported !
table pmk_snapshot_datanode_stat complete data imported !
table a complete data imported !
table class complete data imported !
table course complete data imported !
table customer_t1 complete data imported !
table customer_t2 complete data imported !
table mytable complete data imported !
table school_department complete data imported !
table student complete data imported !
table t1 complete data imported !
table t2 complete data imported !
table t_test complete data imported !
table teacher complete data imported !
table web_returns_p2 complete data imported !
Finish reading 87 SQL statements!
end restore operation ...
restore operation successful
total time: 1836 ms
[omm@node0 ~]$
[omm@node0 ~]$ gsql -d db_tpcc03 -p 15400 -r
gsql ((openGauss 5.0.0 build a07d57c3) compiled at 2023-03-29 03:37:13 commit 0 last mr )
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type "help" for help.
db_tpcc03=# select * from customer_t1;c_customer_sk | c_customer_id | c_first_name | c_last_name
---------------+---------------+--------------+-------------3769 | hello | | 6885 | maps | Joes | 4321 | tpcds | Lily | 9527 | world | James |
(4 rows)
db_tpcc03=# \q
[omm@node0 ~]$
4.4.5 gs_restore 导入示例 4
[omm@node0 ~]$ gs_restore -W Bigdata@123 /home/omm/logical/backup/MPPDB_backup.dmp -p 15400 -d db_tpcc01
start restore operation ...
……
Error from TOC entry 3728; 1259 41282 INDEX inx_stu01 omm
could not execute query: ERROR: relation "inx_stu01" already existsCommand was: CREATE INDEX inx_stu01 ON student USING btree (std_name) TABLESPACE
pg_default;
Finish reading 87 SQL statements!
end restore operation ...
WARNING: errors ignored on restore: 69
restore operation successful
total time: 1166 ms
[omm@node0 ~]$ gsql -d db_tpcc01 -p 15400 -r
gsql ((openGauss 5.0.0 build a07d57c3) compiled at 2023-03-29 03:37:13 commit 0 last mr )
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type "help" for help.
db_tpcc01=# select * from customer_t1;c_customer_sk | c_customer_id | c_first_name | c_last_name
---------------+---------------+--------------+-------------3769 | hello | | 6885 | maps | Joes | 4321 | tpcds | Lily | 9527 | world | James |
(4 rows)
db_tpcc01=# \q
[omm@node0 ~]$
[omm@node0 ~]$ gs_restore -W Bigdata@123 /home/omm/logical/backup/MPPDB_backup.dmp -p 15400 -d db_tpcc01 -e -c
start restore operation ...
table mytable complete data imported !
table pmk_configuration complete data imported !
table pmk_meta_data complete data imported !
table pmk_snapshot complete data imported !
table pmk_snapshot_datanode_stat complete data imported !
table a complete data imported !
table class complete data imported !
table course complete data imported !
table customer_t1 complete data imported !
table customer_t2 complete data imported !
table mytable complete data imported !
table school_department complete data imported !
table student complete data imported !
table t1 complete data imported !
table t2 complete data imported !
table t_test complete data imported !
table teacher complete data imported !
table web_returns_p2 complete data imported !
Finish reading 87 SQL statements!
end restore operation ...
restore operation successful
total time: 1285 ms
[omm@node0 ~]$ gsql -d db_tpcc01 -p 15400 -r
gsql ((openGauss 5.0.0 build a07d57c3) compiled at 2023-03-29 03:37:13 commit 0 last mr )
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type "help" for help.
db_tpcc01=# select * from customer_t1;c_customer_sk | c_customer_id | c_first_name | c_last_name
---------------+---------------+--------------+-------------3769 | hello | | 6885 | maps | Joes | 4321 | tpcds | Lily | 9527 | world | James |
(4 rows)
db_tpcc01=# \q
[omm@node0 ~]$
4.4.6 gs_restore 导入示例 5
[root@node0 ~]# su - omm
Last login: Thu Apr 18 11:39:13 CST 2024 on pts/0Welcome to 5.10.0-153.12.0.92.oe2203sp2.x86_64System information as of time: 2024年 04月 18日 星期四 11:48:17 CSTSystem load: 0.08
Processes: 196
Memory used: 38.3%
Swap used: 10.5%
Usage On: 26%
IP address: 192.168.28.131
Users online: 1
To run a command as administrator(user "root"),use "sudo <command>".
[omm@node0 ~]$ gsql -d postgres -p 15400 -r
gsql ((openGauss 5.0.0 build a07d57c3) compiled at 2023-03-29 03:37:13 commit 0 last mr )
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type "help" for help.
openGauss=# DROP DATABASE IF EXISTS db_tpcc04;
NOTICE: database "db_tpcc04" does not exist, skipping
DROP DATABASE
openGauss=# CREATE DATABASE db_tpcc04;
CREATE DATABASE
openGauss=#
openGauss=# \q
[omm@node0 ~]$
[omm@node0 ~]$ gs_restore /home/omm/logical/backup/MPPDB_backup.dmp -p 15400 -d db_tpcc04 -n public -t customer_t1
table customer_t1 complete data imported !
Finish reading 87 SQL statements!
end restore operation ...
restore operation successful
total time: 34 ms
[omm@node0 ~]$ gsql -d db_tpcc04 -p 15400 -r
gsql ((openGauss 5.0.0 build a07d57c3) compiled at 2023-03-29 03:37:13 commit 0 last mr )
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type "help" for help.
db_tpcc04=# select * from customer_t1;
c_customer_sk | c_customer_id | c_first_name | c_last_name
---------------+---------------+--------------+-------------3769 | hello | | 6885 | maps | Joes | 4321 | tpcds | Lily | 9527 | world | James |
(4 rows)
db_tpcc04=# select * from lucy.mytable;
ERROR: schema "lucy" does not exist
LINE 1: select * from lucy.mytable;^
db_tpcc04=#
db_tpcc04=# select * from customer_t2;
ERROR: relation "customer_t2" does not exist on dn_6001
LINE 1: select * from customer_t2;^
db_tpcc04=#