使用 pgBackRest 还原到某个时间点(PITR,--type=time)
PostgreSQL备份与 PITR命令
sudo systemctl stop postgresql-16
sudo -u postgres pgbackrest --stanza=<stanza> --delta --type=time "--target=2026-10-08 14:30:00+03" --target-action=pause restore
sudo systemctl start postgresql-16
# Recovery stops at the target (read-only); validate the data:
psql -X -c "SELECT pg_is_in_recovery(), pg_last_xact_replay_timestamp();"
# If correct, end recovery and open read-write on a new timeline:
psql -X -c "SELECT pg_wal_replay_resume();"作用
还原一个在目标时间之前完成的备份,并只回放 WAL 到目标点;target-action=pause 会停在目标点以便验证数据,pg_wal_replay_resume() 执行提升。
适用场景
用于回到误操作 DELETE/DROP/UPDATE 等逻辑错误发生之前的那一刻。
注意
该集群上目标点之后的所有变更都会丢失;尽可能在单独的服务器上运行 PITR,并用 pg_dump 只将受影响的数据迁回。在目标中明确写出时区。pgBackRest 2.24+ 会自动选择目标点之前的合适备份;在更早的版本中,请用 --set 指定一个在目标点之前完成的备份。如果目标早于备份的结束时间,还原无法达到一致状态。