您的位置:首页 > 博客中心 > 数据库 >

PostgreSQL备份之pg_rman

时间:2022-03-14 14:16

一、 安装

PostgreSQL版本是9.4.4

# git clone -b REL9_4_STABLE https://github.com/ossc-db/pg_rman.git
Initialized empty Git repository in /opt/pg_rman/.git/
remote: Counting objects: 1939, done.
remote: Total 1939 (delta 0), reused 0 (delta 0), pack-reused 1939
Receiving objects: 100% (1939/1939), 702.67 KiB | 311 KiB/s, done.
Resolving deltas: 100% (1393/1393), done.
# yum install -y pam-devel readline-devel zlib-devel
# make
# make install
/bin/mkdir -p ‘/opt/pg94/bin‘
/usr/bin/install -c  pg_rman ‘/opt/pg94/bin‘
# pg_rman --version
pg_rman 1.3.1

二、使用

首先需要初始化备份目录

# mkdir -p /data/pg_backup
# mkdir -p /data/test_xlog_94/fulltime
# pg_rman  init --backup-path=/data/pg_backup/
WARNING: ARCLOG_PATH is not set yet
DETAIL: The archive_command is not set in postgresql.conf.
HINT: Please set ARCLOG_PATH in pg_rman.ini or environmental variable.
# vim /data/pg_backup/pg_rman.ini
ARCLOG_PATH=‘/data/test_xlog_94/fulltime‘

在master节点上制作一个全备,在从库上需要指定--host, --standby-host=172.17.5.47 --standby-port=5432

#  pg_rman backup --backup-mode=full --backup-path=/data/pg_backup/ --pgdata=/data/test_pgdata_94/ -U postgres -d postgres                                             
INFO: copying database files
NOTICE:  pg_stop_backup complete, all required WAL segments have been archived
INFO: copying archived WAL files
INFO: backup complete
HINT: Please execute ‘pg_rman validate‘ to verify the files are correctly copied.
# pg_rman show detail --backup-path=/data/pg_backup/
============================================================================================================
 StartTime           Mode  Duration    Data  ArcLog  SrvLog   Total  Compressed  CurTLI  ParentTLI  Status  
============================================================================================================
2015-09-07 02:52:22  FULL        0m   422MB   106kB    ----   394MB       false       1          0  OK

使用备份进行恢复

# mkdir restore_test
# pg_rman restore --backup-path=/data/pg_backup/ --pgdata=/data/restore_test/
WARNING: could not open pg_controldata file "/data/restore_test//global/pg_control": No such file or directory
WARNING: could not open pg_controldata file "/data/restore_test//global/pg_control": No such file or directory
INFO: backup "2015-09-07 02:52:22" 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: "2015-09-07 02:52:22"
INFO: copying online WAL files and server log files
INFO: clearing restore destination
INFO: validate: "2015-09-07 02:52:22" backup and archive log files by SIZE
INFO: backup "2015-09-07 02:52:22" is valid
INFO: restoring database files from the full mode backup "2015-09-07 02:52:22"
INFO: searching incremental backup to be restored
INFO: searching backup which contained archived WAL files to be restored
INFO: backup "2015-09-07 02:52:22" is valid
INFO: restoring WAL files from backup "2015-09-07 02:52:22"
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.

由于是用root用户进行备份和恢复的,目录权限也是root,需要修改为postgres

# ls -ld /data/restore_test/
drwxr-xr-x. 18 root root 4096 Sep  7 03:00 restore_test/
# cat /data/restore_test/recovery.conf 
# recovery.conf generated by pg_rman 1.3.1
restore_command = ‘cp /data/test_pgdata_94/pg_xlog/%f %p‘
recovery_target_timeline = ‘1‘
# chown -R postgres:postgres /data/restore_test/
# chmod 0700 /data/restore_test/
# ls -ld /data/restore_test/
drwx------. 18 postgres postgres 4096 Sep  7 03:05 /data/restore_test/

将端口修改为5433,并启动恢复的数据库

$ pg_ctl -D /data/restore_test/ start
server starting
[postgres@QA-5-45 data]$ [    2015-09-07 07:05:40.291 UTC 31889 55ed3744.7c91 1 0]LOG:  redirecting log output to logging collector process
[    2015-09-07 07:05:40.291 UTC 31889 55ed3744.7c91 2 0]HINT:  Future log output will appear in directory "pg_log".
$ psql -p 5433
psql (9.4.4)
Type "help" for help.

恢复到指定时间点的话,执行restore的时候使用如下参数:

  --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

会将相应的参数写到recovery.conf里面


参考:

本类排行

今日推荐

热门手游