本文目录
- 简介
- pg_rman的用法
- pg_rman使用前提
- 创建备份目录
- 设置环境变量
- pg_rman init 初始化 - pg_rman两种备份方式
- 全量备份
- 增量备份 - pg_rman删除备份方式
- pg_rman数据两种恢复方式
- 原地覆盖式恢复
- 设置新的$PGDATA目录恢复
简介
pg_rman是一款专门为postgresql设计的在线备份恢复的工具。其支持在线(热备份)和基于时间点备份方式。
postgresql10以上版本都是自带pg_rman工具的,如果没有请单独安装。
pg_rman的用法
此处无需记忆,对文章后面使用的参数不理解时,再回来看。
pg_rman manage backup/recovery of PostgreSQL database.
Usage:
pg_rman OPTION init
pg_rman OPTION backup
pg_rman OPTION restore
pg_rman OPTION show [DATE]
pg_rman OPTION show detail [DATE]
pg_rman OPTION validate [DATE]
pg_rman OPTION delete DATE
pg_rman OPTION purge
Common Options:
-D, --pgdata=PATH location of the database storage area
-A, --arclog-path=PATH location of archive WAL storage area
-S, --srvlog-path=PATH location of server log storage area
-B, --backup-path=PATH location of the backup storage area
-c, --check show what would have been done
-v, --verbose show what detail messages
-P, --progress show progress of processed files
Backup options:
-b, --backup-mode=MODE full, incremental, or archive
-s, --with-serverlog also backup server log files
-Z, --compress-data compress data backup with zlib
-C, --smooth-checkpoint do smooth checkpoint before backup
-F, --full-backup-on-error switch to full backup mode
if pg_rman cannot find validate full backup
on current timeline
NOTE: this option is only used in --backup-mode=incremental or archive.
--keep-data-generations=NUM keep NUM generations of full data backup
--keep-data-days=NUM keep enough data backup to recover to N days ago
--keep-arclog-files=NUM keep NUM of archived WAL
--keep-arclog-days=DAY keep archived WAL modified in DAY days
--keep-srvlog-files=NUM keep NUM of serverlogs
--keep-srvlog-days=DAY keep serverlog modified in DAY days
--standby-host=HOSTNAME standby host when taking backup from standby
--standby-port=PORT standby port when taking backup from standby
Restore options:
--recovery-target-time time stamp up to which recovery will proceed
--recovery-target-xid transaction ID up to which recovery will proceed
--recovery-target-inclusive whether we stop just after the recovery target
--recovery-target-timeline recovering into a particular timeline
--hard-copy copying archivelog not symbolic link
Catalog options:
-a, --show-all show deleted backup too
Delete options:
-f, --force forcibly delete backup older than given DATE
Connection options:
-d, --dbname=DBNAME database to connect
-h, --host=HOSTNAME database server host or socket directory
-p, --port=PORT database server port
-U, --username=USERNAME user name to connect as
-w, --no-password never prompt for password
-W, --password force password prompt
Generic options:
-q, --quiet don't show any INFO or DEBUG messages
--debug show DEBUG messages
--help show this help, then exit
--version output version information, then exit
pg_rman使用前提
1、创建备份目录
$ mkdir /database-backup/postgresql-backup/{fullbackup,walbackup,pg_srvlog} -p
$ chown -R postgres.postgres /database-backup/postgresql-backup/
2、设置环境变量
$ vim .bash_profile
export BACKUP_PATH=/database-backup/postgresql-backup/fullbackup
export ARCLOG_PATH=/database-backup/postgresql-backup/walbackup
export SRVLOG_PATH=/database-backup/postgresql-backup/pg_srvlog
$ source .profile
3、修改postgresql.conf配置文件
$ vim data/postgresql.conf
archive_mode = on
archive_command = 'DATE=`date +%Y%m%d`; DIR="/database-backup/postgresql-backup/walbackup/"; (test -d $DIR || mkdir -p $DIR) && cp %p $DIR/%f'
3、pg_rman init 初始化
$ pg_rman init
INFO: ARCLOG_PATH is set to '/database-backup/postgresql-backup/walbackup'
INFO: SRVLOG_PATH is set to '/database-backup/postgresql-backup/pg_srvlog'
pg_rman两种备份方式
全量备份
1、备份
$ pg_rman backup --backup-mode=full --with-serverlog --progress
INFO: copying database files
Processed 1298 of 1298 files, skipped 0
INFO: copying archived WAL files
Processed 3 of 3 files, skipped 0
INFO: copying server log files
INFO: backup complete
INFO: Please execute 'pg_rman validate' to verify the files are correctly copied.
2、校验备份集
重点注意:pg_rman 的备份必须都是经过验证过的,否则不能进行恢复和增量备份。
$ pg_rman validate
INFO: validate: "2018-10-14 14:55:34" backup, archive log files and server log files by CRC
INFO: backup "2018-10-14 14:55:34" is valid
3、pg_rman 列出备份集
$ pg_rman show
=====================================================================
StartTime EndTime Mode Size TLI Status
=====================================================================
2020-04-16 12:37:12 2020-04-16 12:37:51 FULL 541MB 2 OK
查看生成的备份文件所在目录
$ ls -l /database-backup/postgresql-backup/fullbackup/
total 20
drwx------ 3 postgres postgres 4096 Oct 14 14:55 20181014
drwx------ 4 postgres postgres 4096 Oct 14 14:53 backup
-rw-rw-r-- 1 postgres postgres 119 Oct 14 14:53 pg_rman.ini
-rw-rw-r-- 1 postgres postgres 40 Oct 14 14:53 system_identifier
drwx------ 2 postgres postgres 4096 Oct 14 14:53 timeline_history
增量备份
重点注意:
增量备份是基于文件系统的update time时间线.
增量备份前提:
- 必须要有个对应的全库备份。
- 当全库备份后需要验证备份集。
1、验证备份集
如上,我们已得到一个全库备份。因此只需要从验证备份开始。
$ pg_rman validate
INFO: validate: "2018-10-14 15:59:57" backup and archive log files by CRC
INFO: backup "2018-10-14 15:59:57" is valid
2、备份
$ pg_rman backup --backup-mode incremental --progress --compress-data
INFO: copying database files
Processed 1290 of 1290 files, skipped 1262
INFO: copying archived WAL files
Processed 46 of 46 files, skipped 43
INFO: backup complete
INFO: Please execute 'pg_rman validate' to verify the files are correctly copied.
3、再次校验备份集
$ pg_rman validate
INFO: validate: "2020-04-16 13:18:29" backup and archive log files by CRC
INFO: backup "2020-04-16 13:18:29" is valid
4、pg_rman 列出备份集
$ pg_rman show
=====================================================================
StartTime EndTime Mode Size TLI Status
=====================================================================
2020-04-16 13:18:29 2020-04-16 13:18:32 INCR 54kB 2 OK
2020-04-16 12:37:12 2020-04-16 12:37:51 FULL 541MB 2 OK
删除备份
删除备份有两种方式
1、直接在fullback文件夹里面删除对应时间点的备份
2、使用 pg_rman delete -f "时间点" 删除。再删除增量备份的同时,自动会将全量备份也删除掉。如下
pg_rman delete -f '2018-10-14 15:59:57'
INFO: delete the backup with start time: "2018-10-14 15:59:57"
INFO: delete the backup with start time: "2018-10-14 14:55:34"
$ pg_rman show
=====================================================================
StartTime EndTime Mode Size TLI Status
=====================================================================
pg_rman数据两种恢复方式
重点注意:恢复时需要先停库。
原地覆盖式恢复
pg_rman restore -B /postgresql-backup/backups/ --recovery-target-time "2020-04-16 13:18:32" --hard-copy
--如果不指定recovery-target-time,则恢复到最新时间
--如果不指定hard-copy,则归档日志目录里的归档日志是使用的硬连接指向备份目录中的归档日志,加了这个参数的话,则是直接把备份目录中的归档日志拷贝到归档日志目录
INFO: the recovery target timeline ID is not given
INFO: use timeline ID of current database cluster as recovery target: 2
INFO: calculating timeline branches to be used to recovery target point
INFO: searching latest full backup which can be used as restore start point
INFO: found the full backup can be used as base in recovery: "2020-04-16 12:37:12"
INFO: copying online WAL files and server log files
INFO: clearing restore destination
INFO: validate: "2020-04-16 12:37:12" backup, archive log files and server log files by SIZE
INFO: backup "2020-04-16 12:37:12" is valid
INFO: restoring database files from the full mode backup "2020-04-16 12:37:12"
INFO: searching incremental backup to be restored
INFO: validate: "2020-04-16 13:18:29" backup and archive log files by SIZE
INFO: backup "2020-04-16 13:18:29" is valid
INFO: restoring database files from the incremental mode backup "2020-04-16 13:18:29"
INFO: searching backup which contained archived WAL files to be restored
INFO: backup "2020-04-16 13:18:29" is valid
INFO: restoring WAL files from backup "2020-04-16 13:18:29"
INFO: restoring online WAL files and server log files
INFO: generating recovery.conf
INFO: restore complete
HINT: Recovery will start automatically when the PostgreSQL server is started.
设置新的$PGDATA目录式恢复
1、创建新的data目录,并修改权限
$ sudo mkdir -p /restore_test/data # 创建数据目录。
$ sudo chown -R postgres:postgres /restore_test/
$ chmod 700 ./data #权限必须修改,否则会报错。
2、修改postgres用户的环境变量
vim ~/.bash_profile
export PGDATA=/restore_test/data
3、恢复
pg_rman restore -B /postgresql-backup/backups/ --recovery-target-time "2020-04-16 13:18:32" --hard-copy
WARNING: pg_controldata file "/restore_test/data/global/pg_control" does not exist
INFO: the recovery target timeline ID is not given
INFO: use timeline ID of latest full backup as recovery target: 2
INFO: calculating timeline branches to be used to recovery target point
INFO: searching latest full backup which can be used as restore start point
INFO: found the full backup can be used as base in recovery: "2020-04-16 12:37:12"
INFO: copying online WAL files and server log files
INFO: clearing restore destination
INFO: validate: "2020-04-16 12:37:12" backup, archive log files and server log files by SIZE
INFO: backup "2020-04-16 12:37:12" is valid
INFO: restoring database files from the full mode backup "2020-04-16 12:37:12"
INFO: searching incremental backup to be restored
INFO: validate: "2020-04-16 13:18:29" backup and archive log files by SIZE
INFO: backup "2020-04-16 13:18:29" is valid
INFO: restoring database files from the incremental mode backup "2020-04-16 13:18:29"
INFO: searching backup which contained archived WAL files to be restored
INFO: backup "2020-04-16 13:18:29" is valid
INFO: restoring WAL files from backup "2020-04-16 13:18:29"
INFO: restoring online WAL files and server log files
INFO: generating recovery.conf
INFO: restore complete
HINT: Recovery will start automatically when the PostgreSQL server is started.
网友评论