pg_start_backup() 和 pg_stop_backup()

os: centos 7.4
db: postgresql 10

常见的手动备份

#!/bin/bash
#---------- Global Parameter ---------
PG_DATADIR=/data/data5432/main
PG_WALARCHIVEDIR=/data/backup/pgwalarchive
PG_BACKUPDIR=/data/backup/pgbackup

OSUSER=postgres

PGSQLHOST=127.0.0.1
PGSQLPORT=5432
PGSQLDATABASE=postgres
PGSQLUSER=postgres

SSHPORT=51000
DATE=`date +%Y%m%d%H%M%S`

echo "`date` postgresql base backup start..."

su - $OSUSER -c "/usr/bin/psql -c 'select pg_start_backup('$DATE',true);'"
tar --exclude=$PG_DATADIR/postmaster.pid \
--exclude=$PG_DATADIR/pg_wal \
--exclude=$PG_DATADIR/log \
-Pcf - $PG_DATADIR | pigz -p 8  | split --bytes=10G - $PG_BACKUPDIR/${DATE}_base.tgz.
su - $OSUSER -c "/usr/bin/psql -c 'select pg_stop_backup();'"

echo "`date` postgresql base backup end... "

select pg_start_backup();

默认情况下,pg_start_backup 会花费很长时间来完成。这是因为它会执行一个检查点,而检查点所需要的I/O在相当一段时间内将会被传播,默认情况下这段时间是内部检查点间隔的一半(参见配置参数 checkpoint_completion_target)。这通常是你所希望的,因为它能将对查询处理的影响最小化。如果你要尽快开始备份,可使用:

select pg_start_backup('$DATE',true);

pg_start_backup

postgres=# select * from pg_proc where proname like '%start_backup%';
-[ RECORD 1 ]---+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
proname         | pg_start_backup
pronamespace    | 11
proowner        | 10
prolang         | 12
procost         | 1
prorows         | 0
provariadic     | 0
protransform    | -
proisagg        | f
proiswindow     | f
prosecdef       | f
proleakproof    | f
proisstrict     | t
proretset       | f
provolatile     | v
proparallel     | r
pronargs        | 3
pronargdefaults | 2
prorettype      | 3220
proargtypes     | 25 16 16
proallargtypes  | 
proargmodes     | 
proargnames     | {label,fast,exclusive}
proargdefaults  | ({CONST :consttype 16 :consttypmod -1 :constcollid 0 :constlen 1 :constbyval true :constisnull false :location 481 :constvalue 1 [ 0 0 0 0 0 0 0 0 ]} {CONST :consttype 16 :consttypmod -1 :constcollid 0 :constlen 1 :constbyval true :constisnull false :location 514 :constvalue 1 [ 1 0 0 0 0 0 0 0 ]})
protrftypes     | 
prosrc          | pg_start_backup
probin          | 
proconfig       | 
proacl          | {postgres=X/postgres}

可以看到该函数的参数 proargnames | {label,fast,exclusive}

label : 备份标签
fast : 快速 checkpoint
exclusive : 排他性

pg_stop_backup

select pg_stop_backup();

postgres=# select * from pg_proc where proname like '%stop_backup%';
-[ RECORD 1 ]---+-------------------------------------------------------------------------------------------------------------------------------------------------------
proname         | pg_stop_backup
pronamespace    | 11
proowner        | 10
prolang         | 12
procost         | 1
prorows         | 0
provariadic     | 0
protransform    | -
proisagg        | f
proiswindow     | f
prosecdef       | f
proleakproof    | f
proisstrict     | t
proretset       | f
provolatile     | v
proparallel     | r
pronargs        | 0
pronargdefaults | 0
prorettype      | 3220
proargtypes     | 
proallargtypes  | 
proargmodes     | 
proargnames     | 
proargdefaults  | 
protrftypes     | 
prosrc          | pg_stop_backup
probin          | 
proconfig       | 
proacl          | {postgres=X/postgres}
-[ RECORD 2 ]---+-------------------------------------------------------------------------------------------------------------------------------------------------------
proname         | pg_stop_backup
pronamespace    | 11
proowner        | 10
prolang         | 12
procost         | 1
prorows         | 1000
provariadic     | 0
protransform    | -
proisagg        | f
proiswindow     | f
prosecdef       | f
proleakproof    | f
proisstrict     | t
proretset       | t
provolatile     | v
proparallel     | r
pronargs        | 2
pronargdefaults | 1
prorettype      | 2249
proargtypes     | 16 16
proallargtypes  | {16,16,3220,25,25}
proargmodes     | {i,i,o,o,o}
proargnames     | {exclusive,wait_for_archive,lsn,labelfile,spcmapfile}
proargdefaults  | ({CONST :consttype 16 :consttypmod -1 :constcollid 0 :constlen 1 :constbyval true :constisnull false :locatio
n 104 :constvalue 1 [ 1 0 0 0 0 0 0 0 ]})
protrftypes     | 
prosrc          | pg_stop_backup_v2
probin          | 
proconfig       | 
proacl          | {postgres=X/postgres}

参考:
http://postgres.cn/docs/10/continuous-archiving.html#BACKUP-BASE-BACKUP

发布了710 篇原创文章 · 获赞 70 · 访问量 49万+

猜你喜欢

转载自blog.csdn.net/ctypyb2002/article/details/104069957