使用pg_basebackup对Postgre进行备份与恢复的实现

postgres 版本9.4.23

暂时在windows下对postgres进行备份恢复。

在进行备份之前,在认证文件中增加一行,否则无法进行备份

host  replication   postgres    127.0.0.1/24      md5

开始使用pg_basebackup命令进行备份。当pgbak文件夹不存在的时候,备份的过程中会自动创建该文件夹。

关于pg_basebackup的命令,可以

c:\postgresql.4\bin>pg_basebackup -h 127.0.0.1 -u postgres -d c:\pgbak -ft -r -z -v --checkpoint=fast
口令:
pg_basebackup: initiating base backup, waiting for checkpoint to complete
pg_basebackup: checkpoint completed
已复制     1 个文件。
已复制     1 个文件。
已复制     1 个文件。
notice: pg_stop_backup complete, all required wal segments have been archived
pg_basebackup: base backup completed

进行模拟破坏。停掉库。 删除掉data目录(注意wal日志以.backup结尾及以后的文件不要删除,因为这些文件没有备份,恢复的时候要使用)。把备份好的tar.gz文件解压到data。

在恢复之前,修改加压后的tar.gz里面的recovery.conf文件(其实是指定归档的位置,这样就会进行恢复)。否则无法启动数据库。修改后的内容如下(#屏蔽掉了备份生成的命令):

#standby_mode = 'on'
#primary_conninfo = 'user=postgres password=postgres host=127.0.0.1 port=5432 sslmode=prefer sslcompression=1 krbsrvname=postgres'
 
restore_command = 'c:\\postgresql\.4\\data\\pg_xlog\\archive_status\\%f' 

启动postgres数据库

c:\postgresql.4\bin>pg_ctl start -d c:\postgresql.4\data
正在启动服务器进程
 
c:\postgresql.4\bin>2019-08-06 21:22:29 hkt 日志: 日志输出重定向到日志收集进
程
2019-08-06 21:22:29 hkt 提示: 后续的日志输出将出现在目录 "pg_log"中.
已复制     1 个文件。
已复制     1 个文件。
c:\postgresql.4\bin>

这样数据库就起来了。恢复正常。

下面是恢复后的wal日志文件情况。

恢复的时候,碰到的一些问题

问题1 。这个原因,是因为在recovery.conf中没有指定restore_command。是按照replicate来进行复制了。所以出错。

2019-08-06 21:20:25 hkt log: database system was shut down in recovery at 2019-08-06 21:15:46 hkt
2019-08-06 21:20:25 hkt log: could not open tablespace directory "pg_tblspc/16419/pg_9.4_201409291": no such file or directory
2019-08-06 21:20:25 hkt log: could not open tablespace directory "pg_tblspc/16577/pg_9.4_201409291": no such file or directory
2019-08-06 21:20:25 hkt log: starting archive recovery
'c:\postgresql.4\data\pg_xlog\archive_status
2019-08-06 21:20:25 hkt log: database system was shut down in recovery at 2019-08-06 21:15:46 hkt
2019-08-06 21:20:25 hkt log: could not open tablespace directory "pg_tblspc/16419/pg_9.4_201409291": no such file or directory
2019-08-06 21:20:25 hkt log: could not open tablespace directory "pg_tblspc/16577/pg_9.4_201409291": no such file or directory
2019-08-06 21:20:25 hkt log: starting archive recovery
'c:\postgresql\9.4\data\pg_xlog\archive_status\0000000100000000000000d6' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
2019-08-06 21:20:25 hkt log: invalid checkpoint record
2019-08-06 21:20:25 hkt fatal: could not locate required checkpoint record
2019-08-06 21:20:25 hkt hint: if you are not restoring from a backup, try removing the file "c:/postgresql/9.4/data/backup_label".
2019-08-06 21:20:25 hkt 日志: 启动进程 (pid 544) 已退出, 退出代码 1
2019-08-06 21:20:25 hkt 日志: 由于启动进程失败, 终止启动
00000100000000000000d6' 不是内部或外部命令,也不是可运行的程序 或批处理文件。 2019-08-06 21:20:25 hkt log: invalid checkpoint record 2019-08-06 21:20:25 hkt fatal: could not locate required checkpoint record 2019-08-06 21:20:25 hkt hint: if you are not restoring from a backup, try removing the file "c:/postgresql/9.4/data/backup_label". 2019-08-06 21:20:25 hkt 日志: 启动进程 (pid 544) 已退出, 退出代码 1 2019-08-06 21:20:25 hkt 日志: 由于启动进程失败, 终止启动

问题2,这样原因,是因为在恢复的时候,tar.gz文件夹里面,包含有自己建立的表空间。而在模拟故障的是时候,表空间没有被删除,也就是说,没有使用tar.gz里面的表空间文件覆盖原来的表空间。覆盖后ok。

2019-08-06 21:22:29 hkt log: database system was shut down in recovery at 2019-08-06 21:15:46 hkt
2019-08-06 21:22:29 hkt log: could not open tablespace directory "pg_tblspc/16419/pg_9.4_201409291": no such file or directory
2019-08-06 21:22:29 hkt log: could not open tablespace directory "pg_tblspc/16577/pg_9.4_201409291": no such file or directory
2019-08-06 21:22:29 hkt log: starting archive recovery
'c:\postgresql.4\data\pg_xlog\archive_status
2019-08-06 21:22:29 hkt log: database system was shut down in recovery at 2019-08-06 21:15:46 hkt
2019-08-06 21:22:29 hkt log: could not open tablespace directory "pg_tblspc/16419/pg_9.4_201409291": no such file or directory
2019-08-06 21:22:29 hkt log: could not open tablespace directory "pg_tblspc/16577/pg_9.4_201409291": no such file or directory
2019-08-06 21:22:29 hkt log: starting archive recovery
'c:\postgresql\9.4\data\pg_xlog\archive_status\0000000100000000000000d6' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
2019-08-06 21:22:29 hkt log: redo starts at 0/d6000090
2019-08-06 21:22:29 hkt log: consistent recovery state reached at 0/d60000b8
'c:\postgresql\9.4\data\pg_xlog\archive_status\0000000100000000000000d7' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
2019-08-06 21:22:29 hkt log: record with zero length at 0/d7000090
2019-08-06 21:22:29 hkt log: redo done at 0/d7000028
'c:\postgresql\9.4\data\pg_xlog\archive_status\0000000100000000000000d7' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
'c:\postgresql\9.4\data\pg_xlog\archive_status\00000002.history' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
2019-08-06 21:22:29 hkt log: selected new timeline id: 2
'c:\postgresql\9.4\data\pg_xlog\archive_status\00000001.history' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
2019-08-06 21:22:29 hkt fatal: the database system is starting up
2019-08-06 21:22:29 hkt log: archive recovery complete
2019-08-06 21:22:29 hkt log: multixact member wraparound protections are now enabled
2019-08-06 21:22:29 hkt 日志: 数据库系统准备接受连接
2019-08-06 21:22:29 hkt log: autovacuum launcher started
2019-08-06 21:22:30 hkt log: statement: set datestyle=iso; set client_min_messages=notice; select set_config('bytea_output','escape',false) from pg_settings where name = 'bytea_output'; set client_encoding='unicode';
2019-08-06 21:22:30 hkt log: statement: 
select
db.oid as did, db.datname, db.datallowconn,
pg_encoding_to_char(db.encoding) as serverencoding,
has_database_privilege(db.oid, 'create') as cancreate, datlastsysoid
from
pg_database db
where db.datname = current_database()
2019-08-06 21:22:30 hkt log: statement: 
select
oid as id, rolname as name, rolsuper as is_superuser,
rolcreaterole as can_create_role, rolcreatedb as can_create_db
from
pg_catalog.pg_roles
where
rolname = current_user
2019-08-06 21:22:30 hkt log: statement: /*pga4dash*/
00000100000000000000d6' 不是内部或外部命令,也不是可运行的程序 或批处理文件。 2019-08-06 21:22:29 hkt log: redo starts at 0/d6000090 2019-08-06 21:22:29 hkt log: consistent recovery state reached at 0/d60000b8 'c:\postgresql.4\data\pg_xlog\archive_status
2019-08-06 21:22:29 hkt log: database system was shut down in recovery at 2019-08-06 21:15:46 hkt
2019-08-06 21:22:29 hkt log: could not open tablespace directory "pg_tblspc/16419/pg_9.4_201409291": no such file or directory
2019-08-06 21:22:29 hkt log: could not open tablespace directory "pg_tblspc/16577/pg_9.4_201409291": no such file or directory
2019-08-06 21:22:29 hkt log: starting archive recovery
'c:\postgresql\9.4\data\pg_xlog\archive_status\0000000100000000000000d6' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
2019-08-06 21:22:29 hkt log: redo starts at 0/d6000090
2019-08-06 21:22:29 hkt log: consistent recovery state reached at 0/d60000b8
'c:\postgresql\9.4\data\pg_xlog\archive_status\0000000100000000000000d7' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
2019-08-06 21:22:29 hkt log: record with zero length at 0/d7000090
2019-08-06 21:22:29 hkt log: redo done at 0/d7000028
'c:\postgresql\9.4\data\pg_xlog\archive_status\0000000100000000000000d7' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
'c:\postgresql\9.4\data\pg_xlog\archive_status\00000002.history' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
2019-08-06 21:22:29 hkt log: selected new timeline id: 2
'c:\postgresql\9.4\data\pg_xlog\archive_status\00000001.history' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
2019-08-06 21:22:29 hkt fatal: the database system is starting up
2019-08-06 21:22:29 hkt log: archive recovery complete
2019-08-06 21:22:29 hkt log: multixact member wraparound protections are now enabled
2019-08-06 21:22:29 hkt 日志: 数据库系统准备接受连接
2019-08-06 21:22:29 hkt log: autovacuum launcher started
2019-08-06 21:22:30 hkt log: statement: set datestyle=iso; set client_min_messages=notice; select set_config('bytea_output','escape',false) from pg_settings where name = 'bytea_output'; set client_encoding='unicode';
2019-08-06 21:22:30 hkt log: statement: 
select
db.oid as did, db.datname, db.datallowconn,
pg_encoding_to_char(db.encoding) as serverencoding,
has_database_privilege(db.oid, 'create') as cancreate, datlastsysoid
from
pg_database db
where db.datname = current_database()
2019-08-06 21:22:30 hkt log: statement: 
select
oid as id, rolname as name, rolsuper as is_superuser,
rolcreaterole as can_create_role, rolcreatedb as can_create_db
from
pg_catalog.pg_roles
where
rolname = current_user
2019-08-06 21:22:30 hkt log: statement: /*pga4dash*/
00000100000000000000d7' 不是内部或外部命令,也不是可运行的程序 或批处理文件。 2019-08-06 21:22:29 hkt log: record with zero length at 0/d7000090 2019-08-06 21:22:29 hkt log: redo done at 0/d7000028 'c:\postgresql.4\data\pg_xlog\archive_status
2019-08-06 21:22:29 hkt log: database system was shut down in recovery at 2019-08-06 21:15:46 hkt
2019-08-06 21:22:29 hkt log: could not open tablespace directory "pg_tblspc/16419/pg_9.4_201409291": no such file or directory
2019-08-06 21:22:29 hkt log: could not open tablespace directory "pg_tblspc/16577/pg_9.4_201409291": no such file or directory
2019-08-06 21:22:29 hkt log: starting archive recovery
'c:\postgresql\9.4\data\pg_xlog\archive_status\0000000100000000000000d6' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
2019-08-06 21:22:29 hkt log: redo starts at 0/d6000090
2019-08-06 21:22:29 hkt log: consistent recovery state reached at 0/d60000b8
'c:\postgresql\9.4\data\pg_xlog\archive_status\0000000100000000000000d7' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
2019-08-06 21:22:29 hkt log: record with zero length at 0/d7000090
2019-08-06 21:22:29 hkt log: redo done at 0/d7000028
'c:\postgresql\9.4\data\pg_xlog\archive_status\0000000100000000000000d7' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
'c:\postgresql\9.4\data\pg_xlog\archive_status\00000002.history' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
2019-08-06 21:22:29 hkt log: selected new timeline id: 2
'c:\postgresql\9.4\data\pg_xlog\archive_status\00000001.history' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
2019-08-06 21:22:29 hkt fatal: the database system is starting up
2019-08-06 21:22:29 hkt log: archive recovery complete
2019-08-06 21:22:29 hkt log: multixact member wraparound protections are now enabled
2019-08-06 21:22:29 hkt 日志: 数据库系统准备接受连接
2019-08-06 21:22:29 hkt log: autovacuum launcher started
2019-08-06 21:22:30 hkt log: statement: set datestyle=iso; set client_min_messages=notice; select set_config('bytea_output','escape',false) from pg_settings where name = 'bytea_output'; set client_encoding='unicode';
2019-08-06 21:22:30 hkt log: statement: 
select
db.oid as did, db.datname, db.datallowconn,
pg_encoding_to_char(db.encoding) as serverencoding,
has_database_privilege(db.oid, 'create') as cancreate, datlastsysoid
from
pg_database db
where db.datname = current_database()
2019-08-06 21:22:30 hkt log: statement: 
select
oid as id, rolname as name, rolsuper as is_superuser,
rolcreaterole as can_create_role, rolcreatedb as can_create_db
from
pg_catalog.pg_roles
where
rolname = current_user
2019-08-06 21:22:30 hkt log: statement: /*pga4dash*/
00000100000000000000d7' 不是内部或外部命令,也不是可运行的程序 或批处理文件。 'c:\postgresql.4\data\pg_xlog\archive_status
2019-08-06 21:22:29 hkt log: database system was shut down in recovery at 2019-08-06 21:15:46 hkt
2019-08-06 21:22:29 hkt log: could not open tablespace directory "pg_tblspc/16419/pg_9.4_201409291": no such file or directory
2019-08-06 21:22:29 hkt log: could not open tablespace directory "pg_tblspc/16577/pg_9.4_201409291": no such file or directory
2019-08-06 21:22:29 hkt log: starting archive recovery
'c:\postgresql\9.4\data\pg_xlog\archive_status\0000000100000000000000d6' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
2019-08-06 21:22:29 hkt log: redo starts at 0/d6000090
2019-08-06 21:22:29 hkt log: consistent recovery state reached at 0/d60000b8
'c:\postgresql\9.4\data\pg_xlog\archive_status\0000000100000000000000d7' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
2019-08-06 21:22:29 hkt log: record with zero length at 0/d7000090
2019-08-06 21:22:29 hkt log: redo done at 0/d7000028
'c:\postgresql\9.4\data\pg_xlog\archive_status\0000000100000000000000d7' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
'c:\postgresql\9.4\data\pg_xlog\archive_status\00000002.history' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
2019-08-06 21:22:29 hkt log: selected new timeline id: 2
'c:\postgresql\9.4\data\pg_xlog\archive_status\00000001.history' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
2019-08-06 21:22:29 hkt fatal: the database system is starting up
2019-08-06 21:22:29 hkt log: archive recovery complete
2019-08-06 21:22:29 hkt log: multixact member wraparound protections are now enabled
2019-08-06 21:22:29 hkt 日志: 数据库系统准备接受连接
2019-08-06 21:22:29 hkt log: autovacuum launcher started
2019-08-06 21:22:30 hkt log: statement: set datestyle=iso; set client_min_messages=notice; select set_config('bytea_output','escape',false) from pg_settings where name = 'bytea_output'; set client_encoding='unicode';
2019-08-06 21:22:30 hkt log: statement: 
select
db.oid as did, db.datname, db.datallowconn,
pg_encoding_to_char(db.encoding) as serverencoding,
has_database_privilege(db.oid, 'create') as cancreate, datlastsysoid
from
pg_database db
where db.datname = current_database()
2019-08-06 21:22:30 hkt log: statement: 
select
oid as id, rolname as name, rolsuper as is_superuser,
rolcreaterole as can_create_role, rolcreatedb as can_create_db
from
pg_catalog.pg_roles
where
rolname = current_user
2019-08-06 21:22:30 hkt log: statement: /*pga4dash*/
000002.history' 不是内部或外部命令,也不是可运行的程序 或批处理文件。 2019-08-06 21:22:29 hkt log: selected new timeline id: 2 'c:\postgresql.4\data\pg_xlog\archive_status
2019-08-06 21:22:29 hkt log: database system was shut down in recovery at 2019-08-06 21:15:46 hkt
2019-08-06 21:22:29 hkt log: could not open tablespace directory "pg_tblspc/16419/pg_9.4_201409291": no such file or directory
2019-08-06 21:22:29 hkt log: could not open tablespace directory "pg_tblspc/16577/pg_9.4_201409291": no such file or directory
2019-08-06 21:22:29 hkt log: starting archive recovery
'c:\postgresql\9.4\data\pg_xlog\archive_status\0000000100000000000000d6' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
2019-08-06 21:22:29 hkt log: redo starts at 0/d6000090
2019-08-06 21:22:29 hkt log: consistent recovery state reached at 0/d60000b8
'c:\postgresql\9.4\data\pg_xlog\archive_status\0000000100000000000000d7' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
2019-08-06 21:22:29 hkt log: record with zero length at 0/d7000090
2019-08-06 21:22:29 hkt log: redo done at 0/d7000028
'c:\postgresql\9.4\data\pg_xlog\archive_status\0000000100000000000000d7' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
'c:\postgresql\9.4\data\pg_xlog\archive_status\00000002.history' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
2019-08-06 21:22:29 hkt log: selected new timeline id: 2
'c:\postgresql\9.4\data\pg_xlog\archive_status\00000001.history' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
2019-08-06 21:22:29 hkt fatal: the database system is starting up
2019-08-06 21:22:29 hkt log: archive recovery complete
2019-08-06 21:22:29 hkt log: multixact member wraparound protections are now enabled
2019-08-06 21:22:29 hkt 日志: 数据库系统准备接受连接
2019-08-06 21:22:29 hkt log: autovacuum launcher started
2019-08-06 21:22:30 hkt log: statement: set datestyle=iso; set client_min_messages=notice; select set_config('bytea_output','escape',false) from pg_settings where name = 'bytea_output'; set client_encoding='unicode';
2019-08-06 21:22:30 hkt log: statement: 
select
db.oid as did, db.datname, db.datallowconn,
pg_encoding_to_char(db.encoding) as serverencoding,
has_database_privilege(db.oid, 'create') as cancreate, datlastsysoid
from
pg_database db
where db.datname = current_database()
2019-08-06 21:22:30 hkt log: statement: 
select
oid as id, rolname as name, rolsuper as is_superuser,
rolcreaterole as can_create_role, rolcreatedb as can_create_db
from
pg_catalog.pg_roles
where
rolname = current_user
2019-08-06 21:22:30 hkt log: statement: /*pga4dash*/
000001.history' 不是内部或外部命令,也不是可运行的程序 或批处理文件。 2019-08-06 21:22:29 hkt fatal: the database system is starting up 2019-08-06 21:22:29 hkt log: archive recovery complete 2019-08-06 21:22:29 hkt log: multixact member wraparound protections are now enabled 2019-08-06 21:22:29 hkt 日志: 数据库系统准备接受连接 2019-08-06 21:22:29 hkt log: autovacuum launcher started 2019-08-06 21:22:30 hkt log: statement: set datestyle=iso; set client_min_messages=notice; select set_config('bytea_output','escape',false) from pg_settings where name = 'bytea_output'; set client_encoding='unicode'; 2019-08-06 21:22:30 hkt log: statement: select db.oid as did, db.datname, db.datallowconn, pg_encoding_to_char(db.encoding) as serverencoding, has_database_privilege(db.oid, 'create') as cancreate, datlastsysoid from pg_database db where db.datname = current_database() 2019-08-06 21:22:30 hkt log: statement: select oid as id, rolname as name, rolsuper as is_superuser, rolcreaterole as can_create_role, rolcreatedb as can_create_db from pg_catalog.pg_roles where rolname = current_user 2019-08-06 21:22:30 hkt log: statement: /*pga4dash*/

补充:pgsql的pg_basebackup

pg_basebackup:是从pgsql提供的一个方便基础备份的工具。经常用来搭建流复制环境

属于物理备份。pgsql的逻辑备份是pg_dump工具。

参数说明(pg_basebackup –help 详细查看):

-h 指定连接的数据库的主机名或ip地址,这里就是主库的ip。

-u 指定连接的用户名,专门负责流复制的repl用户。

-f 指定了输出的格式,支持p(原样输出)或者t(tar格式输出)。

-x 表示备份开始后,启动另一个流复制连接从主库接收wal日志。

-p 表示允许在备份过程中实时的打印备份的进度。

-r 表示会在备份结束后自动生成recovery.conf文件,这样也就避免了手动创建。(12.0有差异)

-d 指定把备份写到哪个目录,这里尤其要注意一点就是做基础备份之前从库的数据目录(比如:/usr/local/postgresql/data)目录需要手动清空。

-l 表示指定一个备份的标识

如:pg_basebackup -h 192.168.23.193 -p 3012 -u testacc -f p -p -x s -r -d /data -r 50m -l label_1576225683″

注意:

12 版本pg_basebackup 命令的 -r 参数的效果和之前不同,主要体现在:

命令执行后在 $pgdata 目录创建 standby.signal 标识文件,文件内容为空

命令执行后在 $pgdata 目录的 postgresql.auto.conf 文件中添加 primary_conninfo 参数信息

以上为个人经验,希望能给大家一个参考,也希望大家多多支持www.887551.com。如有错误或未考虑完全的地方,望不吝赐教。

(0)
上一篇 2022年3月21日
下一篇 2022年3月21日

相关推荐