欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PotgreSQL?基于時(shí)間點(diǎn)恢復(fù)過程

 更新時(shí)間:2023年08月08日 15:18:49   作者:Bing@DBA  
本篇文章介紹?PostgreSQL?基于時(shí)間點(diǎn)恢復(fù)(point-in-time-recover)需要的條件及恢復(fù)過程,屬于操作說明,對(duì)PotgreSQL?時(shí)間點(diǎn)恢復(fù)相關(guān)知識(shí)感興趣的朋友跟隨小編一起看看吧

前言

本篇文章介紹 PostgreSQL 基于時(shí)間點(diǎn)恢復(fù)(point-in-time-recover)需要的條件及恢復(fù)過程,屬于操作說明。

1. WAL 日志

WAL(Write Ahead Log) 日志優(yōu)先寫機(jī)制,常用于關(guān)系型數(shù)據(jù)庫維護(hù)事務(wù) 持久性、一致性 的一種常見手段,例如 MySQL 中的 Redo Log 也屬于是 WAL 日志,不過在 MySQL 中如果要實(shí)現(xiàn) PITR 要使用 Binlog 日志,這塊兒我理解 MySQL 也可以用 Redo 去做復(fù)制的,也可以用 Redo 做 PITR 但 MySQL 是多引擎架構(gòu),不僅僅有事務(wù)引擎,其它引擎不支持 Redo,所以才使用 Binlog 應(yīng)用于 PITR 和主從復(fù)制。

在 PostgreSQL 只有事務(wù)引擎,所以 WAL 日志既可以應(yīng)用于事務(wù)系統(tǒng),也可以于主從同步和 PITR。

1.1 WAL 配置

想要實(shí)現(xiàn) PITR 相當(dāng)于一個(gè)全量備份 + 增量備份,那 WAL 就是用于做增量備份,此時(shí)就需要啟用 WAL 歸檔:

wal_level = replica             # minimal, replica, or logical
archive_mode = on               # enables archiving; off, on, or always
                                # (change requires restart)
                                # 這里的目標(biāo)是歸檔的目錄,需要提前創(chuàng)建
archive_command = 'cp %p /data/pgsql12/archive/%f && echo %f >> /data/pgsql12/archive/archive.list'             # command to use to archive a logfile segment

配置完成后需要重啟數(shù)據(jù)庫:

pg_ctl -D /data/pgsql12/data/ -l /data/pgsql12/logs/start.log restart

2. pg_basebackup

pg_basebackup 是 PostgreSQL 自帶的物理熱備工具,不過僅支持備份整個(gè)實(shí)例,無法進(jìn)行單庫單表備份,可用于備庫搭建:

pg_basebackup [OPTION]...
參數(shù)含義
-D/–pg-data備份文件目錄,表示將備份文件寫入到那個(gè)目錄下
-F/–format默認(rèn)為p、可選擇p、t
-r/–max-rate傳輸數(shù)據(jù)的最大速率限制
-R/–write-recovery-conf輸出用于replication的配置信息
-X, --wal-method指定復(fù)制wal日志的方式,有none、fetch、stream,推薦使用stream避免接收wal信息是源端日志被覆蓋
-z, --gzip是否壓縮,配合-F t使用
-Z, --compress=0-9壓縮等級(jí),數(shù)字越大壓縮率越大、越消耗CPU資源
-c, --checkpoint設(shè)置checkpoint的模式:fast、spread
-C, --create-slot創(chuàng)建復(fù)制槽
-S, --slot=SLOTNAME指定復(fù)制槽名稱
-l, --label=LABEL指定一個(gè)備份標(biāo)識(shí),便于運(yùn)維人員后續(xù)的維護(hù)
-n, --no-cleando not clean up after errors
-N, --no-syncdo not wait for changes to be written safely to disk
-P, --progress打印備份進(jìn)度信息
-v, --verbose輸出詳細(xì)的信息

2.1 常用命令

用于基本的備份恢復(fù)常用命令:

