由于需要,对数据库的基于时间点恢复(PITR)做了过程记录,以此来记录过程和问题。
OS : fedora 18
工作流程:
第一次实验:
数据库:PostgreSQL9.0.13
1、修改配置文件 postgresql.conf
archive_mode = on
archive_command = 'cp -i %p /home/sure/mywork/archive/%f'
wal_level = hot_standby
2、启动数据库
我是在初始化之后,直接修改的数据库,进而直接启动数据库的。
./pg_ctl -D ../data -l logfile start
3、基于文件级别的持续备份
a.模拟数据
CREATE TABLE tab1(a1 int);
INSERT INTO tab1 VALUES (1),(2),(3);
b.备份
postgres=# SELECT now();
now
-------------------------------
2013-11-21 13:35:12.493791+08
(1 row)
postgres=# select pg_start_backup('bak_ws_2013-11-21 13:35:12');
pg_start_backup
-----------------
0/2000020
(1 row)
c.打包data
tar -cvzf data.tar data
d.结束并切换归档
postgres=# select pg_stop_backup();
NOTICE: pg_stop_backup complete, all required WAL segments have been archived
pg_stop_backup
----------------
0/20000D8
(1 row)
postgres=# select pg_switch_xlog();
pg_switch_xlog
----------------
0/3000000
(1 row)
这时会再data下产生一个backup_label的文件,记录了可以查看内容有checkpoint时间,基础备份的开始和结束时间,以及标签名称等。例如这样:
START WAL LOCATION: 0/2000020 (file 000000010000000000000002)
STOP WAL LOCATION: 0/20000D8 (file 000000010000000000000002)
CHECKPOINT LOCATION: 0/2000058
START TIME: 2013-11-21 13:35:40 CST
LABEL: bak_ws_2013-11-21 13:35:12
STOP TIME: 2013-11-21 13:36:17 CST
e.再次插入数据
postgres=# CREATE TABLE tab2(a1 int);
CREATE TABLE
postgres=# INSERT INTO tab2 VALUES (1),(2),(3);
INSERT 0 3
4、模拟毁坏并进行恢复
a.结束PG服务
postgres=# \q
$ ./pg_ctl -D ../data stop
waiting for server to shut down.... done
server stopped
b.模拟数据库毁坏
rm -rf data
c.恢复备份文件data.tar
$ tar xvf data.tar
d.删除pg_xlog文件夹并重建
$ rm -rf pg_xlog
$ mkdir -p pg_xlog/archive_status
e.拷贝recovery.conf文件并修改
我的文件是这样的:
restore_command = 'cp /home/sure/mywork/archivedir/%f "%p"'
archive_cleanup_command='pg_archivecleanup /home/sure/mywork/archivedir %r'
recovery_target_time='2013-11-21 13:35:12'
recovery_target_time这是用户自行设定的,如果不写则会恢复到之前接收到的最后一个归档文件。
f.重启数据库查看恢复结果
[sure@localhost bin]$ ./pg_ctl -D ../data -l logfile3 start
server starting
[sure@localhost bin]$ ./psql postgres sure
psql (9.0.13)
Type "help" for help.
postgres=# \d
List of relations
Schema | Name | Type | Owner
--------+------+-------+-------
public | tab1 | table | sure
(1 row)
我在此仅恢复到第一次模拟数据(即3.a)。下面是我未写recovery_target_time的结果:
$ ./pg_ctl -D ../data -l logfile4 start
server starting
$ ./psql postgres sure
psql (9.0.13)
Type "help" for help.
postgres=# \d
List of relations
Schema | Name | Type | Owner
--------+------+-------+-------
public | tab1 | table | sure
public | tab2 | table | sure
(2 rows)
postgres=# SELECT * from tab2;
a1
----
1
2
3
(3 rows)
注:结束后,recovery.conf会改名变成recovery.done。
要注意的是,如果恢复过一次,并设置时间点,下次直接修改recovery_target_time,不会发生效果。