使用 PostgreSQL 時間點恢復(Point-In-Time Recovery)的多種數據恢復技術
引言
本文面向那些已經具備一定數據庫經驗、但希望進一步學習 PostgreSQL 基礎知識的初學者。假設已在 Ubuntu 環境中正確安裝 PostgreSQL,本文的全部操作基于 PostgreSQL 16(開發版本)與 Ubuntu 22.10 系統完成。文章將介紹三種常用的數據恢復方法:
- 恢復至最新狀態。
- 基于日志序列號(LSN, Log Sequence Number)的恢復。
- 基于時間戳的恢復。
時間點恢復(Point-In-Time Recovery,簡稱 PITR)是一種通過重放預寫日志(Write Ahead Logging,WAL)文件,將數據庫恢復至指定時間點狀態的技術手段。
為說明該過程,可構建兩個數據庫副本,在其中一個數據庫中插入數據,并將生成的 WAL 文件進行歸檔,隨后令另一數據庫重放該日志文件,以實現狀態同步。
此過程類似于數據庫因故障導致部分數據丟失的情形,可依靠 WAL 文件將系統恢復至故障前狀態。重放 WAL 文件相當于自上一次檢查點起,按順序重新執行全部數據庫操作,直至指定時間點,從而實現數據庫的精確恢復。
開始使用
首先,創建一個新的數據庫,并建立用于存放歸檔文件的文件夾。
$ initdb -D data/
$ mkdir archive/
隨后,編輯該數據庫的 postgresql.conf 配置文件。
port = 5432
wal_level = replica
archive_mode = on
archive_command = 'cp %p $HOME/pg/archive/%f'
archive_timeout = 60
接著,復制整個數據庫集群(由于此時尚未創建任何表或數據,實際上并無可備份的內容)。
$ cp -rp data/* data.backup/
啟動主數據庫。
$ pg_ctl -D data/ -l data.log start
$ createuser -s postgres
在數據庫中插入測試數據。
$ psql -U postgres -c "create database test"
CREATE DATABASE
$ psql -U postgres -d test -c "create table t1 (id int, val text)"
CREATE TABLE
$ psql -U postgres -d test -c "insert into t1 values (1, 'hello')"
INSERT 0 1
$ psql -U postgres -d test -c "insert into t1 values (2, 'world')"
INSERT 0 1
$ psql -U postgres -d test -c "insert into t1 values (3, 'foo')"
INSERT 0 1
$ psql -U postgres -d test -c "insert into t1 values (4, 'bar')"
INSERT 0 1
檢查歸檔目錄,確認歸檔命令執行是否正常。若未生成歸檔文件,可手動觸發數據庫生成未完成的 WAL 文件。但需注意,未完成的 WAL 文件與已完成文件大小一致,頻繁執行可能導致數據庫體積快速膨脹。
$ psql -U postgres -c "select pg_switch_wal()"
pg_switch_wal
---------------
0/187BBC8
(1 row)
$ ls -l archive/
total 16384
-rw------- 1 tristen tristen 16777216 Apr 1 09:49 000000010000000000000001
停止主服務器。
$ pg_ctl -D data stop
編輯副本數據庫的 postgresql.conf 文件,將 recovery_target_timeline 參數設置為 "latest",以便讓該副本重放 WAL 文件到最新的狀態。
port = 5433
archive_mode = off
restore_command = 'cp $HOME/pg/archive/%f %p'
recovery_target_timeline = 'latest'
創建 recovery.signal 文件,用于指示數據庫以恢復模式啟動。
$ touch data.backup/recovery.signal
啟動副本服務器,此時系統將進入恢復模式并開始重放 WAL 文件。
$ pg_ctl -D data.backup -l data.backup.log start
$ tail data.backup.log
2023-04-14 09:50:16.459 PDT [21530] LOG: redo done at 0/2000060 system usage: CPU: user: 0.00 s, system: 0.01 s, elapsed: 0.03 s
2023-04-14 09:50:16.459 PDT [21530] LOG: last completed transaction was at log time 2023-04-14 09:49:13.53674-07
2023-04-14 09:50:16.468 PDT [21530] LOG: restored log file "000000010000000000000002" from archive
cp: cannot stat '/home/tristen/pg/archive/00000002.history': No such file or directory
2023-04-14 09:50:16.479 PDT [21530] LOG: selected new timeline ID: 2
cp: cannot stat '/home/tristen/pg/archive/00000001.history': No such file or directory
2023-04-14 09:50:16.496 PDT [21530] LOG: archive recovery complete
2023-04-14 09:50:16.497 PDT [21528] LOG: checkpoint starting: end-of-recovery immediate wait
2023-04-14 09:50:16.513 PDT [21528] LOG: checkpoint complete: wrote 905 buffers (5.5%); 0 WAL file(s) added, 0 removed, 2 recycled; write=0.006 s, sync=0.007 s, total=0.018 s; sync files=259, longest=0.001 s, average=0.001 s; distance=28136 kB, estimate=28136 kB; lsn=0/3000028, redo lsn=0/3000028
2023-04-14 09:50:16.515 PDT [21525] LOG: database system is ready to accept connections
最后,檢查副本數據庫中的數據。
$ psql -U postgres -d test -c "select * from t1" -p 5433
id | val
----+-------
1 | hello
2 | world
3 | foo
4 | bar
(4 rows)
結果顯示,備份已成功重放 WAL 文件,并恢復至最新的變更狀態。
使用 LSN 進行時間點恢復
按照前述步驟執行操作,但在關閉主服務器之前暫停。
隨后,執行命令以獲取主數據庫當前 WAL 文件的日志序列號(LSN, Log Sequence Number)。
$ psql -U postgres -d test -c "select pg_current_wal_insert_lsn()"
pg_current_wal_insert_lsn
---------------------------
0/2000060
(1 row)
將獲取的 LSN 值配置到副本數據庫的 postgresql.conf 文件中。
port = 5433
archive_mode = off
restore_command = 'cp $HOME/pg/archive/%f %p'
recovery_target_lsn = '0/2000060'
完成設置后,關閉主數據庫,并按照前述方式執行恢復操作。
$ pg_ctl -D data stop
$ touch data.backup/recovery.signal
$ pg_ctl -D data.backup -l data.backup.log start
$ psql -U postgres -d test -c "select * from t1" -p 5433
id | val
----+-------
1 | hello
2 | world
3 | foo
4 | bar
(4 rows)
使用時間戳進行時間點恢復
基于時間戳的恢復方式相對復雜,因為需要明確指定數據庫應恢復到的具體時間點。
在操作過程中,可參照默認示例的步驟執行,直至插入數據的階段。
$ psql -U postgres -c "create database test"
CREATE DATABASE
$ psql -U postgres -d test -c "create table t1 (id int, val text)"
CREATE TABLE
$ psql -U postgres -d test -c "insert into t1 values (1, 'hello')"
INSERT 0 1
$ psql -U postgres -d test -c "insert into t1 values (2, 'world')"
INSERT 0 1
$ sleep 60
$ psql -U postgres -c "select now()"
now
-------------------------------
2023-04-14 12:54:23.160389-07
(1 row)
$ psql -U postgres -d test -c "insert into t1 values (3, 'foo')"
INSERT 0 1
$ psql -U postgres -d test -c "insert into t1 values (4, 'bar')"
INSERT 0 1
$ sleep 60
$ psql -U postgres -c "select now()"
now
-------------------------------
2023-04-14 12:55:23.184277-07
(1 row)
在插入數據時,可通過加入 sleep 延時命令來方便管理時間戳。同時,使用 select now() 命令獲取數據庫當前的時間戳。與前述步驟相同,需生成剩余的 WAL 文件,并創建用于恢復的 recovery.signal 文件。
$ psql -U postgres -c "select pg_switch_wal()"
$ pg_ctl -D data stop
$ touch data.backup/recovery.signal
在本次操作中,應在副本數據庫的 postgresql.conf 文件中添加以下配置:
port = 5433
archive_mode = off
restore_command = 'cp $HOME/pg/archive/%f %p'
recovery_target_time = '2023-04-14 12:54:23'
其中,recovery_target_time 參數對應于在插入前兩條數據后、插入后兩條數據之前獲取的時間戳。隨后,啟動副本服務器并查看數據內容:
$ pg_ctl -D data.backup -l data.backup.log start
$ psql -U postgres -d test -c "select * from t1" -p 5433
id | val
----+-------
1 | hello
2 | world
(2 rows)
此時,數據庫中僅包含前兩條數據,符合預期。接著,可將恢復時間設置為比前一次時間戳晚 1 秒,并按以下方式修改 recovery_target_time 參數:
recovery_target_time = '2023-04-14 12:54:24'
由于系統在成功完成恢復后會自動刪除 recovery.signal 文件,因此需重新創建該文件:
$ touch data.backup/recovery.signal
最后,重新啟動副本服務器,并按前述步驟查詢表中數據:
pg_ctl -D data.backup -l data.backup.log restart
psql -U postgres -d test -c "select * from t1" -p 5433
id | val
----+-------
1 | hello
2 | world
3 | foo
4 | bar
(4 rows)
根據向服務器執行插入命令的速度不同,可能需要調整recovery_target_time 參數以獲得理想的恢復結果。若恢復時間設置過晚,可能在日志文件中出現如下錯誤信息:
FATAL: recovery ended before configured recovery target was reached
該錯誤表明指定的恢復時間超出了可恢復范圍,即時間點設置過于靠后。應將恢復時間適當提前數秒后,再次啟動服務器進行恢復。
結論
至此,本文完整演示了多種數據庫時間點恢復方法的實現過程。我們依次介紹了三種典型方式:基于最新時間線的恢復、基于特定 LSN(日志序列號)的恢復,以及基于指定時間戳的恢復。
不同方法適用于不同場景。熟練掌握這些恢復技術,不僅有助于提升數據庫運維與管理的效率,也能在突發數據問題時,確保系統具備更高的可控性與數據安全性。

浙公網安備 33010602011771號