众所周知,Oracle下可以使用rman进行物理备份,支持数据库的全量、增量、归档的备份模式;而PostgreSQL作为开源数据库,也推出了开源功工具pg_rman,同样具备了oracle rman大部分的功能。本文写的很早,近期才翻出来,挂到博客上,希望能帮助需要的朋友。
1、下载安装软件
[root@mydb01 ~]# wget https://github.com/ossc-db/pg_rman/releases/download/V1.3.6/pg_rman-1.3.6-1.pg10.rhel7.x86_64.rpm
[root@mydb01 ~]# yum -y localinstall pg_rman-1.3.6-1.pg10.rhel7.x86_64.rpm
2、配置PG参数
首先设置postgres用户的profile,加入以下内容:
-bash-4.2$ vi .bash_profile
export PATH=/usr/pgsql-10/bin:$PATH
export LD_LIBRARY_PATH=/usr/pgsql-10/lib
export BACKUP_PATH=/u02/backups
-bash-4.2$ vi pg_hba.conf
host all all 0.0.0.0/0 md5
-bash-4.2$ vi postgresql.conf
listen_addresses = '*'
port = 5432
wal_level = archive ------->>若为HOT STANDBY环境则此处设置为hot_standby
archive_mode = on
archive_command = 'test ! -f /u02/archivelog/%f && cp %p /u02/archivelog/%f'
[root@mydb01 ~]# systemctl restart postgresql10.service
3、初始化备份环境
[root@mydb01 ~]# su - postgres
-bash-4.2$ pg_rman init
4、全备份
-bash-4.2$ pg_rman backup --backup-mode=full
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.
INFO: start deleting old archived WAL files from ARCLOG_PATH (keep files = 10, keep days = 10)
INFO: the threshold timestamp calculated by keep days is "2018-03-18 00:00:00"
INFO: start deleting old backup (keep generations = 3 AND keep after = 2017-11-28 00:00:00)
INFO: does not include the backup just tak
备份完成后,可以使用下面的命令进行验证:
-bash-4.2$ pg_rman validate
INFO: validate: "2018-03-28 16:21:34" backup and archive log files by CRC
INFO: backup "2018-03-28 16:21:34" is valid
-bash-4.2$ pg_rman show
=====================================================================
StartTime EndTime Mode Size TLI Status
=====================================================================
2018-03-28 16:21:34 2018-03-28 16:21:36 FULL 4465kB 1 OK
5、增量备份
-bash-4.2$ pg_rman backup --backup-mode=incremental --with-serverlog
INFO: copying database files
INFO: copying archived WAL files
INFO: copying server log files
INFO: backup complete
INFO: Please execute 'pg_rman validate' to verify the files are correctly copied.
INFO: start deleting old archived WAL files from ARCLOG_PATH (keep files = 10, keep days = 10)
INFO: the threshold timestamp calculated by keep days is "2018-03-18 00:00:00"
INFO: start deleting old server files from SRVLOG_PATH (keep files = 10, keep days = 10)
INFO: the threshold timestamp calculated by keep days is "2018-03-18 00:00:00"
INFO: start deleting old backup (keep generations = 3 AND keep after = 2017-11-28 00:00:00)
INFO: does not include the backup just taken
INFO: backup "2018-03-28 16:21:34" should be kept
DETAIL: This is the 1st latest full backup.
-bash-4.2$ pg_rman validate
INFO: validate: "2018-03-28 16:27:57" backup, archive log files and server log files by CRC
INFO: backup "2018-03-28 16:27:57" is valid
-bash-4.2$ pg_rman show
=====================================================================
StartTime EndTime Mode Size TLI Status
=====================================================================
2018-03-28 16:27:57 2018-03-28 16:27:59 INCR 992kB 1 OK
2018-03-28 16:21:34 2018-03-28 16:21:36 FULL 4465kB 1 OK
6、归档备份
-bash-4.2$ pg_rman backup --backup-mode=archive --with-serverlog
INFO: copying archived WAL files
INFO: copying server log files
INFO: backup complete
INFO: Please execute 'pg_rman validate' to verify the files are correctly copied.
INFO: start deleting old archived WAL files from ARCLOG_PATH (keep files = 10, keep days = 10)
INFO: the threshold timestamp calculated by keep days is "2018-03-18 00:00:00"
INFO: start deleting old server files from SRVLOG_PATH (keep files = 10, keep days = 10)
INFO: the threshold timestamp calculated by keep days is "2018-03-18 00:00:00"
INFO: start deleting old backup (keep generations = 3 AND keep after = 2017-11-28 00:00:00)
INFO: does not include the backup just taken
INFO: backup "2018-03-28 16:27:57" should be kept
DETAIL: This belongs to the 1st latest full backup.
INFO: backup "2018-03-28 16:21:34" should be kept
DETAIL: This is the 1st latest full backup.
-bash-4.2$ pg_rman show
=====================================================================
StartTime EndTime Mode Size TLI Status
=====================================================================
2018-03-28 16:28:51 2018-03-28 16:28:52 ARCH 26kB 1 DONE
2018-03-28 16:27:57 2018-03-28 16:27:59 INCR 992kB 1 OK
2018-03-28 16:21:34 2018-03-28 16:21:36 FULL 4465kB 1 OK
7、查看备份集信息
-bash-4.2$ pg_rman show 2018-03-28 16:21:34
# configuration
BACKUP_MODE=FULL
FULL_BACKUP_ON_ERROR=false
WITH_SERVERLOG=false
COMPRESS_DATA=true
# result
TIMELINEID=1
START_LSN=0/02000028
STOP_LSN=0/02000130
START_TIME='2018-03-28 16:21:34'
END_TIME='2018-03-28 16:21:36'
RECOVERY_XID=555
RECOVERY_TIME='2018-03-28 16:21:36'
TOTAL_DATA_BYTES=24535637
READ_DATA_BYTES=24535483
READ_ARCLOG_BYTES=33554741
WRITE_BYTES=4465406
BLOCK_SIZE=8192
XLOG_BLOCK_SIZE=8192
STATUS=OK
-bash-4.2$ pg_rman show 2018-03-28 16:27:57
# configuration
BACKUP_MODE=INCREMENTAL
FULL_BACKUP_ON_ERROR=false
WITH_SERVERLOG=true
COMPRESS_DATA=true
# result
TIMELINEID=1
START_LSN=0/04000028
STOP_LSN=0/040000f8
START_TIME='2018-03-28 16:27:57'
END_TIME='2018-03-28 16:27:59'
RECOVERY_XID=561
RECOVERY_TIME='2018-03-28 16:27:59'
TOTAL_DATA_BYTES=32585789
READ_DATA_BYTES=8186187
READ_ARCLOG_BYTES=33554741
READ_SRVLOG_BYTES=518
WRITE_BYTES=992901
BLOCK_SIZE=8192
XLOG_BLOCK_SIZE=8192
STATUS=OK
8、删除备份
-bash-4.2$ pg_rman delete "2018-03-28 16:28:51"
WARNING: cannot delete backup with start time "2018-03-28 16:28:51"
DETAIL: This is the archive backup necessary for successful recovery.
WARNING: cannot delete backup with start time "2018-03-28 16:27:57"
DETAIL: This is the incremental backup necessary for successful recovery.
WARNING: cannot delete backup with start time "2018-03-28 16:21:34"
DETAIL: This is the latest full backup necessary for successful recovery.
9、恢复数据库
删除/var/lib/pgsql/10/data文件夹或者清空,然后进行恢复操作。如果data文件夹是新建的,则必须注意data目录的属性为700,否则在恢复的时候会报错。
-bash-4.2$ pg_rman restore --recovery-target-time "2018-03-29 14:35:53"
WARNING: pg_controldata file "/var/lib/pgsql/10/data/global/pg_control" does not exist
INFO: backup "2018-03-29 14:30:11" is valid
INFO: the recovery target timeline ID is not given
INFO: use timeline ID of latest full backup as recovery target: 1
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: "2018-03-29 14:30:11"
INFO: copying online WAL files and server log files
INFO: clearing restore destination
INFO: validate: "2018-03-29 14:30:11" backup and archive log files by SIZE
INFO: backup "2018-03-29 14:30:11" is valid
INFO: restoring database files from the full mode backup "2018-03-29 14:30:11"
INFO: searching incremental backup to be restored
INFO: searching backup which contained archived WAL files to be restored
INFO: backup "2018-03-29 14:30:11" is valid
INFO: restoring WAL files from backup "2018-03-29 14:30:11"
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.
-bash-4.2$ pg_ctl start
waiting for server to start....2018-03-29 14:43:47.613 CST [1465] LOG: listening on IPv4 address "0.0.0.0", port 5432
2018-03-29 14:43:47.613 CST [1465] LOG: listening on IPv6 address "::", port 5432
2018-03-29 14:43:47.619 CST [1465] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2018-03-29 14:43:47.637 CST [1465] LOG: listening on Unix socket "/tmp/.s.PGSQL.5432"
2018-03-29 14:43:47.650 CST [1465] LOG: redirecting log output to logging collector process
2018-03-29 14:43:47.650 CST [1465] HINT: Future log output will appear in directory "log".
. done
server started
更详细的pg_rman用法,请参考官方手册。