PostgreSQL的 PITR實戰---運用 recovery_target_time


磨礪技術珠磯,踐行數據之道,追求卓越價值

回到上一級頁面: PostgreSQL基礎知識與基本操作索引頁     回到頂級頁面:PostgreSQL索引頁

看了很多的例子,沒有發現具體講 recovery_target_time的例子,於是自己作一個吧

在開始之前,先把postgresql.conf的配置設置好:

 wal_level = archive # minimal, archive, or hot_standby # (change requires restart) # - Archiving - archive_mode = on               # allows archiving to be done # (change requires restart) 
archive_command
= 'cp %p /usr/local/pgsql/arch/%f' # command to use to archive a logfile segment #archive_timeout = 0 # force a logfile segment switch after this # number of seconds; 0 disables

然后啟動PostgreSQL:

[postgres@pg201 pgsql]$ ./bin/pg_ctl -D ./data start

在開始備份前,先作一條數據:此時,時間大約是13:40之前:

postgres=# create table test(id integer); CREATE TABLE postgres=# insert into test values(100); INSERT 0 1                

然后開始進行基礎備份:

postgres=# select pg_start_backup('gao'); pg_start_backup ----------------- 
 0/2000020 (1 row)                

tar命令打包:

tar -cvf ./base.tar ./data

結束基礎備份:

postgres=# select pg_stop_backup(); NOTICE: pg_stop_backup complete, all required WAL segments have been archived pg_stop_backup ---------------- 
 0/20000A0 (1 row)                

此時大約是13:40左右,再插入一條數據:

postgres=# insert into test values(200); INSERT 0 1                

等待,到13:50左右,再插入一條數據:

postgres=# insert into test values(300); INSERT 0 1                

為了方便操作,進行一次強制的日志切換:

postgres=# select pg_switch_xlog(); pg_switch_xlog ---------------- 
 0/30002A0 (1 row) postgres=#                 

現在,模擬崩潰,強制殺掉進程:

[root@pg201 ~]# kill -s SIGQUIT Postmaster進程號                        

然后,原有data目錄改名保存,開始恢復過程:

[postgres@pg201 pgsql]$ mv ./data ./data.bak
[postgres@pg201 pgsql]$ tar -xvf base.tar ./data                    

把崩潰時后的online wal log 也准備好:

[postgres@pg201 pgsql]$ rm -rf ./data/pg_xlog [postgres@pg201 pgsql]$ cp -r ./data.bak/pg_xlog/ ./data [postgres@pg201 pgsql]$ cd ./data/pg_xlog [postgres@pg201 pg_xlog]$ [postgres@pg201 archive_status]$ pwd                    
/usr/local/pgsql/data/pg_xlog/archive_status [postgres@pg201 archive_status]$ rm -f * [postgres@pg201 archive_status]$ 

作一個 recovery.conf文件,指明要恢復到13:45,也就是200數據已經出現,300數據尚未插入的時候

[postgres@pg201 data]$ pwd                        
/usr/local/pgsql/data [postgres@pg201 data]$ cat recovery.conf restore_command = 'cp /usr/local/pgsql/arch/%f %p' recovery_target_time = '2013-08-07 13:45:00+08' [postgres@pg201 data]$ 

再次啟動PostgreSQL,發生了恢復:

[postgres@pg201 pgsql]$ ./bin/pg_ctl -D ./data start pg_ctl: another server might be running; trying to start server anyway server starting [postgres@pg201 pgsql]$ LOG: database system was interrupted; last known up at 2013-08-07 13:39:51 CST LOG: starting point-in-time recovery to 2013-08-07 13:45:00+08 LOG: restored log file "000000010000000000000002" from archive LOG: redo starts at 0/2000078 LOG: consistent recovery state reached at 0/3000000 LOG: restored log file "000000010000000000000003" from archive LOG: recovery stopping before commit of transaction 1685, time 2013-08-07 13:51:05.407695+08 LOG: redo done at 0/3000240 LOG: last completed transaction was at log time 2013-08-07 13:40:44.338862+08                                        
cp: cannot stat `/usr/local/pgsql/arch/00000002.history': No such file or directory 
LOG:  selected new timeline ID: 2                                        
cp: cannot stat `/usr/local/pgsql/arch/00000001.history': No such file or directory 
LOG: archive recovery complete LOG: autovacuum launcher started LOG: database system is ready to accept connections 

驗證:

[postgres@pg201 ~]$ cd /usr/local/pgsql [postgres@pg201 pgsql]$ ./bin/psql psql (9.1.2) Type "help" for help. postgres=# select * from test; id -----
 100
 200 (2 rows) postgres=# \q

回到上一級頁面: PostgreSQL基礎知識與基本操作索引頁     回到頂級頁面:PostgreSQL索引頁

磨礪技術珠磯,踐行數據之道,追求卓越價值


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM