You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PostgreSQL大规模数据库增量备份支持及相关需求咨询

大规模PostgreSQL数据库增量备份解决方案

一、支持增量备份的PostgreSQL安装包

  • PostgreSQL 13及以上版本原生支持基于WAL(预写日志)的增量备份能力,无需额外第三方插件即可实现基础增量备份功能。
  • 若需更灵活的块级增量备份、远程备份管理等进阶能力,可搭配barman或pg_rman工具:
    • 官方PostgreSQL安装包:直接通过系统包管理器(如apt install postgresql-16、yum install postgresql-server)安装对应版本,或从PostgreSQL官网下载二进制包部署,确保版本≥13。
    • barman工具包:通过apt install barman(Debian/Ubuntu)或yum install barman(RHEL/CentOS)安装,需提前配置与目标PostgreSQL实例的连接权限。

二、原生增量备份操作步骤(基于WAL+基础备份)

1. 创建初始全量基础备份

使用pg_basebackup生成全量备份,作为增量备份的基准:

pg_basebackup -D /data/postgres/full_backup -F tar -X stream -P -U postgres

参数说明:

  • -D:指定备份存储目录
  • -F tar:以tar格式打包备份文件
  • -X stream:实时流式备份WAL日志,保证备份一致性
  • -P:显示备份进度
  • -U:指定连接数据库的授权用户

2. 开启WAL归档(增量备份核心前提)

修改PostgreSQL配置文件postgresql.conf,启用WAL归档:

wal_level = replica          # 需设置为replica或更高级别
archive_mode = on
archive_command = 'cp %p /data/postgres/wal_archives/%f'  # 将WAL文件复制到归档目录,可替换为scp实现远程归档

修改后重启PostgreSQL服务生效:

sudo systemctl restart postgresql

3. 生成增量备份

PostgreSQL 13+支持--incremental参数,基于上次全量备份生成增量包:

pg_basebackup -D /data/postgres/incremental_backup_20240520 -F tar -X stream -P -U postgres --incremental=/data/postgres/full_backup

该命令仅备份自全量备份以来变化的数据块,大幅减少备份体积。

4. 增量备份恢复流程

  1. 停止PostgreSQL服务:
sudo systemctl stop postgresql
  1. 恢复全量备份到数据目录:
tar xvf /data/postgres/full_backup/base.tar -C /var/lib/postgresql/16/main/  # 替换为你的数据库数据目录
  1. 恢复增量备份覆盖对应数据块:
tar xvf /data/postgres/incremental_backup_20240520/base.tar -C /var/lib/postgresql/16/main/ --strip-components=1
  1. 配置WAL日志恢复命令,在postgresql.conf中添加:
restore_command = 'cp /data/postgres/wal_archives/%f %p'
  1. 启动PostgreSQL服务,系统自动应用WAL日志完成最终恢复:
sudo systemctl start postgresql

三、第三方工具增量备份(以Barman为例)

1. 配置Barman与PostgreSQL实例

编辑Barman配置文件/etc/barman.d/postgres_instance.conf:

[postgres_instance]
description = "Production PostgreSQL Instance"
conninfo = host=localhost user=barman dbname=postgres
backup_directory = /var/lib/barman/postgres_instance
wal_directory = /var/lib/barman/postgres_instance/wals
archiver = on

2. 执行全量备份

barman backup postgres_instance

3. 执行增量备份

barman backup --incremental postgres_instance

4. 基于增量备份恢复到指定时间点

barman recover --target-time "2024-05-20 14:30:00" postgres_instance latest /var/lib/postgresql/16/main/

内容的提问来源于stack exchange,提问作者vaibhav gurav

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 16:27:16