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

openGauss每日一练第20天|学习数据库逻辑备份和恢复技术

原创 Heming 2022-12-13
621

openGauss每日一练第20天

学习数据库的逻辑备份和恢复技术,完成课后作业。

创建逻辑备份目录

使用linux目录mkdir创建一个目录/var/lib/opengauss/backup

omm@e537701a151a:/$ mkdir /var/lib/opengauss/backup
复制

在omm数据库中创建表t1、t2

omm=# CREATE TABLE t1(col int); CREATE TABLE omm=# CREATE TABLE t2(col int);CREATE TABLE t1(col int); CREATE TABLE omm-# \d List of relations Schema | Name | Type | Owner | Storage --------+------+-------+-------+---------------------------------- testsm | t1 | table | omm | {orientation=row,compression=no} testsm | t2 | table | omm | {orientation=row,compression=no} (2 rows)
复制

逻辑备份omm数据库(sql格式)

使用gs_dump程序将omm数据库备份到/var/lib/opengauss/backup目录的bk.sql文件

omm-# \! gs_dump omm -F p -f /var/lib/opengauss/backup/bk.sql gs_dump[port='5432'][omm][2022-12-13 12:21:59]: The total objects number is 398. gs_dump[port='5432'][omm][2022-12-13 12:21:59]: [100.00%] 398 objects have been dumped. gs_dump[port='5432'][omm][2022-12-13 12:21:59]: dump database omm successfully gs_dump[port='5432'][omm][2022-12-13 12:21:59]: total time: 152 ms
复制

备份完成后查看该目录下的文件

omm-# \! ls -il /var/lib/opengauss/backup total 4 1316281 -rw------- 1 omm omm 2269 Dec 13 12:21 bk.sql
复制

使用SQL文件恢复omm数据库

首先删除omm数据库下的所有表

omm=# drop table t2; DROP TABLE omm=# drop table t1; DROP TABLE omm=# \d No relations found.
复制

使用gsql -f参数执行/var/lib/opengauss/backup/bk.sql的逻辑备份脚本

omm=# \! gsql -d omm -f /var/lib/opengauss/backup/bk.dump ALTER TABLE SET CREATE TABLE ALTER TABLE CREATE TABLE ALTER TABLE SET SET REVOKE REVOKE GRANT GRANT total time: 94 ms omm=# \d List of relations Schema | Name | Type | Owner | Storage --------+------+-------+-------+---------------------------------- testsm | t1 | table | omm | {orientation=row,compression=no} testsm | t2 | table | omm | {orientation=row,compression=no} (2 rows)
复制

再次使用\d查询,可以看见我们的表成功恢复了

逻辑备份omm数据库(dump格式)

使用gs_dump程序将omm数据库备份到/var/lib/opengauss/backup目录的bk.dump文件

omm=# \! gs_dump omm -F p -f /var/lib/opengauss/backup/bk.dump gs_dump[port='5432'][omm][2022-12-13 12:28:14]: The total objects number is 398. gs_dump[port='5432'][omm][2022-12-13 12:28:14]: [100.00%] 398 objects have been dumped. gs_dump[port='5432'][omm][2022-12-13 12:28:14]: dump database omm successfully gs_dump[port='5432'][omm][2022-12-13 12:28:14]: total time: 153 ms
复制

备份完成后查看该目录下的文件

omm=# \! ls -il /var/lib/opengauss/backup total 8 1316283 -rw------- 1 omm omm 2269 Dec 13 12:28 bk.dump 1316281 -rw------- 1 omm omm 2269 Dec 13 12:21 bk.sql
复制

使用dump文件恢复omm数据库

首先删除omm数据库下的所有表

omm=# drop table t2; DROP TABLE omm=# drop table t1; DROP TABLE omm=# \d No relations found.
复制

使用gsql -f参数执行/var/lib/opengauss/backup/bk.dump的逻辑备份脚本

omm=# \! gsql -d omm -f /var/lib/opengauss/backup/bk.dump ALTER TABLE SET CREATE TABLE ALTER TABLE CREATE TABLE ALTER TABLE SET SET REVOKE REVOKE GRANT GRANT total time: 94 ms omm=# \d List of relations Schema | Name | Type | Owner | Storage --------+------+-------+-------+---------------------------------- testsm | t1 | table | omm | {orientation=row,compression=no} testsm | t2 | table | omm | {orientation=row,compression=no} (2 rows)
复制

再次使用\d查询,可以看见我们的表成功恢复了

总结

数据库备份可以通过gs_dump工具进行备份,可以将数据库的数据导出成sql或dump格式的文件,恢复备份的方法其实就是执行sql脚本。

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

评论

墨天轮-雪宝君
暂无图片
2年前
评论
暂无图片 0
作业审核合格,一起参与21天openGauss学习打卡活动! 活动详情:https://www.modb.pro/db/551619
2年前
暂无图片 点赞
评论