一、 安装
PostgreSQL版本是9.4.4
1
2
3
4
5
6
7
8
9
10
11
12
13
|
# 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
|
二、使用
1. 准备
首先需要初始化备份目录
1
2
3
4
5
6
7
8
|
# 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'
|
2. 全备份
在master节点上制作一个全备,在从库上需要指定--host, --standby-host=172.17.5.47 --standby-port=5432
1
2
3
4
5
6
7
8
9
10
11
|
# 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
|
3. 使用全备进行恢复
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
# 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//<a href="
http:
//so
.21ops.com
/cse/search
?s=9181936462520079739&entry=1&q=global
" class="
bdcs-inlinelink
" target="
_blank
">global</a>/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
1
2
3
4
5
6
7
8
9
10
|
# 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,并启动恢复的数据库
1
2
3
4
5
6
7
|
$ 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里面
5. archive模式进行增量备份
首先需要制作全备(如上),并对全备进行认证
1
2
3
|
# pg_rman validate --backup-path=/data/pg_backup/
INFO: validate:
"2015-09-07 02:52:22"
backup and archive log files by CRC
INFO: backup
"2015-09-07 02:52:22"
is valid
|
在进行增量备份
1
2
3
4
5
6
7
8
9
10
|
# pg_rman backup --backup-mode=archive --backup-path=/data/pg_backup/ --pgdata=/data/test_pgdata_94/ -U postgres -d postgres --progress
INFO: copying archived WAL files
Processed 7 of 7 files, skipped 7
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:57:07 ARCH 0m ---- 0B ---- 0B
false
1 0 DONE
|
参考:http://ossc-db.github.io/pg_rman/index.html
如果想深入体验LINUX系统的新手,也可以先下载一个方德Linux软件中心试用一下。
免费下载地址:http://www.nfs-cloud.cn:81/appCenter/open/softcenter