PostgreSQL兼容oracle rman备份——pg_rman
PostgreSQL sql 数据库 postgresql0、说明
PostgreSQL支持多种备份数据的方式,如:SQL转储、文件系统级备份、连续归档。但是相较于oracle,oracle自带的rman备份工具是非常强大的,早就已经支持全量、增量、归档的备份模式,支持压缩等。
postgresql可以通过pg_rman插件来实现类似oracle rman的备份功能,这使得pg也有了块级增量备份,对于那种非常庞大的数据库,备份起来就不像只支持全量和归档的模式那么吃力了。
1、pg_rman介绍:
下载地址:https://github.com/ossc-db/pg_rman
pg_rman使用的是pg_start_backup(), copy, pg_stop_backup()的备份模式。
需要注意的是pg_rman使用的不是流复制协议,而是文件拷贝。
pg_rman支持以下几种模式:
- init
Initialize a backup catalog.
- backup
Take an online backup.
- restore
Do restore.
- show
Show backup history. The detail option shows with additional information of each backups.
- validate
Validate backup files. Backups without validation cannot be used for restore and incremental backup.
- delete
Delete backup files.
- purge
Remove deleted backups from backup catalog.
2、pg_rman使用
2.1、使用前提
使用pg_rman之前需要开启数据库的归档和csvlog。
bill@bill=>show log_destination ;
log_destination
-----------------
csvlog
(1 row)
bill@bill=>show log_directory;
log_directory
---------------
log
(1 row)
bill@bill=>show archive_command ;
archive_command
------------------------------------
cp %p /home/pg12/pgdata/archive/%f
(1 row)
2.2、初始化
安装完pg_rman之后,需要在使用前先创建一个初始化目录,用来存放备份的数据。
同时这个目录也会存放一些元数据,例如备份的配置文件,数据库的systemid,时间线文件历史等等。
创建目录:
pg12@oracle-> mkdir -p /home/pg12/pgdata/pg_bk
初始化:
初始化需要指定两个参数,分别为备份目标目录,以及数据库的$PGDATA
pg12@oracle-> pg_rman init -B /home/pg12/pgdata/pg_bk/ -D /home/pg12/pgdata/
INFO: ARCLOG_PATH is set to '/home/pg12/pgdata/archive'
INFO: SRVLOG_PATH is set to '/home/pg12/pgdata//pg_log'
生成的元数据有:
pg12@oracle-> ll
total 8.0K
drwx------ 4 pg12 pg12 32 Jun 28 10:28 backup
-rw-rw-r-- 1 pg12 pg12 81 Jun 28 10:28 pg_rman.ini
-rw-rw-r-- 1 pg12 pg12 40 Jun 28 10:28 system_identifier
drwx------ 2 pg12 pg12 6 Jun 28 10:28 timeline_history
生成的配置文件信息:
pg12@oracle-> cat pg_rman.ini
ARCLOG_PATH='/home/pg12/pgdata/archive'
SRVLOG_PATH='/home/pg12/pgdata//pg_log'
- 1
- 2
- 3
生成的system_identified文件里面存放的是数据库的system id,用于区分备份的数据库是不是一个数据库。
pg12@oracle-> cat system_identifier
SYSTEM_IDENTIFIER='6737655972131162500'
和通过控制文件查看数据库system id一致:
pg12@oracle-> pg_controldata |grep "Database system identifier"
Database system identifier: 6737655972131162500
3、备份
3.1、全量备份
pg12@oracle-> pg_rman backup -B /home/pg12/pgdata/pg_bk/ -D /home/pg12/pgdata/ -b full -h localhost -p 2019 -U bill -d bill
INFO: copying database files
INFO: copying archived WAL files
INFO: backup complete
INFO: Please execute 'pg_rman validate' to verify the files are correctly copied.
备份完需要对文件进行校验确认是否可用:
pg12@oracle-> pg_rman validate -B /home/pg12/pgdata/pg_bk/
INFO: validate: "2020-06-28 10:35:12" backup and archive log files by CRC
INFO: backup "2020-06-28 10:35:12" is valid
每个备份集都包含了一个备份状态文件backup.ini:
# configuration
BACKUP_MODE=FULL
FULL_BACKUP_ON_ERROR=false
WITH_SERVERLOG=false
COMPRESS_DATA=false
# result
TIMELINEID=2
START_LSN=1e/14000060
STOP_LSN=1e/14000170
START_TIME='2020-06-28 10:35:12'
END_TIME='2020-06-28 10:35:33'
RECOVERY_XID=12097462
RECOVERY_TIME='2020-06-28 10:35:33'
TOTAL_DATA_BYTES=2109782735
READ_DATA_BYTES=2109782428
READ_ARCLOG_BYTES=50331998
WRITE_BYTES=2105013889
BLOCK_SIZE=8192
XLOG_BLOCK_SIZE=8192
STATUS=OK
这个文件中包含了很重要的信息,比如LSN,后面LSN将用于比对增量备份时对比数据块的LSN是否发生了变化,是否需要备份。
3.2、增量备份
pg12@oracle-> pg_rman backup -B /home/pg12/pgdata/pg_bk/ -D /home/pg12/pgdata/ -b incremental -h localhost -p 2019 -U bill -d bill
INFO: copying database files
INFO: copying archived WAL files
INFO: backup complete
INFO: Please execute 'pg_rman validate' to verify the files are correctly copied.
校验:
pg12@oracle-> pg_rman validate -B /home/pg12/pgdata/pg_bk/
INFO: validate: "2020-06-28 10:41:30" backup and archive log files by CRC
INFO: backup "2020-06-28 10:41:30" is valid
查看备份集:
pg12@oracle-> pg_rman show -B /home/pg12/pgdata/pg_bk/
=====================================================================
StartTime EndTime Mode Size TLI Status
=====================================================================
2020-06-28 10:41:30 2020-06-28 10:41:39 INCR 2239MB 2 OK
2020-06-28 10:35:12 2020-06-28 10:35:33 FULL 2105MB 2 OK
删除指定时间的备份集:
pg12@oracle-> pg_rman delete "2020-06-28 10:41:39" -B /home/pg12/pgdata/pg_bk/
标签:PostgreSQL,pg12,28,pg,rman,backup,备份
From: https://www.cnblogs.com/yaoyangding/p/17023840.html