当前位置: 首页 > 工具软件 > pg_probackup > 使用案例 >

pg_basebackup初识

申屠晟
2023-12-01

什么是pg_basebackup?

1.pg_basebackup是pg自带的备份工具,它用来做pg的基础备份。pg_basebackup可以用作PITR,也可以用来构造log-shipping standby和stream standby
2.pg_basebackup总是备份整个pg,不能做单独的database备份。如果要单独备份某个database或者schema,应该用pg_dump。
3.pg_basebackup会备份除了临时文件的所有文件,包括data目录下的所有数据文件,配置文件等等

使用pg_basebackup的前提

1.连接数据库的用户必须有login和replication权限。replication是pg自带的用来initiate replication的role。
create role lzl login replication encrypted password 'lzl';
2.pg_hba.conf必须允许登陆
3.max_wal_senders不能设置的过低,应该讲sender的数量设置为比从库数稍大一些的值。pg9.6及以下默认值是0,pg10及以上默认值是10。

pg_basebackup参数

Options controlling the output:
  -D, --pgdata=DIRECTORY receive base backup into directory    --备份存放地址
  -F, --format=p|t       output format (plain (default), tar)  --存放方式,普通(p默认,也就是目录文件的方式存放)或t-tar包
  -r, --max-rate=RATE    maximum transfer rate to transfer data directory   --传输最大速率,可以控制带宽和IO
                         (in kB/s, or use suffix "k" or "M")
  -R, --write-recovery-conf                                        --写recovery.conf
                         write recovery.conf for replication
  -S, --slot=SLOTNAME    replication slot to use                   --只能跟-x一起使用,指定slot可以保证,在backup和replication间,源库不会把需要的WAL日志给删除
  -T, --tablespace-mapping=OLDDIR=NEWDIR                           --重新定位表空间的位置
                         relocate tablespace in OLDDIR to NEWDIR
  -x, --xlog             include required WAL files in backup (fetch mode)  --相当于-X fetch
  -X, --xlog-method=fetch|stream                                 --在备份过程中,pg_baseback会备份那些必要的WAL文件,也可以--xlogdir=XLOGDIR指定备份WAL文件的目录
                         include required WAL files with specified method   --fetch表示备份结束后收集WAL文件,如果备份很长的话,需要注意wal_keep_segments参数的使用
      --xlogdir=XLOGDIR  location for the transaction log directory --stream方式收集WAL日志,额外开一个源库的连接,也就是pg_baseback用到2个sender进程所以要注意max_wal_senders参数的使用
  -z, --gzip             compress tar output                    --在tar模式下,可以使用gzip压缩
  -Z, --compress=0-9     compress tar output with given compression level  --压缩等级0-9,0表示不压缩,9表示最大压缩

General options:
  -c, --checkpoint=fast|spread                                    --pg_basebackup会发起一次checkpoint,可以选择checkpoint的模式,fast或者spread。如果没有指定fast,备份任务可能需要一段时间等待checkpoint
                         set fast or spread checkpointing
  -l, --label=LABEL      set backup label                         --此次备份的标签
  -P, --progress         show progress information                --在备份开始前对整个数据库进行一次计算
  -v, --verbose          output verbose messages                --输出更详细的信息
  -V, --version          output version information, then exit  --输出版本
  -?, --help             show this help, then exit                --help

Connection options:                                                --连接到数据的一些参数,跟psql用法差不多
  -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)

示例

--备份数据库并打包压缩
[pg@lzl ~]$ pg_basebackup -h 172.17.100.150 -p 5433  -U lzl  -D /tmp/bk -Ft -z -P 
22840/22840 kB (100%), 1/1 tablespace

--普通备份,类似拷贝的结果
[pg@lzl ~]$ pg_basebackup -h 172.17.100.150 -p 5433  -U lzl  -D /tmp/bk1 -P
22840/22840 kB (100%), 1/1 tablespace

参考文档:

https://www.postgresql.org/docs/9.6/app-pgbasebackup.html

 类似资料: