PG高可用之pg_basebackup

1.pg_basebackup简介

pg_basebackup是PostgreSQL提供的基础备份工具,属于客户端工具。它在不影响数据库的其他客户端的情况下进行,它可以用于时间点恢复和作为日志传送或流复制备用服务器的起点 。

优点:

  • 可以远程备份
  • 可以在备库使用进行备份
  • 操作简介,易操作

限制:

  • 只能全库备份,不能细粒度的备份
  • 不能备份归档日志
  • 必须在pg_hba.conf配置允许连接,非超级用户需要赋予replication权限
  • 需要配置max_wal_senders参数

2.pg_basebackup工作原理

  • 生成检查点,开启full_page_write,创建backup_label
  • 通过Stream协议连接数据库,wal sender进程想pg_basebackup发送物理文件
  • pg_basebackup接收到文件后写入到目标位置

3.pg_basebackup使用方法

[pg14@test bin]$ pg_basebackup --help
pg_basebackup takes a base backup of a running PostgreSQL server.

Usage:
  pg_basebackup [OPTION]...

Options controlling the output:
  -D, --pgdata=DIRECTORY receive base backup into directory
  -F, --format=p|t       output format (plain (default), tar)
  -r, --max-rate=RATE    maximum transfer rate to transfer data directory
                         (in kB/s, or use suffix "k" or "M")
  -R, --write-recovery-conf
                         write configuration for replication
  -T, --tablespace-mapping=OLDDIR=NEWDIR
                         relocate tablespace in OLDDIR to NEWDIR
      --waldir=WALDIR    location for the write-ahead log directory
  -X, --wal-method=none|fetch|stream
                         include required WAL files with specified method
  -z, --gzip             compress tar output
  -Z, --compress=0-9     compress tar output with given compression level

General options:
  -c, --checkpoint=fast|spread
                         set fast or spread checkpointing
  -C, --create-slot      create replication slot
  -l, --label=LABEL      set backup label
  -n, --no-clean         do not clean up after errors
  -N, --no-sync          do not wait for changes to be written safely to disk
  -P, --progress         show progress information
  -S, --slot=SLOTNAME    replication slot to use
  -v, --verbose          output verbose messages
  -V, --version          output version information, then exit
      --manifest-checksums=SHA{224,256,384,512}|CRC32C|NONE
                         use algorithm for manifest checksums
      --manifest-force-encode
                         hex encode all file names in manifest
      --no-estimate-size do not estimate backup size in server side
      --no-manifest      suppress generation of backup manifest
      --no-slot          prevent creation of temporary replication slot
      --no-verify-checksums
                         do not verify checksums
  -?, --help             show this help, then exit

Connection options:
  -d, --dbname=CONNSTR   connection string
  -h, --host=HOSTNAME    database server host or socket directory
  -p, --port=PORT        database server port number
  -s, --status-interval=INTERVAL
                         time between status packets sent to server (in seconds)
  -U, --username=NAME    connect as specified database user
  -w, --no-password      never prompt for password
  -W, --password         force password prompt (should happen automatically)

Report bugs to <pgsql-bugs@lists.postgresql.org>.
PostgreSQL home page: <https://www.postgresql.org/>

选项解释

  • -D | --pgdata :设置备份目录,如果目录不存在,则自动创建。必须配置选项
  • -F | --format=p|t 输出格式(plain(默认),tar)
  • -r | --max-rate=RATE 传输数据目录的最大传输速率(以 kB/s 为单位,或使用后缀“k”或“M”)
  • -R | --write-recovery-conf 用于复制的写入配置
  • -T | --tablespace-mapping=OLDDIR=NEWDIR 将 OLDDIR 中的表空间重定位到 NEWDIR
  • –waldir=WALDIR 预写日志目录的位置
  • -X | --wal-method=none|fetch|stream 包含指定方法所需的 WAL 文件
  • -z | --gzip 压缩 tar 输出
  • -Z | --compress=0-9 使用给定的压缩级别压缩 tar 输出

常规选项:

  • -c | --checkpoint=fast|spread 设置快速或扩展检查点
  • -C | --create-slot 创建复制槽
  • -l | --label=LABEL 设置备份标签
  • -n | --no-clean 出错后不清理
  • -N | --no-sync 不等待更改安全写入磁盘
  • -P | --progress 显示进度信息
  • -S | --slot=SLOTNAME 要使用的复制槽
  • -v | --verbose 输出详细信息
  • -V | --version 输出版本信息,然后退出
  • –no-slot 防止创建临时复制槽
  • –no-verify-checksums 不验证校验和
  • -?, --help 显示此帮助,然后退出

连接选项:

  • -d | --dbname=CONNSTR 连接字符串
  • -h | --host=HOSTNAME 数据库服务器主机或套接字目录
  • -p | --port=PORT 数据库服务器端口号
  • -s | --status-interval=状态包发送到服务器的间隔时间(以秒为单位)
  • -U | --username=NAME 以指定的数据库用户连接
  • -w | --no-password 从不提示输入密码
  • -W | --password 强制密码提示

4.pg_basebackup备份实验

#开启归档

#创建备份用户
postgres=# create user user_bak replication login connection limit 32 password '123456';
CREATE ROLE

#修改pg_hba.conf
添加:
host    replication     user_bak        192.168.18.0/24         password
[pg14@test pgdata]$  pg_ctl reload -D $PGDATA -l /tmp/logfile
server signaled

#创建备份路径
mkdir -p /data/pgdata2/bak

#开始备份
[pg14@test bak]$ pg_basebackup -F t -X s -D /data/pgdata2/bak -h 192.168.18.11 -p 1525 -U user_bak
Password: 
[pg14@test bak]$ ll
total 199704
-rw------- 1 pg14 pg14    138467 Mar 17 09:25 backup_manifest
-rw------- 1 pg14 pg14 187575808 Mar 17 09:25 base.tar
-rw------- 1 pg14 pg14  16778752 Mar 17 09:25 pg_wal.tar

  • 16
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

南風_入弦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值