postgresql 使用pg_rman恢复还原数据库

1、备份准备工作

--注意pg_rman需要一个备份目录
[postgres@webtest bin]$ export BACKUP_PATH=/u01/backup
注意,这个可以直接编辑在.bash_profile文件里面
--创建并初始化目录
[postgres@webtest bin]$ mkdir -p /u01/backup
[postgres@webtest bin]$ ./pg_rman init -B /u01/backup

2、postgresql备份
--首先进行一次全备份
[postgres@webtest bin]$ ./pg_rman backup --backup-mode=full
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.
--检查备份情况
[postgres@webtest bin]$ ./pg_rman show
==========================================================
 StartTime           Mode  Duration    Size   TLI  Status
==========================================================
2014-07-24 15:38:35  FULL        0m  1603MB     1  DONE
2014-07-22 17:10:23  FULL        0m    93MB     1  OK
可以看到,已经存在一次备份,刚才备份的没有进行validate,所以是DONE状态的。
--进行一次增量备份
[postgres@webtest bin]$ ./pg_rman backup --backup-mode=incremental
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.
[postgres@webtest bin]$ ./pg_rman show
==========================================================
 StartTime           Mode  Duration    Size   TLI  Status
==========================================================
2014-07-24 15:51:11  INCR        0m  1610MB     1  DONE
2014-07-24 15:38:35  FULL        0m  1603MB     1  DONE
2014-07-22 17:10:23  FULL        0m    93MB     1  OK
--对备份进行验证
[postgres@webtest bin]$ ./pg_rman validate
INFO: validate: "2014-07-24 15:38:35" backup and archive log files by CRC
INFO: backup "2014-07-24 15:38:35" is valid
INFO: validate: "2014-07-24 15:51:11" backup and archive log files by CRC
INFO: backup "2014-07-24 15:51:11" is valid
[postgres@webtest bin]$ ./pg_rman show
==========================================================
 StartTime           Mode  Duration    Size   TLI  Status
==========================================================
2014-07-24 15:51:11  INCR        0m  1610MB     1  OK
2014-07-24 15:38:35  FULL        0m  1603MB     1  OK
2014-07-22 17:10:23  FULL        0m    93MB     1  OK
3、登录数据库删除数据
[postgres@webtest bin]$ psql mytest
psql (9.4.1)
Type "help" for help.

mytest=# \d
                  List of relations
 Schema |         Name         |   Type   |  Owner  
--------+----------------------+----------+----------
 public | child_t01            | table    | postgres
 public | child_t02            | table    | postgres
 public | child_t03            | table    | postgres
 public | child_t04            | table    | postgres
 public | child_t05            | table    | postgres
 public | child_t06            | table    | postgres
 public | parent               | table    | postgres
 public | parent_dpart_id_seq  | sequence | postgres
 public | parent_person_id_seq | sequence | postgres
(9 rows)

mytest=# \dt
           List of relations
 Schema |   Name    | Type  |  Owner  
--------+-----------+-------+----------
 public | child_t01 | table | postgres
 public | child_t02 | table | postgres
 public | child_t03 | table | postgres
 public | child_t04 | table | postgres
 public | child_t05 | table | postgres
 public | child_t06 | table | postgres
 public | parent    | table | postgres
 public | test_t    | table | postgres
(8 rows)

mytest=# select count(*) from test_t;
 count
-------
    28
(1 row)

--删除test_t表

mytest=# drop table test_t;
DROP TABLE

mytest=# \d
                  List of relations
 Schema |         Name         |   Type   |  Owner  
--------+----------------------+----------+----------
 public | child_t01            | table    | postgres
 public | child_t02            | table    | postgres
 public | child_t03            | table    | postgres
 public | child_t04            | table    | postgres
 public | child_t05            | table    | postgres
 public | child_t06            | table    | postgres
 public | parent               | table    | postgres
 public | parent_dpart_id_seq  | sequence | postgres
 public | parent_person_id_seq | sequence | postgres