pg_basebackup -D 備份文件目錄  -v -P -Upostgres -h 127.0.0.1 -p5432 -R

3. 全備恢復(fù)實(shí)驗(yàn)

3.1 進(jìn)行一個(gè)全備

 pg_basebackup -D /data/pgsql12/backup  -v -P -Upostgres -h 127.0.0.1 -p5432 -R
pg_basebackup: initiating base backup, waiting for checkpoint to complete
pg_basebackup: checkpoint completed
pg_basebackup: write-ahead log start point: 2/6D000028 on timeline 2
pg_basebackup: starting background WAL receiver
pg_basebackup: created temporary replication slot "pg_basebackup_74591"
2455778/2455778 kB (100%), 1/1 tablespace                                         
pg_basebackup: write-ahead log end point: 2/6D000138
pg_basebackup: waiting for background process to finish streaming ...
pg_basebackup: syncing data to disk ...
pg_basebackup: base backup completed

3.2 寫入增量數(shù)據(jù)

postgres=# update pgbench_accounts set abalance = 100 where bid = 41;
UPDATE 100000
postgres=# select pg_current_wal_lsn();
 pg_current_wal_lsn 
--------------------
 2/70D63FD0
postgres=# select now();
              now              
-------------------------------
 2023-08-08 13:51:34.475891+08

3.3 模擬備份恢復(fù)

此時(shí)最壞的事情發(fā)生了,需要恢復(fù)數(shù)據(jù),首先需要先關(guān)閉數(shù)據(jù)庫:

pg_ctl -D /data/pgsql12/data/ stop
waiting for server to shut down.... done
server stopped

將數(shù)據(jù)目錄重命名,將基礎(chǔ)備份

# 重命名備份目錄
mv data data_bak
# 移動(dòng)備份文件至數(shù)據(jù)目錄
mv backup data
# 修改數(shù)據(jù)目錄為 700 否則無法啟動(dòng)
# DETAIL:  Permissions should be u=rwx (0700) or u=rwx,g=rx (0750).
chmod 700 /data/pgsql12/data/

修改數(shù)據(jù)目錄下 postgresql.auto.conf 文件:

restore_command = 'cp /data/pgsql12/archive/%f %p > /data/pgsql12/archive/recovery.log 2>&1 '
# 通過 LSN 方式恢復(fù)
recovery_target_lsn='2/70D63FD0'
# 通過時(shí)間點(diǎn)的方式恢復(fù) 2 選 1
recovery_target_time = '2023-08-08 13:51:34'
# 達(dá)到恢復(fù)目標(biāo)后,數(shù)據(jù)的動(dòng)作 promote 表示可以直接接受連接,測試使用,生產(chǎn)恢復(fù)建議使用 pause(默認(rèn)值)確認(rèn)數(shù)據(jù)恢復(fù)好后,手動(dòng)刪除 recovery.signal 文件即可。
recovery_target_action = 'promote'

配置完這些后,重新啟動(dòng)數(shù)據(jù)庫:

pg_ctl  -D /data/pgsql12/data start -l /data/pgsql12/logs/start.log

驗(yàn)證剛才的增量數(shù)據(jù)是否已經(jīng)恢復(fù):

postgres=# select count(*) from pgbench_accounts where bid = 41 and abalance = 100;
 count  
--------
 100000

總結(jié)

PostgreSQL 想要實(shí)現(xiàn) PITR 需要設(shè)置 wal_level 配置參數(shù)為 replica 或更高級(jí)別,且配置合理的歸檔機(jī)制和歸檔清理機(jī)制,本篇文章只引入恢復(fù)流程,一套生產(chǎn)環(huán)境備份恢復(fù)如何配置,我們將在下一篇文章介紹。

到此這篇關(guān)于PotgreSQL 基于時(shí)間點(diǎn)恢復(fù)的文章就介紹到這了,更多相關(guān)PotgreSQL 時(shí)間點(diǎn)恢復(fù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論