PostgreSQL自带的命令行工具10- pg_basebackup

PostgreSQL自带的命令行工具10- pg_basebackup```bash

基础信息
OS版本:Red Hat Enterprise Linux Server release 7.9 (Maipo)
DB版本:16.2
pg软件目录:/home/pg16/soft
pg数据目录:/home/pg16/data
端口:5777

pg_basebackup 是 PostgreSQL 中用于创建服务器的基本备份(即数据目录的完整备份)的命令行工具。它通过复制数据库集群的文件系统级数据来创建备份,包括数据库文件、配置文件和事务日志。pg_basebackup 提供了一种简单且安全的方式来创建数据库的备份,这对于灾难恢复和主从复制设置非常有用。

通过help查看帮助文档。

[pg16@test backup]$ 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, --target=TARGET[:DETAIL]
                         backup target (if other than client)
  -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=[{client|server}-]METHOD[:DETAIL]
                         compress on client or server as specified
  -Z, --compress=none    do not compress tar output

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/>

基本用法

pg_basebackup [选项]...

常用选项

  • -D, --pgdata=目录:指定备份的目标目录,备份数据将存储在这里。
  • -F, --format=p|t:指定输出格式。p 表示纯文件系统(默认),t 表示 tar 格式。
  • -X, --wal-method=none|fetch|stream:指定事务日志(WAL)的包含方式。none 不包括 WAL 文件,fetch 在备份结束时获取 WAL 文件,stream 在进行备份的同时流式传输 WAL 文件。
  • -Z, --gzip:当使用 tar 格式时,开启这个选项将压缩输出文件。
  • -P, --progress:显示备份过程中的进度信息。
  • -v, --verbose:显示详细的输出信息。
  • -h, --host=主机名:数据库服务器的主机名或 IP 地址。
  • -p, --port=端口号:数据库服务器的端口。
  • -U, --username=用户名:连接数据库服务器的用户名。
  • -W, --password:提示输入连接数据库的密码。

示例

创建名为 /home/pg16/backup/test1 的目录里的数据库备份:

--执行备份
[pg16@test test1]$ pg_basebackup -D /home/pg16/backup/test1 -Fp -X stream -P -v
pg_basebackup: initiating base backup, waiting for checkpoint to complete
pg_basebackup: checkpoint completed
pg_basebackup: write-ahead log start point: 0/24000028 on timeline 1
pg_basebackup: starting background WAL receiver
pg_basebackup: created temporary replication slot "pg_basebackup_124716"
48265/48265 kB (100%), 2/2 tablespaces                                         
pg_basebackup: write-ahead log end point: 0/24000100
pg_basebackup: waiting for background process to finish streaming ...
pg_basebackup: syncing data to disk ...
pg_basebackup: renaming backup_manifest.tmp to backup_manifest
pg_basebackup: base backup completed

--确认
[pg16@test test1]$ pwd
/home/pg16/backup/test1
[pg16@test test1]$ ll
total 340
-rw------- 1 pg16 pg16    227 May  5 02:17 backup_label
-rw------- 1 pg16 pg16 274962 May  5 02:17 backup_manifest
drwx------ 8 pg16 pg16     72 May  5 02:17 base
-rw------- 1 pg16 pg16     44 May  5 02:17 current_logfiles
drwx------ 2 pg16 pg16   4096 May  5 02:17 global
drwx------ 2 pg16 pg16   4096 May  5 02:17 log
drwx------ 2 pg16 pg16      6 May  5 02:17 pg_commit_ts
drwx------ 2 pg16 pg16      6 May  5 02:17 pg_dynshmem
-rw------- 1 pg16 pg16   5740 May  5 02:17 pg_hba.conf
-rw------- 1 pg16 pg16   2640 May  5 02:17 pg_ident.conf
drwx------ 4 pg16 pg16     68 May  5 02:17 pg_logical
drwx------ 4 pg16 pg16     36 May  5 02:17 pg_multixact
drwx------ 2 pg16 pg16      6 May  5 02:17 pg_notify
drwx------ 2 pg16 pg16      6 May  5 02:17 pg_replslot
drwx------ 2 pg16 pg16      6 May  5 02:17 pg_serial
drwx------ 2 pg16 pg16      6 May  5 02:17 pg_snapshots
drwx------ 2 pg16 pg16      6 May  5 02:17 pg_stat
drwx------ 2 pg16 pg16      6 May  5 02:17 pg_stat_tmp
drwx------ 2 pg16 pg16      6 May  5 02:17 pg_subtrans
drwx------ 2 pg16 pg16     19 May  5 02:17 pg_tblspc
drwx------ 2 pg16 pg16      6 May  5 02:17 pg_twophase
-rw------- 1 pg16 pg16      3 May  5 02:17 PG_VERSION
drwx------ 3 pg16 pg16     60 May  5 02:17 pg_wal
drwx------ 2 pg16 pg16     18 May  5 02:17 pg_xact
-rw------- 1 pg16 pg16    136 May  5 02:17 postgresql.auto.conf
-rw------- 1 pg16 pg16  29928 May  5 02:17 postgresql.conf

这个命令创建一个文件系统格式的备份,同时流式传输 WAL 文件以确保备份的一致性,展示备份进度,并提供详细输出。

注意事项

  • 在执行 pg_basebackup 前,确保目标备份目录存在且 PostgreSQL 用户有足够的权限写入该目录。
  • 备份期间,源数据库可以继续正常服务,pg_basebackup 命令实现了热备份。
  • WAL 文件的包含对于备份的恢复是非常重要的。如果选择 stream 方式,可以确保在恢复时不会丢失任何数据。
  • 考虑到安全和资源使用,合理选择备份方式和时间对生产环境尤其重要。

pg_basebackup 提供了一个直接且高效的方式来创建 PostgreSQL 数据库的全备份。对于数据库的灾难恢复规划以及主从复制架构的初始化,这是一个非常实用的工具。

谨记:心存敬畏,行有所止。

相关推荐

  1. PostgreSQL命令工具10- pg_basebackup

    2024-05-12 16:50:03       11 阅读
  2. PostgreSQL命令工具02- createdb

    2024-05-12 16:50:03       10 阅读
  3. PostgreSQL命令工具06- pg_isready

    2024-05-12 16:50:03       12 阅读
  4. PostgreSQL命令工具08- pg_restore

    2024-05-12 16:50:03       15 阅读
  5. PostgreSQL命令工具07- pg_dump

    2024-05-12 16:50:03       9 阅读
  6. PostgreSQL命令工具21- initdb

    2024-05-12 16:50:03       8 阅读
  7. PostgreSQL命令工具14- pg_test_timing

    2024-05-12 16:50:03       11 阅读
  8. PostgreSQL工具介绍

    2024-05-12 16:50:03       9 阅读
  9. postgresql指令命令系列一

    2024-05-12 16:50:03       25 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-05-12 16:50:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-05-12 16:50:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-05-12 16:50:03       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-05-12 16:50:03       20 阅读

热门阅读

  1. Linux上的监控工具:Zabbix、Prometheus、APM和ELK

    2024-05-12 16:50:03       6 阅读
  2. SSL VPN

    SSL VPN

    2024-05-12 16:50:03      10 阅读
  3. 2024.5.12 ubuntu + latex + vscode

    2024-05-12 16:50:03       10 阅读
  4. springboot Redis 支持星号(*) 包括注解@Cache

    2024-05-12 16:50:03       8 阅读
  5. Oracle一键安装脚本安装教程合集

    2024-05-12 16:50:03       10 阅读
  6. SSH简介:网络安全的守护者

    2024-05-12 16:50:03       11 阅读
  7. 对Promise的理解

    2024-05-12 16:50:03       12 阅读
  8. 【C++】引用传递 & 常量引用

    2024-05-12 16:50:03       10 阅读