(9 rows)

mytest=# \q
[postgres@webtest bin]$ psql mytest
psql (9.4.1)
Type "help" for help.

4、进行数据库恢复
--停止数据库
[postgres@webtest bin]$ pg_ctl stop
waiting for server to shut down.... done
server stopped
--查看备份情况
[postgres@webtest bin]$ ./pg_rman show
==========================================================
 StartTime           Mode  Duration    Size   TLI  Status
==========================================================
2014-07-24 15:51:11  INCR        0m  1610MB     1  OK
2014-07-24 15:38:35  FULL        0m  1603MB     1  OK
2014-07-22 17:10:23  FULL        0m    93MB     1  OK
--恢复到最近备份
[postgres@webtest bin]$ ./pg_rman restore -B /u01/backup --recovery-target-time "2014-07-24 15:58:55"
INFO: the recovery target timeline ID is not given
INFO: use timeline ID of current database cluster 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: "2014-07-24 15:38:35"
INFO: copying online WAL files and server log files
INFO: clearing restore destination
INFO: validate: "2014-07-24 15:38:35" backup and archive log files by SIZE
INFO: backup "2014-07-24 15:38:35" is valid
INFO: restoring database files from the full mode backup "2014-07-24 15:38:35"
INFO: searching incremental backup to be restored
INFO: validate: "2014-07-24 15:51:11" backup and archive log files by SIZE
INFO: backup "2014-07-24 15:51:11" is valid
INFO: restoring database files from the incremental mode backup "2014-07-24 15:51:11"
INFO: searching backup which contained archived WAL files to be restored
INFO: backup "2014-07-24 15:51:11" is valid
INFO: restoring WAL files from backup "2014-07-24 15:51:11"
INFO: backup "2014-07-24 15:58:55" is valid
INFO: restoring WAL files from backup "2014-07-24 15:58:55"
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.
--打开数据库
[postgres@webtest bin]$ pg_ctl start
server starting
LOG:  database system was interrupted; last known up at 2014-07-24 15:51:11 CST
LOG:  starting point-in-time recovery to 2014-07-24 15:58:55+08
LOG:  restored log file "000000010000000000000064" from archive
LOG:  redo starts at 0/64000090
LOG:  consistent recovery state reached at 0/640000B8
LOG:  restored log file "000000010000000000000065" from archive
LOG:  restored log file "000000010000000000000066" from archive
LOG:  restored log file "000000010000000000000067" from archive
LOG:  recovery stopping before commit of transaction 1864, time 2014-07-24 15:58:58.065224+08
LOG:  redo done at 0/67000028
LOG:  last completed transaction was at log time 2014-07-24 15:55:47.20653+08
LOG:  restored log file "000000010000000000000066" from archive
cp: cannot stat `/u01/postgresql/arch/00000002.history': No such file or directory
LOG:  selected new timeline ID: 2
cp: cannot stat `/u01/postgresql/arch/00000001.history': No such file or directory
LOG:  archive recovery complete
LOG:  autovacuum launcher started
LOG:  database system is ready to accept connections
在这里,可以看到数据库已经启处于等待连接状态

5、验证恢复情况
[postgres@webtest bin]$ psql mytest
mytest=# \dt
           List of relations
 Schema |   Name    | Type  |  Owner  
--------+-----------+-------+----------
 public | child_t01 | table | postgres
 public | child_t02 | table | postgres
 public | child_t03 | table | postgres
 public | child_t04 | table | postgres
 public | child_t05 | table | postgres
 public | child_t06 | table | postgres
 public | parent    | table | postgres
 public | test_t    | table | postgres
(8 rows)

mytest=# select count(*) from test_t;
 count
-------
    28
(1 row)

mytest=#
可以看到刚才的表已经恢复回来了!

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/29371470/viewspace-2123981/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/29371470/viewspace-2123981/

你可能感兴趣的:(postgresql 使用pg_rman恢复还原数据库)