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