postgresql 物理备份 pg_basebackup

os:centos 7.4
postgresql:10.4

pg_basebackup 可以用来做数据库的备份,也可以用来搭建slaver。

pg_basebackup –help

$ which pg_basebackup
/usr/pgsql-10/bin/pg_basebackup

$ 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 recovery.conf for replication
  -S, --slot=SLOTNAME    replication slot to use
      --no-slot          prevent creation of temporary replication slot
  -T, --tablespace-mapping=OLDDIR=NEWDIR
                         relocate tablespace in OLDDIR to NEWDIR
  -X, --wal-method=none|fetch|stream
                         include required WAL files with specified method
      --waldir=WALDIR    location for the write-ahead log directory
  -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
  -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
  -v, --verbose          output verbose messages
  -V, --version          output version information, then exit
  -?, --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@postgresql.org>.

基础备份

$ pg_basebackup -h localhost -D /tmp/pgsql_backup/ -Ft -R -z -v --checkpoint=fast
pg_basebackup: initiating base backup, waiting for checkpoint to complete
pg_basebackup: checkpoint completed
pg_basebackup: write-ahead log start point: 4/64000060 on timeline 1
pg_basebackup: starting background WAL receiver
pg_basebackup: write-ahead log end point: 4/64000168
pg_basebackup: waiting for background process to finish streaming ...
pg_basebackup: base backup completed
 
 
$ ls -l /tmp/pgsql_backup/
total 490436
-rw-rw-r-- 1 postgres postgres 502112605 Aug 26 11:36 base.tar.gz
-rw------- 1 postgres postgres     88541 Aug 26 11:36 pg_wal.tar.gz


postgresql log 输出

2018-08-26 11:34:54.121 CST,"postgres","",3032,"localhost:56964",5b821fdd.bd8,2,"authentication",2018-08-26 11:34:53 CST,4/117,0,LOG,00000,"replication connection authorized: user=postgres",,,,,,,,,""
2018-08-26 11:34:54.122 CST,"postgres","",3032,"localhost:56964",5b821fdd.bd8,3,"idle",2018-08-26 11:34:53 CST,4/0,0,LOG,00000,"received replication command: IDENTIFY_SYSTEM",,,,,,,,,"pg_basebackup"
2018-08-26 11:34:54.122 CST,"postgres","",3032,"localhost:56964",5b821fdd.bd8,4,"idle",2018-08-26 11:34:53 CST,4/0,0,LOG,00000,"received replication command: BASE_BACKUP LABEL 'pg_basebackup base backup'   FAST NOWAIT  TABLESPACE_MAP",,,,,,,,,"pg_basebackup"
2018-08-26 11:34:54.686 CST,,,1678,,5b821001.68e,1,,2018-08-26 10:27:13 CST,,0,LOG,00000,"checkpoint starting: immediate force wait",,,,,,,,,""
2018-08-26 11:34:54.704 CST,,,1678,,5b821001.68e,2,,2018-08-26 10:27:13 CST,,0,LOG,00000,"checkpoint complete: wrote 0 buffers (0.0%); 0 WAL file(s) added, 1 removed, 0 recycled; write=0.000 s, sync=0.000 s, total=0.017 s; sync files=0, longest=0.000 s, average=0.000 s; distance=37236 kB, estimate=37236 kB",,,,,,,,,""
2018-08-26 11:34:54.788 CST,,,3033,"localhost:56966",5b821fde.bd9,1,"",2018-08-26 11:34:54 CST,,0,LOG,00000,"connection received: host=localhost port=56966",,,,,,,,,""
2018-08-26 11:34:54.789 CST,"postgres","",3033,"localhost:56966",5b821fde.bd9,2,"authentication",2018-08-26 11:34:54 CST,5/1,0,LOG,00000,"replication connection authorized: user=postgres",,,,,,,,,""
2018-08-26 11:34:54.790 CST,"postgres","",3033,"localhost:56966",5b821fde.bd9,3,"idle",2018-08-26 11:34:54 CST,5/0,0,LOG,00000,"received replication command: IDENTIFY_SYSTEM",,,,,,,,,"pg_basebackup"
2018-08-26 11:34:54.791 CST,"postgres","",3033,"localhost:56966",5b821fde.bd9,4,"idle",2018-08-26 11:34:54 CST,5/0,0,LOG,00000,"received replication command: CREATE_REPLICATION_SLOT ""pg_basebackup_3033"" TEMPORARY PHYSICAL RESERVE_WAL",,,,,,,,,"pg_basebackup"
2018-08-26 11:34:55.016 CST,"postgres","",3033,"localhost:56966",5b821fde.bd9,5,"idle",2018-08-26 11:34:54 CST,5/0,0,LOG,00000,"received replication command: START_REPLICATION SLOT ""pg_basebackup_3033"" 4/64000000 TIMELINE 1",,,,,,,,,"pg_basebackup"
2018-08-26 11:36:16.713 CST,"postgres","",3032,"localhost:56964",5b821fdd.bd8,5,"idle",2018-08-26 11:34:53 CST,,0,LOG,00000,"disconnection: session time: 0:01:22.951 user=postgres database= host=localhost port=56964",,,,,,,,,"pg_basebackup"
2018-08-26 11:36:16.716 CST,"postgres","",3033,"localhost:56966",5b821fde.bd9,6,"idle",2018-08-26 11:34:54 CST,,0,LOG,00000,"disconnection: session time: 0:01:21.928 user=postgres database= host=localhost port=56966",,,,,,,,,"pg_basebackup"


可以看到首先做了 checkpoint,然后是 replication。

从备份出的文件 base.tar.gz、pg_wal.tar.gz 来看,是先做了 checkpoint,然后再备份了之后的 wal。

搭建slave

参考另外一篇blog
https://blog.csdn.net/ctypyb2002/article/details/81668935

参考:
http://postgres.cn/docs/10/continuous-archiving.html
http://postgres.cn/docs/10/app-pgbasebackup.html

    原文作者:PostgreSQL
    原文地址: https://www.cnblogs.com/ctypyb2002/p/9792894.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