PostgreSQL教程FG106-PG物理备份pg_basebackup实战教程
本文档详细介绍PostgreSQL数据库物理备份工具pg_basebackup的使用方法,包括基础备份、WAL归档配置、时间点恢复等操作,风哥教程参考PostgreSQL官方文档Backup and Restore内容,适合DBA在生产环境中实施物理备份策略。
Part01-基础概念与理论知识
1.1 PostgreSQL物理备份概念
PostgreSQL物理备份是指直接复制数据库数据文件的备份方式。物理备份包括基础备份和WAL归档两部分,基础备份是数据库文件的一致性副本,WAL归档记录了所有数据变更。通过物理备份可以实现时间点恢复(PITR),将数据库恢复到任意时间点的状态。更多视频教程www.fgedu.net.cn
- 备份和恢复速度快,直接复制文件
- 支持时间点恢复(PITR)
- 适合大型数据库的备份需求
- 备份文件与平台相关,不能跨平台恢复
- 需要配置WAL归档才能实现完整恢复
- 是搭建流复制备库的基础
1.2 PostgreSQL WAL归档机制
# 1. WAL(Write-Ahead Logging)
# – 预写式日志,记录所有数据变更
# – 确保数据一致性和持久性
# – 支持时间点恢复
# 2. WAL文件
# – 位于 pg_wal 目录
# – 默认每个文件16MB
# – 文件名格式:000000010000000000000001
# 3. WAL归档
# – 将已完成的WAL文件复制到归档目录
# – 通过archive_mode和archive_command配置
# – 归档文件用于时间点恢复
# 4. 归档模式配置
archive_mode = on
archive_command = ‘cp %p /postgresql/archive/%f’
# 5. WAL级别
# – minimal:最小日志,不支持归档
# – replica:支持归档和流复制
# – logical:支持逻辑解码
# WAL归档流程
| 步骤 | 操作 |
|——|——|
| 1 | 数据变更写入WAL缓冲区 |
| 2 | WAL缓冲区刷新到WAL文件 |
| 3 | WAL文件写满后触发归档 |
| 4 | archive_command执行归档命令 |
| 5 | 归档成功后WAL文件可被重用 |
1.3 pg_basebackup参数说明
pg_basebackup常用参数:
- -h/–host:数据库服务器主机名
- -p/–port:数据库服务器端口
- -U/–username:连接用户名
- -D/–pgdata:备份目标目录
- -F/–format:输出格式(p/t)
- -X/–wal-method:WAL包含方式(none/fetch/stream)
- -z/–gzip:压缩输出(tar格式)
- -Z/–compress:压缩级别
- -P/–progress:显示进度
- -R/–write-recovery-conf:生成恢复配置
Part02-生产环境规划与建议
2.1 PostgreSQL物理备份规划
# 1. 备份策略
# 基础备份:每周一次完整备份
# WAL归档:持续进行
# 增量备份:使用第三方工具(pgBackRest/Barman)
# 2. 备份目录规划
/postgresql/backup/
├── base/ # 基础备份
│ ├── 20260401/
│ ├── 20260407/
│ └── 20260414/
├── archive/ # WAL归档
│ ├── 000000010000000000000001
│ ├── 000000010000000000000002
│ └── …
└── scripts/ # 备份脚本
# 3. 存储空间估算
# 基础备份空间 = 数据库大小 * 保留份数
# WAL归档空间 = 日均WAL量 * 保留天数
# 总空间 = 基础备份空间 + WAL归档空间 * 1.5
# 4. 网络带宽规划
# 备份期间网络带宽占用
# 建议在业务低峰期执行备份
# 5. 备份保留策略
# 基础备份保留:4周
# WAL归档保留:与基础备份配套
# 重要备份:长期归档存储
2.2 PostgreSQL WAL归档规划
PostgreSQL WAL归档规划:
- 归档目录:独立的存储空间,与数据目录分离
- 归档命令:使用可靠的复制命令
- 归档监控:监控归档延迟和失败
- 归档清理:定期清理过期的归档文件
- 归档压缩:对归档文件进行压缩存储
2.3 PostgreSQL恢复策略规划
# 1. 恢复场景
# – 完全恢复:恢复到最新状态
# – 时间点恢复:恢复到指定时间点
# – 事务恢复:恢复到指定事务ID
# – 命名恢复点:恢复到指定恢复点
# 2. 恢复时间目标
# RTO(Recovery Time Objective):恢复时间目标
# RPO(Recovery Point Objective):恢复点目标
# 3. 恢复流程
# 步骤1:停止数据库服务
# 步骤2:恢复基础备份
# 步骤3:配置恢复参数
# 步骤4:启动恢复
# 步骤5:验证数据完整性
# 4. 恢复测试
# 每月进行一次恢复演练
# 记录恢复时间和步骤
# 优化恢复流程
# 5. 恢复文档
# 编写详细的恢复操作手册
# 包含各种恢复场景的处理方法
# 定期更新恢复文档
Part03-生产环境项目实施方案
3.1 PostgreSQL基础备份实战
3.1.1 配置复制权限
$ psql -U fgedu -d fgedudb
# 创建复制用户
fgedudb=# CREATE USER fgedu_repl WITH REPLICATION ENCRYPTED PASSWORD ‘Fgedu@Repl2026’;
CREATE ROLE
# 配置pg_hba.conf
$ vi /postgresql/fgdata/pg_hba.conf
# 添加以下内容
host replication fgedu_repl 192.168.1.0/24 scram-sha-256
local replication fgedu_repl trust
# 重载配置
$ pg_ctl -D /postgresql/fgdata reload
server signaled
# 验证连接
$ psql -U fgedu_repl -d fgedudb -c “SELECT 1;”
?column?
———-
1
(1 row)
3.1.2 执行基础备份
$ pg_basebackup -h fgedu.localhost -p 5432 -U fgedu_repl -D /postgresql/backup/base/$(date +%Y%m%d) -Fp -Xs -P -R
# 执行过程输出
Password:
WARNING: aborting backup due to backend exiting
… 这里的输出会显示备份进度 …
# 查看备份结果
$ ls -la /postgresql/backup/base/20260407/
total 128
drwx—— 20 pgsql fgedudb 4096 Apr 7 02:00 .
drwxr-xr-x 3 pgsql fgedudb 4096 Apr 7 02:00 ..
-rw——- 1 pgsql fgedudb 3 Apr 7 02:00 PG_VERSION
drwx—— 5 pgsql fgedudb 4096 Apr 7 02:00 base
drwx—— 2 pgsql fgedudb 4096 Apr 7 02:00 global
drwx—— 2 pgsql fgedudb 4096 Apr 7 02:00 pg_commit_ts
drwx—— 2 pgsql fgedudb 4096 Apr 7 02:00 pg_dynshmem
-rw——- 1 pgsql fgedudb 4668 Apr 7 02:00 postgresql.auto.conf
drwx—— 2 pgsql fgedudb 4096 Apr 7 02:00 pg_logical
drwx—— 2 pgsql fgedudb 4096 Apr 7 02:00 pg_multixact
drwx—— 2 pgsql fgedudb 4096 Apr 7 02:00 pg_notify
drwx—— 2 pgsql fgedudb 4096 Apr 7 02:00 pg_replslot
drwx—— 2 pgsql fgedudb 4096 Apr 7 02:00 pg_serial
drwx—— 2 pgsql fgedudb 4096 Apr 7 02:00 pg_snapshots
drwx—— 2 pgsql fgedudb 4096 Apr 7 02:00 pg_stat
drwx—— 2 pgsql fgedudb 4096 Apr 7 02:00 pg_stat_tmp
drwx—— 2 pgsql fgedudb 4096 Apr 7 02:00 pg_subtrans
drwx—— 2 pgsql fgedudb 4096 Apr 7 02:00 pg_tblspc
drwx—— 2 pgsql fgedudb 4096 Apr 7 02:00 pg_twophase
drwx—— 2 pgsql fgedudb 4096 Apr 7 02:00 pg_wal
-rw——- 1 pgsql fgedudb 224 Apr 7 02:00 postgresql.auto.conf
-rw——- 1 pgsql fgedudb 0 Apr 7 02:00 standby.signal
# 查看备份大小
$ du -sh /postgresql/backup/base/20260407/
50G /postgresql/backup/base/20260407/
# tar格式备份(压缩)
$ pg_basebackup -h fgedu.localhost -p 5432 -U fgedu_repl -D /postgresql/backup/base -Ft -z -Z6 -Xs -P
# 执行过程输出
Password:
31234567/31234567 kB (100%), 2/2 tablespaces
# 查看备份文件
$ ls -lh /postgresql/backup/base/
total 10G
-rw——- 1 pgsql fgedudb 8.0G Apr 7 02:00 base.tar.gz
-rw——- 1 pgsql fgedudb 2.0G Apr 7 02:00 pg_wal.tar.gz
-rw——- 1 pgsql fgedudb 128 Apr 7 02:00 fgedutbs_data.tar.gz
# 备份进度显示
$ pg_basebackup -h fgedu.localhost -p 5432 -U fgedu_repl -D /postgresql/backup/base/$(date +%Y%m%d) -Fp -Xs -P –progress
# 输出示例
31234567/31234567 kB (100%), 2/2 tablespaces
3.1.3 备份验证
$ ls -la /postgresql/backup/base/20260407/PG_VERSION
-rw——- 1 pgsql fgedudb 3 Apr 7 02:00 /postgresql/backup/base/20260407/PG_VERSION
$ cat /postgresql/backup/base/20260407/PG_VERSION
18
# 检查备份信息文件
$ cat /postgresql/backup/base/20260407/backup_label
START WAL LOCATION: 0/2000028 (file 000000010000000000000002)
CHECKPOINT LOCATION: 0/2000060
BACKUP METHOD: streamed
BACKUP FROM: primary
START TIME: 2026-04-07 02:00:00 CST
LABEL: base backup
START TIMELINE: 1
# 检查standby.signal文件(-R参数生成)
$ ls -la /postgresql/backup/base/20260407/standby.signal
-rw——- 1 pgsql fgedudb 0 Apr 7 02:00 /postgresql/backup/base/20260407/standby.signal
# 检查恢复配置
$ cat /postgresql/backup/base/20260407/postgresql.auto.conf
primary_conninfo = ‘user=fgedu_repl password=xxxx host=fgedu.localhost port=5432 sslmode=prefer sslcompression=0 sslcertmode=allow sslsni=1 gssencmode=prefer krbsrvname=postgres gssdelegation=0 target_session_attrs=any load_balance_hosts=disable’
3.2 PostgreSQL WAL归档配置
3.2.1 配置WAL归档
$ vi /postgresql/fgdata/postgresql.conf
# WAL配置
wal_level = replica
archive_mode = on
archive_command = ‘cp %p /postgresql/archive/%f && echo “Archived %f” >> /postgresql/scripts/logs/archive.log’
max_wal_senders = 10
wal_keep_size = 1GB
# 重启数据库使配置生效
$ pg_ctl -D /postgresql/fgdata restart
waiting for server to shut down…. done
server stopped
waiting for server to start…. done
server started
# 创建归档目录
$ mkdir -p /postgresql/archive
$ chown pgsql:fgedudb /postgresql/archive
$ chmod 750 /postgresql/archive
# 验证归档配置
$ psql -U fgedu -d fgedudb -c “SHOW archive_mode;”
archive_mode
————–
on
(1 row)
$ psql -U fgedu -d fgedudb -c “SHOW archive_command;”
archive_command
——————————————————-
cp %p /postgresql/archive/%f && echo “Archived %f” >> /postgresql/scripts/logs/archive.log
(1 row)
# 手动触发WAL切换
$ psql -U fgedu -d fgedudb -c “SELECT pg_switch_wal();”
pg_switch_wal
—————
0/2000148
(1 row)
# 查看归档文件
$ ls -la /postgresql/archive/
total 16384
-rw——- 1 pgsql fgedudb 16777216 Apr 7 02:05 000000010000000000000001
-rw——- 1 pgsql fgedudb 16777216 Apr 7 02:10 000000010000000000000002
# 查看归档日志
$ cat /postgresql/scripts/logs/archive.log
Archived 000000010000000000000001
Archived 000000010000000000000002
3.2.2 WAL归档监控
$ psql -U fgedu -d fgedudb
# 查看归档统计信息
fgedudb=# SELECT * FROM pg_stat_archiver;
-[ RECORD 1 ]——+——————————
archived_count | 100
last_archived_wal | 000000010000000000000064
last_archived_time | 2026-04-07 02:00:00.123456+08
failed_count | 0
last_failed_wal |
last_failed_time |
stats_reset | 2026-01-01 00:00:00+08
# 查看当前WAL位置
fgedudb=# SELECT pg_current_wal_lsn();
pg_current_wal_lsn
——————–
0/65000128
(1 row)
# 查看WAL文件名
fgedudb=# SELECT pg_walfile_name(pg_current_wal_lsn());
pg_walfile_name
————————-
000000010000000000000065
(1 row)
# 查看WAL目录大小
fgedudb=# SELECT pg_size_pretty(sum(size)) FROM pg_ls_waldir();
pg_size_pretty
—————-
256 MB
(1 row)
# 查看归档延迟
fgedudb=# SELECT
archived_count,
failed_count,
last_archived_wal,
now() – last_archived_time as archive_delay
FROM pg_stat_archiver;
archived_count | failed_count | last_archived_wal | archive_delay
—————-+————–+——————–+—————
100 | 0 | 000000010000000000000064 | 00:05:00
(1 row)
# 监控WAL发送进程
fgedudb=# SELECT * FROM pg_stat_replication;
-[ RECORD 1 ]—-+——————————
pid | 12345
usesysid | 16384
usename | fgedu_repl
fgapplication_name | walreceiver
client_addr | 192.168.1.101
client_hostname |
client_port | 54321
backend_start | 2026-04-07 01:00:00.123456+08
backend_xmin |
state | streaming
sent_lsn | 0/65000128
write_lsn | 0/65000128
flush_lsn | 0/65000128
replay_lsn | 0/65000128
write_lag |
flush_lag |
replay_lag |
sync_priority | 0
sync_state | async
3.3 PostgreSQL时间点恢复实战
3.3.1 准备恢复环境
# 1. 停止数据库服务
$ pg_ctl -D /postgresql/fgdata stop
waiting for server to shut down…. done
server stopped
# 2. 备份当前数据目录(可选)
$ mv /postgresql/fgdata /postgresql/fgdata.bak
# 3. 恢复基础备份
$ cp -r /postgresql/backup/base/20260407 /postgresql/fgdata
$ chown -R pgsql:fgedudb /postgresql/fgdata
$ chmod 700 /postgresql/fgdata
# 4. 准备WAL归档文件
$ ls /postgresql/archive/ | head -10
000000010000000000000001
000000010000000000000002
000000010000000000000003
000000010000000000000004
000000010000000000000005
000000010000000000000006
000000010000000000000007
000000010000000000000008
000000010000000000000009
000000010000000000000010
3.3.2 配置时间点恢复
# 1. 创建恢复配置文件
$ vi /postgresql/fgdata/postgresql.auto.conf
# 恢复配置
restore_command = ‘cp /postgresql/archive/%f %p’
recovery_target_time = ‘2026-04-07 10:30:00’
recovery_target_action = ‘promote’
# 2. 创建恢复信号文件
$ touch /postgresql/fgdata/recovery.signal
# 3. 启动恢复
$ pg_ctl -D /postgresql/fgdata start
waiting for server to start…. done
server started
# 4. 查看恢复日志
$ tail -f /postgresql/fgdata/log/postgresql-*.log
2026-04-07 10:00:00.123 CST [12345] LOG: starting archive recovery
2026-04-07 10:00:00.456 CST [12345] LOG: restored log file “000000010000000000000001” from archive
2026-04-07 10:00:01.789 CST [12345] LOG: redo starts at 0/2000028
2026-04-07 10:00:02.012 CST [12345] LOG: consistent recovery state reached at 0/3000000
…
2026-04-07 10:05:00.345 CST [12345] LOG: recovery stopping before commit of transaction 12345, time 2026-04-07 10:30:00
2026-04-07 10:05:01.678 CST [12345] LOG: redo done at 0/65000000
2026-04-07 10:05:02.901 CST [12345] LOG: selected new timeline ID: 2
2026-04-07 10:05:03.234 CST [12345] LOG: archive recovery complete
2026-04-07 10:05:04.567 CST [12345] LOG: database system is ready to accept connections
# 5. 验证恢复结果
$ psql -U fgedu -d fgedudb -c “SELECT count(*) FROM fgedu_orders;”
count
——-
500000
(1 row)
# 6. 检查恢复时间点
$ psql -U fgedu -d fgedudb -c “SELECT pg_is_in_recovery();”
pg_is_in_recovery
——————-
f
(1 row)
Part04-生产案例与实战讲解
4.1 PostgreSQL自动物理备份案例
# pg_basebackup_auto.sh
# from:www.itpux.com.qq113257174.wx:itpux-com
# web: http://www.fgedu.net.cn
# PostgreSQL自动物理备份脚本
# 配置变量
PGHOME=/postgresql/fgapp
PGHOST=fgedu.localhost
PGPORT=5432
PGUSER=fgedu_repl
BACKUP_DIR=/postgresql/backup/base
ARCHIVE_DIR=/postgresql/archive
LOG_FILE=/postgresql/scripts/logs/pg_basebackup.log
RETENTION_WEEKS=4
log_message() {
echo “$(date ‘+%Y-%m-%d %H:%M:%S’) – $1” >> $LOG_FILE
}
# 检查磁盘空间
check_disk_space() {
local available=$(df -BG $BACKUP_DIR | tail -1 | awk ‘{print $4}’ | tr -d ‘G’)
local db_size=$($PGHOME/bin/psql -U fgedu -d fgedudb -t -c “SELECT pg_size_pretty(pg_database_size(‘fgedudb’));” | tr -d ‘ ‘)
if [ $available -lt 100 ]; then
log_message “ERROR: 磁盘空间不足,可用空间: ${available}GB”
return 1
fi
log_message “磁盘空间检查通过,可用空间: ${available}GB”
}
# 执行基础备份
do_base_backup() {
local backup_date=$(date +%Y%m%d)
local backup_path=$BACKUP_DIR/$backup_date
log_message “开始执行基础备份: $backup_path”
$PGHOME/bin/pg_basebackup \
-h $PGHOST \
-p $PGPORT \
-U $PGUSER \
-D $backup_path \
-Fp \
-Xs \
-P \
-R \
>> $LOG_FILE 2>&1
if [ $? -eq 0 ]; then
local size=$(du -sh $backup_path | awk ‘{print $1}’)
log_message “基础备份完成: $backup_path, 大小: $size”
# 记录备份信息
echo “$backup_date|$size|$(date ‘+%Y-%m-%d %H:%M:%S’)” >> $BACKUP_DIR/backup_history.log
else
log_message “ERROR: 基础备份失败”
return 1
fi
}
# 清理过期备份
cleanup_old_backups() {
log_message “开始清理过期备份…”
find $BACKUP_DIR -type d -name “20*” -mtime +$((RETENTION_WEEKS * 7)) -exec rm -rf {} \;
# 清理对应的WAL归档
local oldest_backup=$(ls -d $BACKUP_DIR/20* | head -1 | xargs basename)
if [ -n “$oldest_backup” ]; then
log_message “保留最早备份: $oldest_backup”
fi
log_message “过期备份清理完成”
}
# 发送告警
send_alert() {
local message=$1
/postgresql/scripts/alert/send_dingtalk.sh “$message”
}
# 主函数
main() {
log_message “==========================================”
log_message “开始执行PostgreSQL物理备份”
log_message “==========================================”
check_disk_space && \
do_base_backup && \
cleanup_old_backups
if [ $? -eq 0 ]; then
log_message “备份成功完成”
else
log_message “ERROR: 备份过程中出现错误”
send_alert “【告警】PostgreSQL物理备份失败,请检查日志”
fi
log_message “==========================================”
log_message “备份任务结束”
log_message “==========================================”
}
main
4.2 PostgreSQL备库搭建案例
# 场景:搭建流复制备库
# 1. 主库配置
$ vi /postgresql/fgdata/postgresql.conf
# 主库参数配置
listen_addresses = ‘*’
wal_level = replica
max_wal_senders = 10
wal_keep_size = 1GB
hot_standby = on
# 2. 主库pg_hba.conf配置
$ vi /postgresql/fgdata/pg_hba.conf
host replication fgedu_repl 192.168.1.0/24 scram-sha-256
# 3. 重启主库
$ pg_ctl -D /postgresql/fgdata restart
# 4. 在备库服务器执行基础备份
$ pg_basebackup -h 192.168.1.100 -p 5432 -U fgedu_repl -D /postgresql/fgdata -Fp -Xs -P -R
# 执行过程输出
Password:
31234567/31234567 kB (100%), 2/2 tablespaces
# 5. 查看生成的配置
$ cat /postgresql/fgdata/postgresql.auto.conf
primary_conninfo = ‘user=fgedu_repl password=xxxx host=192.168.1.100 port=5432 sslmode=prefer’
# 6. 启动备库
$ pg_ctl -D /postgresql/fgdata start
waiting for server to start…. done
server started
# 7. 验证备库状态
$ psql -U fgedu -d fgedudb -c “SELECT pg_is_in_recovery();”
pg_is_in_recovery
——————-
t
(1 row)
# 8. 主库查看复制状态
$ psql -U fgedu -d fgedudb -c “SELECT * FROM pg_stat_replication;”
-[ RECORD 1 ]—-+——————————
pid | 12345
usename | fgedu_repl
fgapplication_name | walreceiver
client_addr | 192.168.1.101
state | streaming
sent_lsn | 0/65000128
write_lsn | 0/65000128
flush_lsn | 0/65000128
replay_lsn | 0/65000128
sync_state | async
# 9. 备库查看接收状态
$ psql -U fgedu -d fgedudb -c “SELECT * FROM pg_stat_wal_receiver;”
-[ RECORD 1 ]—-+——————————
status | streaming
received_lsn | 0/65000128
latest_end_lsn | 0/65000128
4.3 PostgreSQL灾难恢复案例
# 场景:主库数据文件损坏,需要从备份恢复
# 1. 确认故障
$ pg_ctl -D /postgresql/fgdata status
pg_ctl: server is not running
$ tail -50 /postgresql/fgdata/log/postgresql-*.log
2026-04-07 10:00:00.123 CST [12345] PANIC: could not read file “base/16384/12345”: Input/output error
# 2. 确认备份可用
$ ls -la /postgresql/backup/base/
drwx—— 20 pgsql fgedudb 4096 Apr 7 02:00 20260407
# 3. 确认WAL归档可用
$ ls /postgresql/archive/ | wc -l
100
# 4. 停止数据库(如果还在运行)
$ pg_ctl -D /postgresql/fgdata stop -m immediate
# 5. 备份损坏的数据目录
$ mv /postgresql/fgdata /postgresql/fgdata.corrupt
# 6. 恢复基础备份
$ cp -r /postgresql/backup/base/20260407 /postgresql/fgdata
$ chown -R pgsql:fgedudb /postgresql/fgdata
$ chmod 700 /postgresql/fgdata
# 7. 配置恢复参数
$ vi /postgresql/fgdata/postgresql.auto.conf
restore_command = ‘cp /postgresql/archive/%f %p’
recovery_target_action = ‘promote’
# 8. 创建恢复信号
$ touch /postgresql/fgdata/recovery.signal
# 9. 启动恢复
$ pg_ctl -D /postgresql/fgdata start
waiting for server to start…. done
server started
# 10. 监控恢复进度
$ tail -f /postgresql/fgdata/log/postgresql-*.log
2026-04-07 10:30:00.123 CST [12345] LOG: starting archive recovery
2026-04-07 10:30:00.456 CST [12345] LOG: restored log file “000000010000000000000001” from archive
…
2026-04-07 10:35:00.789 CST [12345] LOG: archive recovery complete
2026-04-07 10:35:01.012 CST [12345] LOG: database system is ready to accept connections
# 11. 验证数据完整性
$ psql -U fgedu -d fgedudb -c “SELECT count(*) FROM fgedu_orders;”
count
——-
500000
(1 row)
$ psql -U fgedu -d fgedudb -c “SELECT count(*) FROM fgedu_customers;”
count
——-
100000
(1 row)
# 12. 检查数据库状态
$ psql -U fgedu -d fgedudb -c “SELECT pg_is_in_recovery();”
pg_is_in_recovery
——————-
f
(1 row)
# 13. 执行数据库检查
$ psql -U fgedu -d fgedudb -c “SELECT * FROM pg_database_size(‘fgedudb’);”
pg_database_size
——————
53687091200
# 14. 记录恢复过程
$ echo “$(date ‘+%Y-%m-%d %H:%M:%S’) – 灾难恢复完成” >> /postgresql/scripts/logs/disaster_recovery.log
Part05-风哥经验总结与分享
5.1 PostgreSQL物理备份最佳实践
PostgreSQL物理备份最佳实践:
- 配置WAL归档:确保WAL持续归档
- 定期基础备份:每周至少一次基础备份
- 备份验证:定期验证备份可用性
- 恢复演练:每月进行恢复演练
- 异地存储:备份文件存储到异地
- 监控告警:监控备份和归档状态
5.2 PostgreSQL物理备份脚本
/postgresql/scripts/backup/
├── pg_basebackup_auto.sh # 自动基础备份
├── wal_archive_check.sh # WAL归档检查
├── pitr_restore.sh # 时间点恢复
├── standby_build.sh # 备库搭建
├── disaster_recovery.sh # 灾难恢复
└── conf/
└── backup.conf # 备份配置
5.3 PostgreSQL物理备份检查清单
# 备份前检查
– [ ] 确认WAL归档正常
– [ ] 确认磁盘空间充足
– [ ] 确认复制用户权限
– [ ] 确认网络连接正常
# 备份中监控
– [ ] 监控备份进度
– [ ] 监控磁盘空间
– [ ] 监控系统资源
– [ ] 记录备份日志
# 备份后验证
– [ ] 验证备份文件完整性
– [ ] 验证备份文件大小
– [ ] 验证恢复配置正确
– [ ] 记录备份信息
# 定期检查
– [ ] 检查WAL归档延迟
– [ ] 检查备份保留策略
– [ ] 检查恢复演练结果
– [ ] 检查备份文档更新
本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html
