pg 归档与恢复

触发 Archiving 条件:

  • 单个WAL文件满 (max_wal_size)
  • WAL文件组可用容量不足 (min_wal_size)
  • checkpoint 操作
    1. 手工checkpoint,连接pg服务执行 checkpoint
    2. 自动checkpoint (checkpoint_timeout 参数)

创建存储归档目录

sudo mkdir /data/pg_archive
sudo chown postgres. /data/pg_archive
sudo chmod 700 /data/pg_archive 

设置归档参数

su - postgres

cat <<'EOF' | tee -a $PGDATA/postgresql.conf > /dev/null
# 归档相关配置
archive_mode = on # 开启归档功能
archive_command = 'cp %p /data/pg_archive/%f' # 归档执行的命令
# 归档恢复相关配置
restore_command = 'cp /data/pg_archive/%f %p' # 恢复时执行的问题
recovery_target_timeline = 'latest' # 指定在进行 PITR 恢复时应使用的时间线
archive_cleanup_command = 'pg_archivecleanup /data/pg_archive %r' # 备用服务器中定期清理或删除已经被归档的 WAL 日志文件,以释放存储空间

EOF

重启服务

pg_ctl restart

查看日志

cd $PGDATA/logs/
tail -f postgresql-`date "+%Y-%m-%d"`.log

验证

  1. 配置参数是否生效

    $ psql 
    Password for user postgres: 
    psql (14.10)
    Type "help" for help.
    
    postgres=# select name,setting from pg_settings where name in ('archive_mode','archive_command','archive_timeout','restore_command','recovery_target_timeline','archive_cleanup_command');
        name       |          setting          
    -----------------+---------------------------
    archive_command | cp %p /data/pg_archive/%f
    archive_mode    | on
    archive_timeout | 0
    (3 rows)
    
  2. 手工触发checkpoint

    # select pg_switch_wal();
    pg_switch_wal 
    ---------------
    0/30000F0
    (1 row)
    
  3. 查看归档目录是否有文件

    $ ls /data/pg_archive/
    000000010000000000000001  000000010000000000000002  000000010000000000000003
    
posted @   jiaxzeng  阅读(81)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示