postgresql本身提供了pg_basebackup、pg_dump和pg_restore等工具对数据库进行备份还原操作。我在平时工作中还使用到另外一款开源软件:pg_rman。
一、软件获取
下载地址:https://github.com/ossc-db/pg_rman/releases
有在redhat平台上编译好的rpm包和源码包。如果是centos7上安装的postgresql 12版本,则下载pg_rman-1.3.9-1.pg12.rhel7.x86_64.rpm
二、使用pg_rman备份pg数据库
注:backup-mode可用参数为full(全备), incremental(增量备份), or archive(只备归档)。backup-path是指定要将备份文件保存在哪个位置,注意该目录要给postgres用户读写权限。
–先初始化备份环境,会在备份目录下生成一些文件和目录
$ pg_rman init --backup-mode=full --backup-path=/pgdata/12/backups --arclog-path=/pgdata/12/archive_wals INFO: ARCLOG_PATH is set to '/pgdata/12/archive_wals' INFO: SRVLOG_PATH is set to '/pgdata/12/data/log'
复制
–然后执行全备
$ pg_rman backup --backup-mode=full --backup-path=/pgdata/12/backups INFO: copying database files INFO: copying archived WAL files INFO: backup complete INFO: Please execute 'pg_rman validate' to verify the files are correctly copied.
复制
–检查备份是否有效
$ pg_rman validate --backup-path=/pgdata/12/backups INFO: validate: "2021-12-24 15:13:51" backup and archive log files by CRC INFO: backup "2021-12-24 15:13:51" is valid
复制
–查看备份记录
$ pg_rman show --backup-path=/pgdata/12/backups ===================================================================== StartTime EndTime Mode Size TLI Status ===================================================================== 2021-12-24 15:13:51 2021-12-24 15:13:54 FULL 189MB 1 OK
复制
三、使用pg_rman还原数据库
–停库,删除数据文件所有目录
pg_ctl stop -m fast rm -rf /pgdata/12/data/*
复制
–使用pg_rman恢复数据库
$ pg_rman restore --backup-path=/pgdata/12/backups WARNING: pg_controldata file "/pgdata/12/data/global/pg_control" does not exist INFO: the recovery target timeline ID is not given INFO: use timeline ID of latest full backup as recovery target: 1 INFO: calculating timeline branches to be used to recovery target point INFO: searching latest full backup which can be used as restore start point INFO: found the full backup can be used as base in recovery: "2021-12-24 15:13:51" INFO: copying online WAL files and server log files INFO: clearing restore destination INFO: validate: "2021-12-24 15:13:51" backup and archive log files by SIZE INFO: backup "2021-12-24 15:13:51" is valid INFO: restoring database files from the full mode backup "2021-12-24 15:13:51" INFO: searching incremental backup to be restored INFO: searching backup which contained archived WAL files to be restored INFO: backup "2021-12-24 15:13:51" is valid INFO: restoring WAL files from backup "2021-12-24 15:13:51" INFO: restoring online WAL files and server log files INFO: add recovery related options to postgresql.conf INFO: generating recovery.signal INFO: restore complete HINT: Recovery will start automatically when the PostgreSQL server is started.
复制
–启动pg
$ pg_ctl start waiting for server to start....2021-12-24 15:36:08.658 CST [4555] LOG: starting PostgreSQL 12.2 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44), 64-bit 2021-12-24 15:36:08.658 CST [4555] LOG: listening on IPv4 address "0.0.0.0", port 5432 2021-12-24 15:36:08.659 CST [4555] LOG: listening on IPv6 address "::", port 5432 2021-12-24 15:36:08.665 CST [4555] LOG: listening on Unix socket "/tmp/.s.PGSQL.5432" 2021-12-24 15:36:08.734 CST [4555] LOG: redirecting log output to logging collector process 2021-12-24 15:36:08.734 CST [4555] HINT: Future log output will appear in directory "log". . done server started
复制
–登录测试
$ psql psql (12.2) Type "help" for help. postgres=# \du List of roles Role name | Attributes | Member of -----------+------------------------------------------------------------+----------- pguser | Replication | {} postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {} repuser | Replication +| {} | 5 connections |
复制
关于pg_rman的更多使用参数,可使用如下命令获取:
pg_rman --help
复制
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。
文章被以下合辑收录
评论
您好,您的文章已入选合格奖,10墨值奖励已经到账请查收!
❤️我们还会实时派发您的流量收益。
3年前

评论
相关阅读
王炸!OGG 23ai 终于支持从PostgreSQL备库抽取数据了
曹海峰
429次阅读
2025-03-09 12:54:06
明明想执行的SQL是DELETE、UPDATE,但为什么看到的是SELECT(FDW的实现原理解析)
小满未满、
371次阅读
2025-03-19 23:11:26
PostgreSQL初/中/高级认证考试(3.15)通过考生公示
开源软件联盟PostgreSQL分会
335次阅读
2025-03-20 09:50:36
IvorySQL 4.4 发布 - 基于 PostgreSQL 17.4,增强平台支持
通讯员
216次阅读
2025-03-20 15:31:04
套壳论
梧桐
209次阅读
2025-03-09 10:58:17
命名不规范,事后泪两行
xiongcc
193次阅读
2025-03-13 14:26:08
openHalo问世,全球首款基于PostgreSQL兼容MySQL协议的国产开源数据库
严少安
180次阅读
2025-04-07 12:14:29
PG vs MySQL 执行计划解读的异同点
进击的CJR
136次阅读
2025-03-21 10:50:08
版本发布| IvorySQL 4.4 发布
IvorySQL开源数据库社区
126次阅读
2025-03-13 09:52:33
宝藏PEV,助力你成为SQL优化高手
xiongcc
124次阅读
2025-03-09 23:34:23