pg_probackup
[1] https://postgrespro.com/docs/enterprise/13/app-pgprobackup
- PITR依赖continuous WAL archiving:
Making backups in PAGE backup mode, performing PITR and making backups with ARCHIVE WAL delivery mode require continuous WAL archiving to be enabled.
You can use both STREAM and ARCHIVE backups for point in time recovery as long as the WAL archive is available at least starting from the time the backup was taken.
- PTRACK增量备份模式,及incremental restore,要求initdb时
--data-checksums
. - todo stream,archive有什么区别
初始化
pg_probackup init -B $HOME/probackup
pg_probackup add-instance -B $HOME/probackup -D $HOME/pgdata2 --instance pg5433
archive_mode = on
archive_command = '"$HOME/.local/pgsql/bin/pg_probackup" archive-push -B "$HOME/probackup" --instance pg5433 --wal-file-name=%f'
# pg_ctl start
全量,及PAGE增量备份
pg_probackup backup -B $HOME/probackup --instance pg5433 -b FULL -p 5433 -d postgres
pg_probackup backup -B $HOME/probackup --instance pg5433 -b PAGE -p 5433 -d postgres
pg_probackup backup -B $HOME/probackup --instance pg5433 -b PAGE -p 5433 -d postgres --log-level-file=info
# --log-level-file默认off,打开后默认记录在-B/log
pg_probackup show -B $HOME/probackup
BACKUP INSTANCE 'pg5433'
=================================================================================================================================
Instance Version ID Recovery Time Mode WAL Mode TLI Time Data WAL Zratio Start LSN Stop LSN Status
=================================================================================================================================
pg5433 13 R2RP9H 2021-11-18 20:27:17+08 PAGE ARCHIVE 1/1 1s 106kB 16MB 1.00 0/8000028 0/90000B8 OK
pg5433 13 R2RP38 2021-11-18 20:23:32+08 PAGE ARCHIVE 1/1 1s 106kB 16MB 1.00 0/5000028 0/60000F0 OK
pg5433 13 R2RP1U 2021-11-18 20:22:44+08 FULL ARCHIVE 1/0 6s 23MB 16MB 1.00 0/2000028 0/30000F0 OK
pg_probackup show -B $HOME/probackup --instance pg5433
pg_probackup show -B $HOME/probackup --archive
删除不需要的备份数据
pg_probackup delete -B $HOME/probackup --instance pg5433 --delete-expired
pg_probackup delete -B $HOME/probackup --instance pg5433 --delete-wal
pg_probackup delete -B $HOME/probackup --instance pg5433 --status=ERROR
恢复
pg_probackup validate -B $HOME/probackup --instance pg5433
pg_probackup restore -B $HOME/probackup --instance pg5433 -D $HOME/pgdata2-restored
pg_probackup validate -B $HOME/probackup --instance pg5433 --recovery-target-time="2021-11-18 20:15:32+08"
# => ERROR: Backup satisfying target options is not found.
pg_probackup validate -B $HOME/probackup --instance pg5433 --recovery-target-time="2021-11-18 20:23:32+08"
pg_probackup validate -B $HOME/probackup --instance pg5433 --recovery-target-time="2021-11-18 20:25:32+08"
# => INFO: Backup validation completed successfully on time 2021-11-18 20:23:32+08, xid 485 and LSN 0/60000F0
pg_probackup validate -B $HOME/probackup --instance pg5433 --recovery-target-time="2021-11-18 20:35:32+08"
# => WARNING: Thread [1]: Could not read WAL record at 0/A000000
# ERROR: Thread [1]: WAL segment "/home/fang/probackup/wal/pg5433/00000001000000000000000A" is absent
# WARNING: Recovery can be done up to time 2021-11-18 20:27:17+08, xid 485 and LSN 0/90000B8
# ERROR: Not enough WAL records to time 2021-11-18 20:35:32+08
pg_probackup restore -B $HOME/probackup --instance pg5433 -D $HOME/pgdata2-restored2 --recovery-target-time="2021-11-18 20:23:32+08"
DELTA增量备份
pg_probackup backup -B $HOME/probackup --instance pg5433 -b DELTA -p 5433 -d postgres --log-level-file=info
PTRACK增量备份,要求--data-checksums
pg_probackup backup -B $HOME/probackup --instance pg5433 -b PTRACK -p 5433 -d postgres --log-level-file=info
# WARNING: This PostgreSQL instance was initialized without data block checksums. pg_probackup have no way to detect data block corruption without them. Reinitialize PGDATA with option '--data-checksums'.
# WARNING: Current PostgreSQL role is superuser. It is not recommended to run pg_probackup under superuser.
# ERROR: This PostgreSQL instance does not support ptrack
# WARNING: Backup R2TMDU is running, setting its status to ERROR
pg_probackup show -B $HOME/probackup
BACKUP INSTANCE 'pg5433'
=====================================================================================================================================
Instance Version ID Recovery Time Mode WAL Mode TLI Time Data WAL Zratio Start LSN Stop LSN Status
=====================================================================================================================================
pg5433 13 R2TMDU ---- PTRACK ARCHIVE 0/0 0 0 0 1.00 0/0 0/0 ERROR
pg5433 13 R2TMDK 2021-11-19 21:20:08+08 DELTA ARCHIVE 1/1 1s 106kB 16MB 1.00 0/11000028 0/120000B8 OK
pg5433 13 R2SQBU 2021-11-19 09:47:54+08 PAGE ARCHIVE 1/1 1s 106kB 16MB 1.00 0/E000028 0/F0000B8 OK
pg5433 13 R2SQAS 2021-11-19 09:47:16+08 PAGE ARCHIVE 1/1 1s 106kB 16MB 1.00 0/B000028 0/C0000B8 OK
pg5433 13 R2RP9H 2021-11-18 20:27:17+08 PAGE ARCHIVE 1/1 1s 106kB 16MB 1.00 0/8000028 0/90000B8 OK
pg5433 13 R2RP38 2021-11-18 20:23:32+08 PAGE ARCHIVE 1/1 1s 106kB 16MB 1.00 0/5000028 0/60000F0 OK
pg5433 13 R2RP1U 2021-11-18 20:22:44+08 FULL ARCHIVE 1/0 6s 23MB 16MB 1.00 0/2000028 0/30000F0 OK
在有交易时增量备份
pgbench -i -s 10 postgres
pgbench -c 2 -T 100 -R 100 postgres
pg_probackup backup -B $HOME/probackup --instance pg5433 -b PAGE -p 5433 -d postgres --log-level-file=info
pg_probackup backup -B $HOME/probackup --instance pg5433 -b DELTA -p 5433 -d postgres --log-level-file=info
pg_probackup show -B $HOME/probackup
BACKUP INSTANCE 'pg5433'
====================================================================================================================================
Instance Version ID Recovery Time Mode WAL Mode TLI Time Data WAL Zratio Start LSN Stop LSN Status
====================================================================================================================================
pg5433 13 R2TMOZ 2021-11-19 21:26:59+08 DELTA ARCHIVE 1/1 1s 106kB 16MB 1.00 0/29000028 0/2A0000B8 OK
pg5433 13 R2TMOI 2021-11-19 21:26:42+08 PAGE ARCHIVE 1/1 1s 106kB 16MB 1.00 0/27000028 0/280000B8 OK
pg5433 13 R2TMO9 2021-11-19 21:26:34+08 DELTA ARCHIVE 1/1 2s 106kB 16MB 1.00 0/24000028 0/250000B8 OK
pg5433 13 R2TMO0 2021-11-19 21:26:24+08 PAGE ARCHIVE 1/1 1s 106kB 16MB 1.00 0/22000028 0/230000B8 OK
pg5433 13 R2TMNR 2021-11-19 21:26:16+08 DELTA ARCHIVE 1/1 2s 106kB 16MB 1.00 0/1F000028 0/200000B8 OK
pg5433 13 R2TMNH 2021-11-19 21:26:05+08 PAGE ARCHIVE 1/1 1s 106kB 16MB 1.00 0/1D000028 0/1E0000B8 OK
pg5433 13 R2TMNA 2021-11-19 21:25:58+08 DELTA ARCHIVE 1/1 1s 106kB 16MB 1.00 0/1B000028 0/1C0000F0 OK
pg5433 13 R2TMN1 2021-11-19 21:25:50+08 PAGE ARCHIVE 1/1 2s 106kB 16MB 1.00 0/19000028 0/1A0000B8 OK
pg5433 13 R2TMMR 2021-11-19 21:25:39+08 DELTA ARCHIVE 1/1 1s 106kB 16MB 1.00 0/17000028 0/180000B8 OK
pg5433 13 R2TMM7 2021-11-19 21:25:20+08 PAGE ARCHIVE 1/1 2s 106kB 16MB 1.00 0/14000028 0/150000B8 OK
pg5433 13 R2TMDK 2021-11-19 21:20:08+08 DELTA ARCHIVE 1/1 1s 106kB 16MB 1.00 0/11000028 0/120000B8 OK
pg5433 13 R2SQBU 2021-11-19 09:47:54+08 PAGE ARCHIVE 1/1 1s 106kB 16MB 1.00 0/E000028 0/F0000B8 OK
pg5433 13 R2SQAS 2021-11-19 09:47:16+08 PAGE ARCHIVE 1/1 1s 106kB 16MB 1.00 0/B000028 0/C0000B8 OK
pg5433 13 R2RP9H 2021-11-18 20:27:17+08 PAGE ARCHIVE 1/1 1s 106kB 16MB 1.00 0/8000028 0/90000B8 OK
pg5433 13 R2RP38 2021-11-18 20:23:32+08 PAGE ARCHIVE 1/1 1s 106kB 16MB 1.00 0/5000028 0/60000F0 OK
pg5433 13 R2RP1U 2021-11-18 20:22:44+08 FULL ARCHIVE 1/0 6s 23MB 16MB 1.00 0/2000028 0/30000F0 OK
pg_probackup validate -B $HOME/probackup --instance pg5433 --recovery-target-time="2021-11-19 21:26:32+08"
pg_probackup validate -B $HOME/probackup --instance pg5433 --recovery-target-time="2021-11-19 21:26:52+08"
增量恢复,两种模式(checksum,lsn)都需要--data-checksums
pg_probackup restore -B $HOME/probackup --instance pg5433 -D $HOME/pgdata2 -I lsn
pg_probackup restore -B $HOME/probackup --instance pg5433 -D $HOME/pgdata2 -I lsn
# INFO: Running incremental restore into nonempty directory: "/home/fang/pgdata2"
# WARNING: Postmaster with pid 1405 is running in destination directory "/home/fang/pgdata2"
# INFO: Trying to read pg_control file in destination directory
# ERROR: Incremental restore is not allowed
pg_ctl -D ~/pgdata2 -l ~/repos/postgres/logfile2 stop
# waiting for server to shut down.... done
# server stopped
pg_probackup restore -B $HOME/probackup --instance pg5433 -D $HOME/pgdata2 -I lsn
# INFO: Running incremental restore into nonempty directory: "/home/fang/pgdata2"
# INFO: Trying to read pg_control file in destination directory
# ERROR: Incremental restore in 'lsn' mode require data_checksums to be enabled in destination data directory