暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

openGauss每日一练第20天 | openGauss数据库的逻辑备份与恢复

原创 小马专业莲藕打孔 2022-12-15
267

学习目标

掌握openGauss数据库的逻辑备份和恢复技术。


准备工作

--创建逻辑备份的存储目录:
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"


课程作业

1.逻辑备份和恢复案例1:使用sql格式进行备份和恢复omm数据库

代码

--使用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"


结果

omm@modb:~$ --使用test用户,备份数据库omm:
-bash: $'--\344\275\277\347\224\250test\347\224\250\346\210\267\357\274\214\345\244\207\344\273\275\346\225\260\346\215\256\345\272\223omm\357\274\232': command not found
omm@modb:~$ 
omm@modb:~$ gs_dump -U test -W huawei@1234  omm -F p -f /var/lib/opengauss/backup/backup.sql

gs_dump[port='5432'][omm][2022-12-15 09:25:54]: The total objects number is 413.
gs_dump[port='5432'][omm][2022-12-15 09:25:54]: [100.00%] 413 objects have been dumped.
gs_dump[port='5432'][omm][2022-12-15 09:25:54]: dump database omm successfully
gs_dump[port='5432'][omm][2022-12-15 09:25:54]: total time: 1548  ms
omm@modb:~$  
omm@modb:~$ 
omm@modb:~$ mkdir /var/lib/opengauss/backup
mkdir: cannot create directory '/var/lib/opengauss/backup': File exists
omm@modb:~$ omm@modb:~$  
--使用用户test,执行用gs_dump生成的sql脚本,将数据恢复到testdb数据库中:
-bash: $'--\344\275\277\347\224\250\347\224\250\346\210\267test\357\274\214\346\211\247\350\241\214\347\224\250gs_dump\347\224\237\346\210\220\347\232\204sql\350\204\232\346\234\254\357\274\214\345\260\206\346\225\260\346\215\256\346\201\242\345\244\215\345\210\260testdb\346\225\260\346\215\256\345\272\223\344\270\255\357\274\232': command not found
omm@modb:~$   gsql -d testdb   -U test  -W huawei@1234  -f /var/lib/opengauss/backup/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:~$ 
omm@modb:~$ --验证数据库omm的备份已经被恢复到数据库testdb:
-bash: $'--\351\252\214\350\257\201\346\225\260\346\215\256\345\272\223omm\347\232\204\345\244\207\344\273\275\345\267\262\347\273\217\350\242\253\346\201\242\345\244\215\345\210\260\346\225\260\346\215\256\345\272\223testdb\357\274\232': command not found
omm@modb:~$ --源库(备份的数据库):
-bash: $'--\346\272\220\345\272\223\357\274\210\345\244\207\344\273\275\347\232\204\346\225\260\346\215\256\345\272\223\357\274\211\357\274\232': command not found
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:~$ 
omm@modb:~$ --新库(恢复的数据库):
-bash: $'--\346\226\260\345\272\223\357\274\210\346\201\242\345\244\215\347\232\204\346\225\260\346\215\256\345\272\223\357\274\211\357\274\232': command not found
omm@modb:~$ gsql -d testdb   -U test   -W huawei@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)

2.逻辑备份和恢复案例2:使用dump格式进行备份和恢复omm数据库

代码

--创建测试数据
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"


结果

omm@modb:~$ --创建测试数据
-bash: $'--\345\210\233\345\273\272\346\265\213\350\257\225\346\225\260\346\215\256': command not found
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
omm@modb:~$ 
omm@modb:~$ --使用test用户,备份omm数据库,生成归档格式的备份文件:
-bash: $'--\344\275\277\347\224\250test\347\224\250\346\210\267\357\274\214\345\244\207\344\273\275omm\346\225\260\346\215\256\345\272\223\357\274\214\347\224\237\346\210\220\345\275\222\346\241\243\346\240\274\345\274\217\347\232\204\345\244\207\344\273\275\346\226\207\344\273\266:': command not found
omm@modb:~$ omm@modb:~$ 
gs_dump -U test -W huawei@1234  omm -F p -f /var/lib/opengauss/backup/backup.dump
gs_dump[port='5432'][omm][2022-12-15 09:27:42]: The total objects number is 417.
gs_dump[port='5432'][omm][2022-12-15 09:27:42]: [100.00%] 417 objects have been dumped.
gs_dump[port='5432'][omm][2022-12-15 09:27:42]: dump database omm successfully
gs_dump[port='5432'][omm][2022-12-15 09:27:42]: total time: 1613  ms
omm@modb:~$ 
omm@modb:~$ --使用gs_dump生成的归档文件恢复数据库
-bash: $'--\344\275\277\347\224\250gs_dump\347\224\237\346\210\220\347\232\204\345\275\222\346\241\243\346\226\207\344\273\266\346\201\242\345\244\215\346\225\260\346\215\256\345\272\223': command not found
omm@modb:~$ 
omm@modb:~$  gsql -d testdb   -U test  -W huawei@1234  -f /var/lib/opengauss/backup/backup.dump
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
total time: 21  ms
GRANT
omm@modb:~$ 
omm@modb:~$ --源库(备份的数据库):
-bash: $'--\346\272\220\345\272\223\357\274\210\345\244\207\344\273\275\347\232\204\346\225\260\346\215\256\345\272\223\357\274\211\357\274\232': command not found
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:~$ --新库(恢复的数据库):
-bash: $'--\346\226\260\345\272\223\357\274\210\346\201\242\345\244\215\347\232\204\346\225\260\346\215\256\345\272\223\357\274\211\357\274\232': command not found
omm@modb:~$ gsql -d testdb   -U test   -W huawei@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)


「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论